[FFmpeg-devel,1/3] lavu/videotoolbox: expose conversion routines for color parameters

Message ID 20211218110953.30751-1-rcombs@rcombs.me
State New
Headers
Series [FFmpeg-devel,1/3] lavu/videotoolbox: expose conversion routines for color parameters |

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Ridley Combs Dec. 18, 2021, 11:09 a.m. UTC
Also fixes symbol lookup errors on older macOS when built with a newer SDK,
introduced in 6cab5206b0ad94990c435cb7c5cf3b29675e0231
---
 doc/APIchanges                     |   6 +
 libavutil/hwcontext_videotoolbox.c | 171 +++++++++++++++--------------
 libavutil/hwcontext_videotoolbox.h |  25 +++++
 3 files changed, 119 insertions(+), 83 deletions(-)
  

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index 17aa664ca3..af2fbaafcf 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,12 @@  libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2021-12-xx - xxxxxxxxxx - lavu 57.12.100 - hwcontext_videotoolbox.h
+  Add av_map_videotoolbox_chroma_loc_from_av
+  Add av_map_videotoolbox_color_matrix_from_av
+  Add av_map_videotoolbox_color_primaries_from_av
+  Add av_map_videotoolbox_color_trc_from_av
+
 2021-12-xx - xxxxxxxxxx - lavf 59.10.100 - avformat.h
   Add AVFormatContext io_close2 which returns an int
 
diff --git a/libavutil/hwcontext_videotoolbox.c b/libavutil/hwcontext_videotoolbox.c
index e9567bbf44..4c6d37304c 100644
--- a/libavutil/hwcontext_videotoolbox.c
+++ b/libavutil/hwcontext_videotoolbox.c
@@ -318,7 +318,7 @@  static void vt_unmap(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
     CVPixelBufferUnlockBaseAddress(pixbuf, (uintptr_t)hwmap->priv);
 }
 
-static int vt_pixbuf_set_par(AVHWFramesContext *hwfc,
+static int vt_pixbuf_set_par(void *log_ctx,
                              CVPixelBufferRef pixbuf, const AVFrame *src)
 {
     CFMutableDictionaryRef par = NULL;
@@ -375,31 +375,30 @@  static int vt_pixbuf_set_par(AVHWFramesContext *hwfc,
     return 0;
 }
 
-static int vt_pixbuf_set_chromaloc(AVHWFramesContext *hwfc,
-                                   CVPixelBufferRef pixbuf, const AVFrame *src)
+CFStringRef av_map_videotoolbox_chroma_loc_from_av(enum AVChromaLocation loc)
 {
-    CFStringRef loc = NULL;
-
-    switch (src->chroma_location) {
+    switch (loc) {
     case AVCHROMA_LOC_LEFT:
-        loc = kCVImageBufferChromaLocation_Left;
-        break;
+        return kCVImageBufferChromaLocation_Left;
     case AVCHROMA_LOC_CENTER:
-        loc = kCVImageBufferChromaLocation_Center;
-        break;
+        return kCVImageBufferChromaLocation_Center;
     case AVCHROMA_LOC_TOP:
-        loc = kCVImageBufferChromaLocation_Top;
-        break;
+        return kCVImageBufferChromaLocation_Top;
     case AVCHROMA_LOC_BOTTOM:
-        loc = kCVImageBufferChromaLocation_Bottom;
-        break;
+        return kCVImageBufferChromaLocation_Bottom;
     case AVCHROMA_LOC_TOPLEFT:
-        loc = kCVImageBufferChromaLocation_TopLeft;
-        break;
+        return kCVImageBufferChromaLocation_TopLeft;
     case AVCHROMA_LOC_BOTTOMLEFT:
-        loc = kCVImageBufferChromaLocation_BottomLeft;
-        break;
+        return kCVImageBufferChromaLocation_BottomLeft;
+    default:
+        return NULL;
     }
+}
+
+static int vt_pixbuf_set_chromaloc(void *log_ctx,
+                                   CVPixelBufferRef pixbuf, const AVFrame *src)
+{
+    CFStringRef loc = av_map_videotoolbox_chroma_loc_from_av(src->chroma_location);
 
     if (loc) {
         CVBufferSetAttachment(
@@ -412,109 +411,115 @@  static int vt_pixbuf_set_chromaloc(AVHWFramesContext *hwfc,
     return 0;
 }
 
-static int vt_pixbuf_set_colorspace(AVHWFramesContext *hwfc,
-                                    CVPixelBufferRef pixbuf, const AVFrame *src)
+CFStringRef av_map_videotoolbox_color_matrix_from_av(enum AVColorSpace space)
 {
-    CFStringRef colormatrix = NULL, colorpri = NULL, colortrc = NULL;
-    Float32 gamma = 0;
-
-    switch (src->colorspace) {
+    switch (space) {
     case AVCOL_SPC_BT2020_CL:
     case AVCOL_SPC_BT2020_NCL:
 #if HAVE_KCVIMAGEBUFFERYCBCRMATRIX_ITU_R_2020
-        colormatrix = kCVImageBufferYCbCrMatrix_ITU_R_2020;
-#else
-        colormatrix = CFSTR("ITU_R_2020");
+        if (__builtin_available(macOS 10.11, iOS 9, *))
+            return kCVImageBufferYCbCrMatrix_ITU_R_2020;
 #endif
-        break;
+        return CFSTR("ITU_R_2020");
     case AVCOL_SPC_BT470BG:
     case AVCOL_SPC_SMPTE170M:
-        colormatrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
-        break;
+        return kCVImageBufferYCbCrMatrix_ITU_R_601_4;
     case AVCOL_SPC_BT709:
-        colormatrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
-        break;
+        return kCVImageBufferYCbCrMatrix_ITU_R_709_2;
     case AVCOL_SPC_SMPTE240M:
-        colormatrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
-        break;
+        return kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
     case AVCOL_SPC_UNSPECIFIED:
-        break;
     default:
-        av_log(hwfc, AV_LOG_WARNING, "Color space %s is not supported.\n", av_color_space_name(src->colorspace));
+        return NULL;
     }
+}
 
-    switch (src->color_primaries) {
+CFStringRef av_map_videotoolbox_color_primaries_from_av(enum AVColorPrimaries pri)
+{
+    switch (pri) {
     case AVCOL_PRI_BT2020:
 #if HAVE_KCVIMAGEBUFFERCOLORPRIMARIES_ITU_R_2020
-        colorpri = kCVImageBufferColorPrimaries_ITU_R_2020;
-#else
-        colorpri = CFSTR("ITU_R_2020");
+        if (__builtin_available(macOS 10.11, iOS 9, *))
+            return kCVImageBufferColorPrimaries_ITU_R_2020;
 #endif
-        break;
+        return CFSTR("ITU_R_2020");
     case AVCOL_PRI_BT709:
-        colorpri = kCVImageBufferColorPrimaries_ITU_R_709_2;
-        break;
+        return kCVImageBufferColorPrimaries_ITU_R_709_2;
     case AVCOL_PRI_SMPTE170M:
-        colorpri = kCVImageBufferColorPrimaries_SMPTE_C;
-        break;
+        return kCVImageBufferColorPrimaries_SMPTE_C;
     case AVCOL_PRI_BT470BG:
-        colorpri = kCVImageBufferColorPrimaries_EBU_3213;
-        break;
+        return kCVImageBufferColorPrimaries_EBU_3213;
     case AVCOL_PRI_UNSPECIFIED:
-        break;
     default:
-        av_log(hwfc, AV_LOG_WARNING, "Color primaries %s is not supported.\n", av_color_primaries_name(src->color_primaries));
+        return NULL;
     }
+}
+
+CFStringRef av_map_videotoolbox_color_trc_from_av(enum AVColorTransferCharacteristic trc)
+{
 
-    switch (src->color_trc) {
+    switch (trc) {
     case AVCOL_TRC_SMPTE2084:
 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
-        colortrc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
-#else
-        colortrc = CFSTR("SMPTE_ST_2084_PQ");
+        if (__builtin_available(macOS 10.13, iOS 11, *))
+            return kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
 #endif
-        break;
+        return CFSTR("SMPTE_ST_2084_PQ");
     case AVCOL_TRC_BT2020_10:
     case AVCOL_TRC_BT2020_12:
 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2020
-        colortrc = kCVImageBufferTransferFunction_ITU_R_2020;
-#else
-        colortrc = CFSTR("ITU_R_2020");
+        if (__builtin_available(macOS 10.11, iOS 9, *))
+            return kCVImageBufferTransferFunction_ITU_R_2020;
 #endif
-        break;
+        return CFSTR("ITU_R_2020");
     case AVCOL_TRC_BT709:
-        colortrc = kCVImageBufferTransferFunction_ITU_R_709_2;
-        break;
+        return kCVImageBufferTransferFunction_ITU_R_709_2;
     case AVCOL_TRC_SMPTE240M:
-        colortrc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
-        break;
+        return kCVImageBufferTransferFunction_SMPTE_240M_1995;
     case AVCOL_TRC_SMPTE428:
 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_428_1
-        colortrc = kCVImageBufferTransferFunction_SMPTE_ST_428_1;
-#else
-        colortrc = CFSTR("SMPTE_ST_428_1");
+        if (__builtin_available(macOS 10.12, *, iOS 10))
+            return kCVImageBufferTransferFunction_SMPTE_ST_428_1;
 #endif
-        break;
+        return CFSTR("SMPTE_ST_428_1");
     case AVCOL_TRC_ARIB_STD_B67:
 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
-        colortrc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
-#else
-        colortrc = CFSTR("ITU_R_2100_HLG");
+        if (__builtin_available(macOS 10.13, iOS 11, *))
+            return kCVImageBufferTransferFunction_ITU_R_2100_HLG;
 #endif
-        break;
+        return CFSTR("ITU_R_2100_HLG");
     case AVCOL_TRC_GAMMA22:
-        gamma = 2.2;
-        colortrc = kCVImageBufferTransferFunction_UseGamma;
-        break;
+        return kCVImageBufferTransferFunction_UseGamma;
     case AVCOL_TRC_GAMMA28:
-        gamma = 2.8;
-        colortrc = kCVImageBufferTransferFunction_UseGamma;
-        break;
-    case AVCOL_TRC_UNSPECIFIED:
-        break;
+        return kCVImageBufferTransferFunction_UseGamma;
     default:
-        av_log(hwfc, AV_LOG_WARNING, "Color transfer function %s is not supported.\n", av_color_transfer_name(src->color_trc));
+    case AVCOL_TRC_UNSPECIFIED:
+        return NULL;
     }
+}
+
+static int vt_pixbuf_set_colorspace(void *log_ctx,
+                                    CVPixelBufferRef pixbuf, const AVFrame *src)
+{
+    CFStringRef colormatrix = NULL, colorpri = NULL, colortrc = NULL;
+    Float32 gamma = 0;
+
+    colormatrix = av_map_videotoolbox_color_matrix_from_av(src->colorspace);
+    if (!colormatrix && src->colorspace != AVCOL_SPC_UNSPECIFIED)
+        av_log(log_ctx, AV_LOG_WARNING, "Color space %s is not supported.\n", av_color_space_name(src->colorspace));
+
+    colorpri = av_map_videotoolbox_color_primaries_from_av(src->color_primaries);
+    if (!colorpri && src->color_primaries != AVCOL_PRI_UNSPECIFIED)
+        av_log(log_ctx, AV_LOG_WARNING, "Color primaries %s is not supported.\n", av_color_primaries_name(src->color_primaries));
+
+    colortrc = av_map_videotoolbox_color_trc_from_av(src->color_trc);
+    if (!colortrc && src->color_trc != AVCOL_TRC_UNSPECIFIED)
+        av_log(log_ctx, AV_LOG_WARNING, "Color transfer function %s is not supported.\n", av_color_transfer_name(src->color_trc));
+
+    if (src->color_trc == AVCOL_TRC_GAMMA22)
+        gamma = 2.2;
+    else if (src->color_trc == AVCOL_TRC_GAMMA28)
+        gamma = 2.8;
 
     if (colormatrix) {
         CVBufferSetAttachment(
@@ -550,17 +555,17 @@  static int vt_pixbuf_set_colorspace(AVHWFramesContext *hwfc,
     return 0;
 }
 
-static int vt_pixbuf_set_attachments(AVHWFramesContext *hwfc,
+static int vt_pixbuf_set_attachments(void *log_ctx,
                                      CVPixelBufferRef pixbuf, const AVFrame *src)
 {
     int ret;
-    ret = vt_pixbuf_set_par(hwfc, pixbuf, src);
+    ret = vt_pixbuf_set_par(log_ctx, pixbuf, src);
     if (ret < 0)
         return ret;
-    ret = vt_pixbuf_set_colorspace(hwfc, pixbuf, src);
+    ret = vt_pixbuf_set_colorspace(log_ctx, pixbuf, src);
     if (ret < 0)
         return ret;
-    ret = vt_pixbuf_set_chromaloc(hwfc, pixbuf, src);
+    ret = vt_pixbuf_set_chromaloc(log_ctx, pixbuf, src);
     if (ret < 0)
         return ret;
     return 0;
diff --git a/libavutil/hwcontext_videotoolbox.h b/libavutil/hwcontext_videotoolbox.h
index 62cde07c51..916899e97d 100644
--- a/libavutil/hwcontext_videotoolbox.h
+++ b/libavutil/hwcontext_videotoolbox.h
@@ -60,4 +60,29 @@  uint32_t av_map_videotoolbox_format_from_pixfmt(enum AVPixelFormat pix_fmt);
  */
 uint32_t av_map_videotoolbox_format_from_pixfmt2(enum AVPixelFormat pix_fmt, bool full_range);
 
+/**
+ * Convert an AVChromaLocation to a VideoToolbox/CoreVideo chroma location string.
+ * Returns 0 if no known equivalent was found.
+ */
+CFStringRef av_map_videotoolbox_chroma_loc_from_av(enum AVChromaLocation loc);
+
+/**
+ * Convert an AVColorSpace to a VideoToolbox/CoreVideo color matrix string.
+ * Returns 0 if no known equivalent was found.
+ */
+CFStringRef av_map_videotoolbox_color_matrix_from_av(enum AVColorSpace space);
+
+/**
+ * Convert an AVColorPrimaries to a VideoToolbox/CoreVideo color primaries string.
+ * Returns 0 if no known equivalent was found.
+ */
+CFStringRef av_map_videotoolbox_color_primaries_from_av(enum AVColorPrimaries pri);
+
+/**
+ * Convert an AVColorTransferCharacteristic to a VideoToolbox/CoreVideo color transfer
+ * function string.
+ * Returns 0 if no known equivalent was found.
+ */
+CFStringRef av_map_videotoolbox_color_trc_from_av(enum AVColorTransferCharacteristic trc);
+
 #endif /* AVUTIL_HWCONTEXT_VIDEOTOOLBOX_H */