From patchwork Thu Jan 6 13:30:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Izen X-Patchwork-Id: 33121 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp1653167iog; Thu, 6 Jan 2022 05:31:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzaIQfTeyZ+egstPPPyNj4Ns9Pf6LbU+3DL2MFUOFPpWjqEXzyb223SQY8nCgQNKpnQv/RB X-Received: by 2002:a17:907:a0c8:: with SMTP id hw8mr41158528ejc.555.1641475870474; Thu, 06 Jan 2022 05:31:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641475870; cv=none; d=google.com; s=arc-20160816; b=Iy7rt/EJxkOUPigJih/KgkxbZmLgC80p31H0ifZ7PCiIm9M4u9TYsYHhJBMo4pjgFs Id2jX8DIhDiXws00PO6K3ZPhVA2Gl34AaQrmllE+Z/x78/NJ06PFnSduj5URCHKIn0In DN04AWHbLQwkH1oc9iRm6dZtdgJVWt0bI2nGwBBG2ku+OXXb+UnpqRAWdCnVpjhyaadn P3TM9vsYhFjLZkKIc9mkE8ebenfspocObkRjlJY7aRlTd4yJsuTacsiTkHOZOcvpHFVL ICQZqTQdz8OMpAf/oy5IlCFXwThPQoDVeJTCzNhDjkVFExn3g+jvgtkJD/gL1L06C+VD t1Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=BEQGQiaYSHQ4eucMyZEaNWTWv4Io5L4KlANCKhHhgNk=; b=0syz2Vfe+uahz9e7NslEXl/Yh6X+Gl+Z9YlSS26nCoAYt/zKQ0x2pDOymXupht8hGl vvsuJGr4CZY97lStJCm82jjDxVP9S/lRN0m+G9DLMCYYiw5xDWqrNYPPc7/wmEt/2S1W i+PA/8oEIkLTeKwfL/9zSjv78BBFeh93FkvCVrLQTfsWPIt4aATXeumNteTw7rtEBhlt KePH3SXxLa/Kij3w1xMvP3vuWm4ukPKXt52Igkk6FBvL/HNk+gw32nWzOg/hVqnQ/Che kOjx+8A3NOXg/D24+X0GEYO2/kBmD2yb7V1AczyvGdK7dQsiG0ckUIcynDJMXs6wA3l8 8hvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=KT3cAIEg; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y2si1241503eda.148.2022.01.06.05.31.09; Thu, 06 Jan 2022 05:31:10 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=KT3cAIEg; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B836968AA64; Thu, 6 Jan 2022 15:30:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 61B2F68A8BE for ; Thu, 6 Jan 2022 15:30:50 +0200 (EET) Received: by mail-qt1-f175.google.com with SMTP id bp39so2233637qtb.6 for ; Thu, 06 Jan 2022 05:30:50 -0800 (PST) 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=fwqeJzfc1QjtZWIzQ8lkz2g/9UOSs6oK6aOCIte6moU=; b=KT3cAIEgbHr1xko8nUyLqrb+dO6oDiwZGj0ai5uTrbkQlPeHAgeHQF3Ay6EZXrODEQ PBFDe0Vb+1Ly4tWK9y8acN+dJWQtt8h7eNAtzkKclA9RjdfTGXCmFf1Z7MTe2h62psi/ 7NveuOBWGbjin2D8qKFdrC954Bc3f5pHqST4jyUc6wYLyVO+BK+20nGQIPPpxFB2Lgcz +ePDSGZA48LMXds9Ub0FeZx08snnWsMeb3xHrqOpYLVFOKSchLnyOVPeNq9v8fMhkCEB gWWm4Il/TvnZNgF186QIRmvyQCD5+/fpsHreWQLl5Zc7E5wI0QdBpzfZ5QayZIBI4JJ3 FzUw== 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=fwqeJzfc1QjtZWIzQ8lkz2g/9UOSs6oK6aOCIte6moU=; b=yZaP6xQB2jEB8KtV3IMkudRS0lXug5FrfADzSPOnWN8kch9pD7a8VvNvlb8LwTecyz 7jmMecabm+KY6A7XYmAsDNPV4zBy8BleSklZC2WO0fIY4fPb7bbtUzR8ESB1ss9yZ2fk WNtR/pk7RVqSyDKd9Q9KEEuFUgN/9lUTmsTFxITpVyC6PvXz/RTf1OtrZZMH777YJiwW AdWpWxV6B+rBX2JXmlOKkpOGxFZQ2VRFi5MOkLpH0mMOaIo68jJvHk9JjNwtDKzsdmEf oufNkMcXJauIGwJtjbYTMT1fwYehkBemYYMSBkQmUQ7O2ogspWbdh/vHEOTPbg/AtqiJ 8c0A== X-Gm-Message-State: AOAM530LTs1sLhWNszL4QJNZofWszx59r+V+2frkz/6AFiO/K6R9X6TB HpyMljoyyAgpPrLuNwvMc0P+17N7860= X-Received: by 2002:a05:622a:95:: with SMTP id o21mr52759258qtw.386.1641475849010; Thu, 06 Jan 2022 05:30:49 -0800 (PST) Received: from gauss.local (c-68-41-54-207.hsd1.mi.comcast.net. [68.41.54.207]) by smtp.gmail.com with ESMTPSA id v1sm1690434qtw.65.2022.01.06.05.30.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 05:30:48 -0800 (PST) From: Leo Izen To: ffmpeg-devel@ffmpeg.org Date: Thu, 6 Jan 2022 08:30:15 -0500 Message-Id: <20220106133017.222689-3-leo.izen@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220106133017.222689-1-leo.izen@gmail.com> References: <2059c091-cf7f-fad3-c824-53253ae62b5d@gmail.com> <20220106133017.222689-1-leo.izen@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 3/5] avcodec/libjxl: add Jpeg XL encoding via libjxl 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: OXbkyTzcDVd+ This commit adds encoding support to libavcodec for Jpeg XL images via the external library libjxl. --- configure | 3 +- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/libjxlenc.c | 383 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 libavcodec/libjxlenc.c diff --git a/configure b/configure index 9317485131..9188e56fb5 100755 --- a/configure +++ b/configure @@ -241,7 +241,7 @@ External library support: --enable-libiec61883 enable iec61883 via libiec61883 [no] --enable-libilbc enable iLBC de/encoding via libilbc [no] --enable-libjack enable JACK audio sound server [no] - --enable-libjxl enable JPEG XL decoding via libjxl [no] + --enable-libjxl enable JPEG XL de/encoding via libjxl [no] --enable-libklvanc enable Kernel Labs VANC processing [no] --enable-libkvazaar enable HEVC encoding via libkvazaar [no] --enable-liblensfun enable lensfun lens correction [no] @@ -3331,6 +3331,7 @@ libgsm_ms_encoder_deps="libgsm" libilbc_decoder_deps="libilbc" libilbc_encoder_deps="libilbc" libjxl_decoder_deps="libjxl libjxl_threads" +libjxl_encoder_deps="libjxl libjxl_threads" libkvazaar_encoder_deps="libkvazaar" libmodplug_demuxer_deps="libmodplug" libmp3lame_encoder_deps="libmp3lame" diff --git a/libavcodec/Makefile b/libavcodec/Makefile index b3dcc7dcb1..946deacdcd 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1039,6 +1039,7 @@ OBJS-$(CONFIG_LIBGSM_MS_ENCODER) += libgsmenc.o OBJS-$(CONFIG_LIBILBC_DECODER) += libilbc.o OBJS-$(CONFIG_LIBILBC_ENCODER) += libilbc.o OBJS-$(CONFIG_LIBJXL_DECODER) += libjxldec.o libjxl.o +OBJS-$(CONFIG_LIBJXL_ENCODER) += libjxlenc.o libjxl.o OBJS-$(CONFIG_LIBKVAZAAR_ENCODER) += libkvazaar.o OBJS-$(CONFIG_LIBMP3LAME_ENCODER) += libmp3lame.o OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER) += libopencore-amr.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index b41da3b0de..2e50991652 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -745,6 +745,7 @@ extern const AVCodec ff_libgsm_ms_decoder; extern const AVCodec ff_libilbc_encoder; extern const AVCodec ff_libilbc_decoder; extern const AVCodec ff_libjxl_decoder; +extern const AVCodec ff_libjxl_encoder; extern const AVCodec ff_libmp3lame_encoder; extern const AVCodec ff_libopencore_amrnb_encoder; extern const AVCodec ff_libopencore_amrnb_decoder; diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c new file mode 100644 index 0000000000..4309a91e22 --- /dev/null +++ b/libavcodec/libjxlenc.c @@ -0,0 +1,383 @@ +/* + * JPEG XL encoding support via libjxl + * Copyright (c) 2021 Leo Izen + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * JPEG XL encoder using libjxl + */ + +#include "libavutil/avutil.h" +#include "libavutil/error.h" +#include "libavutil/frame.h" +#include "libavutil/libm.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/pixfmt.h" +#include "libavutil/version.h" + +#include "avcodec.h" +#include "internal.h" + +#include +#include +#include "libjxl.h" + +typedef struct LibJxlEncodeContext { + AVClass *class; + void *runner; + JxlEncoder *encoder; + JxlEncoderFrameSettings *options; + int effort; + float distance; + int modular; + uint8_t *buffer; + size_t buffer_size; +} LibJxlEncodeContext; + +/** + * Map a quality setting for -qscale roughly from libjpeg + * quality numbers to libjxl's butteraugli distance for + * photographic content. + * + * Setting distance explicitly is preferred, but this will + * allow qscale to be used as a fallback. + * + * This function is continuous and injective on [0, 100] which + * makes it monotonic. + * + * @param quality 0.0 to 100.0 quality setting, libjpeg quality + * @return Butteraugli distance between 0.0 and 15.0 + */ +static float quality_to_distance(float quality){ + if (quality >= 100.0) { + return 0.0; + } else if (quality >= 90.0) { + return (100.0 - quality) * 0.10; + } else if (quality >= 30.0) { + return 0.1 + (100.0 - quality) * 0.09; + } else if (quality > 0.0) { + return 15.0 - (59.0 * quality - 4350.0) * quality / 9000.0; + } else { + return 15.0; + } +} + +/** + * Initalize the decoder on a per-frame basis. All of these need to be set + * once each time the decoder is reset, which it must be each frame to make + * the image2 muxer work. + * + * @return 0 upon success, negative on failure. + */ +static int libjxl_init_jxl_encoder(AVCodecContext *avctx) +{ + + LibJxlEncodeContext *ctx = avctx->priv_data; + + /* reset the encoder every frame for image2 muxer */ + JxlEncoderReset(ctx->encoder); + + ctx->options = JxlEncoderFrameSettingsCreate(ctx->encoder, NULL); + if (!ctx->options) { + av_log(avctx, AV_LOG_ERROR, "Failed to create JxlEncoderOptions"); + return AVERROR_EXTERNAL; + } + + /* This needs to be set each time the decoder is reset */ + if (JxlEncoderSetParallelRunner(ctx->encoder, JxlThreadParallelRunner, ctx->runner) + != JXL_ENC_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set JxlThreadParallelRunner"); + return AVERROR_EXTERNAL; + } + + /* these shouldn't fail, libjxl bug notwithstanding */ + if (JxlEncoderFrameSettingsSetOption(ctx->options, JXL_ENC_FRAME_SETTING_EFFORT, ctx->effort) + != JXL_ENC_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set effort to: %d", ctx->effort); + return AVERROR_EXTERNAL; + } + + /* check for negative zero, our default */ + if (1.0f / ctx->distance == 1.0f / -0.0f) { + /* use ffmpeg.c -q option if passed */ + if (avctx->flags & AV_CODEC_FLAG_QSCALE) + ctx->distance = quality_to_distance((float)avctx->global_quality / FF_QP2LAMBDA); + else + /* default 1.0 matches cjxl */ + ctx->distance = 1.0; + } + + /* + * 0.01 is the minimum distance accepted for lossy + * interpreting any positive value less than this as minimum + */ + if (ctx->distance > 0.0 && ctx->distance < 0.01) + ctx->distance = 0.01; + if (JxlEncoderOptionsSetDistance(ctx->options, ctx->distance) != JXL_ENC_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set distance: %f", ctx->distance); + return AVERROR_EXTERNAL; + } + + /* + * In theory the library should automatically enable modular if necessary, + * but it appears it won't at the moment due to a bug. This will still + * work even if that is patched. + */ + if (JxlEncoderFrameSettingsSetOption(ctx->options, JXL_ENC_FRAME_SETTING_MODULAR, + ctx->modular || ctx->distance <= 0.0 ? 1 : -1) != JXL_ENC_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set modular"); + return AVERROR_EXTERNAL; + } + + return 0; +} + +/** + * Global encoder initialization. This only needs to be run once, + * not every frame. + */ +static av_cold int libjxl_encode_init(AVCodecContext *avctx) +{ + LibJxlEncodeContext *ctx = avctx->priv_data; + JxlMemoryManager manager; + + ff_libjxl_init_memory_manager(&manager); + ctx->encoder = JxlEncoderCreate(&manager); + if (!ctx->encoder) { + av_log(avctx, AV_LOG_ERROR, "Failed to create JxlEncoder"); + return AVERROR_EXTERNAL; + } + + ctx->runner = JxlThreadParallelRunnerCreate(&manager, ff_libjxl_get_threadcount(avctx->thread_count)); + if (!ctx->runner) { + av_log(avctx, AV_LOG_ERROR, "Failed to create JxlThreadParallelRunner"); + return AVERROR_EXTERNAL; + } + + ctx->buffer_size = 4096; + ctx->buffer = av_malloc(ctx->buffer_size); + + if (!ctx->buffer){ + av_log(avctx, AV_LOG_ERROR, "Could not allocate encoding buffer"); + return AVERROR(ENOMEM); + } + + return 0; +} + +/** + * Encode an entire frame. Currently animation, is not supported by + * this encoder, so this will always reinitialize a new still image + * and encode a one-frame image (for image2 and image2pipe). + */ +static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet) +{ + LibJxlEncodeContext *ctx = avctx->priv_data; + + const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format); + JxlBasicInfo info; + JxlColorEncoding jxl_color; + JxlPixelFormat jxl_fmt; + JxlEncoderStatus status; + int ff_status; + size_t available = ctx->buffer_size; + size_t bytes_written = 0; + uint8_t *next_out = ctx->buffer; + + ff_status = libjxl_init_jxl_encoder(avctx); + if (ff_status){ + av_log(avctx, AV_LOG_ERROR, "Error frame-initializing JxlEncoder"); + return ff_status; + } + + /* populate the basic info settings */ + JxlEncoderInitBasicInfo(&info); + jxl_fmt.num_channels = pix_desc->nb_components; + info.xsize = frame->width; + info.ysize = frame->height; + info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2; + 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."); + return AVERROR_EXTERNAL; + } + } + + /* bitexact lossless requires there to be no XYB transform */ + info.uses_original_profile = ctx->distance <= 0.0; + + /* + * 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 (info.uses_original_profile){ + if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set JxlColorEncoding"); + return AVERROR_EXTERNAL; + } + } + + if (JxlEncoderSetBasicInfo(ctx->encoder, &info) != JXL_ENC_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set JxlBasicInfo"); + return AVERROR_EXTERNAL; + } + + jxl_fmt.endianness = JXL_LITTLE_ENDIAN; + jxl_fmt.align = frame->linesize[0]; + + 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: %d", status); + return AVERROR_EXTERNAL; + } + + /* + * Run this after the last frame in the image has been passed. + * TODO support animation + */ + JxlEncoderCloseInput(ctx->encoder); + + for (;;){ + status = JxlEncoderProcessOutput(ctx->encoder, &next_out, &available); + if (status == JXL_ENC_ERROR) { + av_log(avctx, AV_LOG_ERROR, "Unspecified libjxl error occurred"); + return AVERROR_EXTERNAL; + } + bytes_written = ctx->buffer_size - available; + if (status == JXL_ENC_SUCCESS) { + /* all data passed has been encoded */ + break; + } + if (status == JXL_ENC_NEED_MORE_OUTPUT) { + /* + * at the moment, libjxl has no way to + * tell us how much space it actually needs + * so we need to malloc loop + */ + ctx->buffer_size = bytes_written * 2; + next_out = av_malloc(ctx->buffer_size); + if (!next_out) { + av_log(avctx, AV_LOG_ERROR, "Error reallocated encoder buffer"); + return AVERROR(ENOMEM); + } + memcpy(next_out, ctx->buffer, bytes_written); + av_freep(&ctx->buffer); + ctx->buffer = next_out; + next_out += bytes_written; + available = ctx->buffer_size - bytes_written; + continue; + } + } + /* + * This buffer will be copied when the generic + * code makes this packet refcounted, + * so we can use the buffer again. + */ + pkt->data = ctx->buffer; + pkt->size = bytes_written; + *got_packet = 1; + return 0; +} + +static av_cold int libjxl_encode_close(AVCodecContext *avctx) +{ + LibJxlEncodeContext *ctx = avctx->priv_data; + + if (ctx->runner) + JxlThreadParallelRunnerDestroy(ctx->runner); + ctx->runner = NULL; + + /* + * destroying the decoder also frees + * ctx->options so we don't need to + */ + if (ctx->encoder) + JxlEncoderDestroy(ctx->encoder); + ctx->encoder = NULL; + + if (ctx->buffer) + av_freep(&ctx->buffer); + ctx->buffer = NULL; + ctx->buffer_size = 0; + + return 0; +} + +#define OFFSET(x) offsetof(LibJxlEncodeContext, x) +#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM + +static const AVOption libjxl_encode_options[] = { + { "effort", "Encoding effort", OFFSET(effort), AV_OPT_TYPE_INT, { .i64 = 7 }, 1, 9, VE }, + { "distance", "Maximum Butteraugli distance (quality setting, " + "lower = better, zero = lossless, default 1.0)", OFFSET(distance), AV_OPT_TYPE_FLOAT, { .dbl = -0.0 }, 0.0, 15.0, VE }, + { "modular", "Force modular mode", OFFSET(modular), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, + { NULL }, +}; + +static const AVClass libjxl_encode_class = { + .class_name = "libjxl", + .item_name = av_default_item_name, + .option = libjxl_encode_options, + .version = LIBAVUTIL_VERSION_INT, +}; + +const AVCodec ff_libjxl_encoder = { + .name = "libjxl", + .long_name = NULL_IF_CONFIG_SMALL("libjxl JPEG XL encoder"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_JPEGXL, + .priv_data_size = sizeof(LibJxlEncodeContext), + .init = libjxl_encode_init, + .encode2 = libjxl_encode_frame, + .close = libjxl_encode_close, + .capabilities = AV_CODEC_CAP_OTHER_THREADS, + .caps_internal = FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP, + .pix_fmts = (const enum AVPixelFormat[]) { + AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, + AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGBA64LE, + AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, + AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_YA16LE, + AV_PIX_FMT_GRAYF32LE, + AV_PIX_FMT_NONE + }, + .priv_class = &libjxl_encode_class, + .wrapper_name = "libjxl", +};