mm-video-legacy: vdec: synchronize color-conversion calls

Synchronize conversion with open/close of converter to
avoid race conditions accessing the color-converter

bug: 17188136
Change-Id: I333f1fa9a922e456f886f5b3d8081baa7ad53a4e
diff --git a/mm-video-legacy/vidc/vdec/inc/omx_vdec.h b/mm-video-legacy/vidc/vdec/inc/omx_vdec.h
index 44f34c9..240ffb6 100644
--- a/mm-video-legacy/vidc/vdec/inc/omx_vdec.h
+++ b/mm-video-legacy/vidc/vdec/inc/omx_vdec.h
@@ -668,6 +668,7 @@
     //*******************MEMBER VARIABLES *************************
     //*************************************************************
     pthread_mutex_t       m_lock;
+    pthread_mutex_t       c_lock;
     //sem to handle the minimum procesing of commands
     sem_t                 m_cmd_lock;
     bool              m_error_propogated;
diff --git a/mm-video-legacy/vidc/vdec/src/omx_vdec.cpp b/mm-video-legacy/vidc/vdec/src/omx_vdec.cpp
index 309a28f..fb66cb0 100644
--- a/mm-video-legacy/vidc/vdec/src/omx_vdec.cpp
+++ b/mm-video-legacy/vidc/vdec/src/omx_vdec.cpp
@@ -543,6 +543,7 @@
   drv_ctx.video_driver_fd = -1;
   m_vendor_config.pData = NULL;
   pthread_mutex_init(&m_lock, NULL);
+  pthread_mutex_init(&c_lock, NULL);
   sem_init(&m_cmd_lock,0,0);
 #ifdef _ANDROID_
   char extradata_value[PROPERTY_VALUE_MAX] = {0};
@@ -582,6 +583,7 @@
   DEBUG_PRINT_HIGH("Waiting on OMX Async Thread exit");
   pthread_join(async_thread_id,NULL);
   pthread_mutex_destroy(&m_lock);
+  pthread_mutex_destroy(&c_lock);
   sem_destroy(&m_cmd_lock);
 #ifdef _ANDROID_
   if (perf_flag)
@@ -9073,6 +9075,7 @@
     DEBUG_PRINT_ERROR("\nupdate_buffer_req: Unsupported color conversion");
     return false;
   }
+  pthread_mutex_lock(&omx->c_lock);
   c2d.close();
   status = c2d.open(omx->drv_ctx.video_resolution.frame_height,
                     omx->drv_ctx.video_resolution.frame_width,
@@ -9099,6 +9102,7 @@
             buffer_alignment_req = omx->drv_ctx.op_buf.alignment;
     }
   }
+  pthread_mutex_unlock(&omx->c_lock);
   return status;
 }
 
@@ -9118,6 +9122,7 @@
     DEBUG_PRINT_ERROR("\n Incorrect color format");
     status = false;
   }
+  pthread_mutex_lock(&omx->c_lock);
   if (status && (drv_color_format != dest_color_format)) {
     if (dest_color_format != OMX_COLOR_FormatYUV420Planar) {
       DEBUG_PRINT_ERROR("\n Unsupported color format for c2d");
@@ -9139,6 +9144,7 @@
       c2d.destroy();
     enabled = false;
   }
+  pthread_mutex_unlock(&omx->c_lock);
   return status;
 }
 
@@ -9169,13 +9175,16 @@
     m_out_mem_ptr_client[index].nTimeStamp = bufadd->nTimeStamp;
     bool status;
     if (!omx->in_reconfig && !omx->output_flush_progress) {
+      pthread_mutex_lock(&omx->c_lock);
       status = c2d.convert(omx->drv_ctx.ptr_outputbuffer[index].pmem_fd,
                   bufadd->pBuffer, bufadd->pBuffer, pmem_fd[index],
                   pmem_baseaddress[index], pmem_baseaddress[index]);
+      pthread_mutex_unlock(&omx->c_lock);
       m_out_mem_ptr_client[index].nFilledLen = buffer_size_req;
       if (!status){
         DEBUG_PRINT_ERROR("\n Failed color conversion %d", status);
-        return NULL;
+        m_out_mem_ptr_client[index].nFilledLen = 0;
+        return &m_out_mem_ptr_client[index];
       }
     } else
       m_out_mem_ptr_client[index].nFilledLen = 0;
@@ -9207,10 +9216,14 @@
 {
   if (!enabled)
     buffer_size = omx->drv_ctx.op_buf.buffer_size;
-  else
+  else {
+    pthread_mutex_lock(&omx->c_lock);
     if (!c2d.get_buffer_size(C2D_OUTPUT,buffer_size)) {
       DEBUG_PRINT_ERROR("\n Get buffer size failed");
+      pthread_mutex_unlock(&omx->c_lock);
       return false;
+    }
+    pthread_mutex_unlock(&omx->c_lock);
   }
   if (buffer_size < omx->drv_ctx.op_buf.buffer_size)
         buffer_size = omx->drv_ctx.op_buf.buffer_size;
@@ -9247,8 +9260,10 @@
   else
     allocated_count = 0;
   if (!allocated_count) {
+    pthread_mutex_lock(&omx->c_lock);
     c2d.close();
     init_members();
+    pthread_mutex_unlock(&omx->c_lock);
   }
   return omx->free_output_buffer(&omx->m_out_mem_ptr[index]);
 }