fix libmix encoder klockwork issues, rename IntelMetadatabuffer APIs

BZ: 50695

fix libmix encoder klockwork issues, remove all unused variables;
rename IntelMetadatabuffer APIs, rename SetBytes to UnSerialize,
GetBytes to Serialize, SetBytes/GetBytes will be dropped later;
Refine encoder code to avoid dynamic memory allocation.
Resubmit patch due to last patch's wrong BZ number issue

Change-Id: Ib072e40aed440c6c9eb641992d631bb7e5f83483
Signed-off-by: Zhao Liang <leo.zhao@intel.com>
Reviewed-on: http://android.intel.com:8080/62249
Reviewed-by: Ding, Haitao <haitao.ding@intel.com>
Tested-by: Ding, Haitao <haitao.ding@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
diff --git a/test/btest.cpp b/test/btest.cpp
index 26f104f..13b790b 100644
--- a/test/btest.cpp
+++ b/test/btest.cpp
@@ -37,7 +37,7 @@
         ret = mb1->SetValueInfo(&vi1);
         ret = mb1->SetExtraValues(ev1, 10);
     }
-    ret = mb1->GetBytes(bytes, size);
+    ret = mb1->Serialize(bytes, size);
     printf("assembling IntelMetadataBuffer %s, ret = %d\n", (ret == IMB_SUCCESS)?"Success":"Fail", ret );    
 
     printf("size = %d, bytes = ", size);
@@ -48,7 +48,7 @@
     printf("\n");
 
     mb2 = new IntelMetadataBuffer();
-    ret = mb2->SetBytes(bytes, size);
+    ret = mb2->UnSerialize(bytes, size);
     printf("parsing IntelMetadataBuffer %s, ret = %d\n", (ret == IMB_SUCCESS)?"Success":"Fail", ret );    
     
     ret = mb2->GetType(t2);
diff --git a/test/mix_encoder.cpp b/test/mix_encoder.cpp
index 38fbc00..d4b88c0 100644
--- a/test/mix_encoder.cpp
+++ b/test/mix_encoder.cpp
@@ -697,7 +697,7 @@
     {
         if (gMode != 4)
         {
-            gIMB[i % gSrcFrames]->GetBytes(data, size);
+            gIMB[i % gSrcFrames]->Serialize(data, size);
      //       printf("srcno =%d, data=%x, size=%d\n", i % gSrcFrames, data, size);
         }else
         {
@@ -729,11 +729,10 @@
 		stat.total_frames, stat.skipped_frames, stat.average_encode_time, stat.max_encode_time, stat.max_encode_frame, \
 		stat.min_encode_time, stat.min_encode_frame );
     }
-    if(gVideoEncoder) {
-        releaseVideoEncoder(gVideoEncoder);
-        gVideoEncoder = NULL;
-    }
 
+    gVideoEncoder->stop();
+    releaseVideoEncoder(gVideoEncoder);
+    gVideoEncoder = NULL;
     
     switch(gMode)
     {
diff --git a/videoencoder/IntelMetadataBuffer.cpp b/videoencoder/IntelMetadataBuffer.cpp
index 531d0ca..eb9fe43 100644
--- a/videoencoder/IntelMetadataBuffer.cpp
+++ b/videoencoder/IntelMetadataBuffer.cpp
@@ -60,6 +60,55 @@
         delete[] mBytes;
 }
 
