diff mbox series

[FFmpeg-devel,v4] avfilter: add pu21 filter

Message ID 20240823191505.269386-1-rajivharlalka009@gmail.com
State New
Headers show
Series [FFmpeg-devel,v4] avfilter: add pu21 filter | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Rajiv Harlalka Aug. 23, 2024, 7:15 p.m. UTC
Adds the PU21 encoding filter for high dynamic range images and video
quality assessment
Mantiuk, R., & Azimi, M. PU21: A novel perceptually uniform encoding
for adapting existing quality metrics for HDR
https://github.com/gfxdisp/pu21

Signed-off-by: Rajiv Harlalka <rajivharlalka009@gmail.com>
---
 Changelog                |   1 +
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/version.h    |   4 +-
 libavfilter/vf_pu21.c    | 175 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 180 insertions(+), 2 deletions(-)
 create mode 100644 libavfilter/vf_pu21.c

Comments

Rajiv Harlalka Aug. 26, 2024, 7:57 a.m. UTC | #1
Bumping up for visibility for review.

On Sat, Aug 24, 2024 at 12:46 AM Rajiv Harlalka <rajivharlalka009@gmail.com>
wrote:

> Adds the PU21 encoding filter for high dynamic range images and video
> quality assessment
> Mantiuk, R., & Azimi, M. PU21: A novel perceptually uniform encoding
> for adapting existing quality metrics for HDR
> https://github.com/gfxdisp/pu21
>
> Signed-off-by: Rajiv Harlalka <rajivharlalka009@gmail.com>
> ---
>  Changelog                |   1 +
>  libavfilter/Makefile     |   1 +
>  libavfilter/allfilters.c |   1 +
>  libavfilter/version.h    |   4 +-
>  libavfilter/vf_pu21.c    | 175 +++++++++++++++++++++++++++++++++++++++
>  5 files changed, 180 insertions(+), 2 deletions(-)
>  create mode 100644 libavfilter/vf_pu21.c
>
> diff --git a/Changelog b/Changelog
> index 571b27ad98..552aa6028a 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -18,6 +18,7 @@ version <next>:
>  - D3D12VA HEVC encoder
>  - Cropping metadata parsing and writing in Matroska and MP4/MOV de/muxers
>  - Intel QSV-accelerated VVC decoding
> +- Addition of vf_pu21 encoding filter
>
>
>  version 7.0:
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 63088e9286..102315f5bf 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -121,6 +121,7 @@ OBJS-$(CONFIG_AXCORRELATE_FILTER)            +=
> af_axcorrelate.o
>  OBJS-$(CONFIG_AZMQ_FILTER)                   += f_zmq.o
>  OBJS-$(CONFIG_BANDPASS_FILTER)               += af_biquads.o
>  OBJS-$(CONFIG_BANDREJECT_FILTER)             += af_biquads.o
> +OBJS-$(CONFIG_PU21_FILTER)                   += vf_pu21.o
>  OBJS-$(CONFIG_BASS_FILTER)                   += af_biquads.o
>  OBJS-$(CONFIG_BIQUAD_FILTER)                 += af_biquads.o
>  OBJS-$(CONFIG_BS2B_FILTER)                   += af_bs2b.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 63600e9b58..ced076626c 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -231,6 +231,7 @@ extern const AVFilter ff_vf_convolution_opencl;
>  extern const AVFilter ff_vf_convolve;
>  extern const AVFilter ff_vf_copy;
>  extern const AVFilter ff_vf_coreimage;
> +extern const AVFilter ff_vf_pu21;
>  extern const AVFilter ff_vf_corr;
>  extern const AVFilter ff_vf_cover_rect;
>  extern const AVFilter ff_vf_crop;
> diff --git a/libavfilter/version.h b/libavfilter/version.h
> index d8cd8a2cfb..7e0eb9af97 100644
> --- a/libavfilter/version.h
> +++ b/libavfilter/version.h
> @@ -31,8 +31,8 @@
>
>  #include "version_major.h"
>
> -#define LIBAVFILTER_VERSION_MINOR   2
> -#define LIBAVFILTER_VERSION_MICRO 102
> +#define LIBAVFILTER_VERSION_MINOR   3
> +#define LIBAVFILTER_VERSION_MICRO 100
>
>
>  #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR,
> \
> diff --git a/libavfilter/vf_pu21.c b/libavfilter/vf_pu21.c
> new file mode 100644
> index 0000000000..64804ce60b
> --- /dev/null
> +++ b/libavfilter/vf_pu21.c
> @@ -0,0 +1,175 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
> 02110-1301 USA
> + */
> +
> +#include "libavfilter/avfilter.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/pixdesc.h"
> +#include "libavutil/mem.h"
> +#include "libavutil/imgutils.h"
> +#include "libavfilter/formats.h"
> +#include "libavutil/internal.h"
> +#include "video.h"
> +#include "libavutil/libm.h"
> +
> +enum {
> +    BANDING,
> +    BANDING_GLARE,
> +    PEAKS,
> +    PEAKS_GLARE
> +};
> +
> +typedef struct PU21Context {
> +    const AVClass* class;
> +    double L_min, L_max;
> +    int multiplier;
> +    int mode;
> +
> +    int depth;
> +    int nb_planes;
> +} PU21Context;
> +
> +const float pu21_params[4][7] = {
> +    // Reference: "PU21: A novel perceptually uniform encoding for
> adapting existing quality metrics for HDR"
> +    // Rafał K. Mantiuk and M. Azimi, Picture Coding Symposium 2021
> +    // https://github.com/gfxdisp/pu21
> +    {1.070275272f, 0.4088273932f, 0.153224308f, 0.2520326168f,
> 1.063512885f, 1.14115047f, 521.4527484f},  // BANDING
> +    {0.353487901f, 0.3734658629f, 8.277049286e-05f, 0.9062562627f,
> 0.09150303166f, 0.9099517204f, 596.3148142f},  // BANDING_GLARE
> +    {1.043882782f, 0.6459495343f, 0.3194584211f, 0.374025247f,
> 1.114783422f, 1.095360363f, 384.9217577f},  // PEAKS
> +    {816.885024f, 1479.463946f, 0.001253215609f, 0.9329636822f,
> 0.06746643971f, 1.573435413f, 419.6006374f}  // PEAKS_GLARE
> +};
> +
> +
> +#define OFFSET(x) offsetof(PU21Context, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +static const AVOption pu21_options[] = {
> +    { "multiplier", "multiplier for increasing the pixel value based on
> the screen display model", OFFSET(multiplier), AV_OPT_TYPE_INT, {.i64 = 1},
> 1, 10000, FLAGS},
> +    { "mode", "Set the mode of the PU21 encoding", OFFSET(mode),
> AV_OPT_TYPE_INT, {.i64 = BANDING_GLARE}, BANDING, PEAKS_GLARE, FLAGS},
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(pu21);
> +
> +static av_cold int pu21_init(AVFilterContext* ctx) {
> +    PU21Context* pu21 = ctx->priv;
> +    pu21->mode = BANDING_GLARE;
> +    pu21->multiplier = 1;
> +    return 0;
> +}
> +
> +static float apply_pu21(float pixel_val, PU21Context* pu21) {
> +    float Y, V;
> +    const float* pu21_param = pu21_params[pu21->mode];
> +    Y = pixel_val * (pu21->multiplier);
> +    V = fmax(pu21_param[6] * (powf((pu21_param[0] + pu21_param[1] *
> powf(Y, pu21_param[3])) / (1 + pu21_param[2] * powf(Y, pu21_param[3])),
> pu21_param[4]) - pu21_param[5]), 0);
> +    return V;
> +}
> +
> +#define DEFINE_PU21_FILTER(name, type, div)
>            \
> +static void pu21_encode_##name(AVFilterContext* ctx, AVFrame* in,
> AVFrame* out)     \
> +{
>            \
> +    PU21Context* pu21 = ctx->priv;
>           \
> +    const int height = in->height;
>           \
> +    const int width = in->width;
>           \
> +
>           \
> +    int plane;
>           \
> +    for (plane = 0; plane < pu21->nb_planes; plane++) {
>            \
> +        const type* src = (const type *)in->data[plane];
>           \
> +        type* dst = (type *)out->data[plane];
>            \
> +        int  linesize = in->linesize[plane] / div;
>           \
> +        type pixel_val;
>            \
> +        for (int y = 0; y < height; y++) {
>           \
> +            for (int x = 0; x < width; x++) {
>            \
> +                pixel_val = src[y * linesize + x];
>           \
> +                dst[y * linesize + x] = (type)apply_pu21(pixel_val,
> pu21);          \
> +            }
>            \
> +        }
>            \
> +    }
>            \
> +}
> +
> +DEFINE_PU21_FILTER(8, uint8_t, 1)
> +DEFINE_PU21_FILTER(16, uint16_t, 2)
> +DEFINE_PU21_FILTER(32, float, 4)
> +
> +static int filter_frame(AVFilterLink* inlink, AVFrame* input) {
> +    AVFilterContext* ctx = inlink->dst;
> +    PU21Context* pu21 = ctx->priv;
> +
> +    AVFilterLink* outlink = ctx->outputs[0];
> +    AVFrame* out;
> +
> +    if (av_frame_is_writable(input)) {
> +        out = input;
> +    } else {
> +        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
> +        if (!out) {
> +            av_frame_free(&input);
> +            return AVERROR(ENOMEM);
> +        }
> +        av_frame_copy_props(out, input);
> +    }
> +
> +    if (pu21->depth <= 8) {
> +        pu21_encode_8(ctx, input, out);
> +    } else if (pu21->depth <= 16) {
> +        pu21_encode_16(ctx, input, out);
> +    } else {
> +        pu21_encode_32(ctx, input, out);
> +    }
> +
> +    if (out != input)
> +        av_frame_free(&input);
> +    return ff_filter_frame(outlink, out);
> +}
> +
> +
> +static int config_input(AVFilterLink* inlink) {
> +    const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(inlink->format);
> +    PU21Context* s = inlink->dst->priv;
> +
> +    s->depth = desc->comp[0].depth;
> +    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
> +
> +    return 0;
> +}
> +
> +static const AVFilterPad pu21_inputs[] = {
> +    {
> +        .name = "default",
> +        .type = AVMEDIA_TYPE_VIDEO,
> +        .config_props = config_input,
> +        .filter_frame = filter_frame,
> +    }
> +};
> +
> +static const AVFilterPad pu21_outputs[] = {
> +    {
> +        .name = "default",
> +        .type = AVMEDIA_TYPE_VIDEO,
> +    }
> +};
> +
> +const AVFilter ff_vf_pu21 = {
> +    .name = "pu21",
> +    .description = NULL_IF_CONFIG_SMALL("Convert HDR linear values to PU
> values using PU21 transform"),
> +    .priv_size = sizeof(PU21Context),
> +    .init = pu21_init,
> +    FILTER_INPUTS(pu21_inputs),
> +    FILTER_OUTPUTS(pu21_outputs),
> +    .priv_class = &pu21_class,
> +    .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
> +};
> --
> 2.25.1
>
>
Frank Plowman Aug. 31, 2024, 5:20 p.m. UTC | #2
Hi,

Thanks for your patch.

On 23/08/2024 20:15, Rajiv Harlalka wrote:
> Adds the PU21 encoding filter for high dynamic range images and video
> quality assessment
> Mantiuk, R., & Azimi, M. PU21: A novel perceptually uniform encoding
> for adapting existing quality metrics for HDR
> https://github.com/gfxdisp/pu21
> 
> Signed-off-by: Rajiv Harlalka <rajivharlalka009@gmail.com>
> ---
>  Changelog                |   1 +
>  libavfilter/Makefile     |   1 +
>  libavfilter/allfilters.c |   1 +
>  libavfilter/version.h    |   4 +-
>  libavfilter/vf_pu21.c    | 175 +++++++++++++++++++++++++++++++++++++++
>  5 files changed, 180 insertions(+), 2 deletions(-)
>  create mode 100644 libavfilter/vf_pu21.c
> 
> diff --git a/Changelog b/Changelog
> index 571b27ad98..552aa6028a 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -18,6 +18,7 @@ version <next>:
>  - D3D12VA HEVC encoder
>  - Cropping metadata parsing and writing in Matroska and MP4/MOV de/muxers
>  - Intel QSV-accelerated VVC decoding
> +- Addition of vf_pu21 encoding filter
>  
>  
>  version 7.0:
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 63088e9286..102315f5bf 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -121,6 +121,7 @@ OBJS-$(CONFIG_AXCORRELATE_FILTER)            += af_axcorrelate.o
>  OBJS-$(CONFIG_AZMQ_FILTER)                   += f_zmq.o
>  OBJS-$(CONFIG_BANDPASS_FILTER)               += af_biquads.o
>  OBJS-$(CONFIG_BANDREJECT_FILTER)             += af_biquads.o
> +OBJS-$(CONFIG_PU21_FILTER)                   += vf_pu21.o

This line should be inserted such that the list is in alphabetical order.

>  OBJS-$(CONFIG_BASS_FILTER)                   += af_biquads.o
>  OBJS-$(CONFIG_BIQUAD_FILTER)                 += af_biquads.o
>  OBJS-$(CONFIG_BS2B_FILTER)                   += af_bs2b.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 63600e9b58..ced076626c 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -231,6 +231,7 @@ extern const AVFilter ff_vf_convolution_opencl;
>  extern const AVFilter ff_vf_convolve;
>  extern const AVFilter ff_vf_copy;
>  extern const AVFilter ff_vf_coreimage;
> +extern const AVFilter ff_vf_pu21;

This line should be inserted such that the list is in alphabetical order.

>  extern const AVFilter ff_vf_corr;
>  extern const AVFilter ff_vf_cover_rect;
>  extern const AVFilter ff_vf_crop;
> diff --git a/libavfilter/version.h b/libavfilter/version.h
> index d8cd8a2cfb..7e0eb9af97 100644
> --- a/libavfilter/version.h
> +++ b/libavfilter/version.h
> @@ -31,8 +31,8 @@
>  
>  #include "version_major.h"
>  
> -#define LIBAVFILTER_VERSION_MINOR   2
> -#define LIBAVFILTER_VERSION_MICRO 102
> +#define LIBAVFILTER_VERSION_MINOR   3
> +#define LIBAVFILTER_VERSION_MICRO 100
>  
>  
>  #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
> diff --git a/libavfilter/vf_pu21.c b/libavfilter/vf_pu21.c
> new file mode 100644
> index 0000000000..64804ce60b
> --- /dev/null
> +++ b/libavfilter/vf_pu21.c
> @@ -0,0 +1,175 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "libavfilter/avfilter.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/pixdesc.h"
> +#include "libavutil/mem.h"
> +#include "libavutil/imgutils.h"
> +#include "libavfilter/formats.h"
> +#include "libavutil/internal.h"
> +#include "video.h"
> +#include "libavutil/libm.h"

I think some of these includes are unused.

> +
> +enum {
> +    BANDING,
> +    BANDING_GLARE,
> +    PEAKS,
> +    PEAKS_GLARE
> +};
> +
> +typedef struct PU21Context {
> +    const AVClass* class;
> +    double L_min, L_max;

L_min and L_max are unused

> +    int multiplier;
> +    int mode;
> +
> +    int depth;
> +    int nb_planes;
> +} PU21Context;
> +
> +const float pu21_params[4][7] = {
> +    // Reference: "PU21: A novel perceptually uniform encoding for adapting existing quality metrics for HDR"
> +    // Rafał K. Mantiuk and M. Azimi, Picture Coding Symposium 2021
> +    // https://github.com/gfxdisp/pu21
> +    {1.070275272f, 0.4088273932f, 0.153224308f, 0.2520326168f, 1.063512885f, 1.14115047f, 521.4527484f},  // BANDING
> +    {0.353487901f, 0.3734658629f, 8.277049286e-05f, 0.9062562627f, 0.09150303166f, 0.9099517204f, 596.3148142f},  // BANDING_GLARE
> +    {1.043882782f, 0.6459495343f, 0.3194584211f, 0.374025247f, 1.114783422f, 1.095360363f, 384.9217577f},  // PEAKS
> +    {816.885024f, 1479.463946f, 0.001253215609f, 0.9329636822f, 0.06746643971f, 1.573435413f, 419.6006374f}  // PEAKS_GLARE
> +};

This variable should be marked static.

> +
> +
> +#define OFFSET(x) offsetof(PU21Context, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +static const AVOption pu21_options[] = {
> +    { "multiplier", "multiplier for increasing the pixel value based on the screen display model", OFFSET(multiplier), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 10000, FLAGS},
> +    { "mode", "Set the mode of the PU21 encoding", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = BANDING_GLARE}, BANDING, PEAKS_GLARE, FLAGS},
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(pu21);
> +
> +static av_cold int pu21_init(AVFilterContext* ctx) {
> +    PU21Context* pu21 = ctx->priv;
> +    pu21->mode = BANDING_GLARE;
> +    pu21->multiplier = 1;
> +    return 0;
> +}
> +
> +static float apply_pu21(float pixel_val, PU21Context* pu21) {
> +    float Y, V;
> +    const float* pu21_param = pu21_params[pu21->mode];
> +    Y = pixel_val * (pu21->multiplier);
> +    V = fmax(pu21_param[6] * (powf((pu21_param[0] + pu21_param[1] * powf(Y, pu21_param[3])) / (1 + pu21_param[2] * powf(Y, pu21_param[3])), pu21_param[4]) - pu21_param[5]), 0);
fmax acts on doubles so the result is being needlessly up-casted to a
double then immediately back down to a float.  This could be avoided
with fmaxf.

Nit: This line is still very long, could you further break it up and/or
wrap it?  Ideally giving names to some of the intermediate results to
aid readability.

> +    return V;
> +}
> +
> +#define DEFINE_PU21_FILTER(name, type, div)                                         \
> +static void pu21_encode_##name(AVFilterContext* ctx, AVFrame* in, AVFrame* out)     \
> +{                                                                                   \
> +    PU21Context* pu21 = ctx->priv;                                                  \
> +    const int height = in->height;                                                  \
> +    const int width = in->width;                                                    \
> +                                                                                    \
> +    int plane;                                                                      \
> +    for (plane = 0; plane < pu21->nb_planes; plane++) {                             \
> +        const type* src = (const type *)in->data[plane];                            \
> +        type* dst = (type *)out->data[plane];                                       \
> +        int  linesize = in->linesize[plane] / div;                                  \

Nit: extra space between int and linesize

> +        type pixel_val;                                                             \
> +        for (int y = 0; y < height; y++) {                                          \
> +            for (int x = 0; x < width; x++) {                                       \
> +                pixel_val = src[y * linesize + x];                                  \
> +                dst[y * linesize + x] = (type)apply_pu21(pixel_val, pu21);          \

This naive cast to/from floats might result in some floating-point
precision issues, particularly for u16 inputs.  You could probably get a
better result by scaling to/from a more suitable range for the floats
(e.g. [0,1]) before/after calling apply_pu21.  This is just a hunch
though, I imagine you will be better able than me to assess the impact
of such a change.

> +            }                                                                       \
> +        }                                                                           \
> +    }                                                                               \
> +}
> +
> +DEFINE_PU21_FILTER(8, uint8_t, 1)
> +DEFINE_PU21_FILTER(16, uint16_t, 2)
> +DEFINE_PU21_FILTER(32, float, 4)

