Refine VP8 encode middlerware.

BZ: 135380

Remove ref frame alignment calculation due to driver will do that instead;
Add setconfig/getconfig for VP8 ref frame selection.

Change-Id: Idaa01959e5b47a8b0f9d01a7b4d2eab10cca3bf3
Signed-off-by: Liu Bolun <bolun.liu@intel.com>
Reviewed-on: http://android.intel.com:8080/134059
Reviewed-by: Shi, PingX <pingx.shi@intel.com>
Tested-by: Shi, PingX <pingx.shi@intel.com>
Reviewed-by: cactus <cactus@intel.com>
Tested-by: cactus <cactus@intel.com>
diff --git a/videoencoder/VideoEncoderBase.cpp b/videoencoder/VideoEncoderBase.cpp
index a98a56d..41c069d 100644
--- a/videoencoder/VideoEncoderBase.cpp
+++ b/videoencoder/VideoEncoderBase.cpp
@@ -177,25 +177,11 @@
         stride_aligned = ((mComParams.resolution.width + 15) / 16 ) * 16;
         height_aligned = ((mComParams.resolution.height + 15) / 16 ) * 16;
     }else{
-        if(mComParams.profile == VAProfileVP8Version0_3)
-        {
-           stride_aligned = ((mComParams.resolution.width + 64 + 63) / 64 ) * 64;  //for vsp stride
-           height_aligned = ((mComParams.resolution.height + 64 + 63) / 64 ) * 64;
-        }
-        else
-        {
+           // this alignment is used for AVC. For vp8 encode, driver will handle the alignment
            stride_aligned = ((mComParams.resolution.width + 63) / 64 ) * 64;  //on Merr, stride must be 64 aligned.
            height_aligned = ((mComParams.resolution.height + 31) / 32 ) * 32;
-        }
     }
 
-#if 0
-    if(mComParams.profile == VAProfileVP8Version0_3)
-        attribute_tpi.size = stride_aligned * height_aligned + stride_aligned * ((((mComParams.resolution.height + 1) / 2 + 32)+63)/64) *64;// FW need w*h + w*chrom_height
-    else
-        attribute_tpi.size = stride_aligned * height_aligned * 3 / 2;
-#endif
-
     ValueInfo vinfo;
     vinfo.mode = MEM_MODE_SURFACE;
     vinfo.width = stride_aligned;
@@ -210,16 +196,8 @@
 
     }else {
         mAutoRefSurfaces = new VASurfaceID [mAutoReferenceSurfaceNum];
-
-        if(mComParams.profile == VAProfileVP8Version0_3){
-           VASurfaceAttrib attrib_list[2];
-           VASurfaceAttribExternalBuffers  external_refbuf;
-           setupVP8RefExternalBuf(stride_aligned,height_aligned,&external_refbuf,&attrib_list[0]);
-           vaStatus = vaCreateSurfaces(mVADisplay, VA_RT_FORMAT_YUV420,stride_aligned, height_aligned, mAutoRefSurfaces, mAutoReferenceSurfaceNum, attrib_list, 2);
-        } else {
-            for(int i = 0; i < mAutoReferenceSurfaceNum; i ++)
+        for(int i = 0; i < mAutoReferenceSurfaceNum; i ++)
                 mAutoRefSurfaces[i] = CreateSurfaceFromExternalBuf(0, vinfo);
-        }
     }
     CHECK_VA_STATUS_RETURN("vaCreateSurfaces");
 
@@ -1214,8 +1192,8 @@
         case VideoConfigTypeNALSize:
         case VideoConfigTypeIDRRequest:
         case VideoConfigTypeSliceNum:
