[FFmpeg-devel] lavc/vaapi_encode_h265: respect "slices" option in h265 vaapi encoder

Jun Zhao mypopydev at gmail.com
Wed Aug 2 08:57:23 EEST 2017


From 82eb7d1c3120081a7073cfb379802a28c769ae18 Mon Sep 17 00:00:00 2001
From: Jun Zhao <jun.zhao at intel.com>
Date: Tue, 1 Aug 2017 23:07:34 -0400
Subject: [PATCH V2 4/4] lavc/vaapi_encode_h265: respect "slices" option in
 h265 vaapi encoder

Enable multi-slice support in AVC/H.265 vaapi encoder.

Signed-off-by: Wang, Yi A <yi.a.wang at intel.com>
Signed-off-by: Jun Zhao <jun.zhao at intel.com>
---
 libavcodec/vaapi_encode_h265.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c
index cf6b9388d1..aa85331260 100644
--- a/libavcodec/vaapi_encode_h265.c
+++ b/libavcodec/vaapi_encode_h265.c
@@ -176,6 +176,10 @@ typedef struct VAAPIEncodeH265Context {
     unsigned int ctu_width;
     unsigned int ctu_height;
 
+    int slice_of_ctus;
+    int slice_mod_ctus;
+    int last_ctu_index;
+
     int fixed_qp_idr;
     int fixed_qp_p;
     int fixed_qp_b;
@@ -962,6 +966,7 @@ static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
     VAEncPictureParameterBufferHEVC *vpic = pic->codec_picture_params;
     VAAPIEncodeH265Context          *priv = ctx->priv_data;
     int i;
+    int max_slices;
 
     if (pic->type == PICTURE_TYPE_IDR) {
         av_assert0(pic->display_order == pic->encode_order);
@@ -1024,7 +1029,22 @@ static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx,
         av_assert0(0 && "invalid picture type");
     }
 
-    pic->nb_slices = 1;
+    max_slices = 1;
+    if (ctx->max_slices) {
+        max_slices = FFMIN(priv->ctu_height, ctx->max_slices);
+        if (avctx->slices > max_slices) {
+            av_log(avctx, AV_LOG_WARNING, "The max slices number per frame "
+                   "cannot more than %d.\n", max_slices);
+        } else {
+            max_slices = avctx->slices;
+        }
+    }
+
+    pic->nb_slices = max_slices;
+
+    priv->slice_of_ctus  = (priv->ctu_width * priv->ctu_height) / pic->nb_slices;
+    priv->slice_mod_ctus = (priv->ctu_width * priv->ctu_height) % pic->nb_slices;
+    priv->last_ctu_index = 0;
 
     return 0;
 }
@@ -1047,9 +1067,11 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
     pslice = slice->priv_data;
     mslice = &pslice->misc_slice_params;
 
-    // Currently we only support one slice per frame.
-    vslice->slice_segment_address = 0;
-    vslice->num_ctu_in_slice = priv->ctu_width * priv->ctu_height;
+    vslice->slice_segment_address = priv->last_ctu_index;
+    vslice->num_ctu_in_slice = priv->slice_of_ctus + (priv->slice_mod_ctus > 0 ? 1 : 0);
+    if (priv->slice_mod_ctus > 0)
+        priv->slice_mod_ctus--;
+    priv->last_ctu_index += vslice->num_ctu_in_slice;
 
     switch (pic->type) {
     case PICTURE_TYPE_IDR:
@@ -1103,9 +1125,13 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
     else
         vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp;
 
-    vslice->slice_fields.bits.last_slice_of_pic_flag = 1;
+    if (priv->last_ctu_index == priv->ctu_width * priv->ctu_height)
+        vslice->slice_fields.bits.last_slice_of_pic_flag = 1;
 
-    mslice->first_slice_segment_in_pic_flag = 1;
+    if (vslice->slice_segment_address == 0)
+        mslice->first_slice_segment_in_pic_flag = 1;
+    else
+        mslice->first_slice_segment_in_pic_flag = 0;
 
     if (pic->type == PICTURE_TYPE_IDR) {
         // No reference pictures.
@@ -1198,6 +1224,10 @@ static av_cold int vaapi_encode_h265_configure(AVCodecContext *avctx)
         av_assert0(0 && "Invalid RC mode.");
     }
 
+    if (!ctx->max_slices && avctx->slices > 0)
+        av_log(avctx, AV_LOG_WARNING, "The encode slice option is not "
+               "supported with this VAAPI version.\n");
+
     return 0;
 }
 
-- 
2.11.0



More information about the ffmpeg-devel mailing list