Merge "mm-video-v4l2: vdec: Check for invalid timestamp in ETB"
diff --git a/conf_files/sm6150/sm6150.mk b/conf_files/sm6150/sm6150.mk
index ced4276..57f55fe 100644
--- a/conf_files/sm6150/sm6150.mk
+++ b/conf_files/sm6150/sm6150.mk
@@ -46,6 +46,4 @@
 endif
 
 # Enable Codec2.0 HAL for pure AOSP variants.
-ifeq ($(GENERIC_ODM_IMAGE),true)
 DEVICE_MANIFEST_FILE += hardware/qcom/media/conf_files/$(MSMSTEPPE)/c2_manifest.xml
-endif
\ No newline at end of file
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 3c939af..179f53c 100644
--- a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
+++ b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
@@ -9859,14 +9859,18 @@
     }
 
 #ifdef HYPERVISOR
-    flag = 0;
+    flag &= ~ION_FLAG_CACHED;
 #endif
     ion_info->alloc_data.flags = flag;
     ion_info->alloc_data.len = buffer_size;
 
     ion_info->alloc_data.heap_id_mask = ION_HEAP(ION_SYSTEM_HEAP_ID);
     if (secure_mode && (ion_info->alloc_data.flags & ION_FLAG_SECURE)) {
+#ifdef HYPERVISOR
+        ion_info->alloc_data.heap_id_mask = ION_HEAP(ION_SECURE_DISPLAY_HEAP_ID);
+#else
         ion_info->alloc_data.heap_id_mask = ION_HEAP(MEM_HEAP_ID);
+#endif
     }
 
     /* Use secure display cma heap for obvious reasons. */
diff --git a/mm-video-v4l2/vidc/venc/inc/omx_swvenc_mpeg4.h b/mm-video-v4l2/vidc/venc/inc/omx_swvenc_mpeg4.h
index da0f98a..99f8cb3 100644
--- a/mm-video-v4l2/vidc/venc/inc/omx_swvenc_mpeg4.h
+++ b/mm-video-v4l2/vidc/venc/inc/omx_swvenc_mpeg4.h
@@ -199,6 +199,7 @@
            OMX_U32 port
         );
         int swvenc_input_log_buffers(const char *buffer, int bufferlen);
+        int swvenc_input_log_rotated_buffers(const char *buffer, int bufferlen);
         bool dev_get_hevc_profile(OMX_U32*) { return false; }
         bool dev_handle_client_input_extradata(void*) { return false; }
         void dev_get_color_format_as_string(char *, int, unsigned) {};
