mm-video-v4l2: Enhance decoder dump and fix O/P dump issue

* Add timestamp as id to dump file name
  to avoid overwrite of dump file for same instance
  pointer
* Add sequence count to dump file name to avoid overwrite
  for port reconfig without resolution change
* Dump C2D output as well using vendor.vidc.dec.log.cc.out
  property
* Use correct buffer header for venus ouput dump when
  color conversion is enabled
* Currently when decoder is in dynamic buffer mode,
  output buffers are not mapped and dump feature can't work.
  Add mmap/munmap in dump function to fix it.

Change-Id: Id851c591e6c77b97ae1b4d7bc3eff0c0f62ab905
diff --git a/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h b/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h
index b90b2f8..109d814 100644
--- a/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h
+++ b/mm-video-v4l2/vidc/vdec/inc/omx_vdec.h
@@ -534,15 +534,20 @@
     bool in_buffer_log;
     bool out_buffer_log;
     bool out_meta_buffer_log;
+    bool out_cc_buffer_log;
     char infile_name[PROPERTY_VALUE_MAX + 36];
     char outfile_name[PROPERTY_VALUE_MAX + 36];
+    char ccoutfile_name[PROPERTY_VALUE_MAX + 36];
     char out_ymetafile_name[PROPERTY_VALUE_MAX + 36];
     char out_uvmetafile_name[PROPERTY_VALUE_MAX + 36];
     char log_loc[PROPERTY_VALUE_MAX];
     FILE *infile;
     FILE *outfile;
+    FILE *ccoutfile;
     FILE *out_ymeta_file;
     FILE *out_uvmeta_file;
+    int64_t session_id;
+    int seq_count;
 };
 
 struct dynamic_buf_list {
@@ -1372,6 +1377,7 @@
         struct debug_cap m_debug;
         int log_input_buffers(const char *, int, uint64_t);
         int log_output_buffers(OMX_BUFFERHEADERTYPE *);
+        int log_cc_output_buffers(OMX_BUFFERHEADERTYPE *);
 #ifdef _MSM8974_
         void send_codec_config();
 #endif
diff --git a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
index 6fb7de9..960b8f0 100644
--- a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
+++ b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
@@ -690,11 +690,18 @@
     property_value[0] = '\0';
     property_get("vendor.vidc.dec.log.in", property_value, "0");
     m_debug.in_buffer_log = atoi(property_value);
+    DEBUG_PRINT_HIGH("vendor.vidc.dec.log.in value is %d", m_debug.in_buffer_log);
 
     property_value[0] = '\0';
     property_get("vendor.vidc.dec.log.out", property_value, "0");
     m_debug.out_buffer_log = atoi(property_value);
     snprintf(m_debug.log_loc, PROPERTY_VALUE_MAX, "%s", BUFFER_LOG_LOC);
+    DEBUG_PRINT_HIGH("vendor.vidc.dec.log.out value is %d", m_debug.out_buffer_log);
+
+    property_value[0] = '\0';
+    property_get("vendor.vidc.dec.log.cc.out", property_value, "0");
+    m_debug.out_cc_buffer_log = atoi(property_value);
+    DEBUG_PRINT_HIGH("vendor.vidc.dec.log.cc.out value is %d", m_debug.out_buffer_log);
 
     property_value[0] = '\0';
     property_get("vendor.vidc.dec.meta.log.out", property_value, "0");
@@ -727,6 +734,11 @@
         DEBUG_PRINT_HIGH("DRC enabled");
     }
 
+    struct timeval te;
+    gettimeofday(&te, NULL);
+    m_debug.session_id = te.tv_sec*1000LL + te.tv_usec/1000;
+    m_debug.seq_count = 0;
+
 #ifdef _UBWC_
     property_value[0] = '\0';
     property_get("debug.gralloc.gfx_ubwc_disable", property_value, "0");
