From: Leo Izen <leo.izen@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: Leo Izen <leo.izen@gmail.com>
Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: properly tag output colorspace
Date: Tue, 3 May 2022 11:44:02 -0400
Message-ID: <20220503154403.132671-2-leo.izen@gmail.com> (raw)
In-Reply-To: <20220503154403.132671-1-leo.izen@gmail.com>
Whether an ICC profile is present or not, the decoder
should now properly tag the colorspace of pixel data
received by the decoder.
---
libavcodec/libjxl.h | 9 +++
libavcodec/libjxldec.c | 121 ++++++++++++++++++++++++++++++++++++++++-
2 files changed, 127 insertions(+), 3 deletions(-)
diff --git a/libavcodec/libjxl.h b/libavcodec/libjxl.h
index 5387c438fd..2b388dc45d 100644
--- a/libavcodec/libjxl.h
+++ b/libavcodec/libjxl.h
@@ -27,8 +27,17 @@
#ifndef AVCODEC_LIBJXL_H
#define AVCODEC_LIBJXL_H
+#include "libavutil/rational.h"
+
#include <jxl/memory_manager.h>
+#define FF_JXL_WP_D65_X av_make_q(3127, 10000)
+#define FF_JXL_WP_D65_Y av_make_q(3290, 10000)
+#define FF_JXL_WP_E_X av_make_q(1, 3)
+#define FF_JXL_WP_E_Y av_make_q(1, 3)
+#define FF_JXL_WP_DCI_X av_make_q(314, 1000)
+#define FF_JXL_WP_DCI_Y av_make_q(351, 1000)
+
/**
* Transform threadcount in ffmpeg to one used by libjxl.
*
diff --git a/libavcodec/libjxldec.c b/libavcodec/libjxldec.c
index cd4bca3343..bd308590ab 100644
--- a/libavcodec/libjxldec.c
+++ b/libavcodec/libjxldec.c
@@ -28,9 +28,11 @@
#include "libavutil/buffer.h"
#include "libavutil/common.h"
#include "libavutil/error.h"
+#include "libavutil/mastering_display_metadata.h"
#include "libavutil/mem.h"
#include "libavutil/pixdesc.h"
#include "libavutil/pixfmt.h"
+#include "libavutil/rational.h"
#include "libavutil/frame.h"
#include "avcodec.h"
@@ -189,16 +191,129 @@ 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);
+ 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_ORIGINAL, ctx->iccp->data, iccp_len);
- if (jret != JXL_DEC_SUCCESS)
+ 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);
+ }
}
+ avctx->color_range = frame->color_range = AVCOL_RANGE_JPEG;
+ if (ctx->iccp) {
+ /* if the ICCP is present, libjxl outputs sRGB */
+ if (ctx->jxl_pixfmt.num_channels >= 3)
+ 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 ? AVCOL_TRC_LINEAR : AVCOL_TRC_IEC61966_2_1;
+ } else {
+ JxlColorEncoding jxl_encoding;
+ jret = JxlDecoderGetColorAsEncodedProfile(ctx->decoder, &ctx->jxl_pixfmt, JXL_COLOR_PROFILE_TARGET_DATA, &jxl_encoding);
+ if (jret != JXL_DEC_SUCCESS) {
+ av_log(avctx, AV_LOG_WARNING, "Unable to obtain color encoding from header\n");
+ continue;
+ }
+
+ if (ctx->jxl_pixfmt.num_channels >= 3) {
+ AVMasteringDisplayMetadata *mdm = NULL;
+
+ avctx->colorspace = frame->colorspace = AVCOL_SPC_RGB;
+
+ /* dont set MDM if D65 white point is used */
+ /* unless the custom primaries are set */
+ if (jxl_encoding.primaries == JXL_PRIMARIES_CUSTOM ||
+ jxl_encoding.white_point != JXL_WHITE_POINT_D65) {
+ mdm = av_mastering_display_metadata_create_side_data(frame);
+ if (!mdm)
+ return AVERROR(ENOMEM);
+ }
+
+ switch (jxl_encoding.primaries) {
+ case JXL_PRIMARIES_SRGB:
+ avctx->color_primaries = AVCOL_PRI_BT709;
+ break;
+ case JXL_PRIMARIES_2100:
+ avctx->color_primaries = AVCOL_PRI_BT2020;
+ break;
+ case JXL_PRIMARIES_P3:
+ avctx->color_primaries = AVCOL_PRI_SMPTE431;
+ break;
+ case JXL_PRIMARIES_CUSTOM:
+ mdm->display_primaries[0][0] = av_d2q(jxl_encoding.primaries_red_xy[0], 100000);
+ mdm->display_primaries[0][1] = av_d2q(jxl_encoding.primaries_red_xy[1], 100000);
+ mdm->display_primaries[1][0] = av_d2q(jxl_encoding.primaries_green_xy[0], 100000);
+ mdm->display_primaries[1][1] = av_d2q(jxl_encoding.primaries_green_xy[1], 100000);
+ mdm->display_primaries[2][0] = av_d2q(jxl_encoding.primaries_blue_xy[0], 100000);
+ mdm->display_primaries[2][1] = av_d2q(jxl_encoding.primaries_blue_xy[1], 100000);
+ mdm->has_primaries = 1;
+ break;
+ default:
+ av_log(avctx, AV_LOG_WARNING, "Unknown JXL color primaries: %d\n", jxl_encoding.primaries);
+ avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
+ }
+
+ switch (jxl_encoding.white_point) {
+ case JXL_WHITE_POINT_D65:
+ if (mdm) {
+ mdm->white_point[0] = FF_JXL_WP_D65_X;
+ mdm->white_point[1] = FF_JXL_WP_D65_Y;
+ }
+ break;
+ case JXL_WHITE_POINT_E:
+ mdm->white_point[0] = FF_JXL_WP_E_X;
+ mdm->white_point[1] = FF_JXL_WP_E_Y;
+ break;
+ case JXL_WHITE_POINT_DCI:
+ mdm->white_point[0] = FF_JXL_WP_DCI_X;
+ mdm->white_point[1] = FF_JXL_WP_DCI_Y;
+ break;
+ case JXL_WHITE_POINT_CUSTOM:
+ mdm->white_point[0] = av_d2q(jxl_encoding.white_point_xy[0], 100000);
+ mdm->white_point[1] = av_d2q(jxl_encoding.white_point_xy[1], 100000);
+ break;
+ default:
+ av_log(avctx, AV_LOG_WARNING, "Unknown JXL white point: %d\n", jxl_encoding.white_point);
+ }
+ }
+
+ 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:
+ 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);
+ avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
+ }
+ break;
+ default:
+ av_log(avctx, AV_LOG_WARNING, "Unknown transfer function: %d\n", jxl_encoding.transfer_function);
+ avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
+ }
+ }
+ frame->color_trc = avctx->color_trc;
+ frame->color_primaries = avctx->color_primaries;
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".
next prev parent reply other threads:[~2022-05-03 15:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-03 15:44 [FFmpeg-devel] [PATCH 0/2] JPEG XL Colorspace Fixes Leo Izen
2022-05-03 15:44 ` Leo Izen [this message]
2022-05-03 15:44 ` [FFmpeg-devel] [PATCH 2/2] avcodec/libjxlenc: properly read input colorspace Leo Izen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220503154403.132671-2-leo.izen@gmail.com \
--to=leo.izen@gmail.com \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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