[FFmpeg-devel] [PATCH 1/3] avcodec/libjxlenc: Move JxlBasicInfo to LibJxlEncodeContext

Zsolt Vadász zsolt_vadasz at protonmail.com
Mon Dec 11 19:05:13 EET 2023


---
 libavcodec/libjxlenc.c | 45 +++++++++++++++++++++---------------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index d707f3a61b..92a458d51a 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -49,6 +49,7 @@ typedef struct LibJxlEncodeContext {
     void *runner;
     JxlEncoder *encoder;
     JxlEncoderFrameSettings *options;
+    JxlBasicInfo info;
     int effort;
     float distance;
     int modular;
@@ -95,9 +96,6 @@ 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\n");
@@ -247,7 +245,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
     LibJxlEncodeContext *ctx = avctx->priv_data;
     AVFrameSideData *sd;
     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
-    JxlBasicInfo info;
+    JxlBasicInfo *info = &ctx->info;
     JxlColorEncoding jxl_color;
     JxlPixelFormat jxl_fmt;
     int bits_per_sample;
@@ -260,6 +258,9 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
     size_t bytes_written = 0;
     uint8_t *next_out = ctx->buffer;
 
+    /* reset the encoder every frame for image2 muxer */
+    JxlEncoderReset(ctx->encoder);
+
     ret = libjxl_init_jxl_encoder(avctx);
     if (ret) {
         av_log(avctx, AV_LOG_ERROR, "Error frame-initializing JxlEncoder\n");
@@ -267,31 +268,31 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
     }
 
     /* populate the basic info settings */
-    JxlEncoderInitBasicInfo(&info);
+    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->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;
     bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
-    info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)
+    info->bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)
                            ? avctx->bits_per_raw_sample : bits_per_sample;
-    info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
+    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;
+        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;
     } 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;
+        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;
     }
 
 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
     jxl_bit_depth.bits_per_sample = bits_per_sample;
     jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
     jxl_bit_depth.exponent_bits_per_sample = pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT ?
-                                             info.exponent_bits_per_sample : 0;
+                                             info->exponent_bits_per_sample : 0;
 #endif
 
     /* JPEG XL format itself does not support limited range */
@@ -302,9 +303,9 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
         av_log(avctx, AV_LOG_WARNING, "Unknown color range, assuming full (pc)\n");
 
     /* bitexact lossless requires there to be no XYB transform */
-    info.uses_original_profile = ctx->distance == 0.0;
+    info->uses_original_profile = ctx->distance == 0.0;
 
-    if (JxlEncoderSetBasicInfo(ctx->encoder, &info) != JXL_ENC_SUCCESS) {
+    if (JxlEncoderSetBasicInfo(ctx->encoder, info) != JXL_ENC_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to set JxlBasicInfo\n");
         return AVERROR_EXTERNAL;
     }
@@ -353,7 +354,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
 
     /* This should be implied to be honest
      * but a libjxl bug makes it fail otherwise */
-    if (info.num_color_channels == 1)
+    if (info->num_color_channels == 1)
         jxl_color.color_space = JXL_COLOR_SPACE_GRAY;
     else
         jxl_color.color_space = JXL_COLOR_SPACE_RGB;
@@ -385,7 +386,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
     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.43.0



More information about the ffmpeg-devel mailing list