]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/vaapi_encode_h265.c
avcodec/dca_lbr: Fix "warning: missing braces around initializer"
[ffmpeg] / libavcodec / vaapi_encode_h265.c
index 323efd46a7a460ab7ba3b6cf10f3bceb4c197bd9..05d3aa41dad87964f612599f370a435d84d6d6a6 100644 (file)
@@ -1,18 +1,18 @@
 /*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -182,8 +182,22 @@ typedef struct VAAPIEncodeH265Context {
     int fixed_qp_b;
 
     int64_t last_idr_frame;
+
+    // Rate control configuration.
+    struct {
+        VAEncMiscParameterBuffer misc;
+        VAEncMiscParameterRateControl rc;
+    } rc_params;
+    struct {
+        VAEncMiscParameterBuffer misc;
+        VAEncMiscParameterHRD hrd;
+    } hrd_params;
 } VAAPIEncodeH265Context;
 
+typedef struct VAAPIEncodeH265Options {
+    int qp;
+} VAAPIEncodeH265Options;
+
 
 #define vseq_var(name)     vseq->name, name
 #define vseq_field(name)   vseq->seq_fields.bits.name, name
@@ -806,6 +820,19 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
         vseq->max_transform_hierarchy_depth_intra = 3;
 
         vseq->vui_parameters_present_flag = 0;
+
+        vseq->bits_per_second = avctx->bit_rate;
+        if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
+            vseq->vui_num_units_in_tick = avctx->framerate.num;
+            vseq->vui_time_scale        = avctx->framerate.den;
+        } else {
+            vseq->vui_num_units_in_tick = avctx->time_base.num;
+            vseq->vui_time_scale        = avctx->time_base.den;
+        }
+
+        vseq->intra_period     = ctx->p_per_i * (ctx->b_per_p + 1);
+        vseq->intra_idr_period = vseq->intra_period;
+        vseq->ip_period        = ctx->b_per_p + 1;
     }
 
     {
@@ -841,8 +868,7 @@ static int vaapi_encode_h265_init_sequence_params(AVCodecContext *avctx)
 
         vpic->pic_fields.bits.screen_content_flag = 0;
         vpic->pic_fields.bits.enable_gpu_weighted_prediction = 0;
-
-        //vpic->pic_fields.bits.cu_qp_delta_enabled_flag = 1;
+        vpic->pic_fields.bits.cu_qp_delta_enabled_flag = 1;
     }
 
     {
@@ -1125,6 +1151,80 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx,
     return 0;
 }
 
+static av_cold int vaapi_encode_h265_init_constant_bitrate(AVCodecContext *avctx)
+{
+    VAAPIEncodeContext      *ctx = avctx->priv_data;
+    VAAPIEncodeH265Context *priv = ctx->priv_data;
+    int hrd_buffer_size;
+    int hrd_initial_buffer_fullness;
+
+    if (avctx->rc_buffer_size)
+        hrd_buffer_size = avctx->rc_buffer_size;
+    else
+        hrd_buffer_size = avctx->bit_rate;
+    if (avctx->rc_initial_buffer_occupancy)
+        hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
+    else
+        hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
+
+    priv->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
+    priv->rc_params.rc = (VAEncMiscParameterRateControl) {
+        .bits_per_second   = avctx->bit_rate,
+        .target_percentage = 66,
+        .window_size       = 1000,
+        .initial_qp        = (avctx->qmax >= 0 ? avctx->qmax : 40),
+        .min_qp            = (avctx->qmin >= 0 ? avctx->qmin : 20),
+        .basic_unit_size   = 0,
+    };
+    ctx->global_params[ctx->nb_global_params] =
+        &priv->rc_params.misc;
+    ctx->global_params_size[ctx->nb_global_params++] =
+        sizeof(priv->rc_params);
+
+    priv->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
+    priv->hrd_params.hrd = (VAEncMiscParameterHRD) {
+        .initial_buffer_fullness = hrd_initial_buffer_fullness,
+        .buffer_size             = hrd_buffer_size,
+    };
+    ctx->global_params[ctx->nb_global_params] =
+        &priv->hrd_params.misc;
+    ctx->global_params_size[ctx->nb_global_params++] =
+        sizeof(priv->hrd_params);
+
+    // These still need to be  set for pic_init_qp/slice_qp_delta.
+    priv->fixed_qp_idr = 30;
+    priv->fixed_qp_p   = 30;
+    priv->fixed_qp_b   = 30;
+
+    av_log(avctx, AV_LOG_DEBUG, "Using constant-bitrate = %d bps.\n",
+           avctx->bit_rate);
+    return 0;
+}
+
+static av_cold int vaapi_encode_h265_init_fixed_qp(AVCodecContext *avctx)
+{
+    VAAPIEncodeContext      *ctx = avctx->priv_data;
+    VAAPIEncodeH265Context *priv = ctx->priv_data;
+    VAAPIEncodeH265Options  *opt = ctx->codec_options;
+
+    priv->fixed_qp_p = opt->qp;
+    if (avctx->i_quant_factor > 0.0)
+        priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
+                                    avctx->i_quant_offset) + 0.5);
+    else
+        priv->fixed_qp_idr = priv->fixed_qp_p;
+    if (avctx->b_quant_factor > 0.0)
+        priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
+                                  avctx->b_quant_offset) + 0.5);
+    else
+        priv->fixed_qp_b = priv->fixed_qp_p;
+
+    av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
+           "%d / %d / %d for IDR / P / B frames.\n",
+           priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
+    return 0;
+}
+
 static av_cold int vaapi_encode_h265_init_internal(AVCodecContext *avctx)
 {
     static const VAConfigAttrib default_config_attributes[] = {
@@ -1133,13 +1233,11 @@ static av_cold int vaapi_encode_h265_init_internal(AVCodecContext *avctx)
         { .type  = VAConfigAttribEncPackedHeaders,
           .value = (VA_ENC_PACKED_HEADER_SEQUENCE |
                     VA_ENC_PACKED_HEADER_SLICE) },
-        { .type  = VAConfigAttribRateControl,
-          .value = VA_RC_CQP },
     };
 
     VAAPIEncodeContext      *ctx = avctx->priv_data;
     VAAPIEncodeH265Context *priv = ctx->priv_data;
-    int i;
+    int i, err;
 
     switch (avctx->profile) {
     case FF_PROFILE_HEVC_MAIN:
@@ -1174,26 +1272,19 @@ static av_cold int vaapi_encode_h265_init_internal(AVCodecContext *avctx)
     }
 
     if (avctx->bit_rate > 0) {
-        av_log(avctx, AV_LOG_ERROR, "H.265 constant-bitrate encoding "
-               "is not supported.\n");
-        return AVERROR_PATCHWELCOME;
+        ctx->va_rc_mode = VA_RC_CBR;
+        err = vaapi_encode_h265_init_constant_bitrate(avctx);
+    } else {
+        ctx->va_rc_mode = VA_RC_CQP;
+        err = vaapi_encode_h265_init_fixed_qp(avctx);
     }
+    if (err < 0)
+        return err;
 
-    ctx->va_rc_mode = VA_RC_CQP;
-
-    priv->fixed_qp_p = avctx->global_quality;
-    if (avctx->i_quant_factor > 0.0)
-        priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
-                                    avctx->i_quant_offset) + 0.5);
-    else
-        priv->fixed_qp_idr = priv->fixed_qp_p;
-    if (avctx->b_quant_factor > 0.0)
-        priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
-                                  avctx->b_quant_offset) + 0.5);
-    else
-        priv->fixed_qp_b = priv->fixed_qp_p;
-    av_log(avctx, AV_LOG_DEBUG, "QP = %d / %d / %d for IDR / P / B frames.\n",
-           priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
+    ctx->config_attributes[ctx->nb_config_attributes++] = (VAConfigAttrib) {
+        .type  = VAConfigAttribRateControl,
+        .value = ctx->va_rc_mode,
+    };
 
     ctx->nb_recon_frames = 20;
 
@@ -1226,13 +1317,21 @@ static av_cold int vaapi_encode_h265_init(AVCodecContext *avctx)
     return ff_vaapi_encode_init(avctx, &vaapi_encode_type_h265);
 }
 
+#define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
+                   offsetof(VAAPIEncodeH265Options, x))
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
+static const AVOption vaapi_encode_h265_options[] = {
+    { "qp", "Constant QP (for P frames; scaled by qfactor/qoffset for I/B)",
+      OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 25 }, 0, 52, FLAGS },
+    { NULL },
+};
+
 static const AVCodecDefault vaapi_encode_h265_defaults[] = {
     { "profile",        "1"   },
     { "level",          "51"  },
     { "b",              "0"   },
     { "bf",             "2"   },
     { "g",              "120" },
-    { "global_quality", "25"  },
     { "i_qfactor",      "1.0" },
     { "i_qoffset",      "0.0" },
     { "b_qfactor",      "1.2" },
@@ -1243,6 +1342,7 @@ static const AVCodecDefault vaapi_encode_h265_defaults[] = {
 static const AVClass vaapi_encode_h265_class = {
     .class_name = "h265_vaapi",
     .item_name  = av_default_item_name,
+    .option     = vaapi_encode_h265_options,
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
@@ -1251,7 +1351,8 @@ AVCodec ff_hevc_vaapi_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("H.265/HEVC (VAAPI)"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_HEVC,
-    .priv_data_size = sizeof(VAAPIEncodeContext),
+    .priv_data_size = (sizeof(VAAPIEncodeContext) +
+                       sizeof(VAAPIEncodeH265Options)),
     .init           = &vaapi_encode_h265_init,
     .encode2        = &ff_vaapi_encode2,
     .close          = &ff_vaapi_encode_close,