encoders: add quality level tuning

This patch adds the handling of VAEncMiscParameterTypeQualityLevel,
in gstreamer-vaapi encoders:

  The encoding quality could be set through this structure, if the
  implementation supports multiple quality levels. The quality level set
  through this structure is persistent over the entire coded sequence, or
  until a new structure is being sent. The quality level range can be queried
  through the VAConfigAttribEncQualityRange attribute. A lower value means
  higher quality, and a value of 1 represents the highest quality. The quality
  level setting is used as a trade-off between quality and speed/power
  consumption, with higher quality corresponds to lower speed and higher power
  consumption.

The quality level is set by the element's parameter "quality-level" with a
hard-coded range of 1 to 8.

Later, when the encoder is configured in run time, just before start
processing, the quality level is scaled to the codec range. If
VAConfigAttribEncQualityRange is not available in the used VA backend, then
the quality level is set to zero, which means "disabled".

All the available codecs now process this parameter if it is available.

https://bugzilla.gnome.org/show_bug.cgi?id=778733

Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
This commit is contained in:
Sreerenj Balachandran 2017-04-19 13:04:44 -07:00 committed by Víctor Manuel Jáquez Leal
parent 10eb6efb30
commit 4f343f82e3
9 changed files with 139 additions and 1 deletions

View file

@ -169,9 +169,44 @@ gst_vaapi_encoder_properties_get_default (const GstVaapiEncoderClass * klass)
cdata->encoder_tune_get_type (), cdata->default_encoder_tune,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
* GstVaapiEncoder:quality-level:
*
* The Encoding quality level.
*/
GST_VAAPI_ENCODER_PROPERTIES_APPEND (props,
GST_VAAPI_ENCODER_PROP_QUALITY_LEVEL,
g_param_spec_uint ("quality-level",
"Quality Level",
"Encoding Quality Level ", 1, 8,
4, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
return props;
}
gboolean
gst_vaapi_encoder_ensure_param_quality_level (GstVaapiEncoder * encoder,
GstVaapiEncPicture * picture)
{
GstVaapiEncMiscParam *misc;
VAEncMiscParameterBufferQualityLevel *quality_level;
/* quality level param is not supported */
if (GST_VAAPI_ENCODER_QUALITY_LEVEL (encoder) == 0)
return TRUE;
misc = GST_VAAPI_ENC_QUALITY_LEVEL_MISC_PARAM_NEW (encoder);
if (!misc)
return FALSE;
quality_level = misc->data;
memset (quality_level, 0, sizeof (VAEncMiscParameterBufferQualityLevel));
quality_level->quality_level = encoder->quality_level;
gst_vaapi_enc_picture_add_misc_param (picture, misc);
gst_vaapi_codec_object_replace (&misc, NULL);
return TRUE;
}
/**
* gst_vaapi_encoder_ref:
* @encoder: a #GstVaapiEncoder
@ -686,7 +721,7 @@ gst_vaapi_encoder_reconfigure_internal (GstVaapiEncoder * encoder)
GstVideoInfo *const vip = GST_VAAPI_ENCODER_VIDEO_INFO (encoder);
GstVaapiEncoderStatus status;
GstVaapiVideoPool *pool;
guint codedbuf_size;
guint codedbuf_size, quality_level_max = 0;
/* Generate a keyframe every second */
if (!encoder->keyframe_period)
@ -699,6 +734,16 @@ gst_vaapi_encoder_reconfigure_internal (GstVaapiEncoder * encoder)
if (!gst_vaapi_encoder_ensure_context (encoder))
goto error_reset_context;
if (get_config_attribute (encoder, VAConfigAttribEncQualityRange,
&quality_level_max) && quality_level_max > 0) {
encoder->quality_level =
gst_util_uint64_scale_int_ceil (encoder->quality_level,
quality_level_max, 8);
} else {
encoder->quality_level = 0;
}
GST_INFO ("Quality level is fixed to %d", encoder->quality_level);
codedbuf_size = encoder->codedbuf_pool ?
gst_vaapi_coded_buffer_pool_get_buffer_size (GST_VAAPI_CODED_BUFFER_POOL
(encoder)) : 0;
@ -810,6 +855,10 @@ set_property (GstVaapiEncoder * encoder, gint prop_id, const GValue * value)
case GST_VAAPI_ENCODER_PROP_TUNE:
status = gst_vaapi_encoder_set_tuning (encoder, g_value_get_enum (value));
break;
case GST_VAAPI_ENCODER_PROP_QUALITY_LEVEL:
status = gst_vaapi_encoder_set_quality_level (encoder,
g_value_get_uint (value));
break;
}
return status;
@ -1035,6 +1084,42 @@ error_operation_failed:
}
}
/**
* gst_vaapi_encoder_set_quality_level:
* @encoder: a #GstVaapiEncoder
* @quality_level: the encoder quality level
*
* Notifies the @encoder to use the supplied @quality_level value.
*
* Note: currently, the quality_level can only be specified before
* the last call to gst_vaapi_encoder_set_codec_state(), which shall
* occur before the first frame is encoded. Afterwards, any change to
* this parameter causes gst_vaapi_encoder_set_quality_level() to
* return @GST_VAAPI_ENCODER_STATUS_ERROR_OPERATION_FAILED.
*
* Return value: a #GstVaapiEncoderStatus
*/
GstVaapiEncoderStatus
gst_vaapi_encoder_set_quality_level (GstVaapiEncoder * encoder,
guint quality_level)
{
g_return_val_if_fail (encoder != NULL, 0);
if (encoder->quality_level != quality_level
&& encoder->num_codedbuf_queued > 0)
goto error_operation_failed;
encoder->quality_level = quality_level;
return GST_VAAPI_ENCODER_STATUS_SUCCESS;
/* ERRORS */
error_operation_failed:
{
GST_ERROR ("could not change quality level after encoding started");
return GST_VAAPI_ENCODER_STATUS_ERROR_OPERATION_FAILED;
}
}
/* Initialize default values for configurable properties */
static gboolean
gst_vaapi_encoder_init_properties (GstVaapiEncoder * encoder)