The final parameter could be removed and replaced with sizeof(type) in
the macro body.  This would be easier to follow and eliminate a
potential cause of a bug in future.

> +
> +static int filter_frame(AVFilterLink* inlink, AVFrame* input) {

Nit: As mentioned in my review of v2, FFmpeg uses K&R style where
opening and closing braces of function definitions are on their own
line, just like above for the pu21_encode_* functions.

> +    AVFilterContext* ctx = inlink->dst;
> +    PU21Context* pu21 = ctx->priv;
> +
> +    AVFilterLink* outlink = ctx->outputs[0];
> +    AVFrame* out;
> +
> +    if (av_frame_is_writable(input)) {
> +        out = input;
> +    } else {
> +        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
> +        if (!out) {
> +            av_frame_free(&input);
> +            return AVERROR(ENOMEM);
> +        }
> +        av_frame_copy_props(out, input);
> +    }
> +
> +    if (pu21->depth <= 8) {
> +        pu21_encode_8(ctx, input, out);
> +    } else if (pu21->depth <= 16) {
> +        pu21_encode_16(ctx, input, out);
> +    } else {
> +        pu21_encode_32(ctx, input, out);
> +    }
> +
> +    if (out != input)
> +        av_frame_free(&input);
> +    return ff_filter_frame(outlink, out);
> +}
> +
> +
> +static int config_input(AVFilterLink* inlink) {
> +    const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(inlink->format);
> +    PU21Context* s = inlink->dst->priv;
> +
> +    s->depth = desc->comp[0].depth;
> +    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
> +
> +    return 0;
> +}
> +
> +static const AVFilterPad pu21_inputs[] = {
> +    {
> +        .name = "default",
> +        .type = AVMEDIA_TYPE_VIDEO,
> +        .config_props = config_input,
> +        .filter_frame = filter_frame,
> +    }
> +};
> +
> +static const AVFilterPad pu21_outputs[] = {
> +    {
> +        .name = "default",
> +        .type = AVMEDIA_TYPE_VIDEO,
> +    }
> +};
> +
> +const AVFilter ff_vf_pu21 = {
> +    .name = "pu21",
> +    .description = NULL_IF_CONFIG_SMALL("Convert HDR linear values to PU values using PU21 transform"),
> +    .priv_size = sizeof(PU21Context),
> +    .init = pu21_init,
> +    FILTER_INPUTS(pu21_inputs),
> +    FILTER_OUTPUTS(pu21_outputs),
> +    .priv_class = &pu21_class,
> +    .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
> +};

