[FFmpeg-devel] [PATCH v5 1/2] avcodec/libjxldec: properly tag output colorspace
Leo Izen
leo.izen at gmail.com
Thu May 5 16:50:55 EEST 2022
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
More information about the ffmpeg-devel
mailing list