+
+IntelMetadataBuffer::IntelMetadataBuffer(const IntelMetadataBuffer& imb)
+     :mType(imb.mType), mValue(imb.mValue), mInfo(NULL), mExtraValues(NULL),
+      mExtraValues_Count(imb.mExtraValues_Count), mBytes(NULL), mSize(imb.mSize)
+{
+    if (imb.mInfo)
+        mInfo = new ValueInfo(*imb.mInfo);
+
+    if (imb.mExtraValues)
+    {
+        mExtraValues = new int32_t[mExtraValues_Count];
+        memcpy(mExtraValues, imb.mExtraValues, 4 * mExtraValues_Count);
+    }
+
+    if (imb.mBytes)
+    {
+        mBytes = new uint8_t[mSize];
+        memcpy(mBytes, imb.mBytes, mSize);
+    }
+}
+
+const IntelMetadataBuffer& IntelMetadataBuffer::operator=(const IntelMetadataBuffer& imb)
+{
+    mType = imb.mType;
+    mValue = imb.mValue;
+    mInfo = NULL;
+    mExtraValues = NULL;
+    mExtraValues_Count = imb.mExtraValues_Count;
+    mBytes = NULL;
+    mSize = imb.mSize;
+
+    if (imb.mInfo)
+        mInfo = new ValueInfo(*imb.mInfo);
+
+    if (imb.mExtraValues)
+    {
+        mExtraValues = new int32_t[mExtraValues_Count];
+        memcpy(mExtraValues, imb.mExtraValues, 4 * mExtraValues_Count);
+    }
+
+    if (imb.mBytes)
+    {
+        mBytes = new uint8_t[mSize];
+        memcpy(mBytes, imb.mBytes, mSize);
+    }
+
+    return *this;
+}
+
 IMB_Result IntelMetadataBuffer::GetType(MetadataBufferType& type)
 {
     type = mType;
@@ -143,7 +192,7 @@
     return IMB_SUCCESS;
 }
 
-IMB_Result IntelMetadataBuffer::SetBytes(uint8_t* data, uint32_t size)
+IMB_Result IntelMetadataBuffer::UnSerialize(uint8_t* data, uint32_t size)
 {
     if (!data || size == 0)
         return IMB_INVAL_PARAM;
@@ -214,7 +263,12 @@
     return IMB_SUCCESS;
 }
 
