vp9HWR: fix free issue in multi-thread

Bug: 18080142
Change-Id: Ie17b80dda21a97712fa8dfa35dbf6671ffc15dcb
Signed-off-by: ywan171 <yi.a.wang@intel.com>
diff --git a/videocodec/OMXVideoDecoderVP9HWR.cpp b/videocodec/OMXVideoDecoderVP9HWR.cpp
index 8497f09..8566913 100644
--- a/videocodec/OMXVideoDecoderVP9HWR.cpp
+++ b/videocodec/OMXVideoDecoderVP9HWR.cpp
@@ -106,32 +106,32 @@
                           unsigned int new_size,
                           vpx_codec_frame_buffer_t *fb)
 {
-    (void)user_priv;
+    OMXVideoDecoderVP9HWR * p = (OMXVideoDecoderVP9HWR *)user_priv;
     if (fb == NULL) {
         return -1;
     }
 
     // TODO: Adaptive playback case needs to reconsider
-    if (extNativeBufferSize < new_size) {
+    if (p->extNativeBufferSize < new_size) {
         LOGE("Provided frame buffer size < requesting min size.");
         return -1;
     }
 
     int i;
-    for (i = 0; i < extMappedNativeBufferCount; i++ ) {
-        if ((extMIDs[i]->m_render_done == true) &&
-            (extMIDs[i]->m_released == true)) {
-            fb->data = extMIDs[i]->m_usrAddr;
-            fb->size = extNativeBufferSize;
-            fb->fb_stride = extActualBufferStride;
-            fb->fb_height_stride = extActualBufferHeightStride;
+    for (i = 0; i < p->extMappedNativeBufferCount; i++ ) {
+        if ((p->extMIDs[i]->m_render_done == true) &&
+            (p->extMIDs[i]->m_released == true)) {
+            fb->data = p->extMIDs[i]->m_usrAddr;
+            fb->size = p->extNativeBufferSize;
+            fb->fb_stride = p->extActualBufferStride;
+            fb->fb_height_stride = p->extActualBufferHeightStride;
             fb->fb_index = i;
-            extMIDs[i]->m_released = false;
+            p->extMIDs[i]->m_released = false;
             break;
         }
     }
 
-    if (i == extMappedNativeBufferCount) {
+    if (i == p->extMappedNativeBufferCount) {
         LOGE("No available frame buffer in pool.");
         return -1;
     }
@@ -143,18 +143,18 @@
 int releaseVP9FrameBuffer(void *user_priv, vpx_codec_frame_buffer_t *fb)
 {
     int i;
-    user_priv = user_priv; // to remove warning
+    OMXVideoDecoderVP9HWR * p = (OMXVideoDecoderVP9HWR *)user_priv;
     if (fb == NULL) {
         return -1;
     }
-    for (i = 0; i < extMappedNativeBufferCount; i++ ) {
-        if (fb->data == extMIDs[i]->m_usrAddr) {
-            extMIDs[i]->m_released = true;
+    for (i = 0; i < p->extMappedNativeBufferCount; i++ ) {
+        if (fb->data == p->extMIDs[i]->m_usrAddr) {
+            p->extMIDs[i]->m_released = true;
             break;
         }
     }
 
-    if (i == extMappedNativeBufferCount) {
+    if (i == p->extMappedNativeBufferCount) {
         LOGE("Not found matching frame buffer in pool, libvpx's wrong?");
         return -1;
     }
@@ -182,7 +182,7 @@
     if (vpx_codec_set_frame_buffer_functions((vpx_codec_ctx_t *)mCtx,
                                     getVP9FrameBuffer,
                                     releaseVP9FrameBuffer,
-                                    NULL)) {
+                                    this)) {
       LOGE("Failed to configure external frame buffers");
       return OMX_ErrorNotReady;
     }
@@ -367,7 +367,6 @@
         delete extMIDs[i]->m_surface;
         free(extMIDs[i]);
     }
-
     return OMXComponentCodecBase::ProcessorDeinit();
 }
 
diff --git a/videocodec/OMXVideoDecoderVP9HWR.h b/videocodec/OMXVideoDecoderVP9HWR.h
index 069b182..e55f54e 100644
--- a/videocodec/OMXVideoDecoderVP9HWR.h
+++ b/videocodec/OMXVideoDecoderVP9HWR.h
@@ -43,25 +43,19 @@
 #define DECODE_WITH_GRALLOC_BUFFER
 #define VPX_DECODE_BORDER 0
 
-// Make it global to be accessed by callback realloc func
 #define MAX_NATIVE_BUFFER_COUNT 64
-vaapiMemId* extMIDs[MAX_NATIVE_BUFFER_COUNT];
-int extUtilBufferCount;
-int extMappedNativeBufferCount;
-unsigned int extNativeBufferSize;
-
-// These two strides are passed into libvpx to indicate the external buffer size
-// in case that video demension is smaller than these, libvpx inside should
-// ajust the start point of address of decoded y/v/u component.
-// This is especially for adaptive playback case. External buffer is always allocated
-// (or mapped from vaSurface) to a pre-set max size.
-int extActualBufferStride;
-int extActualBufferHeightStride;
 
 class OMXVideoDecoderVP9HWR : public OMXVideoDecoderBase {
 public:
     OMXVideoDecoderVP9HWR();
     virtual ~OMXVideoDecoderVP9HWR();
+    vaapiMemId* extMIDs[MAX_NATIVE_BUFFER_COUNT];
+    int extUtilBufferCount;
+    int extMappedNativeBufferCount;
+    unsigned int extNativeBufferSize;
+    // (or mapped from vaSurface) to a pre-set max size.
+    int extActualBufferStride;
+    int extActualBufferHeightStride;
 
 protected:
     virtual OMX_ERRORTYPE InitInputPortFormatSpecific(OMX_PARAM_PORTDEFINITIONTYPE *paramPortDefinitionInput);
@@ -98,7 +92,6 @@
     friend int reallocVP9FrameBuffer(void *user_priv, unsigned int new_size, vpx_codec_frame_buffer_t *fb);
 
     DECLARE_HANDLER(OMXVideoDecoderVP9HWR, ParamVideoVp9);
-
 private:
     OMX_ERRORTYPE initDecoder();
     OMX_ERRORTYPE destroyDecoder();