-        case VideoConfigTypeVP8: {
-
+        case VideoConfigTypeVP8:
+        case VideoConfigTypeVP8ReferenceFrame: {
             ret = derivedSetConfig(videoEncConfig);
             break;
         }
@@ -1737,12 +1715,8 @@
         //TODO: need optimization for both width/height not aligned case
         VASurfaceID surfaceId;
         unsigned int stride_aligned;
-        if(mComParams.profile == VAProfileVP8Version0_3)
-            stride_aligned = ((mComParams.resolution.width + 31) / 32 ) * 32;
-        else
-            stride_aligned = ((mComParams.resolution.width + 63) / 64 ) * 64;
-
-            vaStatus = vaCreateSurfaces(mVADisplay, VA_RT_FORMAT_YUV420,
+        stride_aligned = ((mComParams.resolution.width + 63) / 64 ) * 64;
+        vaStatus = vaCreateSurfaces(mVADisplay, VA_RT_FORMAT_YUV420,
                     stride_aligned, map->vinfo.height, &surfaceId, 1, NULL, 0);
 
         map->surface = surfaceId;
@@ -2251,40 +2225,3 @@
 
     return surface;
 }
-
-Encode_Status VideoEncoderBase::setupVP8RefExternalBuf(uint32_t stride_aligned,
-                                                       uint32_t height_aligned,
-                                                       VASurfaceAttribExternalBuffers *buf,
-                                                       VASurfaceAttrib *attrib_list)
-{
-    int ref_height_uv = (mComParams.resolution.height/ 2 + 32 + 63) & (~63);
-    buf->pixel_format = VA_FOURCC_NV12;
-    buf->width = stride_aligned;
-    buf->height = height_aligned;
-    buf->data_size = stride_aligned * height_aligned + stride_aligned * ref_height_uv;
-    buf->num_buffers = mAutoReferenceSurfaceNum;
-    buf->num_planes = 3;
-    buf->pitches[0] = stride_aligned;
-    buf->pitches[1] = stride_aligned;
-    buf->pitches[2] = stride_aligned;
-    buf->pitches[3] = 0;
-    buf->offsets[0] = 0;
-    buf->offsets[1] = stride_aligned*height_aligned;
-    buf->offsets[2] = buf->offsets[1];
-    buf->offsets[3] = 0;
-    buf->buffers = (unsigned long *)calloc(buf->num_buffers, sizeof(unsigned long));
-    buf->flags = 0;
-    buf->private_data = NULL;
-
-    attrib_list[0].type = (VASurfaceAttribType)VASurfaceAttribMemoryType;
-    attrib_list[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
-    attrib_list[0].value.type = VAGenericValueTypeInteger;
-    attrib_list[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
-
-    attrib_list[1].type = (VASurfaceAttribType)VASurfaceAttribExternalBufferDescriptor;
-    attrib_list[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
-    attrib_list[1].value.type = VAGenericValueTypePointer;
-    attrib_list[1].value.value.p = (void *)buf;
-
-    return 0;
-}
diff --git a/videoencoder/VideoEncoderBase.h b/videoencoder/VideoEncoderBase.h
index 82ac1f1..d43878b 100644
--- a/videoencoder/VideoEncoderBase.h
+++ b/videoencoder/VideoEncoderBase.h
@@ -100,11 +100,6 @@
     Encode_Status querySupportedSurfaceMemTypes();
     Encode_Status copySurfaces(VASurfaceID srcId, VASurfaceID destId);
     VASurfaceID CreateSurfaceFromExternalBuf(int32_t value, ValueInfo& vinfo);
-    Encode_Status setupVP8RefExternalBuf(uint32_t stride_aligned,
-                                         uint32_t height_aligned,
-                                         VASurfaceAttribExternalBuffers *buf,
-                                         VASurfaceAttrib *attrib_list);
-
 
 protected:
 
diff --git a/videoencoder/VideoEncoderDef.h b/videoencoder/VideoEncoderDef.h
index 426db02..d91ed23 100644
--- a/videoencoder/VideoEncoderDef.h
+++ b/videoencoder/VideoEncoderDef.h
@@ -303,6 +303,7 @@
     VideoConfigTypeIDRRequest,
     VideoConfigTypeSliceNum,
     VideoConfigTypeVP8,
+    VideoConfigTypeVP8ReferenceFrame,
 
     VideoParamsConfigExtension
 };
@@ -586,44 +587,54 @@
 
 struct VideoParamsVP8 : VideoParamConfigSet {
 
-	uint32_t profile;
-	uint32_t error_resilient;
-	uint32_t num_token_partitions;
-	uint32_t kf_auto;
-	uint32_t kf_min_dist;
-	uint32_t kf_max_dist;
-	uint32_t min_qp;
-	uint32_t max_qp;
-	uint32_t init_qp;
-	uint32_t rc_undershoot;
-	uint32_t rc_overshoot;
-	uint32_t hrd_buf_size;
-	uint32_t hrd_buf_initial_fullness;
-	uint32_t hrd_buf_optimal_fullness;
+        uint32_t profile;
+        uint32_t error_resilient;
+        uint32_t num_token_partitions;
+        uint32_t kf_auto;
+        uint32_t kf_min_dist;
+        uint32_t kf_max_dist;
+        uint32_t min_qp;
+        uint32_t max_qp;
+        uint32_t init_qp;
+        uint32_t rc_undershoot;
+        uint32_t rc_overshoot;
+        uint32_t hrd_buf_size;
+        uint32_t hrd_buf_initial_fullness;
+        uint32_t hrd_buf_optimal_fullness;
 
-	VideoParamsVP8() {
-		type = VideoParamsTypeVP8;
-		size = sizeof(VideoParamsVP8);
-	}
+        VideoParamsVP8() {
+                type = VideoParamsTypeVP8;
+                size = sizeof(VideoParamsVP8);
+        }
 };
 
 struct VideoConfigVP8 : VideoParamConfigSet {
 
-	uint32_t force_kf;
-	uint32_t no_ref_last;
-	uint32_t no_ref_gf;
-	uint32_t no_ref_arf;
-	uint32_t refresh_last;
-	uint32_t refresh_golden_frame;
-	uint32_t refresh_alternate_frame;
-	uint32_t refresh_entropy_probs;
-	uint32_t value;
-	unsigned char sharpness_level;
+        uint32_t force_kf;
+        uint32_t refresh_entropy_probs;
+        uint32_t value;
+        unsigned char sharpness_level;
 
-	VideoConfigVP8 () {
-		type = VideoConfigTypeVP8;
-		size = sizeof(VideoConfigVP8);
-	}
+        VideoConfigVP8 () {
+                type = VideoConfigTypeVP8;
+                size = sizeof(VideoConfigVP8);
+        }
 };
 
+struct VideoConfigVP8ReferenceFrame : VideoParamConfigSet {
+
+        uint32_t no_ref_last;
+        uint32_t no_ref_gf;
+        uint32_t no_ref_arf;
+        uint32_t refresh_last;
+        uint32_t refresh_golden_frame;
+        uint32_t refresh_alternate_frame;
+
+        VideoConfigVP8ReferenceFrame () {
+                type = VideoConfigTypeVP8ReferenceFrame;
+                size = sizeof(VideoConfigVP8ReferenceFrame);
+        }
+};
+
+
 #endif /*  __VIDEO_ENCODER_DEF_H__ */
diff --git a/videoencoder/VideoEncoderVP8.cpp b/videoencoder/VideoEncoderVP8.cpp
index 10edc2b..617813d 100644
--- a/videoencoder/VideoEncoderVP8.cpp
+++ b/videoencoder/VideoEncoderVP8.cpp
@@ -16,33 +16,34 @@
 VideoEncoderVP8::VideoEncoderVP8()
     :VideoEncoderBase() {
 
-	mVideoParamsVP8.profile = 0;
-	mVideoParamsVP8.error_resilient = 0;
-	mVideoParamsVP8.num_token_partitions = 4;
-	mVideoParamsVP8.kf_auto = 1;
-	mVideoParamsVP8.kf_min_dist = 0;
-	mVideoParamsVP8.kf_max_dist = 30;
-	mVideoParamsVP8.min_qp = 4;
-	mVideoParamsVP8.max_qp = 63;
-	mVideoParamsVP8.init_qp = 26;
-	mVideoParamsVP8.rc_undershoot = 100;
-	mVideoParamsVP8.rc_overshoot = 100;
-	mVideoParamsVP8.hrd_buf_size = 6000;
-	mVideoParamsVP8.hrd_buf_initial_fullness = 4000;
-	mVideoParamsVP8.hrd_buf_optimal_fullness = 5000;
+        mVideoParamsVP8.profile = 0;
+        mVideoParamsVP8.error_resilient = 0;
+        mVideoParamsVP8.num_token_partitions = 4;
+        mVideoParamsVP8.kf_auto = 1;
+        mVideoParamsVP8.kf_min_dist = 0;
+        mVideoParamsVP8.kf_max_dist = 30;
+        mVideoParamsVP8.min_qp = 4;
+        mVideoParamsVP8.max_qp = 63;
+        mVideoParamsVP8.init_qp = 26;
+        mVideoParamsVP8.rc_undershoot = 100;
+        mVideoParamsVP8.rc_overshoot = 100;
+        mVideoParamsVP8.hrd_buf_size = 6000;
+        mVideoParamsVP8.hrd_buf_initial_fullness = 4000;
+        mVideoParamsVP8.hrd_buf_optimal_fullness = 5000;
 
-	mVideoConfigVP8.force_kf = 0;
-	mVideoConfigVP8.no_ref_last = 0;
-	mVideoConfigVP8.no_ref_gf = 1;
-	mVideoConfigVP8.no_ref_arf = 1;
-	mVideoConfigVP8.refresh_last = 1;
-	mVideoConfigVP8.refresh_golden_frame = 1;
-	mVideoConfigVP8.refresh_alternate_frame = 1;
-	mVideoConfigVP8.refresh_entropy_probs = 0;
-	mVideoConfigVP8.value = 0;
-	mVideoConfigVP8.sharpness_level = 2;
+        mVideoConfigVP8.force_kf = 0;
+        mVideoConfigVP8.refresh_entropy_probs = 0;
+        mVideoConfigVP8.value = 0;
+        mVideoConfigVP8.sharpness_level = 2;
 
-	mComParams.profile = VAProfileVP8Version0_3;
+        mVideoConfigVP8ReferenceFrame.no_ref_last = 0;
+        mVideoConfigVP8ReferenceFrame.no_ref_gf = 0;
+        mVideoConfigVP8ReferenceFrame.no_ref_arf = 0;
+        mVideoConfigVP8ReferenceFrame.refresh_last = 1;
+        mVideoConfigVP8ReferenceFrame.refresh_golden_frame = 1;
+        mVideoConfigVP8ReferenceFrame.refresh_alternate_frame = 1;
+
+        mComParams.profile = VAProfileVP8Version0_3;
 }
 
 VideoEncoderVP8::~VideoEncoderVP8() {
@@ -91,16 +92,16 @@
     vp8PicParam.pic_flags.value = 0;
     vp8PicParam.ref_flags.bits.force_kf = mVideoConfigVP8.force_kf; //0;
     if(!vp8PicParam.ref_flags.bits.force_kf) {
-        vp8PicParam.ref_flags.bits.no_ref_last = mVideoConfigVP8.no_ref_last;
-        vp8PicParam.ref_flags.bits.no_ref_arf = mVideoConfigVP8.no_ref_arf;
-        vp8PicParam.ref_flags.bits.no_ref_gf = mVideoConfigVP8.no_ref_gf;
+        vp8PicParam.ref_flags.bits.no_ref_last = mVideoConfigVP8ReferenceFrame.no_ref_last;
+        vp8PicParam.ref_flags.bits.no_ref_arf = mVideoConfigVP8ReferenceFrame.no_ref_arf;
+        vp8PicParam.ref_flags.bits.no_ref_gf = mVideoConfigVP8ReferenceFrame.no_ref_gf;
     }
     vp8PicParam.pic_flags.bits.refresh_entropy_probs = 0;
     vp8PicParam.sharpness_level = 2;
     vp8PicParam.pic_flags.bits.num_token_partitions = 2;
-    vp8PicParam.pic_flags.bits.refresh_last = mVideoConfigVP8.refresh_last;
-    vp8PicParam.pic_flags.bits.refresh_golden_frame = mVideoConfigVP8.refresh_golden_frame;
-    vp8PicParam.pic_flags.bits.refresh_alternate_frame = mVideoConfigVP8.refresh_alternate_frame;
+    vp8PicParam.pic_flags.bits.refresh_last = mVideoConfigVP8ReferenceFrame.refresh_last;
+    vp8PicParam.pic_flags.bits.refresh_golden_frame = mVideoConfigVP8ReferenceFrame.refresh_golden_frame;
+    vp8PicParam.pic_flags.bits.refresh_alternate_frame = mVideoConfigVP8ReferenceFrame.refresh_alternate_frame;
 
     vaStatus = vaCreateBuffer(
             mVADisplay, mVAContext,
@@ -246,35 +247,91 @@
 	VideoParamsVP8 *encParamsVP8 = reinterpret_cast <VideoParamsVP8*> (videoEncParams);
 
 	if (encParamsVP8->size != sizeof(VideoParamsVP8)) {
-		return ENCODE_INVALID_PARAMS;
-	}
+	       return ENCODE_INVALID_PARAMS;
+        }
 
-	*encParamsVP8 = mVideoParamsVP8;
-	return ENCODE_SUCCESS;
+        *encParamsVP8 = mVideoParamsVP8;
+        return ENCODE_SUCCESS;
 }
 
 Encode_Status VideoEncoderVP8::derivedGetConfig(VideoParamConfigSet *videoEncConfig) {
 
-	CHECK_NULL_RETURN_IFFAIL(videoEncConfig);
-	VideoConfigVP8 *encConfigVP8 = reinterpret_cast<VideoConfigVP8*> (videoEncConfig);
+        CHECK_NULL_RETURN_IFFAIL(videoEncConfig);
 
-	if (encConfigVP8->size != sizeof(VideoConfigVP8)) {
-		return ENCODE_INVALID_PARAMS;
-	}
+        switch (videoEncConfig->type)
+        {
+                case VideoConfigTypeVP8:{
+                        VideoConfigVP8 *encConfigVP8 =
+                                reinterpret_cast<VideoConfigVP8*> (videoEncConfig);
 
-	*encConfigVP8 = mVideoConfigVP8;
-	return ENCODE_SUCCESS;
+                        if (encConfigVP8->size != sizeof(VideoConfigVP8)) {
+                                return ENCODE_INVALID_PARAMS;
+                        }
+
+                        *encConfigVP8 = mVideoConfigVP8;
+                }
+                break;
+
+                case VideoConfigTypeVP8ReferenceFrame:{
+
+                        VideoConfigVP8ReferenceFrame *encConfigVP8ReferenceFrame =
+                                reinterpret_cast<VideoConfigVP8ReferenceFrame*> (videoEncConfig);
+
+                        if (encConfigVP8ReferenceFrame->size != sizeof(VideoConfigVP8ReferenceFrame)) {
+                                return ENCODE_INVALID_PARAMS;
+                        }
+
+                        *encConfigVP8ReferenceFrame = mVideoConfigVP8ReferenceFrame;
+
+                }
+                break;
+
+                default: {
+            LOG_E ("Invalid Config Type");
+            break;
+                }
+       }
+
+       return ENCODE_SUCCESS;
 }
 
 Encode_Status VideoEncoderVP8::derivedSetConfig(VideoParamConfigSet *videoEncConfig) {
 
-	CHECK_NULL_RETURN_IFFAIL(videoEncConfig);
-	VideoConfigVP8 *encConfigVP8 = reinterpret_cast<VideoConfigVP8*> (videoEncConfig);
+        CHECK_NULL_RETURN_IFFAIL(videoEncConfig);
 
-	if (encConfigVP8->size != sizeof(VideoConfigVP8)) {
-		return ENCODE_INVALID_PARAMS;
-	}
+        //LOGE ("%s begin",__func__);
 
-	mVideoConfigVP8 = *encConfigVP8;
-	return ENCODE_SUCCESS;
+        switch (videoEncConfig->type)
+        {
+                case VideoConfigTypeVP8:{
+                        VideoConfigVP8 *encConfigVP8 =
+                                reinterpret_cast<VideoConfigVP8*> (videoEncConfig);
+
+                        if (encConfigVP8->size != sizeof(VideoConfigVP8)) {
+                                return ENCODE_INVALID_PARAMS;
+                        }
+
+                        mVideoConfigVP8 = *encConfigVP8;
+                }
+                break;
+
+                case VideoConfigTypeVP8ReferenceFrame:{
+                        VideoConfigVP8ReferenceFrame *encConfigVP8ReferenceFrame =
+                                reinterpret_cast<VideoConfigVP8ReferenceFrame*> (videoEncConfig);
+
+                        if (encConfigVP8ReferenceFrame->size != sizeof(VideoConfigVP8ReferenceFrame)) {
+                                return ENCODE_INVALID_PARAMS;
+                        }
+
+                        mVideoConfigVP8ReferenceFrame = *encConfigVP8ReferenceFrame;
+
+                }
+                break;
+
+                default: {
+            LOG_E ("Invalid Config Type");
+            break;
+                }
+        }
+        return ENCODE_SUCCESS;
 }
diff --git a/videoencoder/VideoEncoderVP8.h b/videoencoder/VideoEncoderVP8.h
index efc3199..cacb7c1 100644
--- a/videoencoder/VideoEncoderVP8.h
+++ b/videoencoder/VideoEncoderVP8.h
@@ -42,6 +42,7 @@
 
 	VideoConfigVP8 mVideoConfigVP8;
 	VideoParamsVP8 mVideoParamsVP8;
+	VideoConfigVP8ReferenceFrame mVideoConfigVP8ReferenceFrame;
 };
 
 #endif /* __VIDEO_ENCODER_VP8_H__ */