Go to the documentation of this file.
43 #include <jxl/encode.h>
44 #include <jxl/thread_parallel_runner.h>
78 return (100.0 -
quality) * 0.10;
80 return 0.1 + (100.0 -
quality) * 0.09;
99 JxlEncoderReset(
ctx->encoder);
101 ctx->options = JxlEncoderFrameSettingsCreate(
ctx->encoder,
NULL);
108 if (JxlEncoderSetParallelRunner(
ctx->encoder, JxlThreadParallelRunner,
ctx->runner)
109 != JXL_ENC_SUCCESS) {
115 if (JxlEncoderFrameSettingsSetOption(
ctx->options, JXL_ENC_FRAME_SETTING_EFFORT,
ctx->effort)
116 != JXL_ENC_SUCCESS) {
122 if (
ctx->distance < 0.0) {
135 if (
ctx->distance > 0.0 &&
ctx->distance < 0.01)
136 ctx->distance = 0.01;
137 if (JxlEncoderSetFrameDistance(
ctx->options,
ctx->distance) != JXL_ENC_SUCCESS) {
147 if (JxlEncoderFrameSettingsSetOption(
ctx->options, JXL_ENC_FRAME_SETTING_MODULAR,
148 ctx->modular ||
ctx->distance <= 0.0 ? 1 : -1) != JXL_ENC_SUCCESS) {
163 JxlMemoryManager manager;
166 ctx->encoder = JxlEncoderCreate(&manager);
178 ctx->buffer_size = 4096;
199 jxl_color->primaries = JXL_PRIMARIES_SRGB;
200 jxl_color->white_point = JXL_WHITE_POINT_D65;
203 jxl_color->primaries = JXL_PRIMARIES_2100;
204 jxl_color->white_point = JXL_WHITE_POINT_D65;
207 jxl_color->primaries = JXL_PRIMARIES_P3;
208 jxl_color->white_point = JXL_WHITE_POINT_DCI;
211 jxl_color->primaries = JXL_PRIMARIES_P3;
212 jxl_color->white_point = JXL_WHITE_POINT_D65;
215 av_log(avctx,
AV_LOG_WARNING,
"Unknown primaries, assuming BT.709/sRGB. Colors may be wrong.\n");
216 jxl_color->primaries = JXL_PRIMARIES_SRGB;
217 jxl_color->white_point = JXL_WHITE_POINT_D65;
225 jxl_color->primaries = JXL_PRIMARIES_CUSTOM;
226 jxl_color->white_point = JXL_WHITE_POINT_CUSTOM;
228 jxl_color->primaries_red_xy[0] =
av_q2d(
desc->prim.r.x);
229 jxl_color->primaries_red_xy[1] =
av_q2d(
desc->prim.r.y);
230 jxl_color->primaries_green_xy[0] =
av_q2d(
desc->prim.g.x);
231 jxl_color->primaries_green_xy[1] =
av_q2d(
desc->prim.g.y);
232 jxl_color->primaries_blue_xy[0] =
av_q2d(
desc->prim.b.x);
233 jxl_color->primaries_blue_xy[1] =
av_q2d(
desc->prim.b.y);
234 jxl_color->white_point_xy[0] =
av_q2d(
desc->wp.x);
235 jxl_color->white_point_xy[1] =
av_q2d(
desc->wp.y);
251 JxlColorEncoding jxl_color;
252 JxlPixelFormat jxl_fmt;
254 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
255 JxlBitDepth jxl_bit_depth;
257 JxlEncoderStatus jret;
260 size_t bytes_written = 0;
261 uint8_t *next_out =
ctx->buffer;
270 JxlEncoderInitBasicInfo(&
info);
274 info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
275 info.num_color_channels = jxl_fmt.num_channels -
info.num_extra_channels;
279 info.alpha_bits = (
info.num_extra_channels > 0) *
info.bits_per_sample;
281 info.exponent_bits_per_sample =
info.bits_per_sample > 16 ? 8 : 5;
282 info.alpha_exponent_bits =
info.alpha_bits ?
info.exponent_bits_per_sample : 0;
283 jxl_fmt.data_type =
info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : JXL_TYPE_FLOAT16;
285 info.exponent_bits_per_sample = 0;
286 info.alpha_exponent_bits = 0;
287 jxl_fmt.data_type =
info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;
290 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
291 jxl_bit_depth.bits_per_sample = bits_per_sample;
292 jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
294 info.exponent_bits_per_sample : 0;
300 av_log(avctx,
AV_LOG_WARNING,
"This encoder does not support limited (tv) range, colors will be wrong!\n");
305 info.uses_original_profile =
ctx->distance == 0.0;
307 if (JxlEncoderSetBasicInfo(
ctx->encoder, &
info) != JXL_ENC_SUCCESS) {
314 jxl_color.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
319 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_709;
322 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
325 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
328 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_DCI;
331 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_PQ;
334 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_HLG;
337 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
338 jxl_color.gamma = 1/2.2f;
341 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
342 jxl_color.gamma = 1/2.8f;
346 av_log(avctx,
AV_LOG_WARNING,
"Unknown transfer function, assuming Linear Light. Colors may be wrong.\n");
347 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
349 av_log(avctx,
AV_LOG_WARNING,
"Unknown transfer function, assuming IEC61966-2-1/sRGB. Colors may be wrong.\n");
350 jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
356 if (
info.num_color_channels == 1)
357 jxl_color.color_space = JXL_COLOR_SPACE_GRAY;
359 jxl_color.color_space = JXL_COLOR_SPACE_RGB;
368 if (sd && sd->
size && JxlEncoderSetICCProfile(
ctx->encoder, sd->
data, sd->
size) != JXL_ENC_SUCCESS)
370 if (JxlEncoderSetColorEncoding(
ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)
373 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
374 if (JxlEncoderSetFrameBitDepth(
ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)
380 if (JxlEncoderGetRequiredCodestreamLevel(
ctx->encoder) > 5) {
381 if (JxlEncoderSetCodestreamLevel(
ctx->encoder, 10) != JXL_ENC_SUCCESS)
385 jxl_fmt.endianness = JXL_NATIVE_ENDIAN;
388 if (JxlEncoderAddImageFrame(
ctx->options, &jxl_fmt,
frame->
data[0], jxl_fmt.align *
info.ysize) != JXL_ENC_SUCCESS) {
397 JxlEncoderCloseInput(
ctx->encoder);
400 jret = JxlEncoderProcessOutput(
ctx->encoder, &next_out, &
available);
401 if (jret == JXL_ENC_ERROR) {
407 if (jret == JXL_ENC_SUCCESS)
409 if (jret == JXL_ENC_NEED_MORE_OUTPUT) {
416 size_t new_size =
ctx->buffer_size * 2;
421 ctx->buffer_size = new_size;
422 next_out =
ctx->buffer + bytes_written;
434 memcpy(
pkt->
data,
ctx->buffer, bytes_written);
445 JxlThreadParallelRunnerDestroy(
ctx->runner);
453 JxlEncoderDestroy(
ctx->encoder);
461 #define OFFSET(x) offsetof(LibJxlEncodeContext, x)
462 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
466 {
"distance",
"Maximum Butteraugli distance (quality setting, "
502 .p.wrapper_name =
"libjxl",
enum AVColorTransferCharacteristic color_trc
JxlEncoderFrameSettings * options
#define AV_LOG_WARNING
Something somehow does not look correct.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
AVPixelFormat
Pixel format.
size_t ff_libjxl_get_threadcount(int threads)
Transform threadcount in ffmpeg to one used by libjxl.
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Struct that contains both white point location and primaries location, providing the complete descrip...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
enum AVColorPrimaries color_primaries
This structure describes decoded (raw) audio or video data.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
@ AVCOL_RANGE_JPEG
Full range content.
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
AVColorPrimaries
Chromaticity coordinates of the source primaries.
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
AVCodec p
The public AVCodec.
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
int flags
AV_CODEC_FLAG_*.
#define AV_PIX_FMT_GRAY16
#define FF_CODEC_ENCODE_CB(func)
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int global_quality
Global quality for codecs which cannot change it per frame.
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_PIX_FMT_GRAYF32
#define CODEC_LONG_NAME(str)
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
#define AV_PIX_FMT_RGBA64
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
uint8_t nb_components
The number of components each pixel has, (1-4)
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
static int libjxl_init_jxl_encoder(AVCodecContext *avctx)
Initalize the decoder on a per-frame basis.
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
@ AVCOL_RANGE_UNSPECIFIED
@ AVCOL_PRI_BT2020
ITU-R BT2020.
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Encode an entire frame.
const FFCodec ff_libjxl_encoder
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
#define AVERROR_EXTERNAL
Generic error in an external library.
static float quality_to_distance(float quality)
Map a quality setting for -qscale roughly from libjpeg quality numbers to libjxl's butteraugli distan...
@ AVCOL_TRC_BT709
also ITU-R BT1361
static int libjxl_populate_primaries(void *avctx, JxlColorEncoding *jxl_color, enum AVColorPrimaries prm)
Populate a JxlColorEncoding with the given enum AVColorPrimaries.
static av_cold int libjxl_encode_init(AVCodecContext *avctx)
Global encoder initialization.
static av_cold int libjxl_encode_close(AVCodecContext *avctx)
const char * name
Name of the codec implementation.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
#define FF_CODEC_CAP_ICC_PROFILES
Codec supports embedded ICC profiles (AV_FRAME_DATA_ICC_PROFILE).
static const AVClass libjxl_encode_class
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
static const AVOption libjxl_encode_options[]
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Structure to hold side data for an AVFrame.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
This structure stores compressed data.
void ff_libjxl_init_memory_manager(JxlMemoryManager *manager)
Initialize and populate a JxlMemoryManager with av_malloc() and av_free() so libjxl will use these fu...
static float distance(float x, float y, int band)
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.