Re: [FFmpeg-devel] [PATCH] libavcodec/qsvenc: Add skip_frame support to qsvenc

2022-11-06 Thread Xiang, Haihao
On Wed, 2022-11-02 at 16:11 +0800, wenbin.chen-at-intel@ffmpeg.org wrote:
> From: Wenbin Chen 
> 
> Add skip_frame support to qsvenc. Use per-frame metadata
> "qsv_skip_frame" to control it. skip_frame option defines the behavior
> of qsv_skip_frame.
> no_skip: Frame skipping is disabled.
> insert_dummy: Encoder inserts into bitstream frame where all macroblocks
> are encoded as skipped.
> insert_nothing: Similar to insert_dummy, but encoder inserts nothing.
> The skipped frames are still used in brc. For example, gop still include
> skipped frames, and the frames after skipped frames will be larger in
> size.
> brc_only: skip_frame metadata indicates the number of missed frames
> before the current frame.
> 
> Signed-off-by: Wenbin Chen 
> ---
>  doc/encoders.texi| 36 
>  libavcodec/qsvenc.c  | 36 
>  libavcodec/qsvenc.h  | 13 +
>  libavcodec/qsvenc_h264.c |  1 +
>  libavcodec/qsvenc_hevc.c |  1 +
>  5 files changed, 87 insertions(+)
> 
> diff --git a/doc/encoders.texi b/doc/encoders.texi
> index 53dd02fd28..59f39d18f6 100644
> --- a/doc/encoders.texi
> +++ b/doc/encoders.texi
> @@ -3564,6 +3564,24 @@ bitrate, @var{target_bitrate}, within the accuracy
> range @var{avbr_accuracy},
>  after a @var{avbr_Convergence} period. This method does not follow HRD and
> the
>  instant bitrate is not capped or padded.
>  
> +@item @var{skip_frame}
> +Use per-frame metadata "qsv_skip_frame" to skip frame when encoding. This
> option
> +defines the usage of this metadata.
> +@table @samp
> +@item no_skip
> +Frame skipping is disabled.
> +@item insert_dummy
> +Encoder inserts into bitstream frame where all macroblocks are encoded as
> +skipped.
> +@item insert_nothing
> +Similar to insert_dummy, but encoder inserts nothing into bitstream. The
> skipped
> +frames are still used in brc. For example, gop still include skipped frames,
> and
> +the frames after skipped frames will be larger in size.
> +@item brc_only
> +skip_frame metadata indicates the number of missed frames before the current
> +frame.
> +@end table
> +
>  @end table
>  
>  @subsection HEVC Options
> @@ -3742,6 +3760,24 @@ bitrate, @var{target_bitrate}, within the accuracy
> range @var{avbr_accuracy},
>  after a @var{avbr_Convergence} period. This method does not follow HRD and
> the
>  instant bitrate is not capped or padded.
>  
> +@item @var{skip_frame}
> +Use per-frame metadata "qsv_skip_frame" to skip frame when encoding. This
> option
> +defines the usage of this metadata.
> +@table @samp
> +@item no_skip
> +Frame skipping is disabled.
> +@item insert_dummy
> +Encoder inserts into bitstream frame where all macroblocks are encoded as
> +skipped.
> +@item insert_nothing
> +Similar to insert_dummy, but encoder inserts nothing into bitstream. The
> skipped
> +frames are still used in brc. For example, gop still include skipped frames,
> and
> +the frames after skipped frames will be larger in size.
> +@item brc_only
> +skip_frame metadata indicates the number of missed frames before the current
> +frame.
> +@end table
> +
>  @end table
>  
>  @subsection MPEG2 Options
> diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
> index 0db774ea63..4bfa65c575 100644
> --- a/libavcodec/qsvenc.c
> +++ b/libavcodec/qsvenc.c
> @@ -329,6 +329,22 @@ static void dump_video_param(AVCodecContext *avctx,
> QSVEncContext *q,
> "MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP:
> %"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
> co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, co2-
> >MinQPB, co2->MaxQPB);
>  av_log(avctx, AV_LOG_VERBOSE, "DisableDeblockingIdc: %"PRIu32" \n",
> co2->DisableDeblockingIdc);
> +
> +switch (co2->SkipFrame) {
> +case MFX_SKIPFRAME_NO_SKIP:
> +av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: no_skip\n");
> +break;
> +case MFX_SKIPFRAME_INSERT_DUMMY:
> +av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: insert_dummy\n");
> +break;
> +case MFX_SKIPFRAME_INSERT_NOTHING:
> +av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: insert_nothing\n");
> +break;
> +case MFX_SKIPFRAME_BRC_ONLY:
> +av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: brc_only\n");
> +break;
> +default: break;
> +}
>  }
>  
>  if (co3) {
> @@ -991,6 +1007,8 @@ static int init_video_param(AVCodecContext *avctx,
> QSVEncContext *q)
>  q->old_max_qp_b = q->max_qp_b;
>  if (q->mbbrc >= 0)
>  q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON :
> MFX_CODINGOPTION_OFF;
> +if (q->skip_frame >= 0)
> +q->extco2.SkipFrame = q->skip_frame;
>  
>  q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
>  q->extco2.Header.BufferSz = sizeof(q->extco2);
> @@ -1911,6 +1929,19 @@ static int 

[FFmpeg-devel] [PATCH] libavcodec/qsvenc: Add skip_frame support to qsvenc

2022-11-02 Thread wenbin . chen-at-intel . com
From: Wenbin Chen 

Add skip_frame support to qsvenc. Use per-frame metadata
"qsv_skip_frame" to control it. skip_frame option defines the behavior
of qsv_skip_frame.
no_skip: Frame skipping is disabled.
insert_dummy: Encoder inserts into bitstream frame where all macroblocks
are encoded as skipped.
insert_nothing: Similar to insert_dummy, but encoder inserts nothing.
The skipped frames are still used in brc. For example, gop still include
skipped frames, and the frames after skipped frames will be larger in
size.
brc_only: skip_frame metadata indicates the number of missed frames
before the current frame.

Signed-off-by: Wenbin Chen 
---
 doc/encoders.texi| 36 
 libavcodec/qsvenc.c  | 36 
 libavcodec/qsvenc.h  | 13 +
 libavcodec/qsvenc_h264.c |  1 +
 libavcodec/qsvenc_hevc.c |  1 +
 5 files changed, 87 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 53dd02fd28..59f39d18f6 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3564,6 +3564,24 @@ bitrate, @var{target_bitrate}, within the accuracy range 
@var{avbr_accuracy},
 after a @var{avbr_Convergence} period. This method does not follow HRD and the
 instant bitrate is not capped or padded.
 
+@item @var{skip_frame}
+Use per-frame metadata "qsv_skip_frame" to skip frame when encoding. This 
option
+defines the usage of this metadata.
+@table @samp
+@item no_skip
+Frame skipping is disabled.
+@item insert_dummy
+Encoder inserts into bitstream frame where all macroblocks are encoded as
+skipped.
+@item insert_nothing
+Similar to insert_dummy, but encoder inserts nothing into bitstream. The 
skipped
+frames are still used in brc. For example, gop still include skipped frames, 
and
+the frames after skipped frames will be larger in size.
+@item brc_only
+skip_frame metadata indicates the number of missed frames before the current
+frame.
+@end table
+
 @end table
 
 @subsection HEVC Options
@@ -3742,6 +3760,24 @@ bitrate, @var{target_bitrate}, within the accuracy range 
@var{avbr_accuracy},
 after a @var{avbr_Convergence} period. This method does not follow HRD and the
 instant bitrate is not capped or padded.
 
+@item @var{skip_frame}
+Use per-frame metadata "qsv_skip_frame" to skip frame when encoding. This 
option
+defines the usage of this metadata.
+@table @samp
+@item no_skip
+Frame skipping is disabled.
+@item insert_dummy
+Encoder inserts into bitstream frame where all macroblocks are encoded as
+skipped.
+@item insert_nothing
+Similar to insert_dummy, but encoder inserts nothing into bitstream. The 
skipped
+frames are still used in brc. For example, gop still include skipped frames, 
and
+the frames after skipped frames will be larger in size.
+@item brc_only
+skip_frame metadata indicates the number of missed frames before the current
+frame.
+@end table
+
 @end table
 
 @subsection MPEG2 Options
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 0db774ea63..4bfa65c575 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -329,6 +329,22 @@ static void dump_video_param(AVCodecContext *avctx, 
QSVEncContext *q,
"MinQPI: %"PRIu8"; MaxQPI: %"PRIu8"; MinQPP: %"PRIu8"; MaxQPP: 
%"PRIu8"; MinQPB: %"PRIu8"; MaxQPB: %"PRIu8"\n",
co2->MinQPI, co2->MaxQPI, co2->MinQPP, co2->MaxQPP, 
co2->MinQPB, co2->MaxQPB);
 av_log(avctx, AV_LOG_VERBOSE, "DisableDeblockingIdc: %"PRIu32" \n", 
co2->DisableDeblockingIdc);
+
+switch (co2->SkipFrame) {
+case MFX_SKIPFRAME_NO_SKIP:
+av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: no_skip\n");
+break;
+case MFX_SKIPFRAME_INSERT_DUMMY:
+av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: insert_dummy\n");
+break;
+case MFX_SKIPFRAME_INSERT_NOTHING:
+av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: insert_nothing\n");
+break;
+case MFX_SKIPFRAME_BRC_ONLY:
+av_log(avctx, AV_LOG_VERBOSE, "SkipFrame: brc_only\n");
+break;
+default: break;
+}
 }
 
 if (co3) {
@@ -991,6 +1007,8 @@ static int init_video_param(AVCodecContext *avctx, 
QSVEncContext *q)
 q->old_max_qp_b = q->max_qp_b;
 if (q->mbbrc >= 0)
 q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : 
MFX_CODINGOPTION_OFF;
+if (q->skip_frame >= 0)
+q->extco2.SkipFrame = q->skip_frame;
 
 q->extco2.Header.BufferId = MFX_EXTBUFF_CODING_OPTION2;
 q->extco2.Header.BufferSz = sizeof(q->extco2);
@@ -1911,6 +1929,19 @@ static int set_roi_encode_ctrl(AVCodecContext *avctx, 
const AVFrame *frame,
 return 0;
 }
 
+static void set_skip_frame_encode_ctrl(AVCodecContext *avctx, const AVFrame 
*frame,
+   mfxEncodeCtrl *enc_ctrl)
+{
+AVDictionaryEntry* skip_frame_dict = NULL;
+if (!frame->metadata)
+return;
+skip_frame_dict =