[Libva] [ROI 2/2] Encoding: Add ROI example

Pengfei Qu Pengfei.Qu at intel.com
Tue Jun 28 11:03:58 UTC 2016


From: Zhao Yakui <yakui.zhao at intel.com>

Signed-off-by: Zhao Yakui <yakui.zhao at intel.com>
Signed-off-by: pjl <ceciliapeng at intel.com>
Signed-off-by: ceciliapeng <cecilia.peng at intel.com>
Signed-off-by: Pengfei Qu <Pengfei.Qu at intel.com>
---
 test/encode/avcenc.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 71 insertions(+), 4 deletions(-)

diff --git a/test/encode/avcenc.c b/test/encode/avcenc.c
index 74729fb..9cfc5b4 100644
--- a/test/encode/avcenc.c
+++ b/test/encode/avcenc.c
@@ -154,6 +154,7 @@ static struct {
     VABufferID packed_sei_header_param_buf_id;   /* the SEI buffer */
     VABufferID packed_sei_buf_id;
     VABufferID misc_parameter_hrd_buf_id;
+    VABufferID misc_parameter_roi_buf_id;
 
     int num_slices;
     int codedbuf_i_size;
@@ -185,7 +186,7 @@ static void create_encode_pipe()
 {
     VAEntrypoint entrypoints[5];
     int num_entrypoints,slice_entrypoint;
-    VAConfigAttrib attrib[2];
+    VAConfigAttrib attrib[3];
     int major_ver, minor_ver;
     VAStatus va_status;
 
@@ -209,8 +210,11 @@ static void create_encode_pipe()
     /* find out the format for the render target, and rate control mode */
     attrib[0].type = VAConfigAttribRTFormat;
     attrib[1].type = VAConfigAttribRateControl;
+
+    /* This is to query whether the ROI is supported */
+    attrib[2].type = VAConfigAttribEncROI;
     vaGetConfigAttributes(va_dpy, avcenc_context.profile, select_entrypoint,
-                          &attrib[0], 2);
+                          &attrib[0], 3);
 
     if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
         /* not find desired YUV420 RT format */
@@ -225,9 +229,15 @@ static void create_encode_pipe()
 
     attrib[0].value = VA_RT_FORMAT_YUV420; /* set to desired RT format */
     attrib[1].value = avcenc_context.rate_control_method; /* set to desired RC mode */
+    /* This is to create one context with ROI supported
+     * Only when it is supported, it is possible to pass ROI buffer on-the fly
+     * so that it can use the given ROI config for one frame.
+     * If ROI buffer is not passed, it will continue the original encoding mode.
+     */
+    attrib[2].value = 0x0101;
 
     va_status = vaCreateConfig(va_dpy, avcenc_context.profile, select_entrypoint,
-                               &attrib[0], 2,&avcenc_context.config_id);
+                               &attrib[0], 3,&avcenc_context.config_id);
     CHECK_VASTATUS(va_status, "vaCreateConfig");
 
     /* Create a context for this decode pipe */
@@ -814,16 +824,68 @@ static int begin_picture(FILE *yuv_fp, int frame_num, int display_num, int slice
 
     vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_hrd_buf_id);
 
