diff mbox series

[FFmpeg-devel,2/2] lavc/qsvdec: export AVFilmGrainParams side data

Message ID 20210323030007.590540-2-haihao.xiang@intel.com
State New
Headers show
Series [FFmpeg-devel,1/2] lavc/qsv: allow to add more parameter buffers to QSV frame
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Xiang, Haihao March 23, 2021, 3 a.m. UTC
When AV_CODEC_EXPORT_DATA_FILM_GRAIN is present, AV1 decoder should
disable film grain application and export the corresponding side data
---
 libavcodec/qsv_internal.h |  3 ++
 libavcodec/qsvdec.c       | 88 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)

Comments

Xiang, Haihao April 12, 2021, 6:36 a.m. UTC | #1
Hi Mark / Zhong,

Could you please have a look at this patch when you get some time?

Thanks
Haihao


> When AV_CODEC_EXPORT_DATA_FILM_GRAIN is present, AV1 decoder should
> disable film grain application and export the corresponding side data
> ---
>  libavcodec/qsv_internal.h |  3 ++
>  libavcodec/qsvdec.c       | 88 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 91 insertions(+)
> 
> diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h
> index 1d94d429e8..754581087d 100644
> --- a/libavcodec/qsv_internal.h
> +++ b/libavcodec/qsv_internal.h
> @@ -76,6 +76,9 @@ typedef struct QSVFrame {
>      mfxFrameSurface1 surface;
>      mfxEncodeCtrl enc_ctrl;
>      mfxExtDecodedFrameInfo dec_info;
> +#if QSV_VERSION_ATLEAST(1, 34)
> +    mfxExtAV1FilmGrainParam av1_film_grain_param;
> +#endif
>      mfxExtBuffer *ext_param[QSV_MAX_FRAME_EXT_PARAMS];
>      int num_ext_params;
>  
> diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
> index 55cf9f35c5..e34441fc0b 100644
> --- a/libavcodec/qsvdec.c
> +++ b/libavcodec/qsvdec.c
> @@ -38,6 +38,7 @@
>  #include "libavutil/pixfmt.h"
>  #include "libavutil/time.h"
>  #include "libavutil/imgutils.h"
> +#include "libavutil/film_grain_params.h"
>  
>  #include "avcodec.h"
>  #include "internal.h"
> @@ -334,6 +335,11 @@ static int qsv_decode_header(AVCodecContext *avctx,
> QSVContext *q,
>          return ff_qsv_print_error(avctx, ret,
>                  "Error decoding stream header");
>  
> +#if QSV_VERSION_ATLEAST(1, 34)
> +    if (avctx->codec_id == AV_CODEC_ID_AV1)
> +        param->mfx.FilmGrain = (avctx->export_side_data &
> AV_CODEC_EXPORT_DATA_FILM_GRAIN) ? 0 : param->mfx.FilmGrain;
> +#endif
> +
>      return 0;
>  }
>  
> @@ -373,6 +379,12 @@ static int alloc_frame(AVCodecContext *avctx, QSVContext
> *q, QSVFrame *frame)
>      frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
>      frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
>      ff_qsv_frame_add_ext_param(avctx, frame, (mfxExtBuffer *)&frame-
> >dec_info);
> +#if QSV_VERSION_ATLEAST(1, 34)
> +    frame->av1_film_grain_param.Header.BufferId =
> MFX_EXTBUFF_AV1_FILM_GRAIN_PARAM;
> +    frame->av1_film_grain_param.Header.BufferSz = sizeof(frame-
> >av1_film_grain_param);
> +    frame->av1_film_grain_param.FilmGrainFlags = 0;
> +    ff_qsv_frame_add_ext_param(avctx, frame, (mfxExtBuffer *)&frame-
> >av1_film_grain_param);
> +#endif
>  
>      frame->used = 1;
>  
> @@ -443,6 +455,73 @@ static QSVFrame *find_frame(QSVContext *q,
> mfxFrameSurface1 *surf)
>      return NULL;
>  }
>  
> +#if QSV_VERSION_ATLEAST(1, 34)
> +static int qsv_export_film_grain(AVCodecContext *avctx,
> mfxExtAV1FilmGrainParam *ext_param, AVFrame *frame)
> +{
> +    AVFilmGrainParams *fgp;
> +    AVFilmGrainAOMParams *aom;
> +    int i;
> +
> +    if (!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_APPLY))
> +        return 0;
> +
> +    fgp = av_film_grain_params_create_side_data(frame);
> +
> +    if (!fgp)
> +        return AVERROR(ENOMEM);
> +
> +    fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
> +    fgp->seed = ext_param->GrainSeed;
> +    aom = &fgp->codec.aom;
> +
> +    aom->chroma_scaling_from_luma = !!(ext_param->FilmGrainFlags &
> MFX_FILM_GRAIN_CHROMA_SCALING_FROM_LUMA);
> +    aom->scaling_shift = ext_param->GrainScalingMinus8 + 8;
> +    aom->ar_coeff_lag = ext_param->ArCoeffLag;
> +    aom->ar_coeff_shift = ext_param->ArCoeffShiftMinus6 + 6;
> +    aom->grain_scale_shift = ext_param->GrainScaleShift;
> +    aom->overlap_flag = !!(ext_param->FilmGrainFlags &
> MFX_FILM_GRAIN_OVERLAP);
> +    aom->limit_output_range = !!(ext_param->FilmGrainFlags &
> MFX_FILM_GRAIN_CLIP_TO_RESTRICTED_RANGE);
> +
> +    aom->num_y_points = ext_param->NumYPoints;
> +
> +    for (i = 0; i < aom->num_y_points; i++) {
> +        aom->y_points[i][0] = ext_param->PointY[i].Value;
> +        aom->y_points[i][1] = ext_param->PointY[i].Scaling;
> +    }
> +
> +    aom->num_uv_points[0] = ext_param->NumCbPoints;
> +
> +    for (i = 0; i < aom->num_uv_points[0]; i++) {
> +        aom->uv_points[0][i][0] = ext_param->PointCb[i].Value;
> +        aom->uv_points[0][i][1] = ext_param->PointCb[i].Scaling;
> +    }
> +
> +    aom->num_uv_points[1] = ext_param->NumCrPoints;
> +
> +    for (i = 0; i < aom->num_uv_points[1]; i++) {
> +        aom->uv_points[1][i][0] = ext_param->PointCr[i].Value;
> +        aom->uv_points[1][i][1] = ext_param->PointCr[i].Scaling;
> +    }
> +
> +    for (i = 0; i < 24; i++)
> +        aom->ar_coeffs_y[i] = ext_param->ArCoeffsYPlus128[i] - 128;
> +
> +    for (i = 0; i < 25; i++) {
> +        aom->ar_coeffs_uv[0][i] = ext_param->ArCoeffsCbPlus128[i] - 128;
> +        aom->ar_coeffs_uv[1][i] = ext_param->ArCoeffsCrPlus128[i] - 128;
> +    }
> +
> +    aom->uv_mult[0] = ext_param->CbMult;
> +    aom->uv_mult[1] = ext_param->CrMult;
> +    aom->uv_mult_luma[0] = ext_param->CbLumaMult;
> +    aom->uv_mult_luma[1] = ext_param->CrLumaMult;
> +    aom->uv_offset[0] = ext_param->CbOffset;
> +    aom->uv_offset[1] = ext_param->CrOffset;
> +
> +    return 0;
> +}
> +#endif
> +
>  static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
>                        AVFrame *frame, int *got_frame,
>                        const AVPacket *avpkt)
> @@ -546,6 +625,15 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext
> *q,
>  
>          outsurf = &out_frame->surface;
>  
> +#if QSV_VERSION_ATLEAST(1, 34)
> +        if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
> +            ret = qsv_export_film_grain(avctx, &out_frame-
> >av1_film_grain_param, frame);
> +
> +            if (ret < 0)
> +                return ret;
> +        }
> +#endif
> +
>  #if FF_API_PKT_PTS
>  FF_DISABLE_DEPRECATION_WARNINGS
>          frame->pkt_pts = outsurf->Data.TimeStamp;
diff mbox series