-IMB_Result IntelMetadataBuffer::GetBytes(uint8_t* &data, uint32_t& size)
+IMB_Result IntelMetadataBuffer::SetBytes(uint8_t* data, uint32_t size)
+{
+    return UnSerialize(data, size);
+}
+
+IMB_Result IntelMetadataBuffer::Serialize(uint8_t* &data, uint32_t& size)
 {
     if (mBytes == NULL)
     {
@@ -253,6 +307,11 @@
     return IMB_SUCCESS;
 }
 
+IMB_Result IntelMetadataBuffer::GetBytes(uint8_t* &data, uint32_t& size)
+{
+    return Serialize(data, size);
+}
+
 uint32_t IntelMetadataBuffer::GetMaxBufferSize()
 {
     return 256;
diff --git a/videoencoder/IntelMetadataBuffer.h b/videoencoder/IntelMetadataBuffer.h
index 802ef7b..2eacc5f 100644
--- a/videoencoder/IntelMetadataBuffer.h
+++ b/videoencoder/IntelMetadataBuffer.h
@@ -72,6 +72,9 @@
     IntelMetadataBuffer(MetadataBufferType type, int32_t value);    //for quick generator
     ~IntelMetadataBuffer();
 
+    IntelMetadataBuffer(const IntelMetadataBuffer& imb);
+    const IntelMetadataBuffer& operator=(const IntelMetadataBuffer& imb);
+
     IMB_Result GetType(MetadataBufferType &type);
     IMB_Result SetType(MetadataBufferType type);
     IMB_Result GetValue(int32_t &value);
@@ -81,12 +84,15 @@
     IMB_Result GetExtraValues(int32_t* &values, uint32_t &num);
     IMB_Result SetExtraValues(int32_t *values, uint32_t num);
 
-    //for bytes input, also for parser
+    //for bytes input, also for parser, will be obsoleted
     IMB_Result SetBytes(uint8_t* data, uint32_t size);
-
-    //for bytes output, also for generator
+    //for bytes output, also for generator, will be obsoleted
     IMB_Result GetBytes(uint8_t* &data, uint32_t& size);
 
+    //New API for bytes input/ouput, UnSerialize=SetBytes, Serialize=GetBytes
+    IMB_Result UnSerialize(uint8_t* data, uint32_t size);
+    IMB_Result Serialize(uint8_t* &data, uint32_t& size);
+
     //Static, for get max IntelMetadataBuffer size
     static uint32_t GetMaxBufferSize();
 
diff --git a/videoencoder/VideoEncoderAVC.cpp b/videoencoder/VideoEncoderAVC.cpp
index 5922e6a..738eefa 100644
--- a/videoencoder/VideoEncoderAVC.cpp
+++ b/videoencoder/VideoEncoderAVC.cpp
@@ -197,9 +197,6 @@
     Encode_Status ret = ENCODE_SUCCESS;
     VAStatus vaStatus = VA_STATUS_SUCCESS;
     bool useLocalBuffer = false;
-    uint32_t nalType = 0;
-    uint32_t nalSize = 0;
-    uint32_t nalOffset = 0;
     uint32_t idrPeroid = mComParams.intraPeriod * mVideoParamsAVC.idrInterval;
 
     LOG_V("Begin\n");
@@ -283,8 +280,6 @@
         uint32_t *nalType, uint32_t *nalOffset, uint32_t status) {
     uint32_t pos = 0;
     uint32_t zeroByteCount = 0;
-    uint32_t prefixLength = 0;
-    uint32_t leadingZeroCnt = 0;
     uint32_t singleByteTable[3][2] = {{1,0},{2,0},{2,3}};
     uint32_t dataRemaining = 0;
     uint8_t *dataPtr;
@@ -520,7 +515,6 @@
     uint32_t nalSize = 0;
     uint32_t nalOffset = 0;
     uint32_t sizeCopiedHere = 0;
-    uint32_t sizeToBeCopied = 0;
 
     CHECK_NULL_RETURN_IFFAIL(mCurSegment->buf);
 
@@ -648,7 +642,6 @@
 Encode_Status VideoEncoderAVC::renderMaxSliceSize() {
 
     VAStatus vaStatus = VA_STATUS_SUCCESS;
-    Encode_Status ret = ENCODE_SUCCESS;
     LOG_V( "Begin\n\n");
 
     if (mComParams.rcMode != RATE_CONTROL_VCM) {
@@ -794,12 +787,6 @@
     avcSeqParams.intra_idr_period = mVideoParamsAVC.idrInterval;
     avcSeqParams.picture_width_in_mbs = (mComParams.resolution.width + 15) / 16;
     avcSeqParams.picture_height_in_mbs = (mComParams.resolution.height + 15) / 16;
-    if((avcSeqParams.picture_width_in_mbs >=1920)|| (avcSeqParams.picture_height_in_mbs >=1080))
-    {
-        device_info = vaQueryVendorString(mVADisplay);
-        if(strstr(device_info, "LEXINGTON"))
-            return ENCODE_INVALID_PARAMS;
-    }
 
     level = calcLevel (avcSeqParams.picture_width_in_mbs * avcSeqParams.picture_height_in_mbs);
     avcSeqParams.level_idc = level;
@@ -913,7 +900,6 @@
     VAStatus vaStatus = VA_STATUS_SUCCESS;
 
     uint32_t sliceNum = 0;
-    uint32_t sliceHeight = 0;
     uint32_t sliceIndex = 0;
     uint32_t sliceHeightInMB = 0;
     uint32_t maxSliceNum = 0;
diff --git a/videoencoder/VideoEncoderBase.cpp b/videoencoder/VideoEncoderBase.cpp
index ae29aef..91f9331 100644
--- a/videoencoder/VideoEncoderBase.cpp
+++ b/videoencoder/VideoEncoderBase.cpp
@@ -657,7 +657,6 @@
 Encode_Status VideoEncoderBase::prepareForOutput(
         VideoEncOutputBuffer *outBuffer, bool *useLocalBuffer) {
 
-    Encode_Status ret = ENCODE_SUCCESS;
     VAStatus vaStatus = VA_STATUS_SUCCESS;
     VACodedBufferSegment *vaCodedSeg = NULL;
     uint32_t status = 0;
@@ -741,7 +740,6 @@
 Encode_Status VideoEncoderBase::cleanupForOutput() {
 
     VAStatus vaStatus = VA_STATUS_SUCCESS;
-    Encode_Status ret = ENCODE_SUCCESS;
 
     //mCurSegment is NULL means all data has been copied out
     if (mCurSegment == NULL && mCodedBufferMapped) {
@@ -1741,25 +1739,23 @@
     int32_t *extravalues = NULL;
     unsigned int extravalues_count = 0;
 
-    IntelMetadataBuffer *imb = new IntelMetadataBuffer;
+    IntelMetadataBuffer imb;
     SurfaceMap *map = NULL;
   
     if (mStoreMetaDataInBuffers.isEnabled) {        
         //metadatabuffer mode
         LOG_I("in metadata mode, data=%p, size=%d\n", inBuffer->data, inBuffer->size);
-        if (imb->SetBytes(inBuffer->data, inBuffer->size) != IMB_SUCCESS) {
+        if (imb.UnSerialize(inBuffer->data, inBuffer->size) != IMB_SUCCESS) {
             //fail to parse buffer
-            delete imb;
             return ENCODE_NO_REQUEST_DATA; 
         }
 
-        imb->GetType(type);
-        imb->GetValue(value);
+        imb.GetType(type);
+        imb.GetValue(value);
     } else {
         //raw mode
         LOG_I("in raw mode, data=%p, size=%d\n", inBuffer->data, inBuffer->size);
         if (! inBuffer->data || inBuffer->size == 0) {
-            delete imb;
             return ENCODE_NULL_PTR; 
         }
 
@@ -1775,7 +1771,6 @@
         LOG_I("direct find surface %d from value %x\n", map->surface, value);
         mCurSurface = map->surface;
 
-        delete imb;
         return ret;
     }
 
@@ -1797,8 +1792,8 @@
             vinfo.s3dformat = 0xFFFFFFFF;
         } else {            
             //get all info mapping needs
-            imb->GetValueInfo(pvinfo);                 
-            imb->GetExtraValues(extravalues, extravalues_count);
+            imb.GetValueInfo(pvinfo);
+            imb.GetExtraValues(extravalues, extravalues_count);
   	}
         
     } else {
@@ -1833,7 +1828,6 @@
             mSrcSurfaceMapList = appendSurfaceMap(mSrcSurfaceMapList, map);
         } else {
             delete map;
-            delete imb;
             LOG_E("surface mapping failed, wrong info or meet serious error\n");
             return ret;
         } 
@@ -1842,7 +1836,6 @@
 
     } else {
         //can't map due to no info
-        delete imb;
         LOG_E("surface mapping failed,  missing information\n");
         return ENCODE_NO_REQUEST_DATA;
     }
@@ -1869,8 +1862,6 @@
         }
     }
    
-    delete imb;
- 
     return ret;
 }
 
diff --git a/videoencoder/VideoEncoderH263.cpp b/videoencoder/VideoEncoderH263.cpp
index 68f8741..cbe1e09 100644
--- a/videoencoder/VideoEncoderH263.cpp
+++ b/videoencoder/VideoEncoderH263.cpp
@@ -120,7 +120,6 @@
 Encode_Status VideoEncoderH263::renderSliceParams() {
 
     VAStatus vaStatus = VA_STATUS_SUCCESS;
-    uint32_t sliceNum;
     uint32_t sliceHeight;
     uint32_t sliceHeightInMB;
 
diff --git a/videoencoder/VideoEncoderMP4.cpp b/videoencoder/VideoEncoderMP4.cpp
index 51068f4..d93d774 100644
--- a/videoencoder/VideoEncoderMP4.cpp
+++ b/videoencoder/VideoEncoderMP4.cpp
@@ -22,9 +22,7 @@
 Encode_Status VideoEncoderMP4::getHeaderPos(
         uint8_t *inBuffer, uint32_t bufSize, uint32_t *headerSize) {
 
-    uint8_t *buf = inBuffer;
     uint32_t bytesLeft = bufSize;
-    Encode_Status ret = ENCODE_SUCCESS;
 
     *headerSize = 0;
     CHECK_NULL_RETURN_IFFAIL(inBuffer);