change to use width/height in metadatabuffer to map surface instead of encoder width/height

BZ: 55305

It is to avoid 1088 src picture wrong mapping when encoder width/height is configured as 1080

Change-Id: I6c3769157d1aa598151500effc1efdf9b2bd19d0
Signed-off-by: Zhao Liang <leo.zhao@intel.com>
Reviewed-on: http://android.intel.com:8080/64349
Reviewed-by: Zheng, Hongjiang A <hongjiang.a.zheng@intel.com>
Reviewed-by: Wang, Elaine <elaine.wang@intel.com>
Reviewed-by: Shi, PingX <pingx.shi@intel.com>
Tested-by: Shi, PingX <pingx.shi@intel.com>
Reviewed-by: Jiang, Fei <fei.jiang@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
diff --git a/test/mix_encoder.cpp b/test/mix_encoder.cpp
index d4b88c0..5ec64b5 100644
--- a/test/mix_encoder.cpp
+++ b/test/mix_encoder.cpp
@@ -45,6 +45,8 @@
 static uint32_t gHeight = 720;
 static uint32_t gStride = 1280;
 static uint32_t gFrameRate = 30;
+static uint32_t gEncodeWidth = 0;
+static uint32_t gEncodeHeight = 0;
 
 static char* gFile = (char*)"out.264";
 
@@ -54,6 +56,7 @@
 
 //for uploading src pictures, also for Camera malloc, WiDi clone, raw mode usrptr storage
 static uint8_t* gUsrptr[gSrcFrames];
+static uint8_t* gMallocPtr[gSrcFrames];
 
 //for metadatabuffer transfer
 static IntelMetadataBuffer* gIMB[gSrcFrames] = {NULL};
@@ -177,8 +180,8 @@
     ret = gVideoEncoder->getParameters(&gEncoderParams);
     CHECK_ENCODE_STATUS("getParameters");
     
-    gEncoderParams.resolution.height = gHeight;
-    gEncoderParams.resolution.width = gWidth;
+    gEncoderParams.resolution.height = gEncodeHeight;
+    gEncoderParams.resolution.width = gEncodeWidth;
     gEncoderParams.frameRate.frameRateDenom = 1;
     gEncoderParams.frameRate.frameRateNum = gFrameRate;
     gEncoderParams.rcMode = gRC;
@@ -294,7 +297,8 @@
             
     for(int i = 0; i < gSrcFrames; i ++) 
     {
-        gUsrptr[i] = (uint8_t*)malloc(size);
+        gMallocPtr[i] = (uint8_t*)malloc(size + 4095);
+        gUsrptr[i] = (uint8_t*)((((int )gMallocPtr[i] + 4095) / 4096 ) * 4096);
 
         gIMB[i] = new IntelMetadataBuffer(MetadataBufferTypeCameraSource, (int32_t)gUsrptr[i]);
 
@@ -556,9 +560,15 @@
                 case 's':
                     gSyncEncMode = atoi(optarg);
                     break;
+                case 'k':
+                    gEncodeWidth = atoi(optarg);
+                    break;
+                case 'g':
+                    gEncodeHeight = atoi(optarg);
+                    break;
                 case '?':
                 default:
-         	     printf("\n./mix_encode -c <Codec> -b <Bit rate> -r <Rate control> -w <Width> -h <Height> -n <Frame_num> -m <Mode> -s <Sync mode> -f <Output file>\n");
+                     printf("\n./mix_encode -c <Codec> -b <Bit rate> -r <Rate control> -w <Width> -h <Height> -k <EncodeWidth> -g <EncodeHight> -n <Frame_num> -m <Mode> -s <Sync mode> -f <Output file>\n");
               	     printf("\nCodec:\n");
               	     printf("0: H264 (default)\n1: MPEG4\n2: H263\n");
               	     printf("\nRate control:\n");
@@ -569,6 +579,17 @@
         }
     }
 
+    if (gMode == 5 || gMode == 6)
+    {
+        gWidth = ((gWidth + 15 ) / 16 ) * 16;
+        gHeight = ((gHeight + 15 ) / 16 ) * 16;
+    }
+
+    if (gEncodeWidth == 0 || gEncodeHeight == 0)
+    {
+        gEncodeWidth = gWidth;
+        gEncodeHeight = gHeight;
+    }
     return 0;
 }
 
@@ -619,9 +640,8 @@
     }
     
     printf("\nStart %s Encoding ....\n", codec);
-    printf("Mode is %s, RC mode is %s, Width=%d, Height=%d, Bitrate=%dbps, EncodeFrames=%d, SyncMode=%d, out file is %s\n\n", gModeString[gMode], gRCModeString[gRCMode], gWidth, gHeight, gBitrate, gEncFrames, gSyncEncMode, gFile);
-
-//sleep(10);    
+    printf("Mode is %s, RC mode is %s, Src Width=%d, Height=%d, Encode Width=%d, Height=%d \n", gModeString[gMode], gRCModeString[gRCMode], gWidth, gHeight, gEncodeWidth, gEncodeHeight);
+    printf("Bitrate=%dbps, EncodeFrames=%d, SyncMode=%d, out file is %s\n\n", gBitrate, gEncFrames, gSyncEncMode, gFile);
 
 for(int i=0; i<1; i++)
 {
@@ -659,8 +679,6 @@
             break;
     }
 