Patch

diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h
index 1d94d429e8..754581087d 100644
--- a/libavcodec/qsv_internal.h
+++ b/libavcodec/qsv_internal.h
@@ -76,6 +76,9 @@  typedef struct QSVFrame {
     mfxFrameSurface1 surface;
     mfxEncodeCtrl enc_ctrl;
     mfxExtDecodedFrameInfo dec_info;
+#if QSV_VERSION_ATLEAST(1, 34)
+    mfxExtAV1FilmGrainParam av1_film_grain_param;
+#endif
     mfxExtBuffer *ext_param[QSV_MAX_FRAME_EXT_PARAMS];
     int num_ext_params;
 
diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
index 55cf9f35c5..e34441fc0b 100644
--- a/libavcodec/qsvdec.c
+++ b/libavcodec/qsvdec.c
@@ -38,6 +38,7 @@ 
 #include "libavutil/pixfmt.h"
 #include "libavutil/time.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/film_grain_params.h"
 
 #include "avcodec.h"
 #include "internal.h"
@@ -334,6 +335,11 @@  static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q,
         return ff_qsv_print_error(avctx, ret,
                 "Error decoding stream header");
 
+#if QSV_VERSION_ATLEAST(1, 34)
+    if (avctx->codec_id == AV_CODEC_ID_AV1)
+        param->mfx.FilmGrain = (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) ? 0 : param->mfx.FilmGrain;
+#endif
+
     return 0;
 }
 
