From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 6CF1D42CC3 for ; Tue, 3 May 2022 15:45:01 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EC0A168B373; Tue, 3 May 2022 18:44:44 +0300 (EEST) Received: from mail-io1-f53.google.com (mail-io1-f53.google.com [209.85.166.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B2AA068B2F2 for ; Tue, 3 May 2022 18:44:38 +0300 (EEST) Received: by mail-io1-f53.google.com with SMTP id m6so15212926iob.4 for ; Tue, 03 May 2022 08:44:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0v3b4FjEc8CcZlHY1uXrJBRnQbJZnwfu4mcEGicCX9A=; b=IA1+IDcofsV3SWACe4d7akPtsG4KZqp/mc3L0cJfCq/Zquode0OxHPU2xhPFvyXYEu UKvkx6ubJQYJWkSLbpshd0TDOWue67mxDMsfiRh3Sk2FMRq8CZ0i34cwn/hjUYCkxwIL FNDr6W/d5yNYdGGY5Q7GDJczwNanUvW2mTeT9QtXfd0GVJuOHFJibj9j+OTDwgEB5bBU NWFejK+iEIU3CIag17BmK4BQfdiHuMWmELLLvez0bh2aaWjxjyDINdKjLAca1QDOSkjb deIZhqOEhgqPGqUInIDzBX0daZlTIiUnkQhnpSLY2PIwzPe1mC2xjnN8BDuiYnfpQ22H s6OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0v3b4FjEc8CcZlHY1uXrJBRnQbJZnwfu4mcEGicCX9A=; b=SOkuOvAMQoBLJ1GE08rqDNtg8G1daZ493nDjU4TtOPcNmdidehUnCDPOvZUYQuS5i5 IDcDVevTeskD9KveZ5hkh7Ow1tB7Aa1+UW2hF06zOkpE8Zn1+5qm/7n6MKS4rHu42lgs s8NwMBp/1RXyzwnJ09RGJb0SV+NAHYrsXBgPXK0hgTCq0yMcS0QIxrl8qUyK+PLgdSZc Tm2ouy60NaWptCG/qvq9VOEUAxIdi4hul7l2NPXJfrpgke+0mZ6lpwW1Jpz5b8Qrcx2f xybN/3o1R60FpHhBLyK4WiDzioxoIGodPXAvVktpFFXddFdGrQyoYXbrmAsj+TucDsD9 sYsg== X-Gm-Message-State: AOAM533CuKbFhp0ehABwk/dtym0nJcOoqlEmJS23MqBtc412xThyTJaW TirVWu8T3sTJUv0B+pZSOsRCurdAfX0= X-Google-Smtp-Source: ABdhPJwq5Q1m0hnmZr8HenM5HdndPZDDuUh1cUCCjFyIfTRS+FDyQtM0fLpzB1kzRMpuQWQYUnZyTA== X-Received: by 2002:a6b:d003:0:b0:646:4652:bd57 with SMTP id x3-20020a6bd003000000b006464652bd57mr6323358ioa.51.1651592677088; Tue, 03 May 2022 08:44:37 -0700 (PDT) Received: from gauss.local (c-68-41-54-207.hsd1.mi.comcast.net. [68.41.54.207]) by smtp.gmail.com with ESMTPSA id l3-20020a026643000000b0032b3a7817e8sm3922228jaf.172.2022.05.03.08.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 May 2022 08:44:36 -0700 (PDT) From: Leo Izen To: ffmpeg-devel@ffmpeg.org Date: Tue, 3 May 2022 11:44:03 -0400 Message-Id: <20220503154403.132671-3-leo.izen@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220503154403.132671-1-leo.izen@gmail.com> References: <20220503154403.132671-1-leo.izen@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/libjxlenc: properly read input colorspace X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Leo Izen Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 | 152 +++++++++++++++++++++++++++++++++-------- 1 file changed, 124 insertions(+), 28 deletions(-) diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c index 8bebec6aeb..02ecdd7dc6 100644 --- a/libavcodec/libjxlenc.c +++ b/libavcodec/libjxlenc.c @@ -30,9 +30,11 @@ #include "libavutil/error.h" #include "libavutil/frame.h" #include "libavutil/libm.h" +#include "libavutil/mastering_display_metadata.h" #include "libavutil/opt.h" #include "libavutil/pixdesc.h" #include "libavutil/pixfmt.h" +#include "libavutil/rational.h" #include "libavutil/version.h" #include "avcodec.h" @@ -117,7 +119,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 +135,8 @@ 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; } @@ -219,57 +222,150 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra info.num_color_channels = jxl_fmt.num_channels - info.num_extra_channels; info.bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels; info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample; + if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) { 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) { + 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) { + av_log(avctx, AV_LOG_WARNING, "Unknown color range, assuming full\n"); + } + + /* 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) { + 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_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; } } - /* 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_MASTERING_DISPLAY_METADATA); + if (sd) { + AVMasteringDisplayMetadata *mdm = (AVMasteringDisplayMetadata *) sd; + if (!av_cmp_q(mdm->white_point[0], FF_JXL_WP_D65_X) && + !av_cmp_q(mdm->white_point[1], FF_JXL_WP_D65_Y)) { + jxl_color.white_point = JXL_WHITE_POINT_D65; + } else if (!av_cmp_q(mdm->white_point[0], FF_JXL_WP_E_X) && + !av_cmp_q(mdm->white_point[1], FF_JXL_WP_E_Y)) { + jxl_color.white_point = JXL_WHITE_POINT_E; + } else if (!av_cmp_q(mdm->white_point[0], FF_JXL_WP_DCI_X) && + !av_cmp_q(mdm->white_point[1], FF_JXL_WP_DCI_Y)) { + jxl_color.white_point = JXL_WHITE_POINT_DCI; + } else { + jxl_color.white_point = JXL_WHITE_POINT_CUSTOM; + jxl_color.white_point_xy[0] = av_q2d(mdm->white_point[0]); + jxl_color.white_point_xy[1] = av_q2d(mdm->white_point[1]); + } + } else { + /* assume D65 if otherwise unset, including for grayscale */ + 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"); - } 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 (info.num_color_channels == 1) { + /* This should be implied to be honest + * but a libjxl but makes it fail otherwise */ + jxl_color.color_space = JXL_COLOR_SPACE_GRAY; + } else { + AVMasteringDisplayMetadata *mdm = NULL; + if (sd) + mdm = (AVMasteringDisplayMetadata *) sd; + + /* even if it internally uses XYB we set RGB here + * since that's the color space of the pixel data + * that we are feeding to libjxl */ + jxl_color.color_space = JXL_COLOR_SPACE_RGB; + + if (mdm && mdm->has_primaries) { + jxl_color.primaries = JXL_PRIMARIES_CUSTOM; + jxl_color.primaries_red_xy[0] = av_q2d(mdm->display_primaries[0][0]); + jxl_color.primaries_red_xy[1] = av_q2d(mdm->display_primaries[0][1]); + jxl_color.primaries_green_xy[0] = av_q2d(mdm->display_primaries[1][0]); + jxl_color.primaries_green_xy[1] = av_q2d(mdm->display_primaries[1][1]); + jxl_color.primaries_blue_xy[0] = av_q2d(mdm->display_primaries[2][0]); + jxl_color.primaries_blue_xy[1] = av_q2d(mdm->display_primaries[2][1]); + } else { + switch (avctx->color_primaries) { + case AVCOL_PRI_BT709: + jxl_color.primaries = JXL_PRIMARIES_SRGB; + break; + case AVCOL_PRI_BT2020: + jxl_color.primaries = JXL_PRIMARIES_2100; + break; + case AVCOL_PRI_SMPTE431: + jxl_color.primaries = JXL_PRIMARIES_P3; + break; + default: + av_log(avctx, AV_LOG_WARNING, "Unknown color primaries, assuming BT.709/sRGB\n"); + jxl_color.primaries = JXL_PRIMARIES_SRGB; + } } } + /* bitexact lossless requires there to be no XYB transform */ + info.uses_original_profile = ctx->distance == 0.0; + if (JxlEncoderSetBasicInfo(ctx->encoder, &info) != JXL_ENC_SUCCESS) { av_log(avctx, AV_LOG_ERROR, "Failed to set JxlBasicInfo\n"); return AVERROR_EXTERNAL; } + 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 (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]; - if (JxlEncoderAddImageFrame(ctx->options, &jxl_fmt, frame->data[0], jxl_fmt.align * info.ysize) != JXL_ENC_SUCCESS) { + if (JxlEncoderAddImageFrame(ctx->options, &jxl_fmt, frame->data[0], + jxl_fmt.align * info.ysize) != JXL_ENC_SUCCESS) { av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n"); return AVERROR_EXTERNAL; } -- 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".