Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v6 0/2] JPEG XL Colorspace Fixes
@ 2022-05-09 13:43 Leo Izen
  2022-05-09 13:43 ` [FFmpeg-devel] [PATCH v6 1/2] avcodec/libjxldec: properly tag output colorspace Leo Izen
  2022-05-09 13:43 ` [FFmpeg-devel] [PATCH v6 2/2] avcodec/libjxlenc: properly read input colorspace Leo Izen
  0 siblings, 2 replies; 3+ messages in thread
From: Leo Izen @ 2022-05-09 13:43 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Leo Izen

At this point I no longer consider this patchset to be "WIP" so feel free
to take a look without another version arriving unless I receive some
requested changes.

Changes:
v6:
- some cosmetic changes
- check the AVFrame metadata if AVCodecContext's is missing
v5:
- fix attaching ICCP even if an enum can
  accurately describe the colorspace
v4:
- fix a typo. bluh
v3:
- some cosmetic changes
- properly handle P3 spaces
- properly handle PQ transfer
v2:
- avoid misusing AVMasteringDisplayMetadata
- ignore custom primaries in header (very rare without iccp)

Leo Izen (2):
  avcodec/libjxldec: properly tag output colorspace
  avcodec/libjxlenc: properly read input colorspace

 libavcodec/libjxldec.c | 113 ++++++++++++++++++++++++++++++++++---
 libavcodec/libjxlenc.c | 124 +++++++++++++++++++++++++++++++----------
 2 files changed, 199 insertions(+), 38 deletions(-)

-- 
2.36.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [FFmpeg-devel] [PATCH v6 1/2] avcodec/libjxldec: properly tag output colorspace
  2022-05-09 13:43 [FFmpeg-devel] [PATCH v6 0/2] JPEG XL Colorspace Fixes Leo Izen
@ 2022-05-09 13:43 ` Leo Izen
  2022-05-09 13:43 ` [FFmpeg-devel] [PATCH v6 2/2] avcodec/libjxlenc: properly read input colorspace Leo Izen
  1 sibling, 0 replies; 3+ messages in thread
From: Leo Izen @ 2022-05-09 13:43 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Leo Izen

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 | 113 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 105 insertions(+), 8 deletions(-)

diff --git a/libavcodec/libjxldec.c b/libavcodec/libjxldec.c
index cd4bca3343..813ee279fe 100644
--- a/libavcodec/libjxldec.c
+++ b/libavcodec/libjxldec.c
@@ -135,6 +135,7 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
     uint8_t *buf = avpkt->data;
     size_t remaining = avpkt->size, iccp_len;
     JxlDecoderStatus jret;
+    JxlColorEncoding jxl_encoding;
     int ret;
     *got_frame = 0;
 
