diff mbox series

[FFmpeg-devel,v2,3/3] libavcodec/qsvdec: using suggested num to set init_pool_size

Message ID 20220318062511.382951-3-wenbin.chen@intel.com
State Accepted
Commit 0a0847dbac9bdfd24607572886a0d42cdea1691a
Headers show
Series [FFmpeg-devel,v2,1/3] libavcodec/qsvdec: reinit decoder according to decode() return value | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_aarch64_jetson success Make finished
andriy/make_fate_aarch64_jetson success Make fate finished
andriy/make_armv7_RPi4 success Make finished
andriy/make_fate_armv7_RPi4 success Make fate finished

Commit Message

Chen, Wenbin March 18, 2022, 6:25 a.m. UTC
The init_pool_size is set to be 64 and it is too many.
Use IOSurfQuery to get NumFrameSuggest which is the suggested
number of frame that needed to be allocated when initializing the decoder.
Considering that the hevc_qsv encoder uses the  most frame buffer,
async is 4 (default) and max_b_frames is 8 (default) and decoder
may followed by VPP, use NumFrameSuggest + 16 to set init_pool_size.

Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
Signed-off-by: Guangxin Xu <guangxin.xu@intel.com>
---
 libavcodec/qsvdec.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

Comments

Soft Works March 18, 2022, 7:40 a.m. UTC | #1
> -----Original Message-----
> From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> Wenbin Chen
> Sent: Friday, March 18, 2022 7:25 AM
> To: ffmpeg-devel@ffmpeg.org
> Subject: [FFmpeg-devel] [PATCH v2 3/3] libavcodec/qsvdec: using
> suggested num to set init_pool_size
> 
> The init_pool_size is set to be 64 and it is too many.
> Use IOSurfQuery to get NumFrameSuggest which is the suggested
> number of frame that needed to be allocated when initializing the
> decoder.
> Considering that the hevc_qsv encoder uses the  most frame buffer,
> async is 4 (default) and max_b_frames is 8 (default) and decoder
> may followed by VPP, use NumFrameSuggest + 16 to set init_pool_size.
> 
> Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
> Signed-off-by: Guangxin Xu <guangxin.xu@intel.com>
> ---
>  libavcodec/qsvdec.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
> index 210bd0c1d5..9875d3d632 100644
> --- a/libavcodec/qsvdec.c
> +++ b/libavcodec/qsvdec.c
> @@ -88,7 +88,7 @@ typedef struct QSVContext {
>      uint32_t fourcc;
>      mfxFrameInfo frame_info;
>      AVBufferPool *pool;
> -
> +    int suggest_pool_size;
>      int initialized;
> 
>      // options set by the caller
> @@ -275,7 +275,7 @@ static int qsv_decode_preinit(AVCodecContext
> *avctx, QSVContext *q, enum AVPixel
>          hwframes_ctx->height            = FFALIGN(avctx-
> >coded_height, 32);
>          hwframes_ctx->format            = AV_PIX_FMT_QSV;
>          hwframes_ctx->sw_format         = avctx->sw_pix_fmt;
> -        hwframes_ctx->initial_pool_size = 64 + avctx-
> >extra_hw_frames;
> +        hwframes_ctx->initial_pool_size = q->suggest_pool_size + 16 +
> avctx->extra_hw_frames;
>          frames_hwctx->frame_type        =
> MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
> 
>          ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
> @@ -793,6 +793,9 @@ static int qsv_process_data(AVCodecContext *avctx,
> QSVContext *q,
>      }
> 
>      if (q->reinit_flag || !q->session || !q->initialized) {
> +        mfxFrameAllocRequest request;
> +        memset(&request, 0, sizeof(request));
> +
>          q->reinit_flag = 0;
>          ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
>          if (ret < 0) {
> @@ -802,12 +805,19 @@ static int qsv_process_data(AVCodecContext
> *avctx, QSVContext *q,
>                  av_log(avctx, AV_LOG_ERROR, "Error decoding
> header\n");
>              goto reinit_fail;
>          }
> +        param.IOPattern = q->iopattern;
> 
>          q->orig_pix_fmt = avctx->pix_fmt = pix_fmt =
> ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
> 
>          avctx->coded_width  = param.mfx.FrameInfo.Width;
>          avctx->coded_height = param.mfx.FrameInfo.Height;
> 
> +        ret = MFXVideoDECODE_QueryIOSurf(q->session, &param,
> &request);
> +        if (ret < 0)
> +            return ff_qsv_print_error(avctx, ret, "Error querying IO
> surface");
> +
> +        q->suggest_pool_size = request.NumFrameSuggested;
> +
>          ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
>          if (ret < 0)
>              goto reinit_fail;
> --

Thanks for the patch! I have that on my list for quite a while.
Will look at it shortly.

softworkz
Xiang, Haihao March 28, 2022, 2:26 a.m. UTC | #2
On Fri, 2022-03-18 at 07:40 +0000, Soft Works wrote:
> > -----Original Message-----
> > From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> > Wenbin Chen
> > Sent: Friday, March 18, 2022 7:25 AM
> > To: ffmpeg-devel@ffmpeg.org
> > Subject: [FFmpeg-devel] [PATCH v2 3/3] libavcodec/qsvdec: using
> > suggested num to set init_pool_size
> > 
> > The init_pool_size is set to be 64 and it is too many.
> > Use IOSurfQuery to get NumFrameSuggest which is the suggested
> > number of frame that needed to be allocated when initializing the
> > decoder.
> > Considering that the hevc_qsv encoder uses the  most frame buffer,
> > async is 4 (default) and max_b_frames is 8 (default) and decoder
> > may followed by VPP, use NumFrameSuggest + 16 to set init_pool_size.
> > 
> > Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
> > Signed-off-by: Guangxin Xu <guangxin.xu@intel.com>
> > ---
> >  libavcodec/qsvdec.c | 14 ++++++++++++--
> >  1 file changed, 12 insertions(+), 2 deletions(-)
> > 
> > diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
> > index 210bd0c1d5..9875d3d632 100644
> > --- a/libavcodec/qsvdec.c
> > +++ b/libavcodec/qsvdec.c
> > @@ -88,7 +88,7 @@ typedef struct QSVContext {
> >      uint32_t fourcc;
> >      mfxFrameInfo frame_info;
> >      AVBufferPool *pool;
> > -
> > +    int suggest_pool_size;
> >      int initialized;
> > 
> >      // options set by the caller
> > @@ -275,7 +275,7 @@ static int qsv_decode_preinit(AVCodecContext
> > *avctx, QSVContext *q, enum AVPixel
> >          hwframes_ctx->height            = FFALIGN(avctx-
> > > coded_height, 32);
> > 
> >          hwframes_ctx->format            = AV_PIX_FMT_QSV;
> >          hwframes_ctx->sw_format         = avctx->sw_pix_fmt;
> > -        hwframes_ctx->initial_pool_size = 64 + avctx-
> > > extra_hw_frames;
> > 
> > +        hwframes_ctx->initial_pool_size = q->suggest_pool_size + 16 +
> > avctx->extra_hw_frames;
> >          frames_hwctx->frame_type        =
> > MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
> > 
> >          ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
> > @@ -793,6 +793,9 @@ static int qsv_process_data(AVCodecContext *avctx,
> > QSVContext *q,
> >      }
> > 
> >      if (q->reinit_flag || !q->session || !q->initialized) {
> > +        mfxFrameAllocRequest request;
> > +        memset(&request, 0, sizeof(request));
> > +
> >          q->reinit_flag = 0;
> >          ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
> >          if (ret < 0) {
> > @@ -802,12 +805,19 @@ static int qsv_process_data(AVCodecContext
> > *avctx, QSVContext *q,
> >                  av_log(avctx, AV_LOG_ERROR, "Error decoding
> > header\n");
> >              goto reinit_fail;
> >          }
> > +        param.IOPattern = q->iopattern;
> > 
> >          q->orig_pix_fmt = avctx->pix_fmt = pix_fmt =
> > ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
> > 
> >          avctx->coded_width  = param.mfx.FrameInfo.Width;
> >          avctx->coded_height = param.mfx.FrameInfo.Height;
> > 
> > +        ret = MFXVideoDECODE_QueryIOSurf(q->session, &param,
> > &request);
> > +        if (ret < 0)
> > +            return ff_qsv_print_error(avctx, ret, "Error querying IO
> > surface");
> > +
> > +        q->suggest_pool_size = request.NumFrameSuggested;
> > +
> >          ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
> >          if (ret < 0)
> >              goto reinit_fail;
> > --
> 
> Thanks for the patch! I have that on my list for quite a while.
> Will look at it shortly.

Hi Softworz,

This patchset LGTM and works well, do you have any comment ? 

Thanks
Haihao
Xiang, Haihao April 2, 2022, 8:52 a.m. UTC | #3
On Mon, 2022-03-28 at 02:26 +0000, Xiang, Haihao wrote:
> On Fri, 2022-03-18 at 07:40 +0000, Soft Works wrote:
> > > -----Original Message-----
> > > From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> > > Wenbin Chen
> > > Sent: Friday, March 18, 2022 7:25 AM
> > > To: ffmpeg-devel@ffmpeg.org
> > > Subject: [FFmpeg-devel] [PATCH v2 3/3] libavcodec/qsvdec: using
> > > suggested num to set init_pool_size
> > > 
> > > The init_pool_size is set to be 64 and it is too many.
> > > Use IOSurfQuery to get NumFrameSuggest which is the suggested
> > > number of frame that needed to be allocated when initializing the
> > > decoder.
> > > Considering that the hevc_qsv encoder uses the  most frame buffer,
> > > async is 4 (default) and max_b_frames is 8 (default) and decoder
> > > may followed by VPP, use NumFrameSuggest + 16 to set init_pool_size.
> > > 
> > > Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
> > > Signed-off-by: Guangxin Xu <guangxin.xu@intel.com>
> > > ---
> > >  libavcodec/qsvdec.c | 14 ++++++++++++--
> > >  1 file changed, 12 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
> > > index 210bd0c1d5..9875d3d632 100644
> > > --- a/libavcodec/qsvdec.c
> > > +++ b/libavcodec/qsvdec.c
> > > @@ -88,7 +88,7 @@ typedef struct QSVContext {
> > >      uint32_t fourcc;
> > >      mfxFrameInfo frame_info;
> > >      AVBufferPool *pool;
> > > -
> > > +    int suggest_pool_size;
> > >      int initialized;
> > > 
> > >      // options set by the caller
> > > @@ -275,7 +275,7 @@ static int qsv_decode_preinit(AVCodecContext
> > > *avctx, QSVContext *q, enum AVPixel
> > >          hwframes_ctx->height            = FFALIGN(avctx-
> > > > coded_height, 32);
> > > 
> > >          hwframes_ctx->format            = AV_PIX_FMT_QSV;
> > >          hwframes_ctx->sw_format         = avctx->sw_pix_fmt;
> > > -        hwframes_ctx->initial_pool_size = 64 + avctx-
> > > > extra_hw_frames;
> > > 
> > > +        hwframes_ctx->initial_pool_size = q->suggest_pool_size + 16 +
> > > avctx->extra_hw_frames;
> > >          frames_hwctx->frame_type        =
> > > MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
> > > 
> > >          ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
> > > @@ -793,6 +793,9 @@ static int qsv_process_data(AVCodecContext *avctx,
> > > QSVContext *q,
> > >      }
> > > 
> > >      if (q->reinit_flag || !q->session || !q->initialized) {
> > > +        mfxFrameAllocRequest request;
> > > +        memset(&request, 0, sizeof(request));
> > > +
> > >          q->reinit_flag = 0;
> > >          ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
> > >          if (ret < 0) {
> > > @@ -802,12 +805,19 @@ static int qsv_process_data(AVCodecContext
> > > *avctx, QSVContext *q,
> > >                  av_log(avctx, AV_LOG_ERROR, "Error decoding
> > > header\n");
> > >              goto reinit_fail;
> > >          }
> > > +        param.IOPattern = q->iopattern;
> > > 
> > >          q->orig_pix_fmt = avctx->pix_fmt = pix_fmt =
> > > ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
> > > 
> > >          avctx->coded_width  = param.mfx.FrameInfo.Width;
> > >          avctx->coded_height = param.mfx.FrameInfo.Height;
> > > 
> > > +        ret = MFXVideoDECODE_QueryIOSurf(q->session, &param,
> > > &request);
> > > +        if (ret < 0)
> > > +            return ff_qsv_print_error(avctx, ret, "Error querying IO
> > > surface");
> > > +
> > > +        q->suggest_pool_size = request.NumFrameSuggested;
> > > +
> > >          ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
> > >          if (ret < 0)
> > >              goto reinit_fail;
> > > --
> > 
> > Thanks for the patch! I have that on my list for quite a while.
> > Will look at it shortly.
> 
> Hi Softworz,
> 
> This patchset LGTM and works well, do you have any comment ? 

Ping, I'll apply this patchset in a few days if no more comment.

Regards
Haihao
Xiang, Haihao April 6, 2022, 12:01 p.m. UTC | #4
On Sat, 2022-04-02 at 08:52 +0000, Xiang, Haihao wrote:
> On Mon, 2022-03-28 at 02:26 +0000, Xiang, Haihao wrote:
> > On Fri, 2022-03-18 at 07:40 +0000, Soft Works wrote:
> > > > -----Original Message-----
> > > > From: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> On Behalf Of
> > > > Wenbin Chen
> > > > Sent: Friday, March 18, 2022 7:25 AM
> > > > To: ffmpeg-devel@ffmpeg.org
> > > > Subject: [FFmpeg-devel] [PATCH v2 3/3] libavcodec/qsvdec: using
> > > > suggested num to set init_pool_size
> > > > 
> > > > The init_pool_size is set to be 64 and it is too many.
> > > > Use IOSurfQuery to get NumFrameSuggest which is the suggested
> > > > number of frame that needed to be allocated when initializing the
> > > > decoder.
> > > > Considering that the hevc_qsv encoder uses the  most frame buffer,
> > > > async is 4 (default) and max_b_frames is 8 (default) and decoder
> > > > may followed by VPP, use NumFrameSuggest + 16 to set init_pool_size.
> > > > 
> > > > Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
> > > > Signed-off-by: Guangxin Xu <guangxin.xu@intel.com>
> > > > ---
> > > >  libavcodec/qsvdec.c | 14 ++++++++++++--
> > > >  1 file changed, 12 insertions(+), 2 deletions(-)
> > > > 
> > > > diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
> > > > index 210bd0c1d5..9875d3d632 100644
> > > > --- a/libavcodec/qsvdec.c
> > > > +++ b/libavcodec/qsvdec.c
> > > > @@ -88,7 +88,7 @@ typedef struct QSVContext {
> > > >      uint32_t fourcc;
> > > >      mfxFrameInfo frame_info;
> > > >      AVBufferPool *pool;
> > > > -
> > > > +    int suggest_pool_size;
> > > >      int initialized;
> > > > 
> > > >      // options set by the caller
> > > > @@ -275,7 +275,7 @@ static int qsv_decode_preinit(AVCodecContext
> > > > *avctx, QSVContext *q, enum AVPixel
> > > >          hwframes_ctx->height            = FFALIGN(avctx-
> > > > > coded_height, 32);
> > > > 
> > > >          hwframes_ctx->format            = AV_PIX_FMT_QSV;
> > > >          hwframes_ctx->sw_format         = avctx->sw_pix_fmt;
> > > > -        hwframes_ctx->initial_pool_size = 64 + avctx-
> > > > > extra_hw_frames;
> > > > 
> > > > +        hwframes_ctx->initial_pool_size = q->suggest_pool_size + 16 +
> > > > avctx->extra_hw_frames;
> > > >          frames_hwctx->frame_type        =
> > > > MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
> > > > 
> > > >          ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
> > > > @@ -793,6 +793,9 @@ static int qsv_process_data(AVCodecContext *avctx,
> > > > QSVContext *q,
> > > >      }
> > > > 
> > > >      if (q->reinit_flag || !q->session || !q->initialized) {
> > > > +        mfxFrameAllocRequest request;
> > > > +        memset(&request, 0, sizeof(request));
> > > > +
> > > >          q->reinit_flag = 0;
> > > >          ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
> > > >          if (ret < 0) {
> > > > @@ -802,12 +805,19 @@ static int qsv_process_data(AVCodecContext
> > > > *avctx, QSVContext *q,
> > > >                  av_log(avctx, AV_LOG_ERROR, "Error decoding
> > > > header\n");
> > > >              goto reinit_fail;
> > > >          }
> > > > +        param.IOPattern = q->iopattern;
> > > > 
> > > >          q->orig_pix_fmt = avctx->pix_fmt = pix_fmt =
> > > > ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
> > > > 
> > > >          avctx->coded_width  = param.mfx.FrameInfo.Width;
> > > >          avctx->coded_height = param.mfx.FrameInfo.Height;
> > > > 
> > > > +        ret = MFXVideoDECODE_QueryIOSurf(q->session, &param,
> > > > &request);
> > > > +        if (ret < 0)
> > > > +            return ff_qsv_print_error(avctx, ret, "Error querying IO
> > > > surface");
> > > > +
> > > > +        q->suggest_pool_size = request.NumFrameSuggested;
> > > > +
> > > >          ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
> > > >          if (ret < 0)
> > > >              goto reinit_fail;
> > > > --
> > > 
> > > Thanks for the patch! I have that on my list for quite a while.
> > > Will look at it shortly.
> > 
> > Hi Softworz,
> > 
> > This patchset LGTM and works well, do you have any comment ? 
> 
> Ping, I'll apply this patchset in a few days if no more comment.
> 

Applied, thx

-Haihao
diff mbox series

Patch

diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
index 210bd0c1d5..9875d3d632 100644
--- a/libavcodec/qsvdec.c
+++ b/libavcodec/qsvdec.c
@@ -88,7 +88,7 @@  typedef struct QSVContext {
     uint32_t fourcc;
     mfxFrameInfo frame_info;
     AVBufferPool *pool;
-
+    int suggest_pool_size;
     int initialized;
 
     // options set by the caller
@@ -275,7 +275,7 @@  static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixel
         hwframes_ctx->height            = FFALIGN(avctx->coded_height, 32);
         hwframes_ctx->format            = AV_PIX_FMT_QSV;
         hwframes_ctx->sw_format         = avctx->sw_pix_fmt;
-        hwframes_ctx->initial_pool_size = 64 + avctx->extra_hw_frames;
+        hwframes_ctx->initial_pool_size = q->suggest_pool_size + 16 + avctx->extra_hw_frames;
         frames_hwctx->frame_type        = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
 
         ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
@@ -793,6 +793,9 @@  static int qsv_process_data(AVCodecContext *avctx, QSVContext *q,
     }
 
     if (q->reinit_flag || !q->session || !q->initialized) {
+        mfxFrameAllocRequest request;
+        memset(&request, 0, sizeof(request));
+
         q->reinit_flag = 0;
         ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
         if (ret < 0) {
@@ -802,12 +805,19 @@  static int qsv_process_data(AVCodecContext *avctx, QSVContext *q,
                 av_log(avctx, AV_LOG_ERROR, "Error decoding header\n");
             goto reinit_fail;
         }
+        param.IOPattern = q->iopattern;
 
         q->orig_pix_fmt = avctx->pix_fmt = pix_fmt = ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
 
         avctx->coded_width  = param.mfx.FrameInfo.Width;
         avctx->coded_height = param.mfx.FrameInfo.Height;
 
+        ret = MFXVideoDECODE_QueryIOSurf(q->session, &param, &request);
+        if (ret < 0)
+            return ff_qsv_print_error(avctx, ret, "Error querying IO surface");
+
+        q->suggest_pool_size = request.NumFrameSuggested;
+
         ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
         if (ret < 0)
             goto reinit_fail;