From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id E4A1844C27 for ; Mon, 13 Feb 2023 05:39:44 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6FD6B68B9FF; Mon, 13 Feb 2023 07:39:42 +0200 (EET) Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8D09968B9CA for ; Mon, 13 Feb 2023 07:39:35 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676266780; x=1707802780; h=from:to:subject:date:message-id:mime-version: content-transfer-encoding; bh=AY494Ni8KXIB6rtOveF06LYGnWgA+3n1ROytQRD31kI=; b=Xmn89//WHD4D8aXU9hKwuIiZklVCwd8wzuEF4g+qgzco7zsdDeHzaPob FyVR3woBvUDWuUNqV8fR7KnxiXvI48orR4AZqzEv4wgmkMlYGbBiY9RRZ UgJKEVZSpdjSfCpiC00oZEFyGqo79tCIQKJ3DfifWk85oXgk4k7bFrTm/ BN/HhaYNhcX5pBzFW10Avq82yE5LarLisaHM8byjEDq9aaoUyF702At5V qllfDrF8zlnTi9MBrMDluYi0ljkVAPu0ZHZl13Zn/+4rtuyVN1Llj7drZ 91YKHi+U89SVMi2e3CejDHXMoID9ZPKhHrMvAUonhGmmsxo4s0nluprEI A==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="393214131" X-IronPort-AV: E=Sophos;i="5.97,293,1669104000"; d="scan'208";a="393214131" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2023 21:39:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="670682045" X-IronPort-AV: E=Sophos;i="5.97,293,1669104000"; d="scan'208";a="670682045" Received: from wenbin-z390-aorus-ultra.sh.intel.com ([10.239.35.4]) by fmsmga007.fm.intel.com with ESMTP; 12 Feb 2023 21:39:31 -0800 From: wenbin.chen-at-intel.com@ffmpeg.org To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Feb 2023 13:39:31 +0800 Message-Id: <20230213053931.534552-1-wenbin.chen@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavcodec/qsvenc: Flush cached frames before reset encoder X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Wenbin Chen According to https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#configuration-change. Before calling MFXVideoENCODE_Reset, The application needs to retrieve any cached frames in the SDK encoder. A loop is added before MFXVideoENCODE_Reset to retrieve cached frames and add them to async_fifo, so that dynamic configuration works when async_depth > 1. Signed-off-by: Wenbin Chen --- libavcodec/qsvenc.c | 118 +++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 55 deletions(-) diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 2f0e94a914..f3b488dec8 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -1600,7 +1600,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) q->param.AsyncDepth = q->async_depth; - q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVPacket), 0); + q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVPacket), AV_FIFO_FLAG_AUTO_GROW); if (!q->async_fifo) return AVERROR(ENOMEM); @@ -2296,58 +2296,6 @@ static int update_pic_timing_sei(AVCodecContext *avctx, QSVEncContext *q) return updated; } -static int update_parameters(AVCodecContext *avctx, QSVEncContext *q, - const AVFrame *frame) -{ - int needReset = 0, ret = 0; - - if (!frame || avctx->codec_id == AV_CODEC_ID_MJPEG) - return 0; - - needReset = update_qp(avctx, q); - needReset |= update_max_frame_size(avctx, q); - needReset |= update_gop_size(avctx, q); - needReset |= update_rir(avctx, q); - needReset |= update_low_delay_brc(avctx, q); - needReset |= update_frame_rate(avctx, q); - needReset |= update_bitrate(avctx, q); - needReset |= update_pic_timing_sei(avctx, q); - ret = update_min_max_qp(avctx, q); - if (ret < 0) - return ret; - needReset |= ret; - if (!needReset) - return 0; - - if (avctx->hwaccel_context) { - AVQSVContext *qsv = avctx->hwaccel_context; - int i, j; - q->param.ExtParam = q->extparam; - for (i = 0; i < qsv->nb_ext_buffers; i++) - q->param.ExtParam[i] = qsv->ext_buffers[i]; - q->param.NumExtParam = qsv->nb_ext_buffers; - - for (i = 0; i < q->nb_extparam_internal; i++) { - for (j = 0; j < qsv->nb_ext_buffers; j++) { - if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId) - break; - } - if (j < qsv->nb_ext_buffers) - continue; - q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i]; - } - } else { - q->param.ExtParam = q->extparam_internal; - q->param.NumExtParam = q->nb_extparam_internal; - } - av_log(avctx, AV_LOG_DEBUG, "Parameter change, call msdk reset.\n"); - ret = MFXVideoENCODE_Reset(q->session, &q->param); - if (ret < 0) - return ff_qsv_print_error(avctx, ret, "Error during resetting"); - - return 0; -} - static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame) { @@ -2438,7 +2386,7 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, if (ret < 0) { ret = (ret == MFX_ERR_MORE_DATA) ? - 0 : ff_qsv_print_error(avctx, ret, "Error during encoding"); + AVERROR(EAGAIN) : ff_qsv_print_error(avctx, ret, "Error during encoding"); goto free; } @@ -2466,6 +2414,66 @@ nomem: goto free; } +static int update_parameters(AVCodecContext *avctx, QSVEncContext *q, + const AVFrame *frame) +{ + int needReset = 0, ret = 0; + + if (!frame || avctx->codec_id == AV_CODEC_ID_MJPEG) + return 0; + + needReset = update_qp(avctx, q); + needReset |= update_max_frame_size(avctx, q); + needReset |= update_gop_size(avctx, q); + needReset |= update_rir(avctx, q); + needReset |= update_low_delay_brc(avctx, q); + needReset |= update_frame_rate(avctx, q); + needReset |= update_bitrate(avctx, q); + needReset |= update_pic_timing_sei(avctx, q); + ret = update_min_max_qp(avctx, q); + if (ret < 0) + return ret; + needReset |= ret; + if (!needReset) + return 0; + + if (avctx->hwaccel_context) { + AVQSVContext *qsv = avctx->hwaccel_context; + int i, j; + q->param.ExtParam = q->extparam; + for (i = 0; i < qsv->nb_ext_buffers; i++) + q->param.ExtParam[i] = qsv->ext_buffers[i]; + q->param.NumExtParam = qsv->nb_ext_buffers; + + for (i = 0; i < q->nb_extparam_internal; i++) { + for (j = 0; j < qsv->nb_ext_buffers; j++) { + if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId) + break; + } + if (j < qsv->nb_ext_buffers) + continue; + q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i]; + } + } else { + q->param.ExtParam = q->extparam_internal; + q->param.NumExtParam = q->nb_extparam_internal; + } + + // Flush codec before reset configuration. + while (ret != AVERROR(EAGAIN)) { + ret = encode_frame(avctx, q, NULL); + if (ret < 0 && ret != AVERROR(EAGAIN)) + return ret; + } + + av_log(avctx, AV_LOG_DEBUG, "Parameter change, call msdk reset.\n"); + ret = MFXVideoENCODE_Reset(q->session, &q->param); + if (ret < 0) + return ff_qsv_print_error(avctx, ret, "Error during resetting"); + + return 0; +} + int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, AVPacket *pkt, const AVFrame *frame, int *got_packet) { @@ -2476,7 +2484,7 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, return ret; ret = encode_frame(avctx, q, frame); - if (ret < 0) + if (ret < 0 && ret != AVERROR(EAGAIN)) return ret; if ((av_fifo_can_read(q->async_fifo) >= q->async_depth) || -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".