@@ -189,16 +190,112 @@ 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)
+            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);
+
+            avctx->color_range = frame->color_range = AVCOL_RANGE_JPEG;
+
+            if (jret == JXL_DEC_SUCCESS) {
+                if (ctx->jxl_pixfmt.num_channels >= 3) {
+                    avctx->colorspace = AVCOL_SPC_RGB;
+                    switch (jxl_encoding.primaries) {
+                    case JXL_PRIMARIES_SRGB:
+                        avctx->color_primaries = AVCOL_PRI_BT709;
+                        break;
+                    case JXL_PRIMARIES_2100:
+                        /* BT2020 and BT2100 use the same primaries */
+                        avctx->color_primaries = AVCOL_PRI_BT2020;
+                        break;
+                    case JXL_PRIMARIES_P3:
+                        /* DCI P3 uses DCI, Display P3 uses D65 */
+                        if (jxl_encoding.white_point == JXL_WHITE_POINT_DCI) {
+                            avctx->color_primaries = AVCOL_PRI_SMPTE431;
+                        } else if (jxl_encoding.white_point == JXL_WHITE_POINT_D65) {
+                            avctx->color_primaries = AVCOL_PRI_SMPTE432;
+                        } else {
+                            av_log(avctx, AV_LOG_WARNING, "Unsupported JXL white point: %d\n", jxl_encoding.white_point);
+                            jret = JXL_DEC_ERROR;
+                        }
+                        break;
+                    case JXL_PRIMARIES_CUSTOM:
+                        av_log(avctx, AV_LOG_WARNING, "Custom primaries are unsupported without an ICC profile\n");
+                        jret = JXL_DEC_ERROR;
+                        break;
+                    default:
+                        av_log(avctx, AV_LOG_WARNING, "Unknown JXL color primaries: %d\n", jxl_encoding.primaries);
+                        jret = JXL_DEC_ERROR;
+                    }
+                }
+
+                switch (jxl_encoding.transfer_function) {
+                case JXL_TRANSFER_FUNCTION_709:
+                    avctx->color_trc = AVCOL_TRC_BT709;
+                    break;
+                case JXL_TRANSFER_FUNCTION_LINEAR:
+                    avctx->color_trc = AVCOL_TRC_LINEAR;
+                    break;
+                case JXL_TRANSFER_FUNCTION_SRGB:
+                    avctx->color_trc = AVCOL_TRC_IEC61966_2_1;
+                    break;
+                case JXL_TRANSFER_FUNCTION_PQ:
+                    avctx->color_trc = AVCOL_TRC_SMPTE2084;
+                    break;
+                case JXL_TRANSFER_FUNCTION_DCI:
+                    avctx->color_trc = AVCOL_TRC_SMPTE428;
+                    break;
+                case JXL_TRANSFER_FUNCTION_HLG:
+                    avctx->color_trc = AVCOL_TRC_ARIB_STD_B67;
+                    break;
+                case JXL_TRANSFER_FUNCTION_GAMMA:
+                    if (jxl_encoding.gamma == 2.2) {
+                        avctx->color_trc = AVCOL_TRC_GAMMA22;
+                    } else if (jxl_encoding.gamma == 2.8) {
+                        avctx->color_trc = AVCOL_TRC_GAMMA28;
+                    } else {
+                        av_log(avctx, AV_LOG_WARNING, "Unsupported gamma transfer: %f\n", jxl_encoding.gamma);
+                        jret = JXL_DEC_ERROR;
+                    }
+                    break;
+                default:
+                    av_log(avctx, AV_LOG_WARNING, "Unknown transfer function: %d\n", jxl_encoding.transfer_function);
+                    jret = JXL_DEC_ERROR;
+                }
+            }
+
+            /* fall back on ICCP if enum values fail */
+            if (jret != JXL_DEC_SUCCESS) {
+                jret = JxlDecoderGetICCProfileSize(ctx->decoder, &ctx->jxl_pixfmt, JXL_COLOR_PROFILE_TARGET_DATA, &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_DATA, ctx->iccp->data, iccp_len);
+                    if (jret != JXL_DEC_SUCCESS) {
+                        av_log(avctx, AV_LOG_WARNING, "Unable to obtain ICCP from header\n");
+                        av_buffer_unref(&ctx->iccp);
+                    }
+                }
             }
+
+            if (ctx->iccp) {
+                /* if the ICCP is present, libjxl outputs sRGB */
+                if (ctx->jxl_pixfmt.num_channels >= 3) {
+                    avctx->colorspace = AVCOL_SPC_RGB;
+                    avctx->color_primaries = AVCOL_PRI_BT709;
+                }
+                /* linear sRGB if float values, standard sRGB if int values */
+                avctx->color_trc = ctx->jxl_pixfmt.data_type == JXL_TYPE_FLOAT
+                                    || ctx->jxl_pixfmt.data_type == JXL_TYPE_FLOAT16
+                                    ? AVCOL_TRC_LINEAR : AVCOL_TRC_IEC61966_2_1;
+            }
+
+            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");
-- 
2.36.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [FFmpeg-devel] [PATCH v6 2/2] avcodec/libjxlenc: properly read input colorspace
  2022-05-09 13:43 [FFmpeg-devel] [PATCH v6 0/2] JPEG XL Colorspace Fixes Leo Izen
  2022-05-09 13:43 ` [FFmpeg-devel] [PATCH v6 1/2] avcodec/libjxldec: properly tag output colorspace Leo Izen
@ 2022-05-09 13:43 ` Leo Izen
  1 sibling, 0 replies; 3+ messages in thread