diff --git a/mm-video-v4l2/vidc/venc/inc/omx_video_common.h b/mm-video-v4l2/vidc/venc/inc/omx_video_common.h
index 49a1384..0f173c8 100644
--- a/mm-video-v4l2/vidc/venc/inc/omx_video_common.h
+++ b/mm-video-v4l2/vidc/venc/inc/omx_video_common.h
@@ -1,5 +1,5 @@
 /*--------------------------------------------------------------------------
-Copyright (c) 2010-2018, The Linux Foundation. All rights reserved.
+Copyright (c) 2010-2019, The Linux Foundation. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
@@ -99,13 +99,16 @@
 
 struct venc_debug_cap {
     bool in_buffer_log;
+    bool in_buffer_rotated_log;
     bool out_buffer_log;
     bool extradata_log;
     char infile_name[PROPERTY_VALUE_MAX];
+    char inrotatedfile_name[PROPERTY_VALUE_MAX];
     char outfile_name[PROPERTY_VALUE_MAX];
     char extradatafile_name[PROPERTY_VALUE_MAX];
     char log_loc[PROPERTY_VALUE_MAX];
     FILE *infile;
+    FILE *inrotatedfile;
     FILE *outfile;
     FILE *extradatafile;
 };
diff --git a/mm-video-v4l2/vidc/venc/src/omx_swvenc_mpeg4.cpp b/mm-video-v4l2/vidc/venc/src/omx_swvenc_mpeg4.cpp
index 6fd85a9..0cb6ef2 100644
--- a/mm-video-v4l2/vidc/venc/src/omx_swvenc_mpeg4.cpp
+++ b/mm-video-v4l2/vidc/venc/src/omx_swvenc_mpeg4.cpp
@@ -90,6 +90,10 @@
     m_debug.in_buffer_log = atoi(property_value);
 
     property_value[0] = '\0';
+    property_get("vendor.vidc.enc.log.in.rotated", property_value, "0");
+    m_debug.in_buffer_rotated_log = atoi(property_value);
+
+    property_value[0] = '\0';
     property_get("vendor.vidc.enc.log.out", property_value, "0");
     m_debug.out_buffer_log = atoi(property_value);
 
@@ -552,17 +556,21 @@
                     RETURN(OMX_ErrorUnsupportedSetting);
                 }
 
-                /* set the frame size */
-                Prop.id = SWVENC_PROPERTY_ID_FRAME_SIZE;
-                Prop.info.frame_size.height = portDefn->format.video.nFrameHeight;
-                Prop.info.frame_size.width  = portDefn->format.video.nFrameWidth;
+                // don't update frame size if it's unchanged
+                if (m_sInPortDef.format.video.nFrameWidth != portDefn->format.video.nFrameWidth
+                        || m_sInPortDef.format.video.nFrameHeight != portDefn->format.video.nFrameHeight) {
+                    /* set the frame size */
+                    Prop.id = SWVENC_PROPERTY_ID_FRAME_SIZE;
+                    Prop.info.frame_size.height = portDefn->format.video.nFrameHeight;
+                    Prop.info.frame_size.width  = portDefn->format.video.nFrameWidth;
 
-                Ret = swvenc_setproperty(m_hSwVenc, &Prop);
-                if (Ret != SWVENC_S_SUCCESS)
-                {
-                   DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
-                     __FUNCTION__, Ret);
-                   RETURN(OMX_ErrorUnsupportedSetting);
+                    Ret = swvenc_setproperty(m_hSwVenc, &Prop);
+                    if (Ret != SWVENC_S_SUCCESS)
+                    {
+                        DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
+                                __FUNCTION__, Ret);
+                    RETURN(OMX_ErrorUnsupportedSetting);
+                    }
                 }
 
                 /* set the input frame-rate */
@@ -1849,7 +1857,7 @@
     Prop.info.frame_size.width = inHeight;
 
     DEBUG_PRINT_HIGH("setting flipped dimensions to swencoder, WxH (%d x %d)",
-            inWidth, inHeight);
+            Prop.info.frame_size.width, Prop.info.frame_size.height);
     Ret = swvenc_setproperty(m_hSwVenc, &Prop);
     if (Ret != SWVENC_S_SUCCESS) {
         // currently, set dimensions to encoder can only be called when encoder is
@@ -2388,6 +2396,7 @@
 
     if (m_debug.in_buffer_log)
     {
+       // dump before rotation, un-rotated buffer
        swvenc_input_log_buffers((const char*)ipbuffer.p_buffer, ipbuffer.filled_length);
     }
 
@@ -2396,6 +2405,11 @@
             DEBUG_PRINT_ERROR("rotate failed");
             return OMX_ErrorUndefined;
         }
+        if (m_debug.in_buffer_rotated_log) {
+            // dump after rotation, rotated buffer
+            DEBUG_PRINT_ERROR("dump rotated");
+            swvenc_input_log_rotated_buffers((const char*)ipbuffer.p_buffer, ipbuffer.filled_length);
+        }
     }
 
     Ret = swvenc_emptythisbuffer(m_hSwVenc, &ipbuffer);
