[FFmpeg-devel] [PATCH 1/3] avcodec/videotoolboxenc: use color parameters conversion from avutil

Zhao Zhili quinkblack at foxmail.com
Sun Jul 16 12:13:27 EEST 2023


From: Zhao Zhili <zhilizhao at tencent.com>

---
 libavcodec/videotoolboxenc.c | 137 ++++-------------------------------
 1 file changed, 13 insertions(+), 124 deletions(-)

diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c
index 6114351392..8e493c4f7a 100644
--- a/libavcodec/videotoolboxenc.c
+++ b/libavcodec/videotoolboxenc.c
@@ -1001,132 +1001,20 @@ pbinfo_nomem:
     return AVERROR(ENOMEM);
 }
 
-static int get_cv_color_primaries(AVCodecContext *avctx,
-                                  CFStringRef *primaries)
-{
-    enum AVColorPrimaries pri = avctx->color_primaries;
-    switch (pri) {
-        case AVCOL_PRI_UNSPECIFIED:
-            *primaries = NULL;
-            break;
-
-        case AVCOL_PRI_BT470BG:
-            *primaries = kCVImageBufferColorPrimaries_EBU_3213;
-            break;
-
-        case AVCOL_PRI_SMPTE170M:
-            *primaries = kCVImageBufferColorPrimaries_SMPTE_C;
-            break;
-
-        case AVCOL_PRI_BT709:
-            *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
-            break;
-
-        case AVCOL_PRI_BT2020:
-            *primaries = compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
-            break;
-
-        default:
-            av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not supported.\n", av_color_primaries_name(pri));
-            *primaries = NULL;
-            return -1;
-    }
-
-    return 0;
-}
-
-static int get_cv_transfer_function(AVCodecContext *avctx,
-                                    CFStringRef *transfer_fnc,
-                                    CFNumberRef *gamma_level)
+static int get_cv_gamma(AVCodecContext *avctx,
+                        CFNumberRef *gamma_level)
 {
     enum AVColorTransferCharacteristic trc = avctx->color_trc;
-    Float32 gamma;
+    Float32 gamma = 0;
     *gamma_level = NULL;
 
-    switch (trc) {
-        case AVCOL_TRC_UNSPECIFIED:
-            *transfer_fnc = NULL;
-            break;
-
-        case AVCOL_TRC_BT709:
-            *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
-            break;
-
-        case AVCOL_TRC_SMPTE240M:
-            *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
-            break;
-
-#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
-        case AVCOL_TRC_SMPTE2084:
-            *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
-            break;
-#endif
-#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
-        case AVCOL_TRC_LINEAR:
-            *transfer_fnc = kCVImageBufferTransferFunction_Linear;
-            break;
-#endif
-#if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
-        case AVCOL_TRC_ARIB_STD_B67:
-            *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
-            break;
-#endif
-
-        case AVCOL_TRC_GAMMA22:
-            gamma = 2.2;
-            *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
-            *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
-            break;
-
-        case AVCOL_TRC_GAMMA28:
-            gamma = 2.8;
-            *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
-            *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
-            break;
-
-        case AVCOL_TRC_BT2020_10:
-        case AVCOL_TRC_BT2020_12:
-            *transfer_fnc = compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
-            break;
-
-        default:
-            *transfer_fnc = NULL;
-            av_log(avctx, AV_LOG_ERROR, "Transfer function %s is not supported.\n", av_color_transfer_name(trc));
-            return -1;
-    }
-
-    return 0;
-}
-
-static int get_cv_ycbcr_matrix(AVCodecContext *avctx, CFStringRef *matrix) {
-    switch(avctx->colorspace) {
-        case AVCOL_SPC_BT709:
-            *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
-            break;
-
-        case AVCOL_SPC_UNSPECIFIED:
-        case AVCOL_SPC_RGB:
-            *matrix = NULL;
-            break;
-
-        case AVCOL_SPC_BT470BG:
-        case AVCOL_SPC_SMPTE170M:
-            *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
-            break;
-
-        case AVCOL_SPC_SMPTE240M:
-            *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
-            break;
-
-        case AVCOL_SPC_BT2020_NCL:
-            *matrix = compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
-            break;
-
-        default:
-            av_log(avctx, AV_LOG_ERROR, "Color space %s is not supported.\n", av_color_space_name(avctx->colorspace));
-            return -1;
-    }
+    if (trc == AVCOL_TRC_GAMMA22)
+        gamma = 2.2;
+    else if (trc == AVCOL_TRC_GAMMA28)
+        gamma = 2.8;
 
+    if (gamma != 0)
+        *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
     return 0;
 }
 
@@ -1694,9 +1582,10 @@ static int vtenc_configure_encoder(AVCodecContext *avctx)
 
     vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
 
-    get_cv_transfer_function(avctx, &vtctx->transfer_function, &gamma_level);
-    get_cv_ycbcr_matrix(avctx, &vtctx->ycbcr_matrix);
-    get_cv_color_primaries(avctx, &vtctx->color_primaries);
+    get_cv_gamma(avctx, &gamma_level);
+    vtctx->transfer_function = av_map_videotoolbox_color_trc_from_av(avctx->color_trc);
+    vtctx->ycbcr_matrix = av_map_videotoolbox_color_matrix_from_av(avctx->colorspace);
+    vtctx->color_primaries = av_map_videotoolbox_color_primaries_from_av(avctx->color_primaries);
 
 
     if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
-- 
2.40.1



More information about the ffmpeg-devel mailing list