[FFmpeg-devel] [PATCH 06/19] swscale: add new frame testing API

Niklas Haas ffmpeg at haasn.xyz
Fri Oct 11 01:26:53 EEST 2024


From: Niklas Haas <git at haasn.dev>

Replacing the old sws_isSupported* API with a more consistent family
of functions that follows the same signature and naming convention,
including a placeholder for testing the color space parameters that
we don't currently implement conversions for.

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <git at haasn.dev>
---
 libswscale/swscale.h | 54 ++++++++++++++++++++++++++++++++++++++++++++
 libswscale/utils.c   | 46 +++++++++++++++++++++++++++++++++++++
 libswscale/utils.h   |  2 ++
 3 files changed, 102 insertions(+)

diff --git a/libswscale/swscale.h b/libswscale/swscale.h
index f9fd340240..f36efa4183 100644
--- a/libswscale/swscale.h
+++ b/libswscale/swscale.h
@@ -85,6 +85,60 @@ SwsContext *sws_alloc_context(void);
  */
 void sws_free_context(SwsContext **ctx);
 
+/***************************
+ * Supported frame formats *
+ ***************************/
+
+/**
+ * Test if a given pixel format is supported.
+ *
+ * @param output  If 0, test if compatible with the source/input frame;
+ *                otherwise, with the destination/output frame.
+ * @param format  The format to check.
+ *
+ * @return A positive integer if supported, 0 otherwise.
+ */
+int sws_test_format(enum AVPixelFormat format, int output);
+
+/**
+ * Test if a given color space is supported.
+ *
+ * @param output  If 0, test if compatible with the source/input frame;
+ *                otherwise, with the destination/output frame.
+ * @param colorspace The colorspace to check.
+ *
+ * @return A positive integer if supported, 0 otherwise.
+ */
+int sws_test_colorspace(enum AVColorSpace colorspace, int output);
+
+/**
+ * Test if a given set of color primaries is supported.
+ *
+ * @param output  If 0, test if compatible with the source/input frame;
+ *                otherwise, with the destination/output frame.
+ * @param primaries The color primaries to check.
+ *
+ * @return A positive integer if supported, 0 otherwise.
+ */
+int sws_test_primaries(enum AVColorPrimaries primaries, int output);
+
+/**
+ * Test if a given color transfer function is supported.
+ *
+ * @param output  If 0, test if compatible with the source/input frame;
+ *                otherwise, with the destination/output frame.
+ * @param trc     The color transfer function to check.
+ *
+ * @return A positive integer if supported, 0 otherwise.
+ */
+int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output);
+
+/**
+ * Helper function to run all sws_test_* against a frame. Ignores irrelevant
+ * properties, for example AVColorSpace is not checked for RGB frames.
+ */
+int sws_test_frame(const AVFrame *frame, int output);
+
 /* values for the flags, the stuff on the command line is different */
 #define SWS_FAST_BILINEAR     1
 #define SWS_BILINEAR          2
diff --git a/libswscale/utils.c b/libswscale/utils.c
index 074be65410..22411d3429 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -2712,3 +2712,49 @@ SwsFormat ff_fmt_from_frame(const AVFrame *frame)
 
     return fmt;
 }
+
+int sws_test_format(enum AVPixelFormat format, int output)
+{
+    return output ? sws_isSupportedOutput(format) : sws_isSupportedInput(format);
+}
+
+int sws_test_colorspace(enum AVColorSpace csp, int output)
+{
+    switch (csp) {
+    case AVCOL_SPC_UNSPECIFIED:
+    case AVCOL_SPC_RGB:
+    case AVCOL_SPC_BT709:
+    case AVCOL_SPC_BT470BG:
+    case AVCOL_SPC_SMPTE170M:
+    case AVCOL_SPC_FCC:
+    case AVCOL_SPC_SMPTE240M:
+    case AVCOL_SPC_BT2020_NCL:
+        return 1;
+    default:
+        return 0;
+    }
+}
+
+int sws_test_primaries(enum AVColorPrimaries prim, int output)
+{
+    return 1; /* TODO: implement once primaries conversions are supported */
+}
+
+int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output)
+{
+    return 1; /* TODO: implement once gamma conversions are supported */
+}
+
+int ff_test_fmt(const SwsFormat *fmt, int output)
+{
+    return sws_test_format    (fmt->format, output) &&
+           sws_test_colorspace(fmt->csp,    output) &&
+           sws_test_primaries (fmt->prim,   output) &&
+           sws_test_transfer  (fmt->trc,    output);
+}
+
+int sws_test_frame(const AVFrame *frame, int output)
+{
+    const SwsFormat fmt = ff_fmt_from_frame(frame);
+    return ff_test_fmt(&fmt, output);
+}
diff --git a/libswscale/utils.h b/libswscale/utils.h
index 3d0b08ffe1..6678364abb 100644
--- a/libswscale/utils.h
+++ b/libswscale/utils.h
@@ -67,4 +67,6 @@ static inline int ff_fmt_align(enum AVPixelFormat fmt)
     }
 }
 
+int ff_test_fmt(const SwsFormat *fmt, int output);
+
 #endif /* SWSCALE_UTILS_H */
-- 
2.46.1



More information about the ffmpeg-devel mailing list