@@ -373,6 +379,12 @@  static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
     frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
     frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
     ff_qsv_frame_add_ext_param(avctx, frame, (mfxExtBuffer *)&frame->dec_info);
+#if QSV_VERSION_ATLEAST(1, 34)
+    frame->av1_film_grain_param.Header.BufferId = MFX_EXTBUFF_AV1_FILM_GRAIN_PARAM;
+    frame->av1_film_grain_param.Header.BufferSz = sizeof(frame->av1_film_grain_param);
+    frame->av1_film_grain_param.FilmGrainFlags = 0;
+    ff_qsv_frame_add_ext_param(avctx, frame, (mfxExtBuffer *)&frame->av1_film_grain_param);
+#endif
 
     frame->used = 1;
 
@@ -443,6 +455,73 @@  static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
     return NULL;
 }
 
+#if QSV_VERSION_ATLEAST(1, 34)
+static int qsv_export_film_grain(AVCodecContext *avctx, mfxExtAV1FilmGrainParam *ext_param, AVFrame *frame)
+{
+    AVFilmGrainParams *fgp;
+    AVFilmGrainAOMParams *aom;
+    int i;
+
+    if (!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_APPLY))
+        return 0;
+
+    fgp = av_film_grain_params_create_side_data(frame);
+
+    if (!fgp)
+        return AVERROR(ENOMEM);
+
+    fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
+    fgp->seed = ext_param->GrainSeed;
+    aom = &fgp->codec.aom;
+
+    aom->chroma_scaling_from_luma = !!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_CHROMA_SCALING_FROM_LUMA);
+    aom->scaling_shift = ext_param->GrainScalingMinus8 + 8;
+    aom->ar_coeff_lag = ext_param->ArCoeffLag;
+    aom->ar_coeff_shift = ext_param->ArCoeffShiftMinus6 + 6;
+    aom->grain_scale_shift = ext_param->GrainScaleShift;
+    aom->overlap_flag = !!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_OVERLAP);
+    aom->limit_output_range = !!(ext_param->FilmGrainFlags & MFX_FILM_GRAIN_CLIP_TO_RESTRICTED_RANGE);
+
+    aom->num_y_points = ext_param->NumYPoints;
+
+    for (i = 0; i < aom->num_y_points; i++) {
+        aom->y_points[i][0] = ext_param->PointY[i].Value;
+        aom->y_points[i][1] = ext_param->PointY[i].Scaling;
+    }
+
+    aom->num_uv_points[0] = ext_param->NumCbPoints;
+
+    for (i = 0; i < aom->num_uv_points[0]; i++) {
+        aom->uv_points[0][i][0] = ext_param->PointCb[i].Value;
+        aom->uv_points[0][i][1] = ext_param->PointCb[i].Scaling;
+    }
+
+    aom->num_uv_points[1] = ext_param->NumCrPoints;
+
+    for (i = 0; i < aom->num_uv_points[1]; i++) {
+        aom->uv_points[1][i][0] = ext_param->PointCr[i].Value;
+        aom->uv_points[1][i][1] = ext_param->PointCr[i].Scaling;
+    }
+
+    for (i = 0; i < 24; i++)
+        aom->ar_coeffs_y[i] = ext_param->ArCoeffsYPlus128[i] - 128;
+
+    for (i = 0; i < 25; i++) {
+        aom->ar_coeffs_uv[0][i] = ext_param->ArCoeffsCbPlus128[i] - 128;
+        aom->ar_coeffs_uv[1][i] = ext_param->ArCoeffsCrPlus128[i] - 128;
+    }
+
+    aom->uv_mult[0] = ext_param->CbMult;
+    aom->uv_mult[1] = ext_param->CrMult;
+    aom->uv_mult_luma[0] = ext_param->CbLumaMult;
+    aom->uv_mult_luma[1] = ext_param->CrLumaMult;
+    aom->uv_offset[0] = ext_param->CbOffset;
+    aom->uv_offset[1] = ext_param->CrOffset;
+
+    return 0;
+}
+#endif
+
 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
                       AVFrame *frame, int *got_frame,
                       const AVPacket *avpkt)
@@ -546,6 +625,15 @@  static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
 
         outsurf = &out_frame->surface;
 
+#if QSV_VERSION_ATLEAST(1, 34)
+        if (avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) {
+            ret = qsv_export_film_grain(avctx, &out_frame->av1_film_grain_param, frame);
+
+            if (ret < 0)
+                return ret;
+        }
+#endif
+
 #if FF_API_PKT_PTS
 FF_DISABLE_DEPRECATION_WARNINGS
         frame->pkt_pts = outsurf->Data.TimeStamp;