mm-video: vdec: Separate meta buffers support in secure mode

Extradata is appended at the end of the output buffer
in the normal scenario. But in case of secure playback,
we can not parse the input and output buffers,
therefore extradata support is unavailable in this case.
This change adds the extradata support in case of secure
playback. For secure playback, extradata is now dumped
in the separate meta buffers that enables HLOS to read
and parse it.

Change-Id: I1f23b066ceb72d83c1f2b1d3d219ae39d92e1db1
diff --git a/mm-video-v4l2/vidc/vdec/src/omx_vdec_msm8974.cpp b/mm-video-v4l2/vidc/vdec/src/omx_vdec_msm8974.cpp
index 5057067..5dd72c3 100644
--- a/mm-video-v4l2/vidc/vdec/src/omx_vdec_msm8974.cpp
+++ b/mm-video-v4l2/vidc/vdec/src/omx_vdec_msm8974.cpp
@@ -551,6 +551,7 @@
     h264_parser(NULL),
     client_extradata(0),
     m_reject_avc_1080p_mp (0),
+    m_other_extradata(NULL),
 #ifdef _ANDROID_
     m_enable_android_native_buffers(OMX_FALSE),
     m_use_android_native_buffers(OMX_FALSE),
@@ -1443,6 +1444,11 @@
         secure_mode = true;
         arbitrary_bytes = false;
         role = (OMX_STRING)"OMX.qcom.video.decoder.avc";
+    } else if (!strncmp(role, "OMX.qcom.video.decoder.mpeg2.secure",
+        OMX_MAX_STRINGNAME_SIZE)){
+        secure_mode = true;
+        arbitrary_bytes = false;
+        role = (OMX_STRING)"OMX.qcom.video.decoder.mpeg2";
     }
 
     drv_ctx.video_driver_fd = open(device_name, O_RDWR);
@@ -1727,7 +1733,7 @@
 
         m_state = OMX_StateLoaded;
 #ifdef DEFAULT_EXTRADATA
-        if (eRet == OMX_ErrorNone && !secure_mode)
+        if (eRet == OMX_ErrorNone)
             enable_extradata(DEFAULT_EXTRADATA, true, true);
 #endif
         eRet=get_buffer_req(&drv_ctx.ip_buf);
@@ -3943,6 +3949,13 @@
         }
     }
 #endif
+    if (!m_other_extradata) {
+        m_other_extradata = (OMX_OTHER_EXTRADATATYPE *)malloc(drv_ctx.extradata_info.buffer_size);
+        if (!m_other_extradata) {
+            DEBUG_PRINT_ERROR("Failed to alloc memory\n");
+            return OMX_ErrorInsufficientResources;
+        }
+    }
     return OMX_ErrorNone;
 }
 
@@ -3956,6 +3969,10 @@
     }
     memset(&drv_ctx.extradata_info, 0, sizeof(drv_ctx.extradata_info));
 #endif
+    if (m_other_extradata) {
+        free(m_other_extradata);
+        m_other_extradata = NULL;
+    }
 }
 
 OMX_ERRORTYPE  omx_vdec::use_output_buffer(
@@ -4153,6 +4170,9 @@
             }
         m_pmem_info[i].offset = drv_ctx.ptr_outputbuffer[i].offset;
         m_pmem_info[i].pmem_fd = drv_ctx.ptr_outputbuffer[i].pmem_fd;
+        m_pmem_info[i].size = drv_ctx.ptr_outputbuffer[i].buffer_len;
+        m_pmem_info[i].mapped_size = drv_ctx.ptr_outputbuffer[i].mmaped_size;
+        m_pmem_info[i].buffer = drv_ctx.ptr_outputbuffer[i].bufferaddr;
 
         *bufferHdr = (m_out_mem_ptr + i );
         if (secure_mode)
@@ -4913,6 +4933,9 @@
                 drv_ctx.ptr_outputbuffer[i].offset = drv_ctx.op_buf.buffer_size*i;
                 drv_ctx.ptr_outputbuffer[i].bufferaddr =
                     pmem_baseaddress + (drv_ctx.op_buf.buffer_size*i);
+                m_pmem_info[i].size = drv_ctx.ptr_outputbuffer[i].buffer_len;
+                m_pmem_info[i].mapped_size = drv_ctx.ptr_outputbuffer[i].mmaped_size;
+                m_pmem_info[i].buffer = drv_ctx.ptr_outputbuffer[i].bufferaddr;
 
                 DEBUG_PRINT_LOW("pmem_fd = %d offset = %d address = %p",
                         pmem_fd, drv_ctx.ptr_outputbuffer[i].offset,
@@ -7349,7 +7372,7 @@
         alloc_data->flags |= ION_SECURE;
 
     alloc_data->heap_mask = ION_HEAP(ION_IOMMU_HEAP_ID);
-    if (secure_mode)
+    if (secure_mode && (alloc_data->flags & ION_SECURE))
         alloc_data->heap_mask = ION_HEAP(MEM_HEAP_ID);
     rc = ioctl(fd,ION_IOC_ALLOC,alloc_data);
     if (rc || !alloc_data->handle) {
@@ -8037,11 +8060,18 @@
     if (!drv_ctx.extradata_info.uaddr) {
         return;
     }
-    p_extra = (OMX_OTHER_EXTRADATATYPE *)
+    if (!secure_mode)
+        p_extra = (OMX_OTHER_EXTRADATATYPE *)
         ((unsigned)(pBuffer + p_buf_hdr->nOffset + p_buf_hdr->nFilledLen + 3)&(~3));
+    else
+        p_extra = m_other_extradata;
+
     char *p_extradata = drv_ctx.extradata_info.uaddr + buf_index * drv_ctx.extradata_info.buffer_size;
-    if ((OMX_U8*)p_extra > (pBuffer + p_buf_hdr->nAllocLen))
+
+  if (!secure_mode && ((OMX_U8*)p_extra > (pBuffer + p_buf_hdr->nAllocLen))) {
         p_extra = NULL;
+        return;
+  }
     OMX_OTHER_EXTRADATATYPE *data = (struct OMX_OTHER_EXTRADATATYPE *)p_extradata;
     if (data) {
         while ((consumed_len < drv_ctx.extradata_info.buffer_size)
@@ -8068,7 +8098,7 @@
                     if (m_enable_android_native_buffers)
                         setMetaData((private_handle_t *)native_buffer[buf_index].privatehandle,
                                 PP_PARAM_INTERLACED, (void*)&enable);
-                    if (!secure_mode && (client_extradata & OMX_INTERLACE_EXTRADATA)) {
+                    if (client_extradata & OMX_INTERLACE_EXTRADATA) {
                         append_interlace_extradata(p_extra, payload->format);
                         p_extra = (OMX_OTHER_EXTRADATATYPE *) (((OMX_U8 *) p_extra) + p_extra->nSize);
                     }