-//sleep(10);
-
     //upload src data
     for(int i=0; i<gSrcFrames; i++)
         YUV_generator_planar(gWidth, gHeight, gUsrptr[i], gWidth, gUsrptr[i]+gWidth*gHeight, gWidth, 0, 0, 1);
@@ -740,7 +758,7 @@
         case 4: //Raw
             for(int i=0; i<gSrcFrames; i++)
             {
-                delete gUsrptr[i];
+                free(gMallocPtr[i]);
             }
             break;
         case 1: //WiDi clone
diff --git a/videoencoder/VideoEncoderBase.cpp b/videoencoder/VideoEncoderBase.cpp
index d102165..aaa230e 100644
--- a/videoencoder/VideoEncoderBase.cpp
+++ b/videoencoder/VideoEncoderBase.cpp
@@ -1516,7 +1516,7 @@
     CHECK_VA_STATUS_RETURN("vaUnlockSurface");
 
     vaSurfaceAttrib.count = 1;
-    vaSurfaceAttrib.size = mComParams.resolution.width * mComParams.resolution.height * 3 /2;
+    vaSurfaceAttrib.size = map->vinfo.width * map->vinfo.height * 3 / 2;
     vaSurfaceAttrib.luma_stride = lumaStride;
     vaSurfaceAttrib.chroma_u_stride = chromaUStride;
     vaSurfaceAttrib.chroma_v_stride = chromaVStride;
@@ -1528,7 +1528,7 @@
     vaSurfaceAttrib.type = VAExternalMemoryKernelDRMBufffer;
 
     vaStatus = vaCreateSurfacesWithAttribute(
-            mVADisplay, mComParams.resolution.width, mComParams.resolution.height, VA_RT_FORMAT_YUV420,
+            mVADisplay, map->vinfo.width, map->vinfo.height, VA_RT_FORMAT_YUV420,
             1, &surface, &vaSurfaceAttrib);
 
     CHECK_VA_STATUS_RETURN("vaCreateSurfaceFromKbuf");
@@ -1571,8 +1571,8 @@
 
     vaStatus = vaCreateSurfacesWithAttribute(
             mVADisplay,
-            mComParams.resolution.width,
-            mComParams.resolution.height,
+            map->vinfo.width,
+            map->vinfo.height,
             VA_RT_FORMAT_YUV420,
             1,
             &surface,
@@ -1607,7 +1607,7 @@
     vaSurfaceAttrib.buffers = &buf;
     
     vaSurfaceAttrib.count = 1;
-    vaSurfaceAttrib.size = map->vinfo.lumaStride * mComParams.resolution.height * 3 / 2;
+    vaSurfaceAttrib.size = map->vinfo.lumaStride * map->vinfo.height * 3 / 2;
     vaSurfaceAttrib.luma_stride = map->vinfo.lumaStride;
     vaSurfaceAttrib.chroma_u_stride = map->vinfo.chromStride;
     vaSurfaceAttrib.chroma_v_stride = map->vinfo.chromStride;
@@ -1619,7 +1619,7 @@
     vaSurfaceAttrib.type = VAExternalMemoryKernelDRMBufffer;
 
     vaStatus = vaCreateSurfacesWithAttribute(
-            mVADisplay, mComParams.resolution.width, mComParams.resolution.height, VA_RT_FORMAT_YUV420,
+            mVADisplay, map->vinfo.width, map->vinfo.height, VA_RT_FORMAT_YUV420,
             1, &surface, &vaSurfaceAttrib);
 
     CHECK_VA_STATUS_RETURN("vaCreateSurfaceFromKbuf");
@@ -1650,8 +1650,8 @@
     vaSurfaceAttrib.buffers[0] = (uint32_t)map->value;
     vaStatus = vaCreateSurfacesWithAttribute(
             mVADisplay,
-            mComParams.resolution.width,
-            mComParams.resolution.height,
+            map->vinfo.width,
+            map->vinfo.height,
             VA_RT_FORMAT_YUV420,
             1,
             &surface,
@@ -1686,7 +1686,7 @@
     vaSurfaceAttrib.type = VAExternalMemoryUserPointer;
 
     vaStatus = vaCreateSurfacesWithAttribute(
-            mVADisplay, mComParams.resolution.width, mComParams.resolution.height, VA_RT_FORMAT_YUV420,
+            mVADisplay, map->vinfo.width, map->vinfo.height, VA_RT_FORMAT_YUV420,
             1, &surface, &vaSurfaceAttrib);
 
     CHECK_VA_STATUS_RETURN("vaCreateSurfaceFromMalloc");
@@ -1705,7 +1705,7 @@
 
     Encode_Status status;
 
-LOG_I("surfaceMapping mode=%d, format=%d, lumaStride=%d, width=%d, heith=%d, value=%x\n", map->vinfo.mode, map->vinfo.format, map->vinfo.lumaStride, map->vinfo.width, map->vinfo.height, map->value);
+LOG_I("surfaceMapping mode=%d, format=%d, lumaStride=%d, width=%d, height=%d, value=%x\n", map->vinfo.mode, map->vinfo.format, map->vinfo.lumaStride, map->vinfo.width, map->vinfo.height, map->value);
     switch (map->vinfo.mode) {
         case MEM_MODE_CI:
             status = surfaceMappingForCI(map);