@@ -2873,8 +2887,8 @@
     if (m_debug.out_buffer_log && !m_debug.outfile)
     {
         int size = 0;
-        int width = m_sInPortDef.format.video.nFrameWidth;
-        int height = m_sInPortDef.format.video.nFrameHeight;
+        int width = m_sOutPortDef.format.video.nFrameWidth;
+        int height = m_sOutPortDef.format.video.nFrameHeight;
         if(SWVENC_CODEC_MPEG4 == m_codec)
         {
            size = snprintf(m_debug.outfile_name, PROPERTY_VALUE_MAX,
@@ -2960,6 +2974,59 @@
    RETURN(0);
 }
 
+int omx_venc::swvenc_input_log_rotated_buffers(const char *buffer, int bufferlen)
+{
+   int width = m_sInPortDef.format.video.nFrameWidth;
+   int height = m_sInPortDef.format.video.nFrameHeight;
+   if (m_bIsInFlipDone) {
+       auto v = width;
+       width = height;
+       height = v;
+   }
+   int stride = SWVENC_Y_STRIDE(COLOR_FMT_NV12, width);
+   int scanlines = SWVENC_Y_SCANLINES(COLOR_FMT_NV12, height);
+   char *temp = (char*)buffer;
+
+   if (!m_debug.inrotatedfile)
+   {
+       int size = snprintf(m_debug.inrotatedfile_name, PROPERTY_VALUE_MAX,
+                      "%s/input_enc_rotated_%d_%d_%p.yuv",
+                      m_debug.log_loc, width, height, this);
+       if ((size > PROPERTY_VALUE_MAX) || (size < 0))
+       {
+           DEBUG_PRINT_ERROR("Failed to open input rotated file: %s for logging size:%d",
+                              m_debug.inrotatedfile_name, size);
+           RETURN(-1);
+       }
+       DEBUG_PRINT_LOW("input rotated filename = %s", m_debug.inrotatedfile_name);
+       m_debug.inrotatedfile = fopen (m_debug.inrotatedfile_name, "ab");
+       if (!m_debug.inrotatedfile)
+       {
+           DEBUG_PRINT_HIGH("Failed to open input rotated file: %s for logging",
+              m_debug.inrotatedfile_name);
+           m_debug.inrotatedfile_name[0] = '\0';
+           RETURN(-1);
+       }
+   }
+   if (m_debug.inrotatedfile && buffer && bufferlen)
+   {
+       DEBUG_PRINT_LOW("%s buffer length: %d", __func__, bufferlen);
+       for (int i = 0; i < height; i++)
+       {
+          fwrite(temp, width, 1, m_debug.inrotatedfile);
+          temp += stride;
+       }
+       temp = (char*)(buffer + (stride * scanlines));
+       for(int i = 0; i < height/2; i++)
+       {
+          fwrite(temp, width, 1, m_debug.inrotatedfile);
+          temp += stride;
+      }
+   }
+
+   RETURN(0);
+}
+
 int omx_venc::dev_extradata_log_buffers(char *buffer, bool input)
 {
    ENTER_FUNC();
diff --git a/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp b/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
index 7eb5bf1..10faa15 100644
--- a/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
+++ b/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
@@ -501,6 +501,7 @@
                 &m_sOutPortDef.nBufferSize,
                 m_sOutPortDef.nPortIndex) != true) {
         eRet = OMX_ErrorUndefined;
+        goto init_error;
     }
 
     // Initialize the video color format for input port
diff --git a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
index 139047d..0d2c09a 100755
--- a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
+++ b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
@@ -4162,7 +4162,7 @@
     struct v4l2_buffer buf;
     struct v4l2_requestbuffers bufreq;
     struct v4l2_plane plane[VIDEO_MAX_PLANES];
-    int rc = 0, extra_idx;
+    int rc = 0, extra_idx, c2d_enabled = 0;
     bool interlace_flag = false;
     struct OMX_BUFFERHEADERTYPE *bufhdr;
     LEGACY_CAM_METADATA_TYPE * meta_buf = NULL;
@@ -4538,6 +4538,7 @@
                 // color_format == 1 ==> RGBA to YUV Color-converted buffer
                 // Buffers color-converted via C2D have 601-Limited color
                 if (!streaming[OUTPUT_PORT]) {
+                    c2d_enabled = 1;
                     DEBUG_PRINT_HIGH("Setting colorspace 601-L for Color-converted buffer");
                     venc_set_colorspace(MSM_VIDC_BT601_6_625, 0 /*range-limited*/,
                             MSM_VIDC_TRANSFER_601_6_525, MSM_VIDC_MATRIX_601_6_525);
@@ -4559,9 +4560,9 @@
         }
     }
 
-    if (!streaming[OUTPUT_PORT] &&
+    if (!streaming[OUTPUT_PORT] && (c2d_enabled ||
         (m_sVenc_cfg.inputformat != V4L2_PIX_FMT_NV12_TP10_UBWC &&
-         m_sVenc_cfg.inputformat != V4L2_PIX_FMT_NV12_UBWC)) {
+         m_sVenc_cfg.inputformat != V4L2_PIX_FMT_NV12_UBWC))) {
         if (bframe_implicitly_enabled) {
             DEBUG_PRINT_HIGH("Disabling implicitly enabled B-frames");
             intra_period.num_pframes = nPframes_cache;