[FFmpeg-devel] [PATCH 2/5] lavfi: add ff_detect_color_primaries helper

Niklas Haas ffmpeg at haasn.xyz
Mon Apr 11 18:36:51 EEST 2022


From: Niklas Haas <git at haasn.dev>

Related to #9673, this helper exists to facilitate "guessing" the right
primary tags from a given set of raw primary coefficients.

The cutoff value of 0.001 was chosen by experimentation. The smallest
"false negative" delta observed in practice was 0.023329, while the
largest "false positive" delta was 0.00016. So, a value of 0.001 sits
comfortably in the middle.

Signed-off-by: Niklas Haas <git at haasn.dev>
---
 libavfilter/colorspace.c | 25 +++++++++++++++++++++++++
 libavfilter/colorspace.h |  3 +++
 2 files changed, 28 insertions(+)

diff --git a/libavfilter/colorspace.c b/libavfilter/colorspace.c
index 25e99f4759..8d7b882375 100644
--- a/libavfilter/colorspace.c
+++ b/libavfilter/colorspace.c
@@ -170,6 +170,31 @@ const struct ColorPrimaries *ff_get_color_primaries(enum AVColorPrimaries prm)
     return p;
 }
 
+enum AVColorPrimaries ff_detect_color_primaries(const struct ColorPrimaries *prm)
+{
+    double delta;
+
+    for (enum AVColorPrimaries p = 0; p < AVCOL_PRI_NB; p++) {
+        const struct ColorPrimaries *ref = &color_primaries[p];
+        if (!ref->prim.xr)
+            continue;
+
+        delta = fabs(prm->prim.xr - ref->prim.xr) +
+                fabs(prm->prim.yr - ref->prim.yr) +
+                fabs(prm->prim.yg - ref->prim.yg) +
+                fabs(prm->prim.yg - ref->prim.yg) +
+                fabs(prm->prim.yb - ref->prim.yb) +
+                fabs(prm->prim.yb - ref->prim.yb) +
+                fabs(prm->wp.xw - ref->wp.xw) +
+                fabs(prm->wp.yw - ref->wp.yw);
+
+        if (delta < 0.001)
+            return p;
+    }
+
+    return AVCOL_PRI_UNSPECIFIED;
+}
+
 void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs,
                            double rgb2yuv[3][3])
 {
diff --git a/libavfilter/colorspace.h b/libavfilter/colorspace.h
index fc415fed05..6959133a49 100644
--- a/libavfilter/colorspace.h
+++ b/libavfilter/colorspace.h
@@ -49,6 +49,9 @@ void ff_fill_rgb2xyz_table(const struct PrimaryCoefficients *coeffs,
                            const struct WhitepointCoefficients *wp,
                            double rgb2xyz[3][3]);
 
+/* Returns AVCOL_PRI_UNSPECIFIED if no clear match can be identified */
+enum AVColorPrimaries ff_detect_color_primaries(const struct ColorPrimaries *prm);
+
 const struct ColorPrimaries *ff_get_color_primaries(enum AVColorPrimaries prm);
 const struct LumaCoefficients *ff_get_luma_coefficients(enum AVColorSpace csp);
 void ff_fill_rgb2yuv_table(const struct LumaCoefficients *coeffs,
-- 
2.35.1



More information about the ffmpeg-devel mailing list