* [FFmpeg-devel] [PATCH v2] libavcodec/qsvenc: Flush cached frames before reset encoder
@ 2023-02-13 9:03 wenbin.chen-at-intel.com
2023-02-16 1:23 ` Xiang, Haihao
0 siblings, 1 reply; 2+ messages in thread
From: wenbin.chen-at-intel.com @ 2023-02-13 9:03 UTC (permalink / raw)
To: ffmpeg-devel
From: Wenbin Chen <wenbin.chen@intel.com>
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 <wenbin.chen@intel.com>
---
libavcodec/qsvenc.c | 122 ++++++++++++++++++++++++--------------------
1 file changed, 66 insertions(+), 56 deletions(-)
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 2f0e94a914..3951f40e7b 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;
}
@@ -2448,7 +2396,9 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
ret = 0;
if (*pkt.sync) {
- av_fifo_write(q->async_fifo, &pkt, 1);
+ ret = av_fifo_write(q->async_fifo, &pkt, 1);
+ if (ret < 0)
+ goto free;
} else {
free:
av_freep(&pkt.sync);
@@ -2466,6 +2416,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 +2486,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".
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [FFmpeg-devel] [PATCH v2] libavcodec/qsvenc: Flush cached frames before reset encoder
2023-02-13 9:03 [FFmpeg-devel] [PATCH v2] libavcodec/qsvenc: Flush cached frames before reset encoder wenbin.chen-at-intel.com
@ 2023-02-16 1:23 ` Xiang, Haihao
0 siblings, 0 replies; 2+ messages in thread
From: Xiang, Haihao @ 2023-02-16 1:23 UTC (permalink / raw)
To: ffmpeg-devel
On Ma, 2023-02-13 at 17:03 +0800, wenbin.chen-at-intel.com@ffmpeg.org wrote:
> From: Wenbin Chen <wenbin.chen@intel.com>
>
> 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 <wenbin.chen@intel.com>
> ---
> libavcodec/qsvenc.c | 122 ++++++++++++++++++++++++--------------------
> 1 file changed, 66 insertions(+), 56 deletions(-)
>
> diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
> index 2f0e94a914..3951f40e7b 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;
> }
>
> @@ -2448,7 +2396,9 @@ static int encode_frame(AVCodecContext *avctx,
> QSVEncContext *q,
> ret = 0;
>
> if (*pkt.sync) {
> - av_fifo_write(q->async_fifo, &pkt, 1);
> + ret = av_fifo_write(q->async_fifo, &pkt, 1);
> + if (ret < 0)
> + goto free;
> } else {
> free:
> av_freep(&pkt.sync);
> @@ -2466,6 +2416,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 +2486,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) ||
LGTM, will apply.
Thanks
Haihao
_______________________________________________
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".
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-02-16 1:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-13 9:03 [FFmpeg-devel] [PATCH v2] libavcodec/qsvenc: Flush cached frames before reset encoder wenbin.chen-at-intel.com
2023-02-16 1:23 ` Xiang, Haihao
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git