[FFmpeg-devel] [PATCH 3/3] avcodec/libjxlenc: Add libjxl_animated encoder

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


---
 libavcodec/allcodecs.c |   1 +
 libavcodec/libjxlenc.c | 197 ++++++++++++++++++++++++++++++++---------
 2 files changed, 157 insertions(+), 41 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index b0f004e15c..e6733b0d4f 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -784,6 +784,7 @@ extern const FFCodec ff_libilbc_encoder;
 extern const FFCodec ff_libilbc_decoder;
 extern const FFCodec ff_libjxl_decoder;
 extern const FFCodec ff_libjxl_encoder;
+extern const FFCodec ff_libjxl_animated_encoder;
 extern const FFCodec ff_libmp3lame_encoder;
 extern const FFCodec ff_libopencore_amrnb_encoder;
 extern const FFCodec ff_libopencore_amrnb_decoder;
diff --git a/libavcodec/libjxlenc.c b/libavcodec/libjxlenc.c
index 6110c42a7c..5d437b2c05 100644
--- a/libavcodec/libjxlenc.c
+++ b/libavcodec/libjxlenc.c
@@ -31,14 +31,17 @@
 #include "libavutil/error.h"
 #include "libavutil/frame.h"
 #include "libavutil/libm.h"
+#include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/version.h"

+#include "packet.h"
 #include "avcodec.h"
 #include "encode.h"
 #include "codec_internal.h"
+#include "internal.h"

 #include <jxl/encode.h>
 #include <jxl/thread_parallel_runner.h>
@@ -51,13 +54,14 @@ typedef struct LibJxlEncodeContext {
     JxlEncoderFrameSettings *options;
     JxlBasicInfo info;
     JxlPixelFormat jxl_fmt;
-    int animated;
-    int first_frame;
     int effort;
     float distance;
     int modular;
     uint8_t *buffer;
     size_t buffer_size;
+    /* Only used by libjxl-animated */
+    AVFrame *last;
+    int animated;
 } LibJxlEncodeContext;

 /**
@@ -185,7 +189,6 @@ static av_cold int libjxl_encode_init(AVCodecContext *avctx)
     }

     ctx->animated = 0;
-    ctx->first_frame = 1;

     return 0;
 }
@@ -284,6 +287,14 @@ static int libjxl_encode_init_image(AVCodecContext *avctx, const AVFrame *frame)
         info->alpha_exponent_bits = 0;
         jxl_fmt->data_type = info->bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;
     }
+    if(ctx->animated) {
+        info->have_animation = 1;
+        info->animation.have_timecodes = 0;
+        info->animation.num_loops = 0;
+        info->animation.tps_numerator = frame->time_base.den;
+        info->animation.tps_denominator = frame->time_base.num;
+        avctx->time_base = frame->time_base;
+    }

 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
     jxl_bit_depth.bits_per_sample = bits_per_sample;
@@ -386,37 +397,12 @@ static int libjxl_encode_init_image(AVCodecContext *avctx, const AVFrame *frame)
     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)
+static int libjxl_encode_process_output(AVCodecContext *avctx, size_t *bytes_written)
 {
     LibJxlEncodeContext *ctx = avctx->priv_data;
     JxlEncoderStatus jret;
-    JxlBasicInfo *info = &ctx->info;
-    JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
-    int ret;
-    size_t available = ctx->buffer_size;
-    size_t bytes_written = 0;
     uint8_t *next_out = ctx->buffer;
-
-    if(!ctx->animated || ctx->first_frame) {
-        if((ret = libjxl_encode_init_image(avctx, frame)) < 0)
-            return ret;
-        ctx->first_frame = 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\n");
-        return AVERROR_EXTERNAL;
-    }
-
-    /*
-     * Run this after the last frame in the image has been passed.
-     */
-    JxlEncoderCloseInput(ctx->encoder);
+    size_t available = ctx->buffer_size;

     while (1) {
         jret = JxlEncoderProcessOutput(ctx->encoder, &next_out, &available);
@@ -424,7 +410,7 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
             av_log(avctx, AV_LOG_ERROR, "Unspecified libjxl error occurred\n");
             return AVERROR_EXTERNAL;
         }
-        bytes_written = ctx->buffer_size - available;
+        *bytes_written = ctx->buffer_size - available;
         /* all data passed has been encoded */
         if (jret == JXL_ENC_SUCCESS)
             break;
@@ -441,14 +427,46 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
                 return AVERROR(ENOMEM);
             ctx->buffer = temp;
             ctx->buffer_size = new_size;
-            next_out = ctx->buffer + bytes_written;
-            available = new_size - bytes_written;
+            next_out = ctx->buffer + *bytes_written;
+            available = new_size - *bytes_written;
             continue;
         }
         av_log(avctx, AV_LOG_ERROR, "Bad libjxl event: %d\n", jret);
         return AVERROR_EXTERNAL;
     }

+    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;
+    JxlBasicInfo *info = &ctx->info;
+    JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
+    int ret;
+    size_t bytes_written = 0;
+
+    if((ret = libjxl_encode_init_image(avctx, frame)) < 0)
+        return ret;
+
+    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;
+    }
+
+    /*
+     * Run this after the last frame in the image has been passed.
+     */
+    JxlEncoderCloseInput(ctx->encoder);
+
+    if((ret = libjxl_encode_process_output(avctx, &bytes_written)) < 0)
+        return ret;
+
     ret = ff_get_encode_buffer(avctx, pkt, bytes_written, 0);
     if (ret < 0)
         return ret;
@@ -459,6 +477,70 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra
     return 0;
 }

+static int libjxl_animated_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
+{
+    LibJxlEncodeContext *ctx = avctx->priv_data;
+    int ret = 0;
+
+    if(avctx->frame_num == 0)
+        if((ret = libjxl_encode_init_image(avctx, frame)) < 0)
+            return ret;
+
+    if(!ctx->last && !avctx->internal->draining) {
+        ctx->last = av_frame_clone(frame);
+        *got_packet = 0;
+        return AVERROR(EAGAIN);
+    } else if(!ctx->last && avctx->internal->draining && !frame) {
+        av_log(avctx, AV_LOG_WARNING, "Draining done\n");
+        *got_packet = 0;
+    } else {
+        JxlBasicInfo *info = &ctx->info;
+        JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
+        JxlFrameHeader frame_header;
+        size_t bytes_written = 0;
+
+        JxlEncoderInitFrameHeader(&frame_header);
+        frame_header.duration = ctx->last->duration;
+        pkt->duration = ctx->last->duration;
+        pkt->pts = AV_NOPTS_VALUE;
+        pkt->dts = AV_NOPTS_VALUE;
+
+        if(JxlEncoderSetFrameHeader(ctx->options, &frame_header) != JXL_ENC_SUCCESS) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to set frame header\n");
+            return AVERROR_EXTERNAL;
+        }
+        if (JxlEncoderAddImageFrame(ctx->options, jxl_fmt, ctx->last->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;
+        }
+
+        /*
+         * Run this after the last frame in the image has been passed.
+         */
+        if(avctx->internal->draining) {
+            JxlEncoderCloseInput(ctx->encoder);
+        }
+
+        if((ret = libjxl_encode_process_output(avctx, &bytes_written)) < 0)
+            return ret;
+
+        ret = ff_get_encode_buffer(avctx, pkt, bytes_written, 0);
+        if (ret < 0)
+            return ret;
+
+        memcpy(pkt->data, ctx->buffer, bytes_written);
+        *got_packet = 1;
+
+        if(!avctx->internal->draining) {
+            av_frame_replace(ctx->last, frame);
+        } else {
+            av_frame_free(&ctx->last);
+        }
+    }
+
+    return 0;
+}
+
 static av_cold int libjxl_encode_close(AVCodecContext *avctx)
 {
     LibJxlEncodeContext *ctx = avctx->priv_data;
@@ -480,6 +562,17 @@ static av_cold int libjxl_encode_close(AVCodecContext *avctx)
     return 0;
 }

+static av_cold int libjxl_animated_encode_init(AVCodecContext *avctx)
+{
+    int ret;
+    LibJxlEncodeContext *ctx = avctx->priv_data;
+    if((ret = libjxl_encode_init(avctx)) < 0)
+        return ret;
+    ctx->animated = 1;
+    ret = libjxl_init_jxl_encoder(avctx);
+    return ret;
+}
+
 #define OFFSET(x) offsetof(LibJxlEncodeContext, x)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM

@@ -498,6 +591,15 @@ static const AVClass libjxl_encode_class = {
     .version    = LIBAVUTIL_VERSION_INT,
 };

+static const enum AVPixelFormat libjxl_pix_fmts[] = {
+    AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
+    AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64,
+    AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8,
+    AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
+    AV_PIX_FMT_GRAYF32,
+    AV_PIX_FMT_NONE
+};
+
 const FFCodec ff_libjxl_encoder = {
     .p.name           = "libjxl",
     CODEC_LONG_NAME("libjxl JPEG XL"),
@@ -512,14 +614,27 @@ const FFCodec ff_libjxl_encoder = {
     .caps_internal    = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
                         FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP |
                         FF_CODEC_CAP_ICC_PROFILES,
-    .p.pix_fmts       = (const enum AVPixelFormat[]) {
-        AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
-        AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64,
-        AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8,
-        AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
-        AV_PIX_FMT_GRAYF32,
-        AV_PIX_FMT_NONE
-    },
+    .p.pix_fmts       = libjxl_pix_fmts,
+    .p.priv_class     = &libjxl_encode_class,
+    .p.wrapper_name   = "libjxl",
+};
+
+const FFCodec ff_libjxl_animated_encoder = {
+    .p.name           = "libjxl_animated",
+    CODEC_LONG_NAME("libjxl Animated JPEG XL"),
+    .p.type           = AVMEDIA_TYPE_VIDEO,
+    .p.id             = AV_CODEC_ID_JPEGXL,
+    .priv_data_size   = sizeof(LibJxlEncodeContext),
+    .init             = libjxl_animated_encode_init,
+    FF_CODEC_ENCODE_CB(libjxl_animated_encode_frame),
+    .close            = libjxl_encode_close,
+    .p.capabilities   = AV_CODEC_CAP_OTHER_THREADS |
+                        AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
+                        AV_CODEC_CAP_DELAY,
+    .caps_internal    = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
+                        FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP |
+                        FF_CODEC_CAP_ICC_PROFILES | FF_CODEC_CAP_EOF_FLUSH,
+    .p.pix_fmts       = libjxl_pix_fmts,
     .p.priv_class     = &libjxl_encode_class,
     .p.wrapper_name   = "libjxl",
 };
--
2.43.0



More information about the ffmpeg-devel mailing list