FFmpeg
Data Structures | Macros | Functions | Variables
libjxlenc.c File Reference
#include <string.h>
#include "libavutil/avutil.h"
#include "libavutil/csp.h"
#include "libavutil/error.h"
#include "libavutil/frame.h"
#include "libavutil/libm.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/pixfmt.h"
#include "libavutil/version.h"
#include "avcodec.h"
#include "encode.h"
#include "codec_internal.h"
#include <jxl/encode.h>
#include <jxl/thread_parallel_runner.h>
#include "libjxl.h"

Go to the source code of this file.

Data Structures

struct  LibJxlEncodeContext
 

Macros

#define OFFSET(x)   offsetof(LibJxlEncodeContext, x)
 
#define VE   AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 

Functions

static float quality_to_distance (float quality)
 Map a quality setting for -qscale roughly from libjpeg quality numbers to libjxl's butteraugli distance for photographic content. More...
 
static int libjxl_init_jxl_encoder (AVCodecContext *avctx)
 Initalize the encoder on a per-file basis. More...
 
static av_cold int libjxl_encode_init (AVCodecContext *avctx)
 Global encoder initialization. More...
 
static av_cold int libjxl_anim_encode_init (AVCodecContext *avctx)
 Initializer for the animation encoder. More...
 
static int libjxl_populate_primaries (void *avctx, JxlColorEncoding *jxl_color, enum AVColorPrimaries prm)
 Populate a JxlColorEncoding with the given enum AVColorPrimaries. More...
 
static int libjxl_preprocess_stream (AVCodecContext *avctx, const AVFrame *frame, int animated)
 Sends metadata to libjxl based on the first frame of the stream, such as pixel format, orientation, bit depth, and that sort of thing. More...
 
static int libjxl_preprocess_frame (AVCodecContext *avctx, const AVFrame *frame, const uint8_t **data)
 Sends frame information to libjxl on a per-frame basis. More...
 
static int libjxl_process_output (AVCodecContext *avctx, size_t *bytes_written)
 Run libjxl's output processing loop, reallocating the packet as necessary if libjxl needs more space to work with. More...
 
static int libjxl_encode_frame (AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
 Encode an entire frame. More...
 
static int libjxl_anim_encode_frame (AVCodecContext *avctx, AVPacket *pkt)
 Encode one frame of the animation. More...
 
static av_cold int libjxl_encode_close (AVCodecContext *avctx)
 

Variables

static const AVOption libjxl_encode_options []
 
static const AVClass libjxl_encode_class
 
static enum AVPixelFormat libjxl_supported_pixfmts []
 
const FFCodec ff_libjxl_encoder
 
const FFCodec ff_libjxl_anim_encoder
 

Detailed Description

JPEG XL encoder using libjxl

Definition in file libjxlenc.c.

Macro Definition Documentation

◆ OFFSET

#define OFFSET (   x)    offsetof(LibJxlEncodeContext, x)

Definition at line 657 of file libjxlenc.c.

◆ VE

Definition at line 658 of file libjxlenc.c.

Function Documentation

◆ quality_to_distance()

static float quality_to_distance ( float  quality)
static

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.

Parameters
quality0.0 to 100.0 quality setting, libjpeg quality
Returns
Butteraugli distance between 0.0 and 15.0

Definition at line 81 of file libjxlenc.c.

Referenced by libjxl_encode_init().

◆ libjxl_init_jxl_encoder()

static int libjxl_init_jxl_encoder ( AVCodecContext avctx)
static

Initalize the encoder on a per-file basis.

All of these need to be set once each time the encoder is reset, which is each frame for still images, to make the image2 muxer work. For animation this is run once.

Returns
0 upon success, negative on failure.

Definition at line 102 of file libjxlenc.c.

Referenced by libjxl_anim_encode_init(), and libjxl_encode_frame().

◆ libjxl_encode_init()

static av_cold int libjxl_encode_init ( AVCodecContext avctx)
static

Global encoder initialization.

This only needs to be run once, not every frame.

Definition at line 129 of file libjxlenc.c.

Referenced by libjxl_anim_encode_init().

◆ libjxl_anim_encode_init()

static av_cold int libjxl_anim_encode_init ( AVCodecContext avctx)
static

Initializer for the animation encoder.

This calls the other initializers to prevent code duplication and also allocates the prev-frame used in the encoder.

Definition at line 179 of file libjxlenc.c.

◆ libjxl_populate_primaries()

static int libjxl_populate_primaries ( void *  avctx,
JxlColorEncoding *  jxl_color,
enum AVColorPrimaries  prm 
)
static

Populate a JxlColorEncoding with the given enum AVColorPrimaries.

Returns
< 0 upon failure, >= 0 upon success

Definition at line 203 of file libjxlenc.c.

Referenced by libjxl_preprocess_stream().

◆ libjxl_preprocess_stream()

static int libjxl_preprocess_stream ( AVCodecContext avctx,
const AVFrame frame,
int  animated 
)
static

Sends metadata to libjxl based on the first frame of the stream, such as pixel format, orientation, bit depth, and that sort of thing.

Definition at line 254 of file libjxlenc.c.

Referenced by libjxl_anim_encode_frame(), and libjxl_encode_frame().

◆ libjxl_preprocess_frame()

static int libjxl_preprocess_frame ( AVCodecContext avctx,
const AVFrame frame,
const uint8_t **  data 
)
static

Sends frame information to libjxl on a per-frame basis.

If this is a still image, this is evaluated once per output file. If this is an animated JPEG XL encode, it is called once per frame.

This returns a buffer to the data that should be passed to libjxl (via the argument **data). If the linesize is nonnegative, this will be frame->data[0], although if the linesize is negative, it will be the start of the buffer instead. *data is just a pointer to a location in frame->data so it should not be freed directly.

Definition at line 410 of file libjxlenc.c.

Referenced by libjxl_anim_encode_frame(), and libjxl_encode_frame().

◆ libjxl_process_output()

static int libjxl_process_output ( AVCodecContext avctx,
size_t *  bytes_written 
)
static

Run libjxl's output processing loop, reallocating the packet as necessary if libjxl needs more space to work with.

Definition at line 454 of file libjxlenc.c.

Referenced by libjxl_anim_encode_frame(), and libjxl_encode_frame().

◆ libjxl_encode_frame()

static int libjxl_encode_frame ( AVCodecContext avctx,
AVPacket pkt,
const AVFrame frame,
int *  got_packet 
)
static

Encode an entire frame.

This will always reinitialize a new still image and encode a one-frame image (for image2 and image2pipe).

Definition at line 499 of file libjxlenc.c.

◆ libjxl_anim_encode_frame()

static int libjxl_anim_encode_frame ( AVCodecContext avctx,
AVPacket pkt 
)
static

Encode one frame of the animation.

libjxl requires us to set duration of the frame but we're only promised the PTS, not the duration. As a result we have to buffer a frame and subtract the PTS from the last PTS. The last frame uses the previous frame's calculated duration as a fallback if its duration field is unset.

We also need to tell libjxl if our frame is the last one, which we won't know upon receiving a single frame, so we have to buffer a frame as well and send the "last frame" upon receiving the special EOF frame.

Definition at line 556 of file libjxlenc.c.

◆ libjxl_encode_close()

static av_cold int libjxl_encode_close ( AVCodecContext avctx)
static

Definition at line 634 of file libjxlenc.c.

Variable Documentation

◆ libjxl_encode_options

const AVOption libjxl_encode_options[]
static
Initial value:
= {
{ "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 = -1.0 }, -1.0, 15.0, VE },
{ "modular", "Force modular mode", OFFSET(modular), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
{ "xyb", "Use XYB-encoding for lossy images", OFFSET(xyb),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
{ NULL },
}

Definition at line 660 of file libjxlenc.c.

◆ libjxl_encode_class

const AVClass libjxl_encode_class
static
Initial value:
= {
.class_name = "libjxl",
.item_name = av_default_item_name,
}

Definition at line 670 of file libjxlenc.c.

◆ libjxl_supported_pixfmts

enum AVPixelFormat libjxl_supported_pixfmts[]
static

◆ ff_libjxl_encoder

const FFCodec ff_libjxl_encoder
Initial value:

Definition at line 687 of file libjxlenc.c.

◆ ff_libjxl_anim_encoder

const FFCodec ff_libjxl_anim_encoder
Initial value:

Definition at line 707 of file libjxlenc.c.

AV_CODEC_ID_JPEGXL_ANIM
@ AV_CODEC_ID_JPEGXL_ANIM
Definition: codec_id.h:331
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
AV_PIX_FMT_YA8
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:140
VE
#define VE
Definition: libjxlenc.c:658
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:35
libjxl_anim_encode_init
static av_cold int libjxl_anim_encode_init(AVCodecContext *avctx)
Initializer for the animation encoder.
Definition: libjxlenc.c:179
libjxl_anim_encode_frame
static int libjxl_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt)
Encode one frame of the animation.
Definition: libjxlenc.c:556
OFFSET
#define OFFSET(x)
Definition: libjxlenc.c:657
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:494
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
AV_CODEC_CAP_OTHER_THREADS
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:124
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
AV_PIX_FMT_RGBF32
#define AV_PIX_FMT_RGBF32
Definition: pixfmt.h:587
AV_PIX_FMT_GRAYF32
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:546
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
AV_PIX_FMT_RGBA64
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:500
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
NULL
#define NULL
Definition: coverity.c:32
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:326
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
LibJxlEncodeContext
Definition: libjxlenc.c:48
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
libjxl_encode_frame
static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Encode an entire frame.
Definition: libjxlenc.c:499
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:496
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
AV_PIX_FMT_YA16
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:495
libjxl_encode_init
static av_cold int libjxl_encode_init(AVCodecContext *avctx)
Global encoder initialization.
Definition: libjxlenc.c:129
AV_CODEC_ID_JPEGXL
@ AV_CODEC_ID_JPEGXL
Definition: codec_id.h:317
libjxl_encode_close
static av_cold int libjxl_encode_close(AVCodecContext *avctx)
Definition: libjxlenc.c:634
libjxl_supported_pixfmts
static enum AVPixelFormat libjxl_supported_pixfmts[]
Definition: libjxlenc.c:677
FF_CODEC_CAP_ICC_PROFILES
#define FF_CODEC_CAP_ICC_PROFILES
Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
Definition: codec_internal.h:82
libjxl_encode_class
static const AVClass libjxl_encode_class
Definition: libjxlenc.c:670
libjxl_encode_options
static const AVOption libjxl_encode_options[]
Definition: libjxlenc.c:660
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AV_PIX_FMT_RGBAF32
#define AV_PIX_FMT_RGBAF32
Definition: pixfmt.h:588
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:73
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:231