Enable the optimization about the memory copy saving for Jellybean

BZ: 54443

Android original design: There is a local queue in movie studio used to mantain
the decoded data from the decoder(here need a memory copy from the decoder to the
queue), then movie studio render the data in the queue(here need another memory
copy from the queue to output buffer)

Optimization design: Keep the decoded data(the whole Mediabuffer struct, use the
reference method) in the local queue instead of only copy the Mediabuffer->data()
to the queue, SAVE one memory copy here. In the render process, copy the data from
the Mediabuffer->data() to output buffer.

Change-Id: Ibd06d96cc118c72f86780767b3c344b865d8252a
Signed-off-by: Gu, Wangyi <wangyi.gu@intel.com>
Reviewed-on: http://android.intel.com:8080/63807
Reviewed-by: Tang, Richard <richard.tang@intel.com>
Reviewed-by: Feng, Wei <wei.feng@intel.com>
Reviewed-by: Shi, PingX <pingx.shi@intel.com>
Tested-by: Shi, PingX <pingx.shi@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
diff --git a/frameworks/videoedit/stagefrightshells/Android.mk b/frameworks/videoedit/stagefrightshells/Android.mk
index 2e7c5ef..4c377fa 100644
--- a/frameworks/videoedit/stagefrightshells/Android.mk
+++ b/frameworks/videoedit/stagefrightshells/Android.mk
@@ -23,7 +23,7 @@
     VideoEditorAudioDecoder.cpp \
     VideoEditorMp3Reader.cpp \
     VideoEditor3gpReader.cpp \
-    VideoEditorBuffer.c \
+    VideoEditorBuffer.cpp \
     VideoEditorVideoEncoder.cpp \
     VideoEditorAudioEncoder.cpp \
     IntelVideoEditorUtils.cpp \
diff --git a/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.c b/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.cpp
similarity index 74%
rename from frameworks/videoedit/stagefrightshells/VideoEditorBuffer.c
rename to frameworks/videoedit/stagefrightshells/VideoEditorBuffer.cpp
index 4bed81f..07d158a 100644
--- a/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.c
+++ b/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.cpp
@@ -15,7 +15,7 @@
  */
 /**
 *************************************************************************
-* @file   VideoEditorBuffer.c
+* @file   VideoEditorBuffer.cpp
 * @brief  StageFright shell Buffer
 *************************************************************************
 */
@@ -114,28 +114,31 @@
 
 /**
  ************************************************************************
- M4OSA_ERR VIDEOEDITOR_BUFFER_freePool(VIDEOEDITOR_BUFFER_Pool* ppool)
+ M4OSA_ERR VIDEOEDITOR_BUFFER_freePool_Ext(VIDEOEDITOR_BUFFER_Pool* ppool)
  * @brief   Deallocate a buffer pool
  *
  * @param   ppool      : IN The buffer pool to free
  * @return  Error code
  ************************************************************************
 */
-M4OSA_ERR VIDEOEDITOR_BUFFER_freePool(VIDEOEDITOR_BUFFER_Pool* ppool)
+M4OSA_ERR VIDEOEDITOR_BUFFER_freePool_Ext(VIDEOEDITOR_BUFFER_Pool* ppool)
 {
     M4OSA_ERR err;
-    M4OSA_UInt32  j = 0;
+    M4OSA_UInt32 j = 0;
 
-    ALOGV("VIDEOEDITOR_BUFFER_freePool : ppool = 0x%x", ppool);
+    ALOGV("VIDEOEDITOR_BUFFER_freePool_Ext : ppool = 0x%x", ppool);
 
     err = M4NO_ERROR;
 
     for (j = 0; j < ppool->NB; j++)
     {
-        if(M4OSA_NULL != ppool->pNXPBuffer[j].pData)
+        if(M4OSA_NULL != ppool->pNXPBuffer[j].mBuffer)
         {
-            free(ppool->pNXPBuffer[j].pData);
-            ppool->pNXPBuffer[j].pData = M4OSA_NULL;
+            ppool->pNXPBuffer[j].mBuffer->release();
+            ppool->pNXPBuffer[j].state = VIDEOEDITOR_BUFFER_kEmpty;
+            ppool->pNXPBuffer[j].mBuffer = M4OSA_NULL;
+            ppool->pNXPBuffer[j].size = 0;
+            ppool->pNXPBuffer[j].buffCTS = -1;
         }
     }
 
@@ -200,33 +203,74 @@
     return(err);
 }
 