View file

@ -108,6 +108,7 @@ typedef enum {
GST_VAAPI_ENCODER_PROP_BITRATE,
GST_VAAPI_ENCODER_PROP_KEYFRAME_PERIOD,
GST_VAAPI_ENCODER_PROP_TUNE,
GST_VAAPI_ENCODER_PROP_QUALITY_LEVEL
} GstVaapiEncoderProp;
/**
@ -166,6 +167,10 @@ GstVaapiEncoderStatus
gst_vaapi_encoder_set_tuning (GstVaapiEncoder * encoder,
GstVaapiEncoderTune tuning);
GstVaapiEncoderStatus
gst_vaapi_encoder_set_quality_level (GstVaapiEncoder * encoder,
guint quality_level);
GstVaapiEncoderStatus
gst_vaapi_encoder_get_buffer_with_timeout (GstVaapiEncoder * encoder,
GstVaapiCodedBufferProxy ** out_codedbuf_proxy_ptr, guint64 timeout);

View file

@ -2157,6 +2157,7 @@ error_create_packed_seq_hdr:
static gboolean
ensure_misc_params (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
{
GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
GstVaapiEncMiscParam *misc = NULL;
VAEncMiscParameterRateControl *rate_control;
@ -2204,6 +2205,10 @@ ensure_misc_params (GstVaapiEncoderH264 * encoder, GstVaapiEncPicture * picture)
}
}
if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture))
return FALSE;
return TRUE;
error_create_packed_sei_hdr:

View file

@ -1765,6 +1765,7 @@ error_create_packed_seq_hdr:
static gboolean
ensure_misc_params (GstVaapiEncoderH265 * encoder, GstVaapiEncPicture * picture)
{
GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
GstVaapiEncMiscParam *misc = NULL;
/* HRD params for rate control */
@ -1778,6 +1779,8 @@ ensure_misc_params (GstVaapiEncoderH265 * encoder, GstVaapiEncPicture * picture)
gst_vaapi_codec_object_replace (&misc, NULL);
}
if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture))
return FALSE;
return TRUE;
}

View file

