[FFmpeg-devel] [PATCH] avcodec/libkvazaar: Simplified codec context color settings.

John Mather johnmather at sidefx.com
Fri Sep 29 23:35:59 EEST 2023


Both FFmpeg and libkvazaar follow H.273, so we don't have to utilize a
large LUT.

Signed-off-by: John Mather <johnmather at sidefx.com>
---
 libavcodec/libkvazaar.c | 62 ++++++-----------------------------------
 1 file changed, 8 insertions(+), 54 deletions(-)

diff --git a/libavcodec/libkvazaar.c b/libavcodec/libkvazaar.c
index cd87a9106d..984f78ba65 100644
--- a/libavcodec/libkvazaar.c
+++ b/libavcodec/libkvazaar.c
@@ -56,10 +56,6 @@ static av_cold int libkvazaar_init(AVCodecContext *avctx)
     const kvz_api *const api = ctx->api = kvz_api_get(8);
     kvz_config *cfg = NULL;
     kvz_encoder *enc = NULL;
-    int8_t kvz_fullrange = -1;
-    int8_t kvz_colorprim = -1;
-    int8_t kvz_transfer = -1;
-    int8_t kvz_colormatrix = -1;
 
     /* Kvazaar requires width and height to be multiples of eight. */
     if (avctx->width % 8 || avctx->height % 8) {
@@ -105,56 +101,14 @@ FF_ENABLE_DEPRECATION_WARNINGS
         cfg->rc_algorithm = KVZ_LAMBDA;
     }
 
-    switch (avctx->color_range) {
-        case AVCOL_RANGE_JPEG: kvz_fullrange = 1; break;
-        case AVCOL_RANGE_MPEG: kvz_fullrange = 0; break;
-        default:               kvz_fullrange = 0;
-    }
-    cfg->vui.fullrange = kvz_fullrange;
-
-    switch (avctx->color_primaries) {
-        case AVCOL_PRI_BT709:     kvz_colorprim = 1; break;
-        case AVCOL_PRI_BT470M:    kvz_colorprim = 4; break;
-        case AVCOL_PRI_BT470BG:   kvz_colorprim = 5; break;
-        case AVCOL_PRI_SMPTE170M: kvz_colorprim = 6; break;
-        case AVCOL_PRI_SMPTE240M: kvz_colorprim = 7; break;
-        case AVCOL_PRI_FILM:      kvz_colorprim = 8; break;
-        case AVCOL_PRI_BT2020:    kvz_colorprim = 9; break;
-        default:                  kvz_colorprim = 2;            // undef
-    }
-    cfg->vui.colorprim = kvz_colorprim;
-
-    switch (avctx->color_trc) {
-        case AVCOL_TRC_BT709:        kvz_transfer = 1;  break;
-        case AVCOL_TRC_GAMMA22:      kvz_transfer = 4;  break;  // bt470m
-        case AVCOL_TRC_GAMMA28:      kvz_transfer = 5;  break;  // bt470bg
-        case AVCOL_TRC_SMPTE170M:    kvz_transfer = 6;  break;
-        case AVCOL_TRC_SMPTE240M:    kvz_transfer = 7;  break;
-        case AVCOL_TRC_LINEAR:       kvz_transfer = 8;  break;
-        case AVCOL_TRC_LOG:          kvz_transfer = 9;  break;  // log100
-        case AVCOL_TRC_LOG_SQRT:     kvz_transfer = 10; break;  // log316
-        case AVCOL_TRC_IEC61966_2_4: kvz_transfer = 11; break;
-        case AVCOL_TRC_BT1361_ECG:   kvz_transfer = 12; break;  // bt1361e
-        case AVCOL_TRC_IEC61966_2_1: kvz_transfer = 13; break;
-        case AVCOL_TRC_BT2020_10:    kvz_transfer = 14; break;
-        case AVCOL_TRC_BT2020_12:    kvz_transfer = 15; break;
-        default:                     kvz_transfer = 2;          // undef
-    }
-    cfg->vui.transfer = kvz_transfer;
-
-    switch (avctx->colorspace) {
-        case AVCOL_SPC_RGB:        kvz_colormatrix = 0;  break; // gbr
-        case AVCOL_SPC_BT709:      kvz_colormatrix = 1;  break;
-        case AVCOL_SPC_FCC:        kvz_colormatrix = 5;  break;
-        case AVCOL_SPC_BT470BG:    kvz_colormatrix = 6;  break;
-        case AVCOL_SPC_SMPTE170M:  kvz_colormatrix = 7;  break;
-        case AVCOL_SPC_SMPTE240M:  kvz_colormatrix = 8;  break;
-        case AVCOL_SPC_YCGCO:      kvz_colormatrix = 9;  break;
-        case AVCOL_SPC_BT2020_NCL: kvz_colormatrix = 10; break; // bt2020nc
-        case AVCOL_SPC_BT2020_CL:  kvz_colormatrix = 11; break; // bt2020c
-        default:                   kvz_colormatrix = 2;         // undef
-    }
-    cfg->vui.colormatrix = kvz_colormatrix;
+    if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED)
+        cfg->vui.fullrange = avctx->color_range == AVCOL_RANGE_JPEG;
+    if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED)
+        cfg->vui.colorprim = avctx->color_primaries;
+    if (avctx->color_trc != AVCOL_TRC_UNSPECIFIED)
+        cfg->vui.transfer = avctx->color_trc;
+    if (avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
+        cfg->vui.colormatrix = avctx->colorspace;
 
     if (ctx->kvz_params) {
         AVDictionary *dict = NULL;
-- 
2.39.3



More information about the ffmpeg-devel mailing list