--
Cheers,
Frank
diff mbox series

Patch

diff --git a/Changelog b/Changelog
index 571b27ad98..552aa6028a 100644
--- a/Changelog
+++ b/Changelog
@@ -18,6 +18,7 @@  version <next>:
 - D3D12VA HEVC encoder
 - Cropping metadata parsing and writing in Matroska and MP4/MOV de/muxers
 - Intel QSV-accelerated VVC decoding
+- Addition of vf_pu21 encoding filter
 
 
 version 7.0:
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 63088e9286..102315f5bf 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -121,6 +121,7 @@  OBJS-$(CONFIG_AXCORRELATE_FILTER)            += af_axcorrelate.o
 OBJS-$(CONFIG_AZMQ_FILTER)                   += f_zmq.o
 OBJS-$(CONFIG_BANDPASS_FILTER)               += af_biquads.o
 OBJS-$(CONFIG_BANDREJECT_FILTER)             += af_biquads.o
+OBJS-$(CONFIG_PU21_FILTER)                   += vf_pu21.o
 OBJS-$(CONFIG_BASS_FILTER)                   += af_biquads.o
 OBJS-$(CONFIG_BIQUAD_FILTER)                 += af_biquads.o
 OBJS-$(CONFIG_BS2B_FILTER)                   += af_bs2b.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 63600e9b58..ced076626c 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -231,6 +231,7 @@  extern const AVFilter ff_vf_convolution_opencl;
 extern const AVFilter ff_vf_convolve;
 extern const AVFilter ff_vf_copy;
 extern const AVFilter ff_vf_coreimage;
+extern const AVFilter ff_vf_pu21;
 extern const AVFilter ff_vf_corr;
 extern const AVFilter ff_vf_cover_rect;
 extern const AVFilter ff_vf_crop;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index d8cd8a2cfb..7e0eb9af97 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,8 +31,8 @@ 
 
 #include "version_major.h"
 
-#define LIBAVFILTER_VERSION_MINOR   2
-#define LIBAVFILTER_VERSION_MICRO 102
+#define LIBAVFILTER_VERSION_MINOR   3
+#define LIBAVFILTER_VERSION_MICRO 100
 
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
diff --git a/libavfilter/vf_pu21.c b/libavfilter/vf_pu21.c
new file mode 100644
index 0000000000..64804ce60b
--- /dev/null
+++ b/libavfilter/vf_pu21.c
@@ -0,0 +1,175 @@ 
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavfilter/avfilter.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/mem.h"
+#include "libavutil/imgutils.h"
+#include "libavfilter/formats.h"
+#include "libavutil/internal.h"
+#include "video.h"
+#include "libavutil/libm.h"
+
+enum {
+    BANDING,
+    BANDING_GLARE,
+    PEAKS,
+    PEAKS_GLARE
+};
+
+typedef struct PU21Context {
+    const AVClass* class;
+    double L_min, L_max;
+    int multiplier;
+    int mode;
+
+    int depth;
+    int nb_planes;
+} PU21Context;
+
+const float pu21_params[4][7] = {
+    // Reference: "PU21: A novel perceptually uniform encoding for adapting existing quality metrics for HDR"
+    // Rafał K. Mantiuk and M. Azimi, Picture Coding Symposium 2021
+    // https://github.com/gfxdisp/pu21
+    {1.070275272f, 0.4088273932f, 0.153224308f, 0.2520326168f, 1.063512885f, 1.14115047f, 521.4527484f},  // BANDING
+    {0.353487901f, 0.3734658629f, 8.277049286e-05f, 0.9062562627f, 0.09150303166f, 0.9099517204f, 596.3148142f},  // BANDING_GLARE
+    {1.043882782f, 0.6459495343f, 0.3194584211f, 0.374025247f, 1.114783422f, 1.095360363f, 384.9217577f},  // PEAKS
+    {816.885024f, 1479.463946f, 0.001253215609f, 0.9329636822f, 0.06746643971f, 1.573435413f, 419.6006374f}  // PEAKS_GLARE
+};
+
+
+#define OFFSET(x) offsetof(PU21Context, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption pu21_options[] = {
+    { "multiplier", "multiplier for increasing the pixel value based on the screen display model", OFFSET(multiplier), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 10000, FLAGS},
+    { "mode", "Set the mode of the PU21 encoding", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = BANDING_GLARE}, BANDING, PEAKS_GLARE, FLAGS},
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(pu21);
+
+static av_cold int pu21_init(AVFilterContext* ctx) {
+    PU21Context* pu21 = ctx->priv;
+    pu21->mode = BANDING_GLARE;
+    pu21->multiplier = 1;
+    return 0;
+}
+
+static float apply_pu21(float pixel_val, PU21Context* pu21) {
+    float Y, V;
+    const float* pu21_param = pu21_params[pu21->mode];
+    Y = pixel_val * (pu21->multiplier);
+    V = fmax(pu21_param[6] * (powf((pu21_param[0] + pu21_param[1] * powf(Y, pu21_param[3])) / (1 + pu21_param[2] * powf(Y, pu21_param[3])), pu21_param[4]) - pu21_param[5]), 0);
+    return V;
+}
+
+#define DEFINE_PU21_FILTER(name, type, div)                                         \
+static void pu21_encode_##name(AVFilterContext* ctx, AVFrame* in, AVFrame* out)     \
+{                                                                                   \
+    PU21Context* pu21 = ctx->priv;                                                  \
+    const int height = in->height;                                                  \
+    const int width = in->width;                                                    \
+                                                                                    \
+    int plane;                                                                      \
+    for (plane = 0; plane < pu21->nb_planes; plane++) {                             \
+        const type* src = (const type *)in->data[plane];                            \
+        type* dst = (type *)out->data[plane];                                       \
+        int  linesize = in->linesize[plane] / div;                                  \
+        type pixel_val;                                                             \
+        for (int y = 0; y < height; y++) {                                          \
+            for (int x = 0; x < width; x++) {                                       \
+                pixel_val = src[y * linesize + x];                                  \
+                dst[y * linesize + x] = (type)apply_pu21(pixel_val, pu21);          \
+            }                                                                       \
+        }                                                                           \
+    }                                                                               \
+}
+
+DEFINE_PU21_FILTER(8, uint8_t, 1)
+DEFINE_PU21_FILTER(16, uint16_t, 2)
+DEFINE_PU21_FILTER(32, float, 4)
+
+static int filter_frame(AVFilterLink* inlink, AVFrame* input) {
+    AVFilterContext* ctx = inlink->dst;
+    PU21Context* pu21 = ctx->priv;
+
+    AVFilterLink* outlink = ctx->outputs[0];
+    AVFrame* out;
+
+    if (av_frame_is_writable(input)) {
+        out = input;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&input);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, input);
+    }
+
+    if (pu21->depth <= 8) {
+        pu21_encode_8(ctx, input, out);
+    } else if (pu21->depth <= 16) {
+        pu21_encode_16(ctx, input, out);
+    } else {
+        pu21_encode_32(ctx, input, out);
+    }
+
+    if (out != input)
+        av_frame_free(&input);
+    return ff_filter_frame(outlink, out);
+}
+
+
+static int config_input(AVFilterLink* inlink) {
+    const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(inlink->format);
+    PU21Context* s = inlink->dst->priv;
+
+    s->depth = desc->comp[0].depth;
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+    return 0;
+}
+
+static const AVFilterPad pu21_inputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_input,
+        .filter_frame = filter_frame,
+    }
+};
+
+static const AVFilterPad pu21_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    }
+};
+
+const AVFilter ff_vf_pu21 = {
+    .name = "pu21",
+    .description = NULL_IF_CONFIG_SMALL("Convert HDR linear values to PU values using PU21 transform"),
+    .priv_size = sizeof(PU21Context),
+    .init = pu21_init,
+    FILTER_INPUTS(pu21_inputs),
+    FILTER_OUTPUTS(pu21_outputs),
+    .priv_class = &pu21_class,
+    .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};