diff options
author | Wenbin Chen <wenbin.chen-at-intel.com@ffmpeg.org> | 2022-07-14 16:43:25 +0800 |
---|---|---|
committer | Haihao Xiang <haihao.xiang@intel.com> | 2022-07-20 12:35:23 +0800 |
commit | 52ad617af56e5bd009f488a26ceb4f73595b2477 (patch) | |
tree | 99eed9076f93012fb281ed5ecb3f4042f256c114 /libavcodec | |
parent | 8b82c9cdc74fd227726b81a7c1768b11536c4471 (diff) | |
download | ffmpeg-52ad617af56e5bd009f488a26ceb4f73595b2477.tar.gz |
libavcodec/qsvenc: Use parameter from AVCodecContext to reset qsv codec
Using parameter from AVCodecContext to reset qsv codec is more suitable
for MFXVideoENCODE_Reset()'s usage. Per-frame metadata is more suitable
for the usage of mfxEncodeCtrl being passed to
MFXVideoENCODE_EncodeFrameAsync(). Now change it to use the value
from AVCodecContext.
Because q->param is passed to both "in" and "out" parameters when call
MFXVideoENCODE_Query(), the value in q->param may be changed. New
variables are added to store old configuration, so that we can detect
real parameter change.
Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
Signed-off-by: Haihao Xiang <haihao.xiang@intel.com>
Diffstat (limited to 'libavcodec')
-rw-r--r-- | libavcodec/qsvenc.c | 50 | ||||
-rw-r--r-- | libavcodec/qsvenc.h | 6 |
2 files changed, 30 insertions, 26 deletions
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 5729292f94..7594091a4b 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -735,6 +735,11 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) q->param.mfx.QPI = av_clip(quant * fabs(avctx->i_quant_factor) + avctx->i_quant_offset, 0, 51); q->param.mfx.QPP = av_clip(quant, 0, 51); q->param.mfx.QPB = av_clip(quant * fabs(avctx->b_quant_factor) + avctx->b_quant_offset, 0, 51); + q->old_global_quality = avctx->global_quality; + q->old_i_quant_factor = avctx->i_quant_factor; + q->old_i_quant_offset = avctx->i_quant_offset; + q->old_b_quant_factor = avctx->b_quant_factor; + q->old_b_quant_offset = avctx->b_quant_offset; break; #if QSV_HAVE_AVBR @@ -1618,38 +1623,31 @@ static int set_roi_encode_ctrl(AVCodecContext *avctx, const AVFrame *frame, return 0; } -static int update_qp(AVCodecContext *avctx, QSVEncContext *q, - const AVFrame *frame) +static int update_qp(AVCodecContext *avctx, QSVEncContext *q) { - int updated = 0, qp = 0, new_qp; - char *tail; - AVDictionaryEntry *entry = NULL; + int updated = 0, new_qp = 0; if (avctx->codec_id != AV_CODEC_ID_H264 && avctx->codec_id != AV_CODEC_ID_HEVC) return 0; - entry = av_dict_get(frame->metadata, "qsv_config_qp", NULL, 0); - if (entry && q->param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) { - qp = strtol(entry->value, &tail, 10); - if (*tail) { - av_log(avctx, AV_LOG_WARNING, "Invalid qsv_config_qp string. Ignore this metadata\n"); + if (q->param.mfx.RateControlMethod == MFX_RATECONTROL_CQP) { + UPDATE_PARAM(q->old_global_quality, avctx->global_quality); + UPDATE_PARAM(q->old_i_quant_factor, avctx->i_quant_factor); + UPDATE_PARAM(q->old_i_quant_offset, avctx->i_quant_offset); + UPDATE_PARAM(q->old_b_quant_factor, avctx->b_quant_factor); + UPDATE_PARAM(q->old_b_quant_offset, avctx->b_quant_offset); + if (!updated) return 0; - } - if (qp < 0 || qp > 51) { - av_log(avctx, AV_LOG_WARNING, "Invalid qp, clip to 0 ~ 51\n"); - qp = av_clip(qp, 0, 51); - } - av_log(avctx, AV_LOG_DEBUG, "Configure qp: %d\n",qp); - UPDATE_PARAM(q->param.mfx.QPP, qp); - new_qp = av_clip(qp * fabs(avctx->i_quant_factor) + - avctx->i_quant_offset, 0, 51); - UPDATE_PARAM(q->param.mfx.QPI, new_qp); - new_qp = av_clip(qp * fabs(avctx->b_quant_factor) + - avctx->b_quant_offset, 0, 51); - UPDATE_PARAM(q->param.mfx.QPB, new_qp); + + new_qp = avctx->global_quality / FF_QP2LAMBDA; + q->param.mfx.QPI = av_clip(new_qp * fabs(avctx->i_quant_factor) + + avctx->i_quant_offset, 0, 51); + q->param.mfx.QPP = av_clip(new_qp, 0, 51); + q->param.mfx.QPB = av_clip(new_qp * fabs(avctx->b_quant_factor) + + avctx->b_quant_offset, 0, 51); av_log(avctx, AV_LOG_DEBUG, - "using fixed qp = %d/%d/%d for idr/p/b frames\n", - q->param.mfx.QPI, q->param.mfx.QPP, q->param.mfx.QPB); + "Reset qp = %d/%d/%d for idr/p/b frames\n", + q->param.mfx.QPI, q->param.mfx.QPP, q->param.mfx.QPB); } return updated; } @@ -1662,7 +1660,7 @@ static int update_parameters(AVCodecContext *avctx, QSVEncContext *q, if (!frame) return 0; - needReset = update_qp(avctx, q, frame); + needReset = update_qp(avctx, q); if (!needReset) return 0; diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index b754ac4b56..bffe957526 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -224,6 +224,12 @@ typedef struct QSVEncContext { int min_qp_p; int max_qp_b; int min_qp_b; + // These are used for qp reset + int old_global_quality; + float old_i_quant_factor; + float old_i_quant_offset; + float old_b_quant_factor; + float old_b_quant_offset; } QSVEncContext; int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q); |