@ -496,6 +496,9 @@ set_misc_parameters (GstVaapiEncoderMpeg2 * encoder,
rate_control->basic_unit_size = 0;
gst_vaapi_codec_object_replace (&misc, NULL);
}
if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture))
return FALSE;
return TRUE;
}

View file

@ -319,6 +319,12 @@ gst_vaapi_enc_picture_encode (GstVaapiEncPicture * picture);
G_PASTE (VAEncMiscParameterType, type), \
sizeof (G_PASTE (VAEncMiscParameter, type)))
/* GstVaapiEncFeiMiscParam */
#define GST_VAAPI_ENC_QUALITY_LEVEL_MISC_PARAM_NEW(encoder) \
gst_vaapi_enc_misc_param_new (GST_VAAPI_ENCODER_CAST (encoder), \
VAEncMiscParameterTypeQualityLevel, \
sizeof (VAEncMiscParameterBufferQualityLevel))
/* GstVaapiEncPicture */
#define GST_VAAPI_ENC_PICTURE_NEW(codec, encoder, frame) \
gst_vaapi_enc_picture_new (GST_VAAPI_ENCODER_CAST (encoder), \

View file

@ -164,6 +164,17 @@ G_BEGIN_DECLS
#define GST_VAAPI_ENCODER_TUNE(encoder) \
(GST_VAAPI_ENCODER_CAST (encoder)->tune)
/**
* GST_VAAPI_ENCODER_QUALITY_LEVEL:
* @encoder: a #GstVaapiEncoder
*
* Macro that evaluates to the quality level
* This is an internal macro that does not do any run-time type check.
*/
#undef GST_VAAPI_ENCODER_QUALITY_LEVEL
#define GST_VAAPI_ENCODER_QUALITY_LEVEL(encoder) \
(GST_VAAPI_ENCODER_CAST (encoder)->quality_level)
/* Generate a mask for the supplied tuning option (internal) */
#define GST_VAAPI_ENCODER_TUNE_MASK(TUNE) \
(1U << G_PASTE (GST_VAAPI_ENCODER_TUNE_, TUNE))
@ -216,6 +227,7 @@ struct _GstVaapiEncoder
guint32 rate_control_mask;
guint bitrate; /* kbps */
guint keyframe_period;
guint quality_level;
GMutex mutex;
GCond surface_free;
@ -340,6 +352,11 @@ gst_vaapi_encoder_release_surface (GstVaapiEncoder * encoder,
gst_vaapi_surface_proxy_unref (proxy);
}
G_GNUC_INTERNAL
gboolean
gst_vaapi_encoder_ensure_param_quality_level (GstVaapiEncoder * encoder,
GstVaapiEncPicture * picture);
G_END_DECLS
#endif /* GST_VAAPI_ENCODER_PRIV_H */

View file

@ -313,6 +313,8 @@ ensure_misc_params (GstVaapiEncoderVP8 * encoder, GstVaapiEncPicture * picture)
gst_vaapi_enc_picture_add_misc_param (picture, misc);
gst_vaapi_codec_object_replace (&misc, NULL);
if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture))
return FALSE;
return TRUE;
}

View file

@ -215,6 +215,16 @@ error:
}
}
static gboolean
ensure_misc_params (GstVaapiEncoderVP9 * encoder, GstVaapiEncPicture * picture)
{
GstVaapiEncoder *const base_encoder = GST_VAAPI_ENCODER_CAST (encoder);
if (!gst_vaapi_encoder_ensure_param_quality_level (base_encoder, picture))
return FALSE;
return TRUE;
}
static void
get_ref_indices (guint ref_pic_mode, guint ref_list_idx, guint * last_idx,
guint * gf_idx, guint * arf_idx, guint8 * refresh_frame_flags)
@ -359,6 +369,8 @@ gst_vaapi_encoder_vp9_encode (GstVaapiEncoder * base_encoder,
if (!ensure_sequence (encoder, picture))
goto error;
if (!ensure_misc_params (encoder, picture))
goto error;
if (!ensure_picture (encoder, picture, codedbuf, reconstruct))
goto error;
if (!gst_vaapi_enc_picture_encode (picture))