vaapi_encode_h264: Enable support for more RC modes

Also fixes QP going out of range when modified by the quant factor/offset
values.
This commit is contained in:
Mark Thompson 2019-02-10 19:46:55 +00:00
parent f4d7bd6cdb
commit f5eb08937e
1 changed files with 19 additions and 12 deletions

View File

@ -1065,33 +1065,34 @@ static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
priv->mb_height = FFALIGN(avctx->height, 16) / 16; priv->mb_height = FFALIGN(avctx->height, 16) / 16;
if (ctx->va_rc_mode == VA_RC_CQP) { if (ctx->va_rc_mode == VA_RC_CQP) {
priv->fixed_qp_p = priv->qp; priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
if (avctx->i_quant_factor > 0.0) if (avctx->i_quant_factor > 0.0)
priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor + priv->fixed_qp_idr =
avctx->i_quant_offset) + 0.5); av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
avctx->i_quant_offset) + 0.5, 1, 51);
else else
priv->fixed_qp_idr = priv->fixed_qp_p; priv->fixed_qp_idr = priv->fixed_qp_p;
if (avctx->b_quant_factor > 0.0) if (avctx->b_quant_factor > 0.0)
priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor + priv->fixed_qp_b =
avctx->b_quant_offset) + 0.5); av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
avctx->b_quant_offset) + 0.5, 1, 51);
else else
priv->fixed_qp_b = priv->fixed_qp_p; priv->fixed_qp_b = priv->fixed_qp_p;
priv->sei &= ~SEI_TIMING;
av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = " av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
"%d / %d / %d for IDR- / P- / B-frames.\n", "%d / %d / %d for IDR- / P- / B-frames.\n",
priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b); priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
} else if (ctx->va_rc_mode == VA_RC_CBR || } else {
ctx->va_rc_mode == VA_RC_VBR) {
// These still need to be set for pic_init_qp/slice_qp_delta. // These still need to be set for pic_init_qp/slice_qp_delta.
priv->fixed_qp_idr = 26; priv->fixed_qp_idr = 26;
priv->fixed_qp_p = 26; priv->fixed_qp_p = 26;
priv->fixed_qp_b = 26; priv->fixed_qp_b = 26;
}
} else { if (!ctx->rc_mode->hrd) {
av_assert0(0 && "Invalid RC mode."); // Timing SEI requires a mode respecting HRD parameters.
priv->sei &= ~SEI_TIMING;
} }
if (priv->sei & SEI_IDENTIFIER) { if (priv->sei & SEI_IDENTIFIER) {
@ -1141,6 +1142,8 @@ static const VAAPIEncodeType vaapi_encode_type_h264 = {
FLAG_B_PICTURE_REFERENCES | FLAG_B_PICTURE_REFERENCES |
FLAG_NON_IDR_KEY_PICTURES, FLAG_NON_IDR_KEY_PICTURES,
.default_quality = 20,
.configure = &vaapi_encode_h264_configure, .configure = &vaapi_encode_h264_configure,
.picture_priv_data_size = sizeof(VAAPIEncodeH264Picture), .picture_priv_data_size = sizeof(VAAPIEncodeH264Picture),
@ -1220,6 +1223,9 @@ static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
ctx->slice_block_height = ctx->slice_block_width = 16; ctx->slice_block_height = ctx->slice_block_width = 16;
if (priv->qp > 0)
ctx->explicit_qp = priv->qp;
return ff_vaapi_encode_init(avctx); return ff_vaapi_encode_init(avctx);
} }
@ -1238,9 +1244,10 @@ static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM) #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
static const AVOption vaapi_encode_h264_options[] = { static const AVOption vaapi_encode_h264_options[] = {
VAAPI_ENCODE_COMMON_OPTIONS, VAAPI_ENCODE_COMMON_OPTIONS,
VAAPI_ENCODE_RC_OPTIONS,
{ "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)", { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS }, OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
{ "quality", "Set encode quality (trades off against speed, higher is faster)", { "quality", "Set encode quality (trades off against speed, higher is faster)",
OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
{ "coder", "Entropy coder type", { "coder", "Entropy coder type",