PR #25885 opened by Kilian (lusoris) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25885 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25885.patch
This series adds `bt2408_hable`, a new value of the tonemap filter's `tonemap` option. It maps HDR input to SDR BT.709 by applying the Hable curve to the intensity of IPT, following libplacebo's colour mapping for its `hable` tone mapping with clipping gamut mapping. The existing curves are unchanged. Commits: 1. `avfilter/bt2408_hable`: a helper (`bt2408_hable.c` / `bt2408_hable.h`) ported from the libplacebo v7.360.1 colour map. - The Hable curve is applied to the PQ-encoded intensity of IPT, with the SDR reference white at 203 cd/m^2 (ITU-R BT.2408). - Chroma is scaled towards the intensity axis as the curve compresses intensity. - The result is clipped into BT.709 and encoded for a BT.1886 display with a 1000:1 contrast. - The tone curve is a 256-entry table built once per peak, so the per-pixel work is two 3x3 matrices, six PQ conversions and one table lookup. - The context holds only 32-bit members, so GPU filters can upload it unchanged. 2. `avfilter/vf_tonemap`: the `bt2408_hable` value. - The peak comes from the content light level or the mastering display side data, as for the other curves. The helper is rebuilt only when the peak or the primaries change. - The input must use D65 primaries. Other primaries are refused with `AVERROR(ENOTSUP)`. - The output is tagged BT.709 primaries with a linear transfer, since the helper returns linear light. The content light level and mastering display side data are removed, since they no longer describe the frame. - The options `param` and `desat` are ignored. - `doc/filters.texi` documents the value and adds an HDR10 to SDR example. Tests: - `fate-filter-bt2408-hable` runs `libavfilter/tests/bt2408_hable`. It checks seven colours against a separate double-precision model, a planted wrong peak, the clamp of peaks below 100 cd/m^2, and the refusal of a zero peak, a NaN peak, unspecified primaries and a DCI white point. - `fate-filter-tonemap-bt2408-hable` runs the filter in a graph on the helper's test colours. It checks the peak taken from MaxCLL, the peak taken from the mastering display, the output tags (also for an input tagged PQ), and a planted wrong MaxCLL. Tested on master 681025faf1 (2026-11-08), x86-64 Linux, GCC 16.2.1, default configure: - Each of the two commits builds on its own and passes its FATE tests: `fate-filter-bt2408-hable` at the first commit, both tests at the second. - No compiler warning names a file the series touches. - The existing modes give byte-identical output before and after the series. The check covers none, linear, gamma, clip, reinhard, hable and mobius at peak 0 and peak 4, on gbrpf32le, on gbrapf32le with an input alpha, and on gbrpf32le converted to gbrapf32le after the filter. All 42 framemd5 outputs match master, and 36 of them differ from each other, so the check tells the modes and peaks apart. - The series also merges onto master b2b543c079 without conflict. None of the 14 commits since 681025faf1 touches its files. >From 7edf41a43e9c02981c1921eef5a0a2736d7669ea Mon Sep 17 00:00:00 2001 From: lusoris <[email protected]> Date: Sun, 8 Nov 2026 19:07:13 +0100 Subject: [PATCH 1/2] avfilter/bt2408_hable: add a Hable tone map on IPT intensity Port the libplacebo v7.360.1 colour map for its default-like Hable setting: the Hable curve is applied to the PQ-encoded intensity of IPT, chroma is scaled towards the intensity axis as the curve compresses it, and the result is clipped into BT.709 and encoded for a BT.1886 display with a 1000:1 contrast. The tone curve is a 256-entry table built once per peak, so the per-pixel work is two 3x3 matrices, six PQ conversions and one table lookup. The context struct holds only 32-bit members so that GPU filters can upload it unchanged. A unit test checks seven colours against a separate double precision model, a planted wrong peak, the clamp of peaks below 100 cd/m^2 and the refusal of bad peaks and non-D65 primaries. Signed-off-by: lusoris <[email protected]> --- libavfilter/Makefile | 2 +- libavfilter/bt2408_hable.c | 231 +++++++++++++++++++++++++++++ libavfilter/bt2408_hable.h | 62 ++++++++ libavfilter/tests/bt2408_hable.c | 136 +++++++++++++++++ tests/fate/filter-video.mak | 4 + tests/ref/fate/filter-bt2408-hable | 13 ++ 6 files changed, 447 insertions(+), 1 deletion(-) create mode 100644 libavfilter/bt2408_hable.c create mode 100644 libavfilter/bt2408_hable.h create mode 100644 libavfilter/tests/bt2408_hable.c create mode 100644 tests/ref/fate/filter-bt2408-hable diff --git a/libavfilter/Makefile b/libavfilter/Makefile index dc5c083f013..57af57595d7 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -689,7 +689,7 @@ SKIPHEADERS-$(CONFIG_VULKAN) += vulkan_filter.h SKIPHEADERS-$(CONFIG_SCALE_CUDA_FILTER) += vf_scale_cuda.h TOOLS = graph2dot -TESTPROGS = drawutils filtfmts formats integral +TESTPROGS = bt2408_hable drawutils filtfmts formats integral TESTPROGS-$(CONFIG_DRAWVG_FILTER) += drawvg diff --git a/libavfilter/bt2408_hable.c b/libavfilter/bt2408_hable.c new file mode 100644 index 00000000000..e8b13493dc9 --- /dev/null +++ b/libavfilter/bt2408_hable.c @@ -0,0 +1,231 @@ +/* + * HDR to SDR tone mapping with the Hable curve on the intensity of IPT + * + * The colour map follows libplacebo v7.360.1 with hable tone mapping and + * clipping gamut mapping (src/shaders/colorspace.c: pl_shader_color_map_ex; + * src/tone_mapping.c: pl_tone_map_generate, hable_map), Copyright (C) Niklas + * Haas and the libplacebo contributors, licensed under the GNU Lesser General + * Public License version 2.1 or later. + * + * 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 <math.h> + +#include "libavutil/common.h" +#include "libavutil/csp.h" +#include "libavutil/error.h" + +#include "bt2408_hable.h" +#include "colorspace.h" + +#define SDR_WHITE 203.0 /* cd/m^2, ITU-R BT.2408 */ +#define SDR_CONTRAST 1000.0 +#define HDR_BLACK 1e-6 /* cd/m^2 */ +#define PQ_MAX 10000.0 + +#define PQ_M1 (2610.0 / 4096 / 4) +#define PQ_M2 (2523.0 / 4096 * 128) +#define PQ_C1 (3424.0 / 4096) +#define PQ_C2 (2413.0 / 4096 * 32) +#define PQ_C3 (2392.0 / 4096 * 32) + +/* Hunt-Pointer-Estevez XYZ to LMS, then 4% crosstalk, as for ICtCp. */ +static const double hpe_xyz_to_lms[3][3] = { + { 0.40024, 0.70760, -0.08081 }, + { -0.22630, 1.16532, 0.04570 }, + { 0.0, 0.0, 0.91822 }, +}; + +static const double crosstalk[3][3] = { + { 0.92, 0.04, 0.04 }, + { 0.04, 0.92, 0.04 }, + { 0.04, 0.04, 0.92 }, +}; + +static const float lms_to_ipt[3][3] = { + { 0.4000f, 0.4000f, 0.2000f }, + { 4.4550f, -4.8510f, 0.3960f }, + { 0.8056f, 0.3572f, -1.1628f }, +}; + +static const float ipt_to_lms[3][3] = { + { 1.0f, 0.0975689f, 0.205226f }, + { 1.0f, -0.1138760f, 0.133217f }, + { 1.0f, 0.0326151f, -0.676887f }, +}; + +static double pq_oetf(double nits) +{ + double y = pow(FFMAX(nits, 0.0) / PQ_MAX, PQ_M1); + return pow((PQ_C1 + PQ_C2 * y) / (1.0 + PQ_C3 * y), PQ_M2); +} + +static double pq_eotf(double x) +{ + double p = pow(FFMAX(x, 0.0), 1.0 / PQ_M2); + return pow(FFMAX(p - PQ_C1, 0.0) / (PQ_C2 - PQ_C3 * p), 1.0 / PQ_M1) * PQ_MAX; +} + +static float pq_oetf_f(float nits) +{ + float y = powf(FFMAX(nits, 0.0f) * (float)(1.0 / PQ_MAX), (float)PQ_M1); + return powf(((float)PQ_C1 + (float)PQ_C2 * y) / (1.0f + (float)PQ_C3 * y), (float)PQ_M2); +} + +static float pq_eotf_f(float x) +{ + float p = powf(FFMAX(x, 0.0f), (float)(1.0 / PQ_M2)); + return powf(FFMAX(p - (float)PQ_C1, 0.0f) / ((float)PQ_C2 - (float)PQ_C3 * p), + (float)(1.0 / PQ_M1)) * (float)PQ_MAX; +} + +static double hable(double x) +{ + const double a = 0.15, b = 0.50, c = 0.10, d = 0.20, e = 0.02, f = 0.30; + return (x * (a * x + c * b) + d * e) / (x * (a * x + b) + d * f) - e / f; +} + +static double bt1886_eotf(double x, double min, double max) +{ + const double lb = pow(min, 1 / 2.4), lw = pow(max, 1 / 2.4); + return pow((lw - lb) * x + lb, 2.4); +} + +static double bt1886_oetf(double x, double min, double max) +{ + const double lb = pow(min, 1 / 2.4), lw = pow(max, 1 / 2.4); + return (pow(FFMAX(x, 0.0), 1 / 2.4) - lb) / (lw - lb); +} + +/* RGB of prim to LMS with crosstalk; ENOTSUP unless prim is known and has a D65 white point. */ +static int rgb_to_lms(enum AVColorPrimaries prim, double out[3][3]) +{ + const AVColorPrimariesDesc *desc = av_csp_primaries_desc_from_id(prim); + const AVColorPrimariesDesc *d65 = av_csp_primaries_desc_from_id(AVCOL_PRI_BT709); + double rgb2xyz[3][3], lms[3][3]; + + if (!desc || av_cmp_q(desc->wp.x, d65->wp.x) || av_cmp_q(desc->wp.y, d65->wp.y)) + return AVERROR(ENOTSUP); + /* The table maps white to Y = y_w; PQ needs absolute luminance, Y = 1. */ + ff_fill_rgb2xyz_table(&desc->prim, &desc->wp, rgb2xyz); + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + rgb2xyz[i][j] /= av_q2d(desc->wp.y); + /* ff_matrix_mul_3x3(dst, a, b) computes b * a */ + ff_matrix_mul_3x3(lms, rgb2xyz, hpe_xyz_to_lms); + ff_matrix_mul_3x3(out, lms, crosstalk); + return 0; +} + +/* libplacebo pl_tone_map_params_infer: no inverse tone mapping. */ +static void fill_lut(FFBT2408Hable *s, double peak) +{ + const double out_max = FFMIN(SDR_WHITE, peak) / SDR_WHITE; + const double out_min = SDR_WHITE / SDR_CONTRAST / SDR_WHITE; + const double in_max = peak / SDR_WHITE, in_min = HDR_BLACK / SDR_WHITE; + const double ratio = in_max / out_max, scale = 1.0 / hable(ratio); + + s->lut_min = pq_oetf(HDR_BLACK); + s->lut_max = pq_oetf(peak); + for (int i = 0; i < FF_BT2408_HABLE_LUT_SIZE; i++) { + double t = (double)i / (FF_BT2408_HABLE_LUT_SIZE - 1); + double x = pq_eotf(s->lut_min + t * (s->lut_max - s->lut_min)) / SDR_WHITE; + x = bt1886_oetf(x, in_min, in_max); + x = scale * hable(bt1886_eotf(x, 0, ratio)); + x = bt1886_eotf(bt1886_oetf(x, 0, 1), out_min, out_max); + s->lut[i] = pq_oetf(av_clipd(x, out_min, out_max) * SDR_WHITE); + } +} + +int ff_bt2408_hable_init(FFBT2408Hable *s, enum AVColorPrimaries prim, double peak) +{ + const double lb = pow(1.0 / SDR_CONTRAST, 1 / 2.4), lw = 1.0; + double in[3][3], bt709[3][3], out[3][3]; + int ret; + + if (!(peak > 0.0) || !isfinite(peak)) + return AVERROR(EINVAL); + if ((ret = rgb_to_lms(prim, in)) < 0 || (ret = rgb_to_lms(AVCOL_PRI_BT709, bt709)) < 0) + return ret; + ff_matrix_invert_3x3(bt709, out); + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + s->rgb_to_lms[i][j] = in[i][j] * REFERENCE_WHITE / SDR_WHITE; + s->lms_to_rgb[i][j] = out[i][j]; + } + } + + /* Input below the reference display peak is not expanded. */ + fill_lut(s, av_clipd(peak, FFMIN(SDR_WHITE, REFERENCE_WHITE), PQ_MAX)); + s->black_scale = 1.0 / pow(lw - lb, 2.4); + s->black_offset = lb / (lw - lb); + return 0; +} + +/* out = m * in for a 3x3 matrix and a column vector. */ +static void mul3(const float m[3][3], const float in[3], float out[3]) +{ + for (int i = 0; i < 3; i++) + out[i] = m[i][0] * in[0] + m[i][1] * in[1] + m[i][2] * in[2]; +} + +static float lut_lookup(const FFBT2408Hable *s, float i) +{ + float pos = (i - s->lut_min) / (s->lut_max - s->lut_min) * (FF_BT2408_HABLE_LUT_SIZE - 1); + int idx; + + pos = av_clipf(pos, 0.0f, FF_BT2408_HABLE_LUT_SIZE - 1); + idx = FFMIN((int)pos, FF_BT2408_HABLE_LUT_SIZE - 2); + return s->lut[idx] + (pos - idx) * (s->lut[idx + 1] - s->lut[idx]); +} + +/* Desaturate towards the intensity axis as the tone curve compresses it. */ +static float hull_scale(float i_orig, float i_mapped) +{ + float h_orig = ((i_orig - 6.0f) * i_orig + 9.0f) * i_orig; + float h_mapped = ((i_mapped - 6.0f) * i_mapped + 9.0f) * i_mapped; + + return FFMIN(i_orig / FFMAX(i_mapped, 1e-9f), h_mapped / FFMAX(h_orig, 1e-9f)); +} + +void ff_bt2408_hable_pixel(const FFBT2408Hable *s, const float in[3], float out[3]) +{ + float lms[3], ipt[3], k; + + mul3(s->rgb_to_lms, in, lms); + for (int c = 0; c < 3; c++) + lms[c] = pq_oetf_f(lms[c] * (float)SDR_WHITE); + mul3(lms_to_ipt, lms, ipt); + + k = ipt[0]; + ipt[0] = lut_lookup(s, k); + k = hull_scale(k, ipt[0]); + ipt[1] *= k; + ipt[2] *= k; + + mul3(ipt_to_lms, ipt, lms); + for (int c = 0; c < 3; c++) + lms[c] = pq_eotf_f(lms[c]) * (float)(1.0 / SDR_WHITE); + mul3(s->lms_to_rgb, lms, out); + + /* Encode for BT.1886 with a raised black, then decode as pure power 2.4. */ + for (int c = 0; c < 3; c++) { + float v = powf(av_clipf(out[c], 0.0f, 1.0f) * s->black_scale, 1.0f / 2.4f) - s->black_offset; + out[c] = powf(av_clipf(v, 0.0f, 1.0f), 2.4f); + } +} diff --git a/libavfilter/bt2408_hable.h b/libavfilter/bt2408_hable.h new file mode 100644 index 00000000000..8ca93e6f000 --- /dev/null +++ b/libavfilter/bt2408_hable.h @@ -0,0 +1,62 @@ +/* + * HDR to SDR tone mapping with the Hable curve on the intensity of IPT + * + * 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 + */ + +#ifndef AVFILTER_BT2408_HABLE_H +#define AVFILTER_BT2408_HABLE_H + +#include "libavutil/pixfmt.h" + +#define FF_BT2408_HABLE_LUT_SIZE 256 + +/** + * Everything needed to tone map one frame. All members are 32-bit scalars + * or arrays of them, so the struct can be uploaded unchanged as an OpenCL + * or std430 Vulkan buffer. + */ +typedef struct FFBT2408Hable { + float rgb_to_lms[3][3]; ///< linear input RGB, 1.0 = 100 cd/m^2, to LMS + float lms_to_rgb[3][3]; ///< LMS to linear BT.709 RGB + float lut_min; ///< PQ intensity of the first LUT entry + float lut_max; ///< PQ intensity of the last LUT entry + float lut[FF_BT2408_HABLE_LUT_SIZE]; ///< tone curve on PQ intensity + float black_scale; ///< BT.1886 encoding with a 1000:1 contrast + float black_offset; +} FFBT2408Hable; + +/** + * Fill out for one input colour space and signal peak. + * + * @param prim primaries of the input, with a D65 white point + * @param peak signal peak in cd/m^2 + * @return 0 on success, AVERROR(ENOTSUP) for other primaries, + * AVERROR(EINVAL) for a peak that is not a positive number + */ +int ff_bt2408_hable_init(FFBT2408Hable *s, enum AVColorPrimaries prim, double peak); + +/** + * Tone map one pixel. + * + * @param in linear RGB in the input primaries, 1.0 = 100 cd/m^2 + * @param out linear BT.709 RGB in [0, 1] for a pure power 2.4 display, + * 1.0 = reference white (203 cd/m^2, ITU-R BT.2408) + */ +void ff_bt2408_hable_pixel(const FFBT2408Hable *s, const float in[3], float out[3]); + +#endif /* AVFILTER_BT2408_HABLE_H */ diff --git a/libavfilter/tests/bt2408_hable.c b/libavfilter/tests/bt2408_hable.c new file mode 100644 index 00000000000..cca2b9a6455 --- /dev/null +++ b/libavfilter/tests/bt2408_hable.c @@ -0,0 +1,136 @@ +/* + * 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 <math.h> +#include <stdio.h> + +#include "libavutil/error.h" + +#include "libavfilter/bt2408_hable.c" + +#define PEAK 1000.0 +#define TOL 2e-4f + +/* + * BT.2020 input, 1.0 = 100 cd/m^2, peak 1000 cd/m^2. The expected values + * come from a double precision model of the libplacebo v7.360.1 colour map, + * written separately from this port. + */ +static const struct { + float in[3], out[3]; +} cases[] = { + { { 0.5f, 0.5f, 0.5f }, { 0.1159569f, 0.1159569f, 0.1159570f } }, /* grey */ + { { 2.03f, 2.03f, 2.03f }, { 0.3907243f, 0.3907245f, 0.3907246f } }, /* reference white */ + { { 3.0f, 2.0f, 1.5f }, { 0.7225472f, 0.3582895f, 0.2667108f } }, /* warm highlight */ + { { 0.8f, 1.2f, 2.5f }, { 0.0841885f, 0.2574010f, 0.5698625f } }, /* sky blue */ + { { 0.05f, 0.3f, 4.0f }, { 0.0000000f, 0.0704900f, 1.0000000f } }, /* clipped blue */ + { { 6.0f, 5.5f, 5.8f }, { 0.8534305f, 0.7344222f, 0.7876654f } }, /* near the peak */ + { { 0.0f, 0.0f, 0.0f }, { 0.0000000f, 0.0000000f, 0.0000000f } }, /* black */ +}; + +/* Returns 1 when all three components of got are within TOL of want; a NaN never matches. */ +static int near(const float *got, const float *want) +{ + for (int i = 0; i < 3; i++) + if (!(fabsf(got[i] - want[i]) <= TOL)) + return 0; + return 1; +} + +/* Runs every test colour through s and returns the number of wrong results. */ +static int check_cases(const FFBT2408Hable *s) +{ + int fails = 0; + + for (size_t n = 0; n < FF_ARRAY_ELEMS(cases); n++) { + float out[3]; + ff_bt2408_hable_pixel(s, cases[n].in, out); + if (near(out, cases[n].out)) + printf("case %zu: ok\n", n); + else + printf("case %zu: WRONG %.7f %.7f %.7f\n", n, out[0], out[1], out[2]); + fails += !near(out, cases[n].out); + } + return fails; +} + +/* The planted error: a peak four times too high must miss the expected colour. */ +static int check_planted(void) +{ + FFBT2408Hable s; + float out[3]; + int detected; + + if (ff_bt2408_hable_init(&s, AVCOL_PRI_BT2020, 4 * PEAK) < 0) { + printf("planted wrong peak: init failed\n"); + return 1; + } + ff_bt2408_hable_pixel(&s, cases[2].in, out); + detected = !near(out, cases[2].out); + printf("planted wrong peak: %s\n", detected ? "detected" : "MISSED"); + return !detected; +} + +/* A peak below 100 cd/m^2 is not expanded: it maps like a 100 cd/m^2 peak. */ +static int check_low_peak(void) +{ + FFBT2408Hable low, ref; + float a[3], b[3]; + const float in[3] = { 0.6f, 0.4f, 0.3f }; + int ok; + + if (ff_bt2408_hable_init(&low, AVCOL_PRI_BT2020, 50.0) < 0 || + ff_bt2408_hable_init(&ref, AVCOL_PRI_BT2020, 100.0) < 0) { + printf("peak below 100 cd/m^2 clamps: init failed\n"); + return 1; + } + ff_bt2408_hable_pixel(&low, in, a); + ff_bt2408_hable_pixel(&ref, in, b); + ok = a[0] == b[0] && a[1] == b[1] && a[2] == b[2]; + printf("peak below 100 cd/m^2 clamps: %s\n", ok ? "ok" : "WRONG"); + return !ok; +} + +/* Returns 0 when ff_bt2408_hable_init() refuses prim and peak with the error want. */ +static int expect_refusal(enum AVColorPrimaries prim, double peak, const char *what, int want) +{ + FFBT2408Hable s; + int ret = ff_bt2408_hable_init(&s, prim, peak); + + printf("%s: %s\n", what, ret == want ? "refused" : "ACCEPTED"); + return ret != want; +} + +int main(void) +{ + FFBT2408Hable s; + int fails = 0; + + if (ff_bt2408_hable_init(&s, AVCOL_PRI_BT2020, PEAK) < 0) { + printf("init failed\n"); + return 1; + } + fails += check_cases(&s); + fails += check_planted(); + fails += check_low_peak(); + fails += expect_refusal(AVCOL_PRI_BT2020, 0.0, "zero peak", AVERROR(EINVAL)); + fails += expect_refusal(AVCOL_PRI_BT2020, NAN, "NaN peak", AVERROR(EINVAL)); + fails += expect_refusal(AVCOL_PRI_UNSPECIFIED, PEAK, "unspecified primaries", AVERROR(ENOTSUP)); + fails += expect_refusal(AVCOL_PRI_SMPTE431, PEAK, "DCI white point", AVERROR(ENOTSUP)); + return fails != 0; +} diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak index 82ab48dc68d..e2c25987f23 100644 --- a/tests/fate/filter-video.mak +++ b/tests/fate/filter-video.mak @@ -530,6 +530,10 @@ FATE_FILTER-$(call FILTERFRAMECRC, FPS TESTSRC2 SETPTS SETTB) += fate-filter-fps fate-filter-fps-single-cross-start: CMD = framecrc -lavfi testsrc2=s=16x16:r=25/2:d=0.08,settb=1/25,setpts=PTS-1,fps=1:start_time=0 fate-filter-fps-single-cross-start: REF = $(SRC_PATH)/tests/ref/fate/filter-fps-single +FATE_LIBAVFILTER-yes += fate-filter-bt2408-hable +fate-filter-bt2408-hable: libavfilter/tests/bt2408_hable$(EXESUF) +fate-filter-bt2408-hable: CMD = run libavfilter/tests/bt2408_hable$(EXESUF) + FATE_LIBAVFILTER-yes += fate-filter-drawutils fate-filter-drawutils: libavfilter/tests/drawutils$(EXESUF) fate-filter-drawutils: CMD = run libavfilter/tests/drawutils$(EXESUF) diff --git a/tests/ref/fate/filter-bt2408-hable b/tests/ref/fate/filter-bt2408-hable new file mode 100644 index 00000000000..e8613d010c5 --- /dev/null +++ b/tests/ref/fate/filter-bt2408-hable @@ -0,0 +1,13 @@ +case 0: ok +case 1: ok +case 2: ok +case 3: ok +case 4: ok +case 5: ok +case 6: ok +planted wrong peak: detected +peak below 100 cd/m^2 clamps: ok +zero peak: refused +NaN peak: refused +unspecified primaries: refused +DCI white point: refused -- 2.54.0 >From d4ff55b78bf403a49729b1d136465d46502d57e1 Mon Sep 17 00:00:00 2001 From: lusoris <[email protected]> Date: Sun, 8 Nov 2026 19:07:19 +0100 Subject: [PATCH 2/2] avfilter/vf_tonemap: add the bt2408_hable tone map The new value maps HDR input to SDR BT.709 with the shared bt2408_hable helper. The peak comes from the content light level or the mastering display side data, as for the other curves, and the helper is rebuilt only when the peak or the primaries change. The output is tagged linear BT.709, as the helper returns linear light, and drops the light level side data, since it no longer describes the frame. The existing curves are untouched. The test runs the filter in a graph on the helper's test colours and checks the peak from MaxCLL, the peak from the mastering display, the output tags, also for an input tagged PQ, and a planted wrong MaxCLL. Signed-off-by: lusoris <[email protected]> --- doc/filters.texi | 17 +++ libavfilter/Makefile | 2 +- libavfilter/tests/bt2408_hable.c | 157 ++++++++++++++++++++- libavfilter/vf_tonemap.c | 69 ++++++++- tests/fate/filter-video.mak | 4 + tests/ref/fate/filter-tonemap-bt2408-hable | 5 + 6 files changed, 248 insertions(+), 6 deletions(-) create mode 100644 tests/ref/fate/filter-tonemap-bt2408-hable diff --git a/doc/filters.texi b/doc/filters.texi index ec23628067a..ef90c0a3542 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -24959,6 +24959,11 @@ data should be linearized beforehand (and possibly correctly tagged). ffmpeg -i INPUT -vf zscale=transfer=linear,tonemap=clip,zscale=transfer=bt709,format=yuv420p OUTPUT @end example +Tone map an HDR10 input to SDR BT.709: +@example +ffmpeg -i INPUT -vf zscale=transfer=linear,format=gbrpf32le,tonemap=bt2408_hable,zscale=transfer=bt709:matrix=bt709:range=tv,format=yuv420p OUTPUT +@end example + @subsection Options The filter accepts the following options. @@ -24994,6 +24999,18 @@ important than color and brightness accuracy. Smoothly map out-of-range values, while retaining contrast and colors for in-range material as much as possible. Use it when color accuracy is more important than detail preservation. + +@item bt2408_hable +Map HDR input to SDR BT.709 the way libplacebo does with its @code{hable} +tone mapping and clipping gamut mapping: the Hable curve is applied to the +intensity of IPT, with the SDR reference white at 203 cd/m^2 (ITU-R BT.2408), +and chroma is reduced as the curve compresses intensity. The input must use +D65 primaries, with 1.0 = 100 cd/m^2 as @ref{zscale} produces with its default +@option{npl}. The output is in BT.709 primaries and tagged as such, still in +linear light, so that a following @code{zscale=transfer=bt709} encodes it for +a BT.1886 display with a 1000:1 contrast. The content light level and +mastering display side data are dropped from the output. The options +@option{param} and @option{desat} are ignored. @end table Default is none. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 57af57595d7..e74c0501e03 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -543,7 +543,7 @@ OBJS-$(CONFIG_TLUT2_FILTER) += vf_lut2.o framesync.o OBJS-$(CONFIG_TMEDIAN_FILTER) += vf_xmedian.o framesync.o OBJS-$(CONFIG_TMIDEQUALIZER_FILTER) += vf_tmidequalizer.o OBJS-$(CONFIG_TMIX_FILTER) += vf_mix.o framesync.o -OBJS-$(CONFIG_TONEMAP_FILTER) += vf_tonemap.o +OBJS-$(CONFIG_TONEMAP_FILTER) += vf_tonemap.o bt2408_hable.o OBJS-$(CONFIG_TONEMAP_OPENCL_FILTER) += vf_tonemap_opencl.o opencl.o \ opencl/tonemap.o opencl/colorspace_common.o OBJS-$(CONFIG_TONEMAP_VAAPI_FILTER) += vf_tonemap_vaapi.o vaapi_vpp.o diff --git a/libavfilter/tests/bt2408_hable.c b/libavfilter/tests/bt2408_hable.c index cca2b9a6455..4c0d822cc36 100644 --- a/libavfilter/tests/bt2408_hable.c +++ b/libavfilter/tests/bt2408_hable.c @@ -18,9 +18,15 @@ #include <math.h> #include <stdio.h> +#include <string.h> #include "libavutil/error.h" +#include "libavutil/frame.h" +#include "libavutil/mastering_display_metadata.h" +#include "libavfilter/avfilter.h" +#include "libavfilter/buffersink.h" +#include "libavfilter/buffersrc.h" #include "libavfilter/bt2408_hable.c" #define PEAK 1000.0 @@ -116,11 +122,160 @@ static int expect_refusal(enum AVColorPrimaries prim, double peak, const char *w return ret != want; } -int main(void) +/* Runs buffer -> tonemap=bt2408_hable -> buffersink on one frame. */ +static int run_filter(AVFrame *in, AVFrame *out) +{ + AVFilterGraph *g = avfilter_graph_alloc(); + AVFilterContext *src = NULL, *tm = NULL, *sink = NULL; + char args[128]; + int ret; + + if (!g) + return AVERROR(ENOMEM); + snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=1/25", + in->width, in->height, in->format); + ret = avfilter_graph_create_filter(&src, avfilter_get_by_name("buffer"), "in", args, NULL, g); + if (ret >= 0) + ret = avfilter_graph_create_filter(&tm, avfilter_get_by_name("tonemap"), "tm", + "tonemap=bt2408_hable", NULL, g); + if (ret >= 0) + ret = avfilter_graph_create_filter(&sink, avfilter_get_by_name("buffersink"), "out", NULL, NULL, g); + if (ret >= 0) + ret = avfilter_link(src, 0, tm, 0); + if (ret >= 0) + ret = avfilter_link(tm, 0, sink, 0); + if (ret >= 0) + ret = avfilter_graph_config(g, NULL); + if (ret >= 0) + ret = av_buffersrc_add_frame_flags(src, in, AV_BUFFERSRC_FLAG_KEEP_REF); + if (ret >= 0) + ret = av_buffersrc_add_frame(src, NULL); + if (ret >= 0) + ret = av_buffersink_get_frame(sink, out); + avfilter_graph_free(&g); + return ret; +} + +/* Attaches MaxCLL and mastering-display peak side data to f; 0 leaves a level out. */ +static int add_light_levels(AVFrame *f, unsigned max_cll, unsigned mastering_max) +{ + if (max_cll) { + AVContentLightMetadata *cll = av_content_light_metadata_create_side_data(f); + if (!cll) + return AVERROR(ENOMEM); + cll->MaxCLL = max_cll; + } + if (mastering_max) { + AVMasteringDisplayMetadata *md = av_mastering_display_metadata_create_side_data(f); + if (!md) + return AVERROR(ENOMEM); + md->max_luminance = av_make_q(mastering_max, 1); + md->min_luminance = av_make_q(1, 10000); + md->has_luminance = 1; + } + return 0; +} + +/* One row of the test colours in BT.2020, with the given light levels. */ +static AVFrame *make_frame(unsigned max_cll, unsigned mastering_max, + enum AVColorTransferCharacteristic trc) +{ + const int w = FF_ARRAY_ELEMS(cases); + AVFrame *f = av_frame_alloc(); + + if (!f) + return NULL; + f->format = AV_PIX_FMT_GBRPF32; + f->width = w; + f->height = 1; + f->color_primaries = AVCOL_PRI_BT2020; + f->color_trc = trc; + f->colorspace = AVCOL_SPC_RGB; + if (av_frame_get_buffer(f, 0) < 0 || add_light_levels(f, max_cll, mastering_max) < 0) { + av_frame_free(&f); + return NULL; + } + for (int x = 0; x < w; x++) { + ((float *)f->data[2])[x] = cases[x].in[0]; + ((float *)f->data[0])[x] = cases[x].in[1]; + ((float *)f->data[1])[x] = cases[x].in[2]; + } + return f; +} + +/* Returns 1 when every pixel matches its expected colour. */ +static int frame_matches(const AVFrame *out) +{ + for (int x = 0; x < out->width; x++) { + const float got[3] = { + ((const float *)out->data[2])[x], + ((const float *)out->data[0])[x], + ((const float *)out->data[1])[x], + }; + if (!near(got, cases[x].out)) + return 0; + } + return 1; +} + +/* Returns 1 on a match, 0 on a mismatch, a negative error when the run failed. */ +static int filter_case(unsigned max_cll, unsigned mastering_max, int *tags_ok, + enum AVColorTransferCharacteristic trc) +{ + AVFrame *in = make_frame(max_cll, mastering_max, trc), *out = av_frame_alloc(); + int ret = in && out ? run_filter(in, out) : AVERROR(ENOMEM); + + if (tags_ok) + *tags_ok = 0; + if (ret >= 0) { + if (tags_ok) + *tags_ok = out->color_primaries == AVCOL_PRI_BT709 && + out->color_trc == AVCOL_TRC_LINEAR && + !av_frame_get_side_data(out, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL) && + !av_frame_get_side_data(out, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); + ret = frame_matches(out); + } + av_frame_free(&in); + av_frame_free(&out); + return ret; +} + +/* Runs the filter graph on test frames and verifies peaks and tags. */ +static int check_filter(void) +{ + int fails = 0, tags_ok = 0, ret; + + ret = filter_case(1000, 0, &tags_ok, AVCOL_TRC_LINEAR); + printf("peak from MaxCLL: %s\n", ret == 1 ? "ok" : "WRONG"); + printf("output tagged BT.709, light levels dropped: %s\n", tags_ok ? "ok" : "WRONG"); + fails += ret != 1 || !tags_ok; + + ret = filter_case(1000, 0, &tags_ok, AVCOL_TRC_SMPTE2084); + printf("input tagged PQ, output tagged linear: %s\n", ret >= 0 && tags_ok ? "ok" : "WRONG"); + fails += ret < 0 || !tags_ok; + + ret = filter_case(0, 1000, NULL, AVCOL_TRC_LINEAR); + printf("peak from mastering display: %s\n", ret == 1 ? "ok" : "WRONG"); + fails += ret != 1; + + /* The planted error: a frame whose MaxCLL says 4000 cd/m^2 must map differently. */ + ret = filter_case(4000, 0, NULL, AVCOL_TRC_LINEAR); + printf("planted wrong MaxCLL: %s\n", ret == 0 ? "detected" : "MISSED"); + fails += ret != 0; + return fails; +} + +/* Runs the unit checks, or with the argument "filter" the filter check; exits 1 on any failure. */ +int main(int argc, char **argv) { FFBT2408Hable s; int fails = 0; + if (argc > 1 && !strcmp(argv[1], "filter")) { + av_log_set_level(AV_LOG_QUIET); + return check_filter() != 0; + } + if (ff_bt2408_hable_init(&s, AVCOL_PRI_BT2020, PEAK) < 0) { printf("init failed\n"); return 1; diff --git a/libavfilter/vf_tonemap.c b/libavfilter/vf_tonemap.c index 40bed9b394c..9a20cafdf71 100644 --- a/libavfilter/vf_tonemap.c +++ b/libavfilter/vf_tonemap.c @@ -35,6 +35,7 @@ #include "libavutil/pixdesc.h" #include "avfilter.h" +#include "bt2408_hable.h" #include "colorspace.h" #include "filters.h" #include "video.h" @@ -47,6 +48,7 @@ enum TonemapAlgorithm { TONEMAP_REINHARD, TONEMAP_HABLE, TONEMAP_MOBIUS, + TONEMAP_BT2408_HABLE, TONEMAP_MAX, }; @@ -60,6 +62,10 @@ typedef struct TonemapContext { double peak; const AVLumaCoefficients *coeffs; + + FFBT2408Hable bt2408; + double bt2408_peak; + enum AVColorPrimaries bt2408_prim; } TonemapContext; static av_cold int init(AVFilterContext *ctx) @@ -172,6 +178,23 @@ static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in, *b_out *= sig / sig_orig; } +/* Tone maps the float RGB pixel at (x, y) of in into out with ff_bt2408_hable_pixel(). */ +static void tonemap_bt2408(const FFBT2408Hable *s, AVFrame *out, const AVFrame *in, + const AVPixFmtDescriptor *desc, int x, int y) +{ + float rgb[3], res[3]; + + for (int c = 0; c < 3; c++) { + const AVComponentDescriptor *comp = &desc->comp[c]; + rgb[c] = *(const float *)(in->data[comp->plane] + x * comp->step + y * in->linesize[comp->plane]); + } + ff_bt2408_hable_pixel(s, rgb, res); + for (int c = 0; c < 3; c++) { + const AVComponentDescriptor *comp = &desc->comp[c]; + *(float *)(out->data[comp->plane] + x * comp->step + y * out->linesize[comp->plane]) = res[c]; + } +} + typedef struct ThreadData { AVFrame *in, *out; const AVPixFmtDescriptor *desc; @@ -189,13 +212,40 @@ static int tonemap_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs const int slice_end = ff_slice_pos(in->height, jobnr + 1, nb_jobs); double peak = td->peak; - for (int y = slice_start; y < slice_end; y++) - for (int x = 0; x < out->width; x++) - tonemap(s, out, in, desc, x, y, peak); + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < out->width; x++) { + if (s->tonemap == TONEMAP_BT2408_HABLE) + tonemap_bt2408(&s->bt2408, out, in, desc, x, y); + else + tonemap(s, out, in, desc, x, y, peak); + } + } return 0; } +/* The output is linear SDR BT.709: the HDR metadata of the input no longer applies. */ +static int setup_bt2408(AVFilterContext *ctx, AVFrame *out, const AVFrame *in, double peak) +{ + TonemapContext *s = ctx->priv; + + if (peak != s->bt2408_peak || in->color_primaries != s->bt2408_prim) { + int ret = ff_bt2408_hable_init(&s->bt2408, in->color_primaries, peak * REFERENCE_WHITE); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, "Unsupported primaries '%s' or peak %f\n", + av_color_primaries_name(in->color_primaries), peak); + return ret; + } + s->bt2408_peak = peak; + s->bt2408_prim = in->color_primaries; + } + out->color_primaries = AVCOL_PRI_BT709; + out->color_trc = AVCOL_TRC_LINEAR; + av_frame_remove_side_data(out, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL); + av_frame_remove_side_data(out, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA); + return 0; +} + static int filter_frame(AVFilterLink *link, AVFrame *in) { AVFilterContext *ctx = link->dst; @@ -239,6 +289,15 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) av_log(ctx, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak); } + if (s->tonemap == TONEMAP_BT2408_HABLE) { + ret = setup_bt2408(ctx, out, in, peak); + if (ret < 0) { + av_frame_free(&in); + av_frame_free(&out); + return ret; + } + } + /* load original color space even if pixel format is RGB to compute overbrights */ s->coeffs = av_csp_luma_coeffs_from_avcsp(in->colorspace); if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) { @@ -275,7 +334,8 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) av_frame_free(&in); - ff_update_hdr_metadata(out, peak); + if (s->tonemap != TONEMAP_BT2408_HABLE) + ff_update_hdr_metadata(out, peak); return ff_filter_frame(outlink, out); } @@ -291,6 +351,7 @@ static const AVOption tonemap_options[] = { { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, .unit = "tonemap" }, { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, .unit = "tonemap" }, { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, .unit = "tonemap" }, + { "bt2408_hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_BT2408_HABLE}, 0, 0, FLAGS, .unit = "tonemap" }, { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS }, { "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS }, { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS }, diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak index e2c25987f23..909e4305ed0 100644 --- a/tests/fate/filter-video.mak +++ b/tests/fate/filter-video.mak @@ -534,6 +534,10 @@ FATE_LIBAVFILTER-yes += fate-filter-bt2408-hable fate-filter-bt2408-hable: libavfilter/tests/bt2408_hable$(EXESUF) fate-filter-bt2408-hable: CMD = run libavfilter/tests/bt2408_hable$(EXESUF) +FATE_LIBAVFILTER-$(CONFIG_TONEMAP_FILTER) += fate-filter-tonemap-bt2408-hable +fate-filter-tonemap-bt2408-hable: libavfilter/tests/bt2408_hable$(EXESUF) +fate-filter-tonemap-bt2408-hable: CMD = run libavfilter/tests/bt2408_hable$(EXESUF) filter + FATE_LIBAVFILTER-yes += fate-filter-drawutils fate-filter-drawutils: libavfilter/tests/drawutils$(EXESUF) fate-filter-drawutils: CMD = run libavfilter/tests/drawutils$(EXESUF) diff --git a/tests/ref/fate/filter-tonemap-bt2408-hable b/tests/ref/fate/filter-tonemap-bt2408-hable new file mode 100644 index 00000000000..fe394726810 --- /dev/null +++ b/tests/ref/fate/filter-tonemap-bt2408-hable @@ -0,0 +1,5 @@ +peak from MaxCLL: ok +output tagged BT.709, light levels dropped: ok +input tagged PQ, output tagged linear: ok +peak from mastering display: ok +planted wrong MaxCLL: detected -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