From: Leo Izen @ 2022-05-09 13:43 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Leo Izen

Whether an ICC profile is present or not, the libjxl
encoder wrapper should now properly read colorspace tags
and forward them to libjxl appropriately, rather than just
assume sRGB as before. It will also print warnings when
colorimetric assumptions are made about the input data.
---
 libavcodec/libjxlenc.c | 124 +++++++++++++++++++++++++++++++----------
 1 file changed, 94 insertions(+), 30 deletions(-)

diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index 8bebec6aeb..4c7c838018 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -117,7 +117,7 @@ static int libjxl_init_jxl_encoder(AVCodecContext *avctx)
         return AVERROR_EXTERNAL;
     }
 
-    /* check for negative zero, our default */
+    /* check for negative, our default */
     if (ctx->distance < 0.0) {
         /* use ffmpeg.c -q option if passed */
         if (avctx->flags & AV_CODEC_FLAG_QSCALE)
@@ -133,7 +133,7 @@ static int libjxl_init_jxl_encoder(AVCodecContext *avctx)
      */
     if (ctx->distance > 0.0 && ctx->distance < 0.01)
         ctx->distance = 0.01;
-    if (JxlEncoderOptionsSetDistance(ctx->options, ctx->distance) != JXL_ENC_SUCCESS) {
+    if (JxlEncoderSetFrameDistance(ctx->options, ctx->distance) != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to set distance: %f\n", ctx->distance);
         return AVERROR_EXTERNAL;
     }
@@ -223,49 +223,113 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
         info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
         info.alpha_exponent_bits = info.alpha_bits ? info.exponent_bits_per_sample : 0;
         jxl_fmt.data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : JXL_TYPE_FLOAT16;
-        JxlColorEncodingSetToLinearSRGB(&jxl_color, info.num_color_channels == 1);
     } else {
         info.exponent_bits_per_sample = 0;
         info.alpha_exponent_bits = 0;
         jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;
-        JxlColorEncodingSetToSRGB(&jxl_color, info.num_color_channels == 1);
     }
 
-    if (info.bits_per_sample > 16
-        || info.xsize > (1 << 18) || info.ysize > (1 << 18)
-        || (info.xsize << 4) * (info.ysize << 4) > (1 << 20)) {
-        /*
-         * must upgrade codestream to level 10, from level 5
-         * the encoder will not do this automatically
-         */
-        if (JxlEncoderSetCodestreamLevel(ctx->encoder, 10) != JXL_ENC_SUCCESS) {
-            av_log(avctx, AV_LOG_ERROR, "Could not upgrade JXL Codestream level.\n");
-            return AVERROR_EXTERNAL;
-        }
-    }
+    /* JPEG XL format itself does not support partial range */
+    if (avctx->color_range == AVCOL_RANGE_MPEG ||
+        avctx->color_range == AVCOL_RANGE_UNSPECIFIED && frame->color_range == AVCOL_RANGE_MPEG)
+        av_log(avctx, AV_LOG_ERROR, "This encoder does not support partial(tv) range, colors will be wrong!\n");
+    else if (avctx->color_range != AVCOL_RANGE_JPEG && frame->color_range != AVCOL_RANGE_JPEG)
+        av_log(avctx, AV_LOG_WARNING, "Unknown color range, assuming full\n");
 
     /* bitexact lossless requires there to be no XYB transform */
     info.uses_original_profile = ctx->distance == 0.0;
 
-    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
-    if (sd && sd->size && JxlEncoderSetICCProfile(ctx->encoder, sd->data, sd->size) != JXL_ENC_SUCCESS) {
-        av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
-    } else if (info.uses_original_profile) {
-        /*
-        * the color encoding is not used if uses_original_profile is false
-        * this just works around a bug in libjxl 0.7.0 and lower
-        */
-        if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS) {
-            av_log(avctx, AV_LOG_ERROR, "Failed to set JxlColorEncoding\n");
-            return AVERROR_EXTERNAL;
-        }
-    }
-
     if (JxlEncoderSetBasicInfo(ctx->encoder, &info) != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to set JxlBasicInfo\n");
         return AVERROR_EXTERNAL;
     }
 