+    /* ROI parameter */
+    VAEncMiscParameterBufferROI *misc_roi_param;
+
+    /* restriction: current the driver only supports only one ROI num based on returned config_attribute */
+    int roi_num = 1;
+    vaCreateBuffer(va_dpy,
+                   avcenc_context.context_id,
+                   VAEncMiscParameterBufferType,
+                   sizeof(VAEncMiscParameterBuffer) + sizeof(VAEncMiscParameterBufferROI) + roi_num * sizeof(VAEncROI),
+                   1,
+                   NULL,
+                   &avcenc_context.misc_parameter_roi_buf_id);
+    vaMapBuffer(va_dpy,
+                avcenc_context.misc_parameter_roi_buf_id,
+                (void **)&misc_param);
+    misc_param->type = VAEncMiscParameterTypeROI;
+    misc_roi_param = (VAEncMiscParameterBufferROI *)misc_param->data;
+
+    {
+         /*
+          * Max/Min delta_qp is only used in CBR mode. It is ingored under CQP mode.
+          * max_delta_qp means the allowed upper bound of qp delta. (qp + X)
+          * min_delta_qp means the allowed lower bound of qp delta. (qp -X)
+          * So it will be better that it is positive. Otherwise the driver will
+          * use the default bound setting.
+          */
+         misc_roi_param->max_delta_qp = 3;
+         misc_roi_param->min_delta_qp = 3;
+         /* one example of ROI region conf.
+          * please change it on the fly.
+          */
+         VAEncROI *region_roi =(VAEncROI *)((char *)misc_param + sizeof(VAEncMiscParameterBuffer) +
+                                 sizeof(VAEncMiscParameterBufferROI));
+
+         /*
+          * Under CQP mode roi_value specifies the qp_delta that is added to frame qp
+          * Under CBR mode roi_value specifies the important level (positive means that
+          * it is important. negative means that it is less important).
+          */
+         region_roi->roi_value = 4;
+         region_roi->roi_rectangle.x = 0;
+         region_roi->roi_rectangle.y = 0;
+         region_roi->roi_rectangle.width = 120;
+         region_roi->roi_rectangle.height = 120;
+
+         misc_roi_param->roi = region_roi;
+         misc_roi_param->num_roi = 1;
+    }
+
+    vaUnmapBuffer(va_dpy, avcenc_context.misc_parameter_roi_buf_id);
     return 0;
 }
 
 int avcenc_render_picture()
 {
     VAStatus va_status;
-    VABufferID va_buffers[10];
+    VABufferID va_buffers[20];
     unsigned int num_va_buffers = 0;
     int i;
 
+    memset(&va_buffers, 0xff, sizeof(va_buffers));
+
     va_buffers[num_va_buffers++] = avcenc_context.seq_param_buf_id;
     va_buffers[num_va_buffers++] = avcenc_context.pic_param_buf_id;
 
@@ -848,6 +910,9 @@ int avcenc_render_picture()
     if (avcenc_context.misc_parameter_hrd_buf_id != VA_INVALID_ID)
         va_buffers[num_va_buffers++] =  avcenc_context.misc_parameter_hrd_buf_id;
 
+    if (avcenc_context.misc_parameter_roi_buf_id != VA_INVALID_ID)
+        va_buffers[num_va_buffers++] =  avcenc_context.misc_parameter_roi_buf_id;
+
     va_status = vaBeginPicture(va_dpy,
                                avcenc_context.context_id,
                                surface_ids[avcenc_context.current_input_surface]);
@@ -904,6 +969,7 @@ static void end_picture()
     avcenc_destroy_buffers(&avcenc_context.slice_param_buf_id[0], avcenc_context.num_slices);
     avcenc_destroy_buffers(&avcenc_context.codedbuf_buf_id, 1);
     avcenc_destroy_buffers(&avcenc_context.misc_parameter_hrd_buf_id, 1);
+    avcenc_destroy_buffers(&avcenc_context.misc_parameter_roi_buf_id, 1);
 
     memset(avcenc_context.slice_param, 0, sizeof(avcenc_context.slice_param));
     avcenc_context.num_slices = 0;
@@ -1831,6 +1897,7 @@ static void avcenc_context_init(int width, int height)
     avcenc_context.upload_thread_value = -1;
     avcenc_context.packed_sei_header_param_buf_id = VA_INVALID_ID;
     avcenc_context.packed_sei_buf_id = VA_INVALID_ID;
+    avcenc_context.misc_parameter_roi_buf_id = VA_INVALID_ID;
 
     if (qp_value == -1)
         avcenc_context.rate_control_method = VA_RC_CBR;
-- 
2.7.4



More information about the Libva mailing list