-M4OSA_ERR VIDEOEDITOR_BUFFER_initPoolBuffers(VIDEOEDITOR_BUFFER_Pool* pool,
+/**
+ ************************************************************************
+ void VIDEOEDITOR_BUFFER_getBufferForDecoder(VIDEOEDITOR_BUFFER_Pool* ppool,
+ *         VIDEOEDITOR_BUFFER_Buffer** pNXPBuffer)
+ * @brief   Returns a buffer in a given state
+ *
+ * @param   ppool      : IN The buffer pool
+ * @param   desiredState : IN The buffer state
+ * @param   pNXPBuffer : IN The selected buffer
+ * @return  Error code
+ ************************************************************************
+*/
+void VIDEOEDITOR_BUFFER_getBufferForDecoder(VIDEOEDITOR_BUFFER_Pool* ppool)
+{
+    M4OSA_Bool bFound = M4OSA_FALSE;
+    M4OSA_UInt32 i, ibuf;
+    M4_MediaTime  candidateTimeStamp = (M4_MediaTime)0x7ffffff;
+    ibuf = 0;
+
+    for (i=0; i < ppool->NB; i++)
+    {
+        bFound = (ppool->pNXPBuffer[i].state == VIDEOEDITOR_BUFFER_kEmpty);
+        if (bFound)
+        {
+            break;
+        }
+    }
+
+    if(!bFound)
+    {
+        for(i = 0; i< ppool->NB; i++)
+        {
+            if(ppool->pNXPBuffer[i].state == VIDEOEDITOR_BUFFER_kFilled)
+            {
+               if(ppool->pNXPBuffer[i].buffCTS <= candidateTimeStamp)
+               {
+                  bFound = M4OSA_TRUE;
+                  candidateTimeStamp = ppool->pNXPBuffer[i].buffCTS;
+                  ibuf = i;
+               }
+            }
+        }
+
+        if(M4OSA_TRUE == bFound)
+        {
+           if(M4OSA_NULL != ppool->pNXPBuffer[ibuf].mBuffer) {
+              ppool->pNXPBuffer[ibuf].mBuffer->release();
+              ppool->pNXPBuffer[ibuf].state = VIDEOEDITOR_BUFFER_kEmpty;
+              ppool->pNXPBuffer[ibuf].mBuffer = M4OSA_NULL;
+              ppool->pNXPBuffer[ibuf].size = 0;
+              ppool->pNXPBuffer[ibuf].buffCTS = -1;
+           }
+        }
+
+    }
+
+}
+M4OSA_ERR VIDEOEDITOR_BUFFER_initPoolBuffers_Ext(VIDEOEDITOR_BUFFER_Pool* pool,
     M4OSA_UInt32 lSize)
 {
     M4OSA_ERR     err = M4NO_ERROR;
-    M4OSA_UInt32  index, j;
+    M4OSA_UInt32  index, i, j;
 
     /**
      * Initialize all the buffers in the pool */
     for(index = 0; index < pool->NB; index++)
     {
-        pool->pNXPBuffer[index].pData = M4OSA_NULL;
-        pool->pNXPBuffer[index].pData = (M4OSA_Void*)M4OSA_32bitAlignedMalloc(
-            lSize, VIDEOEDITOR_BUFFER_EXTERNAL,
-            (M4OSA_Char*)("BUFFER_initPoolBuffers: Buffer data"));
-        if(M4OSA_NULL == pool->pNXPBuffer[index].pData)
-        {
-            for (j = 0; j < index; j++)
-            {
-                if(M4OSA_NULL != pool->pNXPBuffer[j].pData)
-                {
-                    free(pool->pNXPBuffer[j].pData);
-                    pool->pNXPBuffer[j].pData = M4OSA_NULL;
-                }
-            }
-            err = M4ERR_ALLOC;
-            return err;
-        }
+        pool->pNXPBuffer[index].mBuffer = M4OSA_NULL;
         pool->pNXPBuffer[index].size = 0;
         pool->pNXPBuffer[index].state = VIDEOEDITOR_BUFFER_kEmpty;
         pool->pNXPBuffer[index].idx = index;
@@ -253,7 +297,7 @@
             {
                 bFound = M4OSA_TRUE;
                 candidateTimeStamp = pool->pNXPBuffer[index].buffCTS;
-                    *pNXPBuffer = &(pool->pNXPBuffer[index]);
+                *pNXPBuffer = &(pool->pNXPBuffer[index]);
             }
         }
     }
diff --git a/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.h b/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.h
new file mode 100644
index 0000000..a180942
--- /dev/null
+++ b/frameworks/videoedit/stagefrightshells/VideoEditorBuffer.h
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+*************************************************************************
+* @file   VideoEditorBuffer.h
+* @brief  StageFright shell Buffer
+*************************************************************************
+*/
+#ifndef   VIDEOEDITOR_BUFFER_H
+#define   VIDEOEDITOR_BUFFER_H
+
+#include "M4OSA_Types.h"
+#include "M4OSA_Debug.h"
+#include "M4OSA_Memory.h"
+#include "M4OSA_CharStar.h"
+#include "M4_Utils.h"
+
+#include "LV_Macros.h"
+
+#include <media/stagefright/MediaBuffer.h>
+using namespace android;
+
+/*--- Core id for VIDEOEDITOR Buffer allocations  ---*/
+#define VIDEOEDITOR_BUFFER_EXTERNAL 0x012F
+
+/* ----- errors  -----*/
+#define M4ERR_NO_BUFFER_AVAILABLE \
+    M4OSA_ERR_CREATE(M4_ERR,VIDEOEDITOR_BUFFER_EXTERNAL,0x000001)
+#define M4ERR_NO_BUFFER_MATCH \
+    M4OSA_ERR_CREATE(M4_ERR,VIDEOEDITOR_BUFFER_EXTERNAL,0x000002)
+
+typedef enum {
+    VIDEOEDITOR_BUFFER_kEmpty = 0,
+    VIDEOEDITOR_BUFFER_kFilled,
+} VIDEOEDITOR_BUFFER_State;
+
+/**
+ ************************************************************************
+ * Structure    LVOMX_BUFFER_Buffer
+ * @brief       One OMX Buffer and data related to it
+ ************************************************************************
+*/
+typedef struct {
+    MediaBuffer* mBuffer;           /**< Pointer to the data for intel platform*/
+    M4OSA_UInt32 size;
+    VIDEOEDITOR_BUFFER_State state; /**< Buffer state */
+    M4OSA_UInt32 idx;               /**< Index of the buffer inside the pool */
+    M4_MediaTime    buffCTS;        /**< Time stamp of the buffer */
+} VIDEOEDITOR_BUFFER_Buffer;
+
+/**
+ ************************************************************************
+ * Structure    LVOMX_BUFFER_Pool
+ * @brief       Structure to manage buffers
+ ************************************************************************
+*/
+typedef struct {
+    VIDEOEDITOR_BUFFER_Buffer* pNXPBuffer;
+    M4OSA_UInt32 NB;
+    M4OSA_Char* poolName;
+} VIDEOEDITOR_BUFFER_Pool;
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif //__cplusplus
+
+/**
+ ************************************************************************
+ M4OSA_ERR VIDEOEDITOR_BUFFER_allocatePool(VIDEOEDITOR_BUFFER_Pool** ppool,
+ *         M4OSA_UInt32 nbBuffers)
+ * @brief   Allocate a pool of nbBuffers buffers
+ *
+ * @param   ppool      : IN The buffer pool to create
+ * @param   nbBuffers  : IN The number of buffers in the pool
+ * @param   poolName   : IN a name given to the pool
+ * @return  Error code
+ ************************************************************************
+*/
+M4OSA_ERR VIDEOEDITOR_BUFFER_allocatePool(VIDEOEDITOR_BUFFER_Pool** ppool,
+        M4OSA_UInt32 nbBuffers, M4OSA_Char* poolName);
+
+/**
+ ************************************************************************
+ M4OSA_ERR VIDEOEDITOR_BUFFER_freePool(LVOMX_BUFFER_Pool* ppool)
+ * @brief   Deallocate a buffer pool
+ *
+ * @param   ppool      : IN The buffer pool to free
+ * @return  Error code
+ ************************************************************************
+*/
+M4OSA_ERR VIDEOEDITOR_BUFFER_freePool(VIDEOEDITOR_BUFFER_Pool* ppool);
+
+/**
+ ************************************************************************
+M4OSA_ERR VIDEOEDITOR_BUFFER_freePool_Ext(LVOMX_BUFFER_Pool* ppool)
+ * @brief   Deallocate a buffer pool
+ *
+ * @param   ppool      : IN The buffer pool to free
+ * @return  Error code
+ ************************************************************************
+*/
+M4OSA_ERR VIDEOEDITOR_BUFFER_freePool_Ext(VIDEOEDITOR_BUFFER_Pool* ppool);
+
+/**
+ ************************************************************************
+ M4OSA_ERR VIDEOEDITOR_BUFFER_getBuffer(VIDEOEDITOR_BUFFER_Pool* ppool,
+ *         VIDEOEDITOR_BUFFER_Buffer** pNXPBuffer)
+ * @brief   Returns a buffer in a given state
+ *
+ * @param   ppool      : IN The buffer pool
+ * @param   desiredState : IN The buffer state
+ * @param   pNXPBuffer : IN The selected buffer
+ * @return  Error code
+ ************************************************************************
+*/
+M4OSA_ERR VIDEOEDITOR_BUFFER_getBuffer(VIDEOEDITOR_BUFFER_Pool* ppool,
+        VIDEOEDITOR_BUFFER_State desiredState,
+        VIDEOEDITOR_BUFFER_Buffer** pNXPBuffer);
+
+/**
+ ************************************************************************
+ void VIDEOEDITOR_BUFFER_getBufferForDecoder(VIDEOEDITOR_BUFFER_Pool* ppool,
+ *         VIDEOEDITOR_BUFFER_Buffer** pNXPBuffer)
+ * @brief   Make sure there are buffers for decoder
+ *
+ * @param   ppool      : IN The buffer pool
+ * @param   desiredState : IN The buffer state
+ * @return  Error code
+ ************************************************************************
+*/
+void VIDEOEDITOR_BUFFER_getBufferForDecoder(VIDEOEDITOR_BUFFER_Pool* ppool);
+
+M4OSA_ERR VIDEOEDITOR_BUFFER_initPoolBuffers(VIDEOEDITOR_BUFFER_Pool* ppool,
+        M4OSA_UInt32 lSize);
+
+M4OSA_ERR VIDEOEDITOR_BUFFER_initPoolBuffers_Ext(VIDEOEDITOR_BUFFER_Pool* ppool,
+        M4OSA_UInt32 lSize);
+
+
+M4OSA_ERR VIDEOEDITOR_BUFFER_getOldestBuffer(VIDEOEDITOR_BUFFER_Pool *pool,
+        VIDEOEDITOR_BUFFER_State desiredState,
+        VIDEOEDITOR_BUFFER_Buffer** pNXPBuffer);
+
+#ifdef __cplusplus
+}
+#endif //__cplusplus
+#endif /*VIDEOEDITOR_BUFFER_H*/
+
diff --git a/frameworks/videoedit/stagefrightshells/VideoEditorVideoDecoder.cpp b/frameworks/videoedit/stagefrightshells/VideoEditorVideoDecoder.cpp
index 9e7bafd..7f5edcd 100644
--- a/frameworks/videoedit/stagefrightshells/VideoEditorVideoDecoder.cpp
+++ b/frameworks/videoedit/stagefrightshells/VideoEditorVideoDecoder.cpp
@@ -25,6 +25,7 @@
  *     HEADERS     *
  *******************/
 
+#include "VideoEditorBuffer.h"
 #include "VideoEditorVideoDecoder_internal.h"
 #include "VideoEditorUtils.h"
 #include "M4VD_Tools.h"
@@ -35,7 +36,7 @@
 /********************
  *   DEFINITIONS    *
  ********************/
-#define MAX_DEC_BUFFERS 10
+#define MAX_DEC_BUFFERS 4
 
 /********************
  *   SOURCE CLASS   *
@@ -853,13 +854,13 @@
     // Configure the buffer pool
     if( M4OSA_NULL != pDecShellContext->m_pDecBufferPool ) {
         ALOGV("VideoDecoder_configureFromMetadata : reset the buffer pool");
-        VIDEOEDITOR_BUFFER_freePool(pDecShellContext->m_pDecBufferPool);
+        VIDEOEDITOR_BUFFER_freePool_Ext(pDecShellContext->m_pDecBufferPool);
         pDecShellContext->m_pDecBufferPool = M4OSA_NULL;
     }
     err =  VIDEOEDITOR_BUFFER_allocatePool(&pDecShellContext->m_pDecBufferPool,
         MAX_DEC_BUFFERS, (M4OSA_Char*)"VIDEOEDITOR_DecodedBufferPool");
     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
-    err = VIDEOEDITOR_BUFFER_initPoolBuffers(pDecShellContext->m_pDecBufferPool,
+    err = VIDEOEDITOR_BUFFER_initPoolBuffers_Ext(pDecShellContext->m_pDecBufferPool,
                 frameSize + pDecShellContext->mGivenWidth * 2);
 
     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
@@ -870,7 +871,7 @@
     } else {
         if( (M4OSA_NULL != pDecShellContext) && \
             (M4OSA_NULL != pDecShellContext->m_pDecBufferPool) ) {
-            VIDEOEDITOR_BUFFER_freePool(pDecShellContext->m_pDecBufferPool);
+            VIDEOEDITOR_BUFFER_freePool_Ext(pDecShellContext->m_pDecBufferPool);
             pDecShellContext->m_pDecBufferPool = M4OSA_NULL;
         }
         ALOGV("VideoEditorVideoDecoder_configureFromMetadata ERROR 0x%X", err);
@@ -891,20 +892,22 @@
     // Release the color converter
     delete pDecShellContext->mI420ColorConverter;
 
+    // Release memory
+    if( pDecShellContext->m_pDecBufferPool != M4OSA_NULL ) {
+        VIDEOEDITOR_BUFFER_freePool_Ext(pDecShellContext->m_pDecBufferPool);
+        pDecShellContext->m_pDecBufferPool = M4OSA_NULL;
+    }
+
     // Destroy the graph
     if( pDecShellContext->mVideoDecoder != NULL ) {
         ALOGV("### VideoEditorVideoDecoder_destroy : releasing decoder");
         pDecShellContext->mVideoDecoder->stop();
         pDecShellContext->mVideoDecoder.clear();
     }
+
     pDecShellContext->mClient.disconnect();
     pDecShellContext->mReaderSource.clear();
 
-    // Release memory
-    if( pDecShellContext->m_pDecBufferPool != M4OSA_NULL ) {
-        VIDEOEDITOR_BUFFER_freePool(pDecShellContext->m_pDecBufferPool);
-        pDecShellContext->m_pDecBufferPool = M4OSA_NULL;
-    }
     SAFE_FREE(pDecShellContext);
     pContext = NULL;
 
@@ -1339,6 +1342,7 @@
     MediaBuffer* pNextBuffer = NULL;
     status_t errStatus;
     bool needSeek = bJump;
+    bool needSave = M4OSA_TRUE;
 
     ALOGV("VideoEditorVideoDecoder_decode begin");
 
@@ -1368,7 +1372,11 @@
     while (pDecoderBuffer == NULL || pDecShellContext->m_lastDecodedCTS + tolerance < *pTime) {
         ALOGV("VideoEditorVideoDecoder_decode, frameCTS = %lf, DecodeUpTo = %lf",
             pDecShellContext->m_lastDecodedCTS, *pTime);
-
+        if (M4OSA_TRUE == needSave) {
+            VIDEOEDITOR_BUFFER_getBufferForDecoder(pDecShellContext->m_pDecBufferPool);
+        } else {
+            needSave = M4OSA_TRUE;
+        }
         // Read the buffer from the stagefright decoder
         if (needSeek) {
             MediaSource::ReadOptions options;
@@ -1388,6 +1396,7 @@
             // If we decoded a buffer before EOS, we still need to put it
             // into the queue.
             if (pDecoderBuffer && bJump) {
+                pDecoderBuffer->add_ref();
                 copyBufferToQueue(pDecShellContext, pDecoderBuffer);
             }
             goto VIDEOEDITOR_VideoDecode_cleanUP;
@@ -1413,14 +1422,11 @@
         // and drop the 0-length buffers.
         if (pNextBuffer->range_length() == 0) {
             pNextBuffer->release();
+            pNextBuffer = NULL;
+            needSave = M4OSA_FALSE;
             continue;
         }
 
-        // Now we have a good next buffer, release the previous one.
-        if (pDecoderBuffer != NULL) {
-            pDecoderBuffer->release();
-            pDecoderBuffer = NULL;
-        }
         pDecoderBuffer = pNextBuffer;
 
         // Record the timestamp of last decoded buffer
@@ -1452,6 +1458,12 @@
             if (lerr != M4NO_ERROR) {
                 goto VIDEOEDITOR_VideoDecode_cleanUP;
             }
+        } else {
+            if (pDecoderBuffer != NULL) {
+                pDecoderBuffer->release();
+                pDecoderBuffer = NULL;
+            }
+            needSave = M4OSA_FALSE;
         }
     }
 
@@ -1463,10 +1475,6 @@
 
 VIDEOEDITOR_VideoDecode_cleanUP:
     *pTime = pDecShellContext->m_lastDecodedCTS;
-    if (pDecoderBuffer != NULL) {
-        pDecoderBuffer->release();
-        pDecoderBuffer = NULL;
-    }
 
     ALOGV("VideoEditorVideoDecoder_decode: end with 0x%x", lerr);
     return lerr;
@@ -1486,7 +1494,11 @@
         lerr = VIDEOEDITOR_BUFFER_getOldestBuffer(
             pDecShellContext->m_pDecBufferPool,
             VIDEOEDITOR_BUFFER_kFilled, &tmpDecBuffer);
+        tmpDecBuffer->mBuffer->release();
         tmpDecBuffer->state = VIDEOEDITOR_BUFFER_kEmpty;
+        tmpDecBuffer->mBuffer = NULL;
+        tmpDecBuffer->size = 0;
+        tmpDecBuffer->buffCTS = -1;
         lerr = M4NO_ERROR;
     }
 
@@ -1494,15 +1506,7 @@
 
     // Color convert or copy from the given MediaBuffer to our buffer
     if (pDecShellContext->mI420ColorConverter) {
-        if (pDecShellContext->mI420ColorConverter->convertDecoderOutputToI420(
-            (uint8_t *)pDecoderBuffer->data(),// ?? + pDecoderBuffer->range_offset(),   // decoderBits
-            pDecShellContext->mGivenWidth,  // decoderWidth
-            pDecShellContext->mGivenHeight,  // decoderHeight
-            pDecShellContext->mCropRect,  // decoderRect
-            tmpDecBuffer->pData /* dstBits */) < 0) {
-            ALOGE("convertDecoderOutputToI420 failed");
-            lerr = M4ERR_NOT_IMPLEMENTED;
-        }
+        tmpDecBuffer->mBuffer = pDecoderBuffer;
     } else if (pDecShellContext->decOuputColorFormat == OMX_COLOR_FormatYUV420Planar) {
         int32_t width = pDecShellContext->m_pVideoStreamhandler->m_videoWidth;
         int32_t height = pDecShellContext->m_pVideoStreamhandler->m_videoHeight;
@@ -1515,20 +1519,20 @@
         {
             M4OSA_MemAddr8 pTmpBuff = (M4OSA_MemAddr8)pDecoderBuffer->data() + pDecoderBuffer->range_offset();
 
-            memcpy((void *)tmpDecBuffer->pData, (void *)pTmpBuff, yPlaneSize);
+            memcpy((void *)((M4OSA_MemAddr8)tmpDecBuffer->mBuffer->data() + tmpDecBuffer->mBuffer->range_offset()), (void *)pTmpBuff, yPlaneSize);
 
             offsetSrc += pDecShellContext->mGivenWidth * pDecShellContext->mGivenHeight;
-            memcpy((void *)((M4OSA_MemAddr8)tmpDecBuffer->pData + yPlaneSize),
+            memcpy((void *)((M4OSA_MemAddr8)tmpDecBuffer->mBuffer->data() + tmpDecBuffer->mBuffer->range_offset() + yPlaneSize),
                 (void *)(pTmpBuff + offsetSrc), uvPlaneSize);
 
             offsetSrc += (pDecShellContext->mGivenWidth >> 1) * (pDecShellContext->mGivenHeight >> 1);
-            memcpy((void *)((M4OSA_MemAddr8)tmpDecBuffer->pData + yPlaneSize + uvPlaneSize),
+            memcpy((void *)((M4OSA_MemAddr8)tmpDecBuffer->mBuffer->data() + tmpDecBuffer->mBuffer->range_offset() + yPlaneSize + uvPlaneSize),
                 (void *)(pTmpBuff + offsetSrc), uvPlaneSize);
         }
         else
         {
             M4OSA_MemAddr8 pTmpBuff = (M4OSA_MemAddr8)pDecoderBuffer->data() + pDecoderBuffer->range_offset();
-            M4OSA_MemAddr8 pTmpBuffDst = (M4OSA_MemAddr8)tmpDecBuffer->pData;
+            M4OSA_MemAddr8 pTmpBuffDst = (M4OSA_MemAddr8)(tmpDecBuffer->mBuffer->data() + tmpDecBuffer->mBuffer->range_offset());
             int32_t index;
 
             for ( index = 0; index < height; index++)
@@ -1562,7 +1566,7 @@
 
     tmpDecBuffer->buffCTS = pDecShellContext->m_lastDecodedCTS;
     tmpDecBuffer->state = VIDEOEDITOR_BUFFER_kFilled;
-    tmpDecBuffer->size = pDecoderBuffer->size();
+    tmpDecBuffer->size = pDecoderBuffer->range_length();
 
     return lerr;
 }
@@ -1573,7 +1577,7 @@
     M4OSA_ERR err = M4NO_ERROR;
     VideoEditorVideoDecoder_Context* pDecShellContext =
         (VideoEditorVideoDecoder_Context*) context;
-    M4OSA_UInt32 lindex, i;
+    M4OSA_UInt32 i;
     M4OSA_UInt8* p_buf_src, *p_buf_dest;
     M4VIFI_ImagePlane tmpPlaneIn, tmpPlaneOut;
     VIDEOEDITOR_BUFFER_Buffer* pTmpVIDEOEDITORBuffer, *pRenderVIDEOEDITORBuffer
@@ -1603,12 +1607,6 @@
         pTmpVIDEOEDITORBuffer = &pDecShellContext->m_pDecBufferPool\
             ->pNXPBuffer[i];
         if (pTmpVIDEOEDITORBuffer->state == VIDEOEDITOR_BUFFER_kFilled) {
-            /** Free all those buffers older than last rendered frame. */
-            if (pTmpVIDEOEDITORBuffer->buffCTS < pDecShellContext->\
-                    m_lastRenderCts) {
-                pTmpVIDEOEDITORBuffer->state = VIDEOEDITOR_BUFFER_kEmpty;
-            }
-
             /** Get the buffer with appropriate timestamp  */
             if ( (pTmpVIDEOEDITORBuffer->buffCTS >= pDecShellContext->\
                     m_lastRenderCts) &&
@@ -1635,7 +1633,7 @@
 
     if( M4OSA_NULL != pDecShellContext->m_pFilter ) {
         // Filtering was requested
-        M4VIFI_ImagePlane tmpPlane[3];
+        M4VIFI_ImagePlane tmpPlane[2];
         // Prepare the output image for conversion
         tmpPlane[0].u_width   =
             pDecShellContext->m_pVideoStreamhandler->m_videoWidth;
@@ -1643,9 +1641,9 @@
             pDecShellContext->m_pVideoStreamhandler->m_videoHeight;
         tmpPlane[0].u_topleft = 0;
         tmpPlane[0].u_stride  = tmpPlane[0].u_width;
-        tmpPlane[0].pac_data  = (M4VIFI_UInt8*)pRenderVIDEOEDITORBuffer->pData;
+        tmpPlane[0].pac_data  = (M4VIFI_UInt8*)(pRenderVIDEOEDITORBuffer->mBuffer->data() + pRenderVIDEOEDITORBuffer->mBuffer->range_offset());
         tmpPlane[1].u_width   = tmpPlane[0].u_width;
-        tmpPlane[1].u_height  = tmpPlane[0].u_height/2;
+        tmpPlane[1].u_height  = tmpPlane[0].u_height>>1;
         tmpPlane[1].u_topleft = 0;
         tmpPlane[1].u_stride  = tmpPlane[0].u_stride;
         tmpPlane[1].pac_data  = tmpPlane[0].pac_data +
@@ -1656,7 +1654,7 @@
     } else {
         // Just copy the YUV420P buffer
         M4OSA_MemAddr8 tempBuffPtr =
-            (M4OSA_MemAddr8)pRenderVIDEOEDITORBuffer->pData;
+            (M4OSA_MemAddr8)(pRenderVIDEOEDITORBuffer->mBuffer->data() + pRenderVIDEOEDITORBuffer->mBuffer->range_offset());
         M4OSA_UInt32 tempWidth =
             pDecShellContext->m_pVideoStreamhandler->m_videoWidth;
         M4OSA_UInt32 tempHeight =
@@ -1666,7 +1664,7 @@
             tempWidth * tempHeight);
         tempBuffPtr += (tempWidth * tempHeight);
         memcpy((void *) pOutputPlane[1].pac_data, (void *)tempBuffPtr,
-            tempWidth * tempHeight/2);
+            tempWidth * (tempHeight>>1));
     }
 
     pDecShellContext->mNbRenderedFrames++;