+    /* rendering intent doesn't matter here
+     * but libjxl will whine if we don't set it */
+    jxl_color.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
+
+    switch (avctx->color_trc && avctx->color_trc != AVCOL_TRC_UNSPECIFIED
+            ? avctx->color_trc : frame->color_trc) {
+    case AVCOL_TRC_BT709:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_709;
+        break;
+    case AVCOL_TRC_LINEAR:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
+        break;
+    case AVCOL_TRC_IEC61966_2_1:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
+        break;
+    case AVCOL_TRC_SMPTE428:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_DCI;
+        break;
+    case AVCOL_TRC_SMPTE2084:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_PQ;
+        break;
+    case AVCOL_TRC_ARIB_STD_B67:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_HLG;
+        break;
+    case AVCOL_TRC_GAMMA22:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
+        jxl_color.gamma = 2.2;
+        break;
+    case AVCOL_TRC_GAMMA28:
+        jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
+        jxl_color.gamma = 2.8;
+        break;
+    default:
+        if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
+            av_log(avctx, AV_LOG_WARNING, "Unknown transfer function, assuming Linear Light\n");
+            jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
+        } else {
+            av_log(avctx, AV_LOG_WARNING, "Unknown transfer function, assuming IEC61966-2-1/sRGB\n");
+            jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
+        }
+    }
+
+    /* This should be implied to be honest
+    * but a libjxl but makes it fail otherwise */
+    if (info.num_color_channels == 1)
+        jxl_color.color_space = JXL_COLOR_SPACE_GRAY;
+    else
+        jxl_color.color_space = JXL_COLOR_SPACE_RGB;
+
+    switch (avctx->color_primaries && avctx->color_primaries != AVCOL_PRI_UNSPECIFIED
+            ? avctx->color_primaries : frame->color_primaries) {
+    case AVCOL_PRI_BT709:
+        jxl_color.primaries = JXL_PRIMARIES_SRGB;
+        jxl_color.white_point = JXL_WHITE_POINT_D65;
+        break;
+    case AVCOL_PRI_BT2020:
+        jxl_color.primaries = JXL_PRIMARIES_2100;
+        jxl_color.white_point = JXL_WHITE_POINT_D65;
+        break;
+    case AVCOL_PRI_SMPTE431:
+        jxl_color.primaries = JXL_PRIMARIES_P3;
+        jxl_color.primaries = JXL_WHITE_POINT_DCI;
+        break;
+    case AVCOL_PRI_SMPTE432:
+        jxl_color.primaries = JXL_PRIMARIES_P3;
+        jxl_color.primaries = JXL_WHITE_POINT_D65;
+        break;
+    default:
+        av_log(avctx, AV_LOG_WARNING, "Unknown color primaries, assuming BT.709/sRGB+D65\n");
+        jxl_color.primaries = JXL_PRIMARIES_SRGB;
+        jxl_color.white_point = JXL_WHITE_POINT_D65;
+    }
+
+    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_ICC_PROFILE);
+    if (sd && sd->size && JxlEncoderSetICCProfile(ctx->encoder, sd->data, sd->size) != JXL_ENC_SUCCESS)
+        av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
+    if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)
+        av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");
+
+    /* depending on basic info, level 10 might
+     * be required instead of level 5 */
+    if (JxlEncoderGetRequiredCodestreamLevel(ctx->encoder) > 5) {
+        if (JxlEncoderSetCodestreamLevel(ctx->encoder, 10) != JXL_ENC_SUCCESS)
+            av_log(avctx, AV_LOG_WARNING, "Could not increase codestream level\n");
+    }
+
     jxl_fmt.endianness = JXL_NATIVE_ENDIAN;
     jxl_fmt.align = frame->linesize[0];
 
-- 
2.36.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-09 13:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-09 13:43 [FFmpeg-devel] [PATCH v6 0/2] JPEG XL Colorspace Fixes Leo Izen
2022-05-09 13:43 ` [FFmpeg-devel] [PATCH v6 1/2] avcodec/libjxldec: properly tag output colorspace Leo Izen
2022-05-09 13:43 ` [FFmpeg-devel] [PATCH v6 2/2] avcodec/libjxlenc: properly read input colorspace Leo Izen

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git