diff --git a/libavfilter/vf_tonemapx.c b/libavfilter/vf_tonemapx.c index f5109330..092dffbc 100644 --- a/libavfilter/vf_tonemapx.c +++ b/libavfilter/vf_tonemapx.c @@ -56,6 +56,9 @@ #include "internal.h" #include "video.h" +#define MIX(x, y, a) ((x) + ((y) - (x)) * (a)) +#define CLAMP(a, b, c) (FFMIN(FFMAX((a), (b)), (c))) + enum TonemapAlgorithm { TONEMAP_NONE, TONEMAP_LINEAR, @@ -168,11 +171,11 @@ static const enum AVPixelFormat out_pix_fmts[] = { }; const double dovi_lms2rgb_matrix[3][3] = - { - { 3.06441879, -2.16597676, 0.10155818}, - {-0.65612108, 1.78554118, -0.12943749}, - { 0.01736321, -0.04725154, 1.03004253}, - }; +{ + { 3.06441879, -2.16597676, 0.10155818}, + {-0.65612108, 1.78554118, -0.12943749}, + { 0.01736321, -0.04725154, 1.03004253}, +}; static void update_dovi_buf(AVFilterContext *ctx) { @@ -316,7 +319,6 @@ inline static void lms2rgb(float* dest, float l, float m, float s, const double dest[2] = l * (float)lms2rgb_matrix[2][0] + m * (float)lms2rgb_matrix[2][1] + s * (float)lms2rgb_matrix[2][2]; } -#define CLAMP(a, b, c) (FFMIN(FFMAX((a), (b)), (c))) inline static void reshape_dovi_yuv(float* dest, float* src, const TonemapIntParams *ctx) { int i; @@ -357,41 +359,40 @@ inline static void reshape_dovi_yuv(float* dest, float* src, const TonemapIntPar coeffs[2] = dovi_coeffs[0*4+2]; coeffs[3] = dovi_coeffs[0*4+3]; -#define mix(x, y, a) ((x) + ((y) - (x)) * (a)) if (i == 0 && dovi_num_pivots > 2) { int t0 = s >= dovi_pivots[0], t1 = s >= dovi_pivots[1]; int t2 = s >= dovi_pivots[2], t3 = s >= dovi_pivots[3]; int t4 = s >= dovi_pivots[4], t5 = s >= dovi_pivots[5], t6 = s >= dovi_pivots[6]; - float m01[4] = { mix(dovi_coeffs[0*4+0], dovi_coeffs[1*4+0], t0), - mix(dovi_coeffs[0*4+1], dovi_coeffs[1*4+1], t0), - mix(dovi_coeffs[0*4+2], dovi_coeffs[1*4+2], t0), - mix(dovi_coeffs[0*4+3], dovi_coeffs[1*4+3], t0) }; - float m23[4] = { mix(dovi_coeffs[2*4+0], dovi_coeffs[3*4+0], t2), - mix(dovi_coeffs[2*4+1], dovi_coeffs[3*4+1], t2), - mix(dovi_coeffs[2*4+2], dovi_coeffs[3*4+2], t2), - mix(dovi_coeffs[2*4+3], dovi_coeffs[3*4+3], t2) }; - float m0123[4] = { mix(m01[0], m23[0], t1), - mix(m01[1], m23[1], t1), - mix(m01[2], m23[2], t1), - mix(m01[3], m23[3], t1) }; - float m45[4] = { mix(dovi_coeffs[4*4+0], dovi_coeffs[5*4+0], t4), - mix(dovi_coeffs[4*4+1], dovi_coeffs[5*4+1], t4), - mix(dovi_coeffs[4*4+2], dovi_coeffs[5*4+2], t4), - mix(dovi_coeffs[4*4+3], dovi_coeffs[5*4+3], t4) }; - float m67[4] = { mix(dovi_coeffs[6*4+0], dovi_coeffs[7*4+0], t6), - mix(dovi_coeffs[6*4+1], dovi_coeffs[7*4+1], t6), - mix(dovi_coeffs[6*4+2], dovi_coeffs[7*4+2], t6), - mix(dovi_coeffs[6*4+3], dovi_coeffs[7*4+3], t6) }; - float m4567[4] = { mix(m45[0], m67[0], t5), - mix(m45[1], m67[1], t5), - mix(m45[2], m67[2], t5), - mix(m45[3], m67[3], t5) }; - - coeffs[0] = mix(m0123[0], m4567[0], t3); - coeffs[1] = mix(m0123[1], m4567[1], t3); - coeffs[2] = mix(m0123[2], m4567[2], t3); - coeffs[3] = mix(m0123[3], m4567[3], t3); + float m01[4] = { MIX(dovi_coeffs[0*4+0], dovi_coeffs[1*4+0], t0), + MIX(dovi_coeffs[0*4+1], dovi_coeffs[1*4+1], t0), + MIX(dovi_coeffs[0*4+2], dovi_coeffs[1*4+2], t0), + MIX(dovi_coeffs[0*4+3], dovi_coeffs[1*4+3], t0) }; + float m23[4] = { MIX(dovi_coeffs[2*4+0], dovi_coeffs[3*4+0], t2), + MIX(dovi_coeffs[2*4+1], dovi_coeffs[3*4+1], t2), + MIX(dovi_coeffs[2*4+2], dovi_coeffs[3*4+2], t2), + MIX(dovi_coeffs[2*4+3], dovi_coeffs[3*4+3], t2) }; + float m0123[4] = { MIX(m01[0], m23[0], t1), + MIX(m01[1], m23[1], t1), + MIX(m01[2], m23[2], t1), + MIX(m01[3], m23[3], t1) }; + float m45[4] = { MIX(dovi_coeffs[4*4+0], dovi_coeffs[5*4+0], t4), + MIX(dovi_coeffs[4*4+1], dovi_coeffs[5*4+1], t4), + MIX(dovi_coeffs[4*4+2], dovi_coeffs[5*4+2], t4), + MIX(dovi_coeffs[4*4+3], dovi_coeffs[5*4+3], t4) }; + float m67[4] = { MIX(dovi_coeffs[6*4+0], dovi_coeffs[7*4+0], t6), + MIX(dovi_coeffs[6*4+1], dovi_coeffs[7*4+1], t6), + MIX(dovi_coeffs[6*4+2], dovi_coeffs[7*4+2], t6), + MIX(dovi_coeffs[6*4+3], dovi_coeffs[7*4+3], t6) }; + float m4567[4] = { MIX(m45[0], m67[0], t5), + MIX(m45[1], m67[1], t5), + MIX(m45[2], m67[2], t5), + MIX(m45[3], m67[3], t5) }; + + coeffs[0] = MIX(m0123[0], m4567[0], t3); + coeffs[1] = MIX(m0123[1], m4567[1], t3); + coeffs[2] = MIX(m0123[2], m4567[2], t3); + coeffs[3] = MIX(m0123[3], m4567[3], t3); } has_mmr_poly = dovi_has_mmr && dovi_has_poly; @@ -447,16 +448,16 @@ static float bt2390(float s, float peak) // SDR peak float dst_peak = 1.0f; float s_pq = inverse_eotf_st2084(s, REFERENCE_WHITE_ALT) * scale; - float maxLum = inverse_eotf_st2084(dst_peak, REFERENCE_WHITE_ALT) * scale; + float max_lum = inverse_eotf_st2084(dst_peak, REFERENCE_WHITE_ALT) * scale; - float ks = 1.5f * maxLum - 0.5f; + float ks = 1.5f * max_lum - 0.5f; float tb = (s_pq - ks) / (1.0f - ks); float tb2 = tb * tb; float tb3 = tb2 * tb; float pb = (2.0f * tb3 - 3.0f * tb2 + 1.0f) * ks + (tb3 - 2.0f * tb2 + tb) * (1.0f - ks) + - (-2.0f * tb3 + 3.0f * tb2) * maxLum; - float sig = (s_pq < ks) ? s_pq : pb; + (-2.0f * tb3 + 3.0f * tb2) * max_lum; + float sig = MIX(pb, s_pq, s_pq < ks); return eotf_st2084(sig * peak_pq, REFERENCE_WHITE_ALT); } @@ -708,7 +709,6 @@ inline static void tonemap_int16(int16_t r_in, int16_t g_in, int16_t b_in, b_lin = (*rgb2rgb)[2][0] * r_lin + (*rgb2rgb)[2][1] * g_lin + (*rgb2rgb)[2][2] * b_lin; } -#define MIX(x,y,a) ((x) * (1 - (a)) + (y) * (a)) /* desaturate to prevent unnatural colors */ if (desat > 0) { float luma = av_q2d(coeffs->cr) * r_lin + av_q2d(coeffs->cg) * g_lin + av_q2d(coeffs->cb) * b_lin; @@ -721,7 +721,6 @@ inline static void tonemap_int16(int16_t r_in, int16_t g_in, int16_t b_in, r_lin *= mapval; g_lin *= mapval; b_lin *= mapval; -#undef MIX *r_out = delin_lut[av_clip_uintp2(r_lin * 32767 + 0.5, 15)]; *g_out = delin_lut[av_clip_uintp2(g_lin * 32767 + 0.5, 15)]; -- 2.34.1