diff --git a/gst-libs/gst/vaapi/gstvaapiencoder_h264.c b/gst-libs/gst/vaapi/gstvaapiencoder_h264.c index fbe5d45ba4..0f046bb6d7 100644 --- a/gst-libs/gst/vaapi/gstvaapiencoder_h264.c +++ b/gst-libs/gst/vaapi/gstvaapiencoder_h264.c @@ -2153,13 +2153,44 @@ error_create_packed_seq_hdr: } } +static gboolean +ensure_control_rate_params (GstVaapiEncoderH264 * encoder, + GstVaapiEncPicture * picture) +{ + GstVaapiEncMiscParam *misc; + + if (GST_VAAPI_ENCODER_RATE_CONTROL (encoder) == GST_VAAPI_RATECONTROL_CQP) + return TRUE; + + /* RateControl params */ + misc = GST_VAAPI_ENC_MISC_PARAM_NEW (RateControl, encoder); + if (!misc) + return FALSE; + + { + VAEncMiscParameterRateControl rate_control = { + .bits_per_second = encoder->bitrate_bits, + .target_percentage = 70, + .window_size = encoder->cpb_length, + .initial_qp = encoder->init_qp, + .min_qp = encoder->min_qp, + }; + + memcpy (misc->data, &rate_control, sizeof (rate_control)); + } + + gst_vaapi_enc_picture_add_misc_param (picture, misc); + gst_vaapi_codec_object_replace (&misc, NULL); + + return TRUE; +} + /* Generates additional control parameters */ static gboolean ensure_misc_params (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture) { GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder); - GstVaapiEncMiscParam *misc = NULL; - VAEncMiscParameterRateControl *rate_control; + GstVaapiEncMiscParam *misc; guint num_roi; /* HRD params */ @@ -2170,23 +2201,11 @@ ensure_misc_params (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture) gst_vaapi_enc_picture_add_misc_param (picture, misc); gst_vaapi_codec_object_replace (&misc, NULL); - /* RateControl params */ + if (!ensure_control_rate_params (encoder, picture)) + return FALSE; + if (GST_VAAPI_ENCODER_RATE_CONTROL (encoder) == GST_VAAPI_RATECONTROL_CBR || GST_VAAPI_ENCODER_RATE_CONTROL (encoder) == GST_VAAPI_RATECONTROL_VBR) { - misc = GST_VAAPI_ENC_MISC_PARAM_NEW (RateControl, encoder); - if (!misc) - return FALSE; - rate_control = misc->data; - memset (rate_control, 0, sizeof (VAEncMiscParameterRateControl)); - rate_control->bits_per_second = encoder->bitrate_bits; - rate_control->target_percentage = 70; - rate_control->window_size = encoder->cpb_length; - rate_control->initial_qp = encoder->init_qp; - rate_control->min_qp = encoder->min_qp; - rate_control->basic_unit_size = 0; - gst_vaapi_enc_picture_add_misc_param (picture, misc); - gst_vaapi_codec_object_replace (&misc, NULL); - if (!encoder->view_idx) { if ((GST_VAAPI_ENC_PICTURE_IS_IDR (picture)) && (GST_VAAPI_ENCODER_PACKED_HEADERS (encoder) & diff --git a/gst-libs/gst/vaapi/gstvaapiencoder_mpeg2.c b/gst-libs/gst/vaapi/gstvaapiencoder_mpeg2.c index 02700920f4..dd878d8820 100644 --- a/gst-libs/gst/vaapi/gstvaapiencoder_mpeg2.c +++ b/gst-libs/gst/vaapi/gstvaapiencoder_mpeg2.c @@ -451,6 +451,38 @@ ensure_picture (GstVaapiEncoderMpeg2 * encoder, GstVaapiEncPicture * picture, return TRUE; } +static gboolean +ensure_control_rate_params (GstVaapiEncoderMpeg2 * encoder, + GstVaapiEncPicture * picture) +{ + GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder); + GstVaapiEncMiscParam *misc; + + if (GST_VAAPI_ENCODER_RATE_CONTROL (encoder) == GST_VAAPI_RATECONTROL_CQP) + return TRUE; + + /* RateControl params */ + misc = GST_VAAPI_ENC_MISC_PARAM_NEW (RateControl, encoder); + if (!misc) + return FALSE; + + { + VAEncMiscParameterRateControl rate_control = { + .bits_per_second = base_encoder->bitrate * 1000, + .target_percentage = 70, + .window_size = 500, + .initial_qp = encoder->cqp, + }; + + memcpy (misc->data, &rate_control, sizeof (rate_control)); + } + + gst_vaapi_enc_picture_add_misc_param (picture, misc); + gst_vaapi_codec_object_replace (&misc, NULL); + + return TRUE; +} + static gboolean set_misc_parameters (GstVaapiEncoderMpeg2 * encoder, GstVaapiEncPicture * picture) @@ -458,7 +490,6 @@ set_misc_parameters (GstVaapiEncoderMpeg2 * encoder, GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder); GstVaapiEncMiscParam *misc = NULL; VAEncMiscParameterHRD *hrd; - VAEncMiscParameterRateControl *rate_control; /* add hrd */ misc = GST_VAAPI_ENC_MISC_PARAM_NEW (HRD, encoder); @@ -475,26 +506,8 @@ set_misc_parameters (GstVaapiEncoderMpeg2 * encoder, } gst_vaapi_codec_object_replace (&misc, NULL); - /* add ratecontrol */ - if (GST_VAAPI_ENCODER_RATE_CONTROL (encoder) == GST_VAAPI_RATECONTROL_CBR) { - misc = GST_VAAPI_ENC_MISC_PARAM_NEW (RateControl, encoder); - if (!misc) - return FALSE; - gst_vaapi_enc_picture_add_misc_param (picture, misc); - rate_control = misc->data; - memset (rate_control, 0, sizeof (VAEncMiscParameterRateControl)); - if (base_encoder->bitrate) - rate_control->bits_per_second = base_encoder->bitrate * 1000; - else - rate_control->bits_per_second = 0; - rate_control->target_percentage = 70; - rate_control->window_size = 500; - rate_control->initial_qp = encoder->cqp; - rate_control->min_qp = 0; - rate_control->basic_unit_size = 0; - gst_vaapi_codec_object_replace (&misc, NULL); - } - + if (!ensure_control_rate_params (encoder, picture)) + return FALSE; if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture)) return FALSE; return TRUE; diff --git a/gst-libs/gst/vaapi/gstvaapiencoder_vp8.c b/gst-libs/gst/vaapi/gstvaapiencoder_vp8.c index 58ee55527b..4a94af03e2 100644 --- a/gst-libs/gst/vaapi/gstvaapiencoder_vp8.c +++ b/gst-libs/gst/vaapi/gstvaapiencoder_vp8.c @@ -257,6 +257,41 @@ error: } } +static gboolean +ensure_control_rate_params (GstVaapiEncoderVP8 * encoder, + GstVaapiEncPicture * picture) +{ + GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder); + GstVaapiEncMiscParam *misc; + + if (GST_VAAPI_ENCODER_RATE_CONTROL (encoder) == GST_VAAPI_RATECONTROL_CQP) + return TRUE; + + /* RateControl params */ + misc = GST_VAAPI_ENC_MISC_PARAM_NEW (RateControl, encoder); + if (!misc) + return FALSE; + + { + VAEncMiscParameterRateControl rate_control = { + .bits_per_second = base_encoder->bitrate * 1000, + .target_percentage = 70, + /* CPB (Coded picture buffer) length in milliseconds, which + * could be provided as a property */ + .window_size = 500, + .initial_qp = encoder->yac_qi, + .min_qp = 1, + }; + + memcpy (misc->data, &rate_control, sizeof (rate_control)); + } + + gst_vaapi_enc_picture_add_misc_param (picture, misc); + gst_vaapi_codec_object_replace (&misc, NULL); + + return TRUE; +} + static gboolean ensure_misc_params (GstVaapiEncoderVP8 * encoder, GstVaapiEncPicture * picture) { @@ -266,6 +301,9 @@ ensure_misc_params (GstVaapiEncoderVP8 * encoder, GstVaapiEncPicture * picture) if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture)) return FALSE; + if (!ensure_control_rate_params (encoder, picture)) + return FALSE; + if (GST_VAAPI_ENCODER_RATE_CONTROL (encoder) != GST_VAAPI_RATECONTROL_CBR) return TRUE; @@ -297,25 +335,6 @@ ensure_misc_params (GstVaapiEncoderVP8 * encoder, GstVaapiEncPicture * picture) gst_vaapi_enc_picture_add_misc_param (picture, misc); gst_vaapi_codec_object_replace (&misc, NULL); - /* RateControl params */ - misc = GST_VAAPI_ENC_MISC_PARAM_NEW (RateControl, encoder); - if (!misc) - return FALSE; - { - VAEncMiscParameterRateControl *rate_control; - rate_control = misc->data; - rate_control->bits_per_second = base_encoder->bitrate * 1000; - rate_control->target_percentage = 70; - /* CPB (Coded picture buffer) length in milliseconds, which could - * be provided as a property */ - rate_control->window_size = 500; - rate_control->initial_qp = encoder->yac_qi; - rate_control->min_qp = 1; - } - - gst_vaapi_enc_picture_add_misc_param (picture, misc); - gst_vaapi_codec_object_replace (&misc, NULL); - return TRUE; }