diff mbox series

[FFmpeg-devel,v7,1/2] avcodec/libjxldec: properly tag output colorspace

Message ID 20220512153428.251357-2-leo.izen@gmail.com
State New
Headers show
Series JPEG XL Colorspace Fixes | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Leo Izen May 12, 2022, 3:34 p.m. UTC
Whether an ICC profile is present or not, the decoder
should now properly tag the colorspace of pixel data
received by the decoder.
---
 libavcodec/libjxldec.c | 128 +++++++++++++++++++++++++++++++++++------
 1 file changed, 109 insertions(+), 19 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/libjxldec.c b/libavcodec/libjxldec.c
index cd4bca3343..651ac56a36 100644
--- a/libavcodec/libjxldec.c
+++ b/libavcodec/libjxldec.c
@@ -47,7 +47,6 @@  typedef struct LibJxlDecodeContext {
     JxlBasicInfo basic_info;
     JxlPixelFormat jxl_pixfmt;
     JxlDecoderStatus events;
-    AVBufferRef *iccp;
 } LibJxlDecodeContext;
 
 static int libjxl_init_jxl_decoder(AVCodecContext *avctx)
@@ -129,12 +128,84 @@  static enum AVPixelFormat libjxl_get_pix_fmt(AVCodecContext *avctx, JxlBasicInfo
     return AV_PIX_FMT_NONE;
 }
 
+static enum AVColorPrimaries libjxl_get_primaries(void *avctx, JxlColorEncoding *encoding)
+{
+    if (encoding->white_point != JXL_WHITE_POINT_D65 &&
+        (encoding->primaries != JXL_PRIMARIES_P3 || encoding->white_point != JXL_WHITE_POINT_DCI)) {
+        av_log(avctx, AV_LOG_WARNING, "Unsupported JXL white point: %d\n", encoding->white_point);
+        return AVCOL_PRI_UNSPECIFIED;
+    }
+    switch (encoding->primaries) {
+    case JXL_PRIMARIES_SRGB:
+        /* BT709 and sRGB use the same primaries */
+        return AVCOL_PRI_BT709;
+    case JXL_PRIMARIES_2100:
+        /* BT2020 and BT2100 use the same primaries */
+        return AVCOL_PRI_BT2020;
+    case JXL_PRIMARIES_P3:
+        /* DCI P3 uses DCI, Display P3 uses D65 */
+        if (encoding->white_point == JXL_WHITE_POINT_DCI)
+            return AVCOL_PRI_SMPTE431;
+        return AVCOL_PRI_SMPTE432;
+    case JXL_PRIMARIES_CUSTOM:
+        av_log(avctx, AV_LOG_WARNING, "Custom primaries are unsupported\n");
+        return AVCOL_PRI_UNSPECIFIED;
+    }
+
+    av_log(avctx, AV_LOG_WARNING, "Unknown JXL color primaries: %d\n", encoding->primaries);
+
+    return AVCOL_PRI_UNSPECIFIED;
+}
+
+static enum AVColorTransferCharacteristic libjxl_get_trc(void *avctx, JxlColorEncoding *encoding)
+{
+    switch (encoding->transfer_function) {
+    case JXL_TRANSFER_FUNCTION_709:
+        return AVCOL_TRC_BT709;
+    case JXL_TRANSFER_FUNCTION_LINEAR:
+        return AVCOL_TRC_LINEAR;
+    case JXL_TRANSFER_FUNCTION_SRGB:
+        return AVCOL_TRC_IEC61966_2_1;
+    case JXL_TRANSFER_FUNCTION_PQ:
+        return AVCOL_TRC_SMPTE2084;
+    case JXL_TRANSFER_FUNCTION_DCI:
+        return AVCOL_TRC_SMPTE428;
+    case JXL_TRANSFER_FUNCTION_HLG:
+        return AVCOL_TRC_ARIB_STD_B67;
+    case JXL_TRANSFER_FUNCTION_GAMMA:
+        if (encoding->gamma > 2.199 && encoding->gamma < 2.201)
+            return AVCOL_TRC_GAMMA22;
+        if (encoding->gamma > 2.799 && encoding->gamma < 2.801)
+            return AVCOL_TRC_GAMMA28;
+        av_log(avctx, AV_LOG_WARNING, "Unsupported gamma transfer: %f\n", encoding->gamma);
+        return AVCOL_TRC_UNSPECIFIED;
+    }
+
+    av_log(avctx, AV_LOG_WARNING, "Unknown transfer function: %d\n", encoding->transfer_function);
+
+    return AVCOL_TRC_UNSPECIFIED;
+}
+
+static int libjxl_populate_encoding(AVCodecContext *avctx, JxlColorEncoding *encoding)
+{
+    if (avctx->colorspace == AVCOL_SPC_RGB)
+        avctx->color_primaries = libjxl_get_primaries(avctx, encoding);
+    avctx->color_trc = libjxl_get_trc(avctx, encoding);
+
+    if (avctx->color_trc == AVCOL_TRC_UNSPECIFIED ||
+        avctx->colorspace == AVCOL_SPC_RGB && avctx->color_primaries == AVCOL_PRI_UNSPECIFIED)
+        return -1;
+
+    return 0;
+}
+
 static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
 {
     LibJxlDecodeContext *ctx = avctx->priv_data;
     uint8_t *buf = avpkt->data;
-    size_t remaining = avpkt->size, iccp_len;
+    size_t remaining = avpkt->size;
     JxlDecoderStatus jret;
+    JxlColorEncoding jxl_encoding;
     int ret;
     *got_frame = 0;
 
@@ -189,16 +260,43 @@  static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
             continue;
         case JXL_DEC_COLOR_ENCODING:
             av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n");
-            jret = JxlDecoderGetICCProfileSize(ctx->decoder, &ctx->jxl_pixfmt, JXL_COLOR_PROFILE_TARGET_ORIGINAL, &iccp_len);
-            if (jret == JXL_DEC_SUCCESS && iccp_len > 0) {
-                av_buffer_unref(&ctx->iccp);
-                ctx->iccp = av_buffer_alloc(iccp_len);
-                if (!ctx->iccp)
-                    return AVERROR(ENOMEM);
-                jret = JxlDecoderGetColorAsICCProfile(ctx->decoder, &ctx->jxl_pixfmt, JXL_COLOR_PROFILE_TARGET_ORIGINAL, ctx->iccp->data, iccp_len);
-                if (jret != JXL_DEC_SUCCESS)
-                    av_buffer_unref(&ctx->iccp);
+            avctx->color_range = frame->color_range = AVCOL_RANGE_JPEG;
+            if (ctx->jxl_pixfmt.num_channels >= 3)
+                avctx->colorspace = AVCOL_SPC_RGB;
+            else
+                avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
+            jret = JxlDecoderGetColorAsEncodedProfile(ctx->decoder, NULL, JXL_COLOR_PROFILE_TARGET_ORIGINAL, &jxl_encoding);
+            if (jret == JXL_DEC_SUCCESS)
+                jret = JxlDecoderSetPreferredColorProfile(ctx->decoder, &jxl_encoding);
+            if (jret == JXL_DEC_SUCCESS)
+                jret = JxlDecoderGetColorAsEncodedProfile(ctx->decoder, &ctx->jxl_pixfmt, JXL_COLOR_PROFILE_TARGET_DATA, &jxl_encoding);
+            if (jret == JXL_DEC_SUCCESS)
+                ret = libjxl_populate_encoding(avctx, &jxl_encoding);
+            else
+                ret = -1;
+
+            /* fall back on HDR gamut if enum values fail */
+            if (ret < 0) {
+                av_log(avctx, AV_LOG_WARNING, "Falling back on HDR gamut\n");
+                jxl_encoding.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
+                jxl_encoding.white_point = JXL_WHITE_POINT_D65;
+                if  (avctx->colorspace == AVCOL_SPC_RGB)
+                    jxl_encoding.primaries = JXL_PRIMARIES_2100;
+                if (ctx->jxl_pixfmt.data_type == JXL_TYPE_FLOAT || ctx->jxl_pixfmt.data_type == JXL_TYPE_FLOAT16)
+                    jxl_encoding.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
+                else
+                    jxl_encoding.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
+                /* this shouldn't fail, barring libjxl decoder bug */
+                jret = JxlDecoderSetPreferredColorProfile(ctx->decoder, &jxl_encoding);
+                if (jret == JXL_DEC_SUCCESS)
+                    ret = libjxl_populate_encoding(avctx, &jxl_encoding);
             }
+            if (ret < 0)
+                av_log(avctx, AV_LOG_WARNING, "Unable to set default colorspace values\n");
+
+            frame->color_trc = avctx->color_trc;
+            frame->color_primaries = avctx->color_primaries;
+            frame->colorspace = avctx->colorspace;
             continue;
         case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
             av_log(avctx, AV_LOG_DEBUG, "NEED_IMAGE_OUT_BUFFER event emitted\n");
@@ -216,13 +314,6 @@  static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
             av_log(avctx, AV_LOG_DEBUG, "FULL_IMAGE event emitted\n");
             frame->pict_type = AV_PICTURE_TYPE_I;
             frame->key_frame = 1;
-            if (ctx->iccp) {
-                AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_ICC_PROFILE, ctx->iccp);
-                if (!sd)
-                    return AVERROR(ENOMEM);
-                /* ownership is transfered, and it is not ref-ed */
-                ctx->iccp = NULL;
-            }
             *got_frame = 1;
             return avpkt->size - remaining;
         case JXL_DEC_SUCCESS:
@@ -260,7 +351,6 @@  static av_cold int libjxl_decode_close(AVCodecContext *avctx)
     if (ctx->decoder)
         JxlDecoderDestroy(ctx->decoder);
     ctx->decoder = NULL;
-    av_buffer_unref(&ctx->iccp);
 
     return 0;
 }