@@ -1864,6 +1876,10 @@
                                             fclose(pThis->m_debug.outfile);
                                             pThis->m_debug.outfile = NULL;
                                         }
+                                        if (pThis->m_debug.ccoutfile) {
+                                            fclose(pThis->m_debug.ccoutfile);
+                                            pThis->m_debug.ccoutfile = NULL;
+                                        }
                                         if (pThis->m_debug.out_ymeta_file) {
                                             fclose(pThis->m_debug.out_ymeta_file);
                                             pThis->m_debug.out_ymeta_file = NULL;
@@ -1872,6 +1888,7 @@
                                             fclose(pThis->m_debug.out_uvmeta_file);
                                             pThis->m_debug.out_uvmeta_file = NULL;
                                         }
+                                        pThis->m_debug.seq_count++;
 
                                         if (pThis->secure_mode && pThis->m_cb.EventHandler && pThis->in_reconfig) {
                                             pThis->prefetchNewBuffers(true);
@@ -2059,22 +2076,47 @@
     return 0;
 }
 
+int omx_vdec::log_cc_output_buffers(OMX_BUFFERHEADERTYPE *buffer) {
+
+    if (!client_buffers.is_color_conversion_enabled() ||
+        !m_debug.out_cc_buffer_log || !buffer || !buffer->nFilledLen)
+        return 0;
+
+    if (m_debug.out_cc_buffer_log && !m_debug.ccoutfile) {
+        snprintf(m_debug.ccoutfile_name, OMX_MAX_STRINGNAME_SIZE, "%s/output_cc_%d_%d_%p_%" PRId64 "_%d.yuv",
+                m_debug.log_loc, drv_ctx.video_resolution.frame_width, drv_ctx.video_resolution.frame_height, this,
+                m_debug.session_id, m_debug.seq_count);
+        m_debug.ccoutfile = fopen (m_debug.ccoutfile_name, "ab");
+        if (!m_debug.ccoutfile) {
+            DEBUG_PRINT_HIGH("Failed to open output file: %s for logging", m_debug.log_loc);
+            m_debug.ccoutfile_name[0] = '\0';
+            return -1;
+        }
+        DEBUG_PRINT_HIGH("Opened CC output file: %s for logging", m_debug.ccoutfile_name);
+    }
+
+    fwrite(buffer->pBuffer, buffer->nFilledLen, 1, m_debug.ccoutfile);
+    return 0;
+}
+
 int omx_vdec::log_output_buffers(OMX_BUFFERHEADERTYPE *buffer) {
     int buf_index = 0;
     char *temp = NULL;
-
+    char *bufaddr = NULL;
     if (!(m_debug.out_buffer_log || m_debug.out_meta_buffer_log) || !buffer || !buffer->nFilledLen)
         return 0;
 
     if (m_debug.out_buffer_log && !m_debug.outfile) {
-        snprintf(m_debug.outfile_name, OMX_MAX_STRINGNAME_SIZE, "%s/output_%d_%d_%p.yuv",
-                m_debug.log_loc, drv_ctx.video_resolution.frame_width, drv_ctx.video_resolution.frame_height, this);
+        snprintf(m_debug.outfile_name, OMX_MAX_STRINGNAME_SIZE, "%s/output_%d_%d_%p_%" PRId64 "_%d.yuv",
+                m_debug.log_loc, drv_ctx.video_resolution.frame_width, drv_ctx.video_resolution.frame_height, this,
+                m_debug.session_id, m_debug.seq_count);
         m_debug.outfile = fopen (m_debug.outfile_name, "ab");
         if (!m_debug.outfile) {
             DEBUG_PRINT_HIGH("Failed to open output file: %s for logging", m_debug.log_loc);
             m_debug.outfile_name[0] = '\0';
             return -1;
         }
+        DEBUG_PRINT_HIGH("Opened output file: %s for logging", m_debug.outfile_name);
     }
 
     if (m_debug.out_meta_buffer_log && !m_debug.out_ymeta_file && !m_debug.out_uvmeta_file) {
@@ -2093,7 +2135,18 @@
     }
 
     buf_index = buffer - m_out_mem_ptr;
-    temp = (char *)drv_ctx.ptr_outputbuffer[buf_index].bufferaddr;
+    bufaddr = (char *)drv_ctx.ptr_outputbuffer[buf_index].bufferaddr;
+    if (dynamic_buf_mode && !secure_mode) {
+        bufaddr = (char *)mmap(NULL, drv_ctx.op_buf.buffer_size,
+                                PROT_READ|PROT_WRITE, MAP_SHARED,
+                                drv_ctx.ptr_outputbuffer[buf_index].pmem_fd,0);
+        //mmap returns (void *)-1 on failure and sets error code in errno.
+        if (bufaddr == MAP_FAILED) {
+            DEBUG_PRINT_ERROR("mmap failed - errno: %d", errno);
+            return -1;
+        }
+    }
+    temp = bufaddr;
 
     if (drv_ctx.output_format == VDEC_YUV_FORMAT_NV12_UBWC ||
             drv_ctx.output_format == VDEC_YUV_FORMAT_NV12_TP10_UBWC) {
@@ -2125,13 +2178,12 @@
             y_meta_plane = MSM_MEDIA_ALIGN(y_meta_stride * y_meta_scanlines, 4096);
             y_plane = MSM_MEDIA_ALIGN(y_stride * y_sclines, 4096);
 
-            temp = (char *)drv_ctx.ptr_outputbuffer[buf_index].bufferaddr;
             for (i = 0; i < y_meta_scanlines; i++) {
                  bytes_written = fwrite(temp, y_meta_stride, 1, m_debug.out_ymeta_file);
                  temp += y_meta_stride;
             }
 
-            temp = (char *)drv_ctx.ptr_outputbuffer[buf_index].bufferaddr + y_meta_plane + y_plane;
+            temp = bufaddr + y_meta_plane + y_plane;
             for(i = 0; i < uv_meta_scanlines; i++) {
                 bytes_written += fwrite(temp, uv_meta_stride, 1, m_debug.out_uvmeta_file);
                 temp += uv_meta_stride;
@@ -2155,13 +2207,18 @@
              bytes_written = fwrite(temp, drv_ctx.video_resolution.frame_width, 1, m_debug.outfile);
              temp += stride;
         }
-        temp = (char *)drv_ctx.ptr_outputbuffer[buf_index].bufferaddr + stride * scanlines;
+        temp = bufaddr + stride * scanlines;
         int stride_c = stride;
         for(i = 0; i < drv_ctx.video_resolution.frame_height/2; i++) {
             bytes_written += fwrite(temp, drv_ctx.video_resolution.frame_width, 1, m_debug.outfile);
             temp += stride_c;
         }
     }
+
+    if (dynamic_buf_mode && !secure_mode) {
+        munmap(drv_ctx.ptr_outputbuffer[buf_index].bufferaddr,
+              drv_ctx.ptr_outputbuffer[buf_index].mmaped_size);
+    }
     return 0;
 }
 
@@ -8367,6 +8424,10 @@
         fclose(m_debug.outfile);
         m_debug.outfile = NULL;
     }
+    if (m_debug.ccoutfile) {
+        fclose(m_debug.ccoutfile);
+        m_debug.ccoutfile = NULL;
+    }
     if (m_debug.out_ymeta_file) {
         fclose(m_debug.out_ymeta_file);
         m_debug.out_ymeta_file = NULL;
@@ -9083,7 +9144,8 @@
         }
 
         if (il_buffer) {
-            log_output_buffers(il_buffer);
+            log_output_buffers(buffer);
+            log_cc_output_buffers(il_buffer);
             if (dynamic_buf_mode) {
                 unsigned int nPortIndex = 0;
                 nPortIndex = buffer-((OMX_BUFFERHEADERTYPE *)client_buffers.get_il_buf_hdr());