FFmpeg
libjxlenc.c
Go to the documentation of this file.
1 /*
2  * JPEG XL encoding support via libjxl
3  * Copyright (c) 2021 Leo Izen <leo.izen@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * JPEG XL encoder using libjxl
25  */
26 
27 #include <string.h>
28 
29 #include "libavutil/avutil.h"
30 #include "libavutil/csp.h"
31 #include "libavutil/display.h"
32 #include "libavutil/error.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/libm.h"
35 #include "libavutil/mem.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/pixfmt.h"
39 #include "libavutil/version.h"
40 
41 #include "avcodec.h"
42 #include "encode.h"
43 #include "codec_internal.h"
44 #include "exif_internal.h"
45 
46 #include <jxl/encode.h>
47 #include <jxl/thread_parallel_runner.h>
48 #include "libjxl.h"
49 
50 typedef struct LibJxlEncodeContext {
51  AVClass *class;
52  void *runner;
53  JxlEncoder *encoder;
54  JxlEncoderFrameSettings *options;
55  int effort;
56  float distance;
57  int modular;
58  int xyb;
59  uint8_t *buffer;
60  size_t buffer_size;
61  JxlPixelFormat jxl_fmt;
62 
63  /* animation stuff */
68 
69 /**
70  * Map a quality setting for -qscale roughly from libjpeg
71  * quality numbers to libjxl's butteraugli distance for
72  * photographic content.
73  *
74  * Setting distance explicitly is preferred, but this will
75  * allow qscale to be used as a fallback.
76  *
77  * This function is continuous and injective on [0, 100] which
78  * makes it monotonic.
79  *
80  * @param quality 0.0 to 100.0 quality setting, libjpeg quality
81  * @return Butteraugli distance between 0.0 and 15.0
82  */
83 static float quality_to_distance(float quality)
84 {
85  if (quality >= 100.0)
86  return 0.0;
87  else if (quality >= 90.0)
88  return (100.0 - quality) * 0.10;
89  else if (quality >= 30.0)
90  return 0.1 + (100.0 - quality) * 0.09;
91  else if (quality > 0.0)
92  return 15.0 + (59.0 * quality - 4350.0) * quality / 9000.0;
93  else
94  return 15.0;
95 }
96 
97 /**
98  * Initialize the encoder on a per-file basis. All of these need to be set
99  * once each time the encoder is reset, which is each frame for still
100  * images, to make the image2 muxer work. For animation this is run once.
101  *
102  * @return 0 upon success, negative on failure.
103  */
105 {
107 
108  /* reset the encoder every frame for image2 muxer */
109  JxlEncoderReset(ctx->encoder);
110 
111  /* This needs to be set each time the encoder is reset */
112  if (JxlEncoderSetParallelRunner(ctx->encoder, JxlThreadParallelRunner, ctx->runner)
113  != JXL_ENC_SUCCESS) {
114  av_log(avctx, AV_LOG_ERROR, "Failed to set JxlThreadParallelRunner\n");
115  return AVERROR_EXTERNAL;
116  }
117 
118  ctx->options = JxlEncoderFrameSettingsCreate(ctx->encoder, NULL);
119  if (!ctx->options) {
120  av_log(avctx, AV_LOG_ERROR, "Failed to create JxlEncoderOptions\n");
121  return AVERROR_EXTERNAL;
122  }
123 
124  return 0;
125 }
126 
127 /**
128  * Global encoder initialization. This only needs to be run once,
129  * not every frame.
130  */
132 {
134  JxlMemoryManager manager;
135 
137  ctx->encoder = JxlEncoderCreate(&manager);
138  if (!ctx->encoder) {
139  av_log(avctx, AV_LOG_ERROR, "Failed to create JxlEncoder\n");
140  return AVERROR_EXTERNAL;
141  }
142 
143  ctx->runner = JxlThreadParallelRunnerCreate(&manager, ff_libjxl_get_threadcount(avctx->thread_count));
144  if (!ctx->runner) {
145  av_log(avctx, AV_LOG_ERROR, "Failed to create JxlThreadParallelRunner\n");
146  return AVERROR_EXTERNAL;
147  }
148 
149  ctx->buffer_size = 4096;
150  ctx->buffer = av_realloc(NULL, ctx->buffer_size);
151 
152  if (!ctx->buffer) {
153  av_log(avctx, AV_LOG_ERROR, "Could not allocate encoding buffer\n");
154  return AVERROR(ENOMEM);
155  }
156 
157  /* check for negative, our default */
158  if (ctx->distance < 0.0) {
159  /* use ffmpeg.c -q option if passed */
160  if (avctx->flags & AV_CODEC_FLAG_QSCALE)
161  ctx->distance = quality_to_distance((float)avctx->global_quality / FF_QP2LAMBDA);
162  else
163  /* default 1.0 matches cjxl */
164  ctx->distance = 1.0;
165  }
166  /*
167  * 0.01 is the minimum distance accepted for lossy
168  * interpreting any positive value less than this as minimum
169  */
170  if (ctx->distance > 0.0 && ctx->distance < 0.01)
171  ctx->distance = 0.01;
172 
173  return 0;
174 }
175 
176 /**
177  * Initializer for the animation encoder. This calls the other initializers
178  * to prevent code duplication and also allocates the prev-frame used in the
179  * encoder.
180  */
182 {
183  int ret;
185 
186  ret = libjxl_encode_init(avctx);
187  if (ret < 0)
188  return ret;
189 
190  ret = libjxl_init_jxl_encoder(avctx);
191  if (ret < 0)
192  return ret;
193 
194  ctx->frame = av_frame_alloc();
195  if (!ctx->frame)
196  return AVERROR(ENOMEM);
197 
198  return 0;
199 }
200 
201 /**
202  * Populate a JxlColorEncoding with the given enum AVColorPrimaries.
203  * @return < 0 upon failure, >= 0 upon success
204  */
205 static int libjxl_populate_primaries(void *avctx, JxlColorEncoding *jxl_color, enum AVColorPrimaries prm)
206 {
207  const AVColorPrimariesDesc *desc;
208 
209  switch (prm) {
210  case AVCOL_PRI_BT709:
211  jxl_color->primaries = JXL_PRIMARIES_SRGB;
212  jxl_color->white_point = JXL_WHITE_POINT_D65;
213  return 0;
214  case AVCOL_PRI_BT2020:
215  jxl_color->primaries = JXL_PRIMARIES_2100;
216  jxl_color->white_point = JXL_WHITE_POINT_D65;
217  return 0;
218  case AVCOL_PRI_SMPTE431:
219  jxl_color->primaries = JXL_PRIMARIES_P3;
220  jxl_color->white_point = JXL_WHITE_POINT_DCI;
221  return 0;
222  case AVCOL_PRI_SMPTE432:
223  jxl_color->primaries = JXL_PRIMARIES_P3;
224  jxl_color->white_point = JXL_WHITE_POINT_D65;
225  return 0;
227  av_log(avctx, AV_LOG_WARNING, "Unknown primaries, assuming BT.709/sRGB. Colors may be wrong.\n");
228  jxl_color->primaries = JXL_PRIMARIES_SRGB;
229  jxl_color->white_point = JXL_WHITE_POINT_D65;
230  return 0;
231  }
232 
234  if (!desc)
235  return AVERROR(EINVAL);
236 
237  jxl_color->primaries = JXL_PRIMARIES_CUSTOM;
238  jxl_color->white_point = JXL_WHITE_POINT_CUSTOM;
239 
240  jxl_color->primaries_red_xy[0] = av_q2d(desc->prim.r.x);
241  jxl_color->primaries_red_xy[1] = av_q2d(desc->prim.r.y);
242  jxl_color->primaries_green_xy[0] = av_q2d(desc->prim.g.x);
243  jxl_color->primaries_green_xy[1] = av_q2d(desc->prim.g.y);
244  jxl_color->primaries_blue_xy[0] = av_q2d(desc->prim.b.x);
245  jxl_color->primaries_blue_xy[1] = av_q2d(desc->prim.b.y);
246  jxl_color->white_point_xy[0] = av_q2d(desc->wp.x);
247  jxl_color->white_point_xy[1] = av_q2d(desc->wp.y);
248 
249  return 0;
250 }
251 
253  const AVPixFmtDescriptor *pix_desc, const JxlBasicInfo *info)
254 {
255  JxlColorEncoding jxl_color;
257  int ret;
258 
259  switch (frame->color_trc && frame->color_trc != AVCOL_TRC_UNSPECIFIED
260  ? frame->color_trc : avctx->color_trc) {
261  case AVCOL_TRC_BT709:
262  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_709;
263  break;
264  case AVCOL_TRC_LINEAR:
265  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
266  break;
268  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
269  break;
270  case AVCOL_TRC_SMPTE428:
271  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_DCI;
272  break;
273  case AVCOL_TRC_SMPTE2084:
274  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_PQ;
275  break;
277  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_HLG;
278  break;
279  case AVCOL_TRC_GAMMA22:
280  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
281  jxl_color.gamma = 1/2.2f;
282  break;
283  case AVCOL_TRC_GAMMA28:
284  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA;
285  jxl_color.gamma = 1/2.8f;
286  break;
287  default:
288  if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
289  av_log(avctx, AV_LOG_WARNING,
290  "Unknown transfer function, assuming Linear Light. Colors may be wrong.\n");
291  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
292  } else {
293  av_log(avctx, AV_LOG_WARNING,
294  "Unknown transfer function, assuming IEC61966-2-1/sRGB. Colors may be wrong.\n");
295  jxl_color.transfer_function = JXL_TRANSFER_FUNCTION_SRGB;
296  }
297  }
298 
299  jxl_color.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
300  if (info->num_color_channels == 1)
301  jxl_color.color_space = JXL_COLOR_SPACE_GRAY;
302  else
303  jxl_color.color_space = JXL_COLOR_SPACE_RGB;
304 
305  ret = libjxl_populate_primaries(avctx, &jxl_color,
306  frame->color_primaries && frame->color_primaries != AVCOL_PRI_UNSPECIFIED
307  ? frame->color_primaries : avctx->color_primaries);
308  if (ret < 0)
309  return ret;
310 
311  if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS) {
312  av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");
313  return AVERROR_EXTERNAL;
314  }
315 
316  return 0;
317 }
318 
319 /**
320  * Sends metadata to libjxl based on the first frame of the stream, such as pixel format,
321  * orientation, bit depth, and that sort of thing.
322  */
323 static int libjxl_preprocess_stream(AVCodecContext *avctx, const AVFrame *frame, int animated)
324 {
326  AVFrameSideData *sd;
327  int32_t *matrix = (int32_t[9]){ 0 };
328  int ret = 0;
329  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(frame->format);
330  JxlBasicInfo info;
331  JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
332  int bits_per_sample;
333  int orientation;
334  AVBufferRef *exif_buffer = NULL;
335 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
336  JxlBitDepth jxl_bit_depth;
337 #endif
338 
339  /* populate the basic info settings */
340  JxlEncoderInitBasicInfo(&info);
341  jxl_fmt->num_channels = pix_desc->nb_components;
342  info.xsize = frame->width;
343  info.ysize = frame->height;
344  info.num_extra_channels = (jxl_fmt->num_channels + 1) & 0x1;
345  info.num_color_channels = jxl_fmt->num_channels - info.num_extra_channels;
346  bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt->num_channels;
347  info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)
348  ? avctx->bits_per_raw_sample : bits_per_sample;
349  info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
350  if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
351  info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
352  info.alpha_exponent_bits = info.alpha_bits ? info.exponent_bits_per_sample : 0;
353  jxl_fmt->data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : JXL_TYPE_FLOAT16;
354  } else {
355  info.exponent_bits_per_sample = 0;
356  info.alpha_exponent_bits = 0;
357  jxl_fmt->data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;
358  }
359 
360  if (info.alpha_bits) {
361  if (avctx->alpha_mode == AVALPHA_MODE_PREMULTIPLIED ||
363  info.alpha_premultiplied = 1;
364  } else if (avctx->alpha_mode != AVALPHA_MODE_STRAIGHT && frame->alpha_mode != AVALPHA_MODE_STRAIGHT) {
365  av_log(avctx, AV_LOG_WARNING, "Unknown alpha mode, assuming straight (independent)\n");
366  }
367  }
368 
369 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
370  jxl_bit_depth.bits_per_sample = bits_per_sample;
371  jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
372  jxl_bit_depth.exponent_bits_per_sample = pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT ?
373  info.exponent_bits_per_sample : 0;
374 #endif
375 
376  /* JPEG XL format itself does not support limited range */
377  if (avctx->color_range == AVCOL_RANGE_MPEG ||
378  avctx->color_range == AVCOL_RANGE_UNSPECIFIED && frame->color_range == AVCOL_RANGE_MPEG)
379  av_log(avctx, AV_LOG_WARNING, "This encoder does not support limited (tv) range, colors will be wrong!\n");
380  else if (avctx->color_range != AVCOL_RANGE_JPEG && frame->color_range != AVCOL_RANGE_JPEG)
381  av_log(avctx, AV_LOG_WARNING, "Unknown color range, assuming full (pc)\n");
382 
383  /* bitexact lossless requires there to be no XYB transform */
384  info.uses_original_profile = ctx->distance == 0.0 || !ctx->xyb;
385 
387  if (sd) {
388  AVExifMetadata ifd = { 0 };
389  AVExifEntry *orient = NULL;
390  uint16_t tag = av_exif_get_tag_id("Orientation");
391  ret = av_exif_parse_buffer(avctx, sd->data, sd->size, &ifd, AV_EXIF_TIFF_HEADER);
392  if (ret >= 0)
393  ret = ff_exif_sanitize_ifd(avctx, frame, &ifd);
394  if (ret >= 0)
395  ret = av_exif_get_entry(avctx, &ifd, tag, 0, &orient);
396  if (ret >= 0 && orient && orient->value.uint[0] >= 1 && orient->value.uint[0] <= 8) {
398  ret = av_exif_remove_entry(avctx, &ifd, tag, 0);
399  } else {
401  }
402  if (ret >= 0)
403  ret = av_exif_write(avctx, &ifd, &exif_buffer, AV_EXIF_TIFF_HEADER);
404  if (ret < 0)
405  av_log(avctx, AV_LOG_WARNING, "unable to process EXIF frame data\n");
406  av_exif_free(&ifd);
407  } else {
409  if (sd)
410  matrix = (int32_t *) sd->data;
411  else
413  }
414 
415  /* av_display_matrix_flip is a right-multipilcation */
416  /* i.e. flip is applied before the previous matrix */
417  if (frame->linesize < 0)
419 
420  orientation = av_exif_matrix_to_orientation(matrix);
421  /* JPEG XL orientation flag agrees with EXIF for values 1-8 */
422  if (orientation) {
423  info.orientation = orientation;
424  } else {
425  av_log(avctx, AV_LOG_WARNING, "singular displaymatrix data\n");
426  info.orientation = frame->linesize[0] >= 0 ? JXL_ORIENT_IDENTITY : JXL_ORIENT_FLIP_VERTICAL;
427  }
428 
429  /* restore the previous value */
430  if (frame->linesize < 0)
432 
433  if (animated) {
434  info.have_animation = 1;
435  info.animation.have_timecodes = 0;
436  info.animation.num_loops = 0;
437  /* avctx->timebase is in seconds per tick, so we take the reciprocol */
438  info.animation.tps_numerator = avctx->time_base.den;
439  info.animation.tps_denominator = avctx->time_base.num;
440  }
441 
442  if (JxlEncoderSetBasicInfo(ctx->encoder, &info) != JXL_ENC_SUCCESS) {
443  av_log(avctx, AV_LOG_ERROR, "Failed to set JxlBasicInfo\n");
445  goto end;
446  }
447 
448  if (info.alpha_bits) {
449  JxlExtraChannelInfo extra_info;
450  JxlEncoderInitExtraChannelInfo(JXL_CHANNEL_ALPHA, &extra_info);
451  extra_info.bits_per_sample = info.alpha_bits;
452  extra_info.exponent_bits_per_sample = info.alpha_exponent_bits;
453  extra_info.alpha_premultiplied = info.alpha_premultiplied;
454 
455  if (JxlEncoderSetExtraChannelInfo(ctx->encoder, 0, &extra_info) != JXL_ENC_SUCCESS) {
456  av_log(avctx, AV_LOG_ERROR, "Failed to set JxlExtraChannelInfo for alpha!\n");
457  return AVERROR_EXTERNAL;
458  }
459  }
460 
462  if (sd && sd->size && JxlEncoderSetICCProfile(ctx->encoder, sd->data, sd->size) != JXL_ENC_SUCCESS) {
463  av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
464  sd = NULL;
465  }
466 
467  if (!sd || !sd->size)
468  libjxl_populate_colorspace(avctx, frame, pix_desc, &info);
469 
470 #if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
471  if (JxlEncoderSetFrameBitDepth(ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)
472  av_log(avctx, AV_LOG_WARNING, "Failed to set JxlBitDepth\n");
473 #endif
474 
475  if (exif_buffer) {
476  if (JxlEncoderUseBoxes(ctx->encoder) != JXL_ENC_SUCCESS)
477  av_log(avctx, AV_LOG_WARNING, "Couldn't enable UseBoxes\n");
478  }
479 
480  /* depending on basic info, level 10 might
481  * be required instead of level 5 */
482  if (JxlEncoderGetRequiredCodestreamLevel(ctx->encoder) > 5) {
483  if (JxlEncoderSetCodestreamLevel(ctx->encoder, 10) != JXL_ENC_SUCCESS)
484  av_log(avctx, AV_LOG_WARNING, "Could not increase codestream level\n");
485  }
486 
487 end:
488  av_buffer_unref(&exif_buffer);
489  return ret;
490 }
491 
492 /**
493  * Sends frame information to libjxl on a per-frame basis. If this is a still image,
494  * this is evaluated once per output file. If this is an animated JPEG XL encode, it
495  * is called once per frame.
496  *
497  * This returns a buffer to the data that should be passed to libjxl (via the
498  * argument **data). If the linesize is nonnegative, this will be frame->data[0],
499  * although if the linesize is negative, it will be the start of the buffer
500  * instead. *data is just a pointer to a location in frame->data so it should not be
501  * freed directly.
502  */
503 static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame *frame, const uint8_t **data)
504 {
506  JxlPixelFormat *jxl_fmt = &ctx->jxl_fmt;
507 
508  /* these shouldn't fail, libjxl bug notwithstanding */
509  if (JxlEncoderFrameSettingsSetOption(ctx->options, JXL_ENC_FRAME_SETTING_EFFORT, ctx->effort)
510  != JXL_ENC_SUCCESS) {
511  av_log(avctx, AV_LOG_ERROR, "Failed to set effort to: %d\n", ctx->effort);
512  return AVERROR_EXTERNAL;
513  }
514 
515  if (JxlEncoderSetFrameDistance(ctx->options, ctx->distance) != JXL_ENC_SUCCESS) {
516  av_log(avctx, AV_LOG_ERROR, "Failed to set distance: %f\n", ctx->distance);
517  return AVERROR_EXTERNAL;
518  }
519 
520  /*
521  * In theory the library should automatically enable modular if necessary,
522  * but it appears it won't at the moment due to a bug. This will still
523  * work even if that is patched.
524  */
525  if (JxlEncoderFrameSettingsSetOption(ctx->options, JXL_ENC_FRAME_SETTING_MODULAR,
526  ctx->modular || ctx->distance <= 0.0 ? 1 : -1) != JXL_ENC_SUCCESS) {
527  av_log(avctx, AV_LOG_ERROR, "Failed to set modular\n");
528  return AVERROR_EXTERNAL;
529  }
530 
531  jxl_fmt->endianness = JXL_NATIVE_ENDIAN;
532  if (frame->linesize[0] >= 0) {
533  jxl_fmt->align = frame->linesize[0];
534  *data = frame->data[0];
535  } else {
536  jxl_fmt->align = -frame->linesize[0];
537  *data = frame->data[0] + frame->linesize[0] * (frame->height - 1);
538  }
539 
540  return 0;
541 }
542 
543 /**
544  * Run libjxl's output processing loop, reallocating the packet as necessary
545  * if libjxl needs more space to work with.
546  */
547 static int libjxl_process_output(AVCodecContext *avctx, size_t *bytes_written)
548 {
550  JxlEncoderStatus jret;
551  size_t available = ctx->buffer_size;
552  uint8_t *next_out = ctx->buffer;
553 
554  while (1) {
555  jret = JxlEncoderProcessOutput(ctx->encoder, &next_out, &available);
556  if (jret == JXL_ENC_ERROR) {
557  av_log(avctx, AV_LOG_ERROR, "Unspecified libjxl error occurred\n");
558  return AVERROR_EXTERNAL;
559  }
560  *bytes_written = ctx->buffer_size - available;
561  /* all data passed has been encoded */
562  if (jret == JXL_ENC_SUCCESS)
563  break;
564  if (jret == JXL_ENC_NEED_MORE_OUTPUT) {
565  /*
566  * at the moment, libjxl has no way to
567  * tell us how much space it actually needs
568  * so we need to malloc loop
569  */
570  uint8_t *temp;
571  size_t new_size = ctx->buffer_size * 2;
572  temp = av_realloc(ctx->buffer, new_size);
573  if (!temp)
574  return AVERROR(ENOMEM);
575  ctx->buffer = temp;
576  ctx->buffer_size = new_size;
577  next_out = ctx->buffer + *bytes_written;
578  available = new_size - *bytes_written;
579  continue;
580  }
581  av_log(avctx, AV_LOG_ERROR, "Bad libjxl event: %d\n", jret);
582  return AVERROR_EXTERNAL;
583  }
584 
585  return 0;
586 }
587 
588 /**
589  * Encode an entire frame. This will always reinitialize a new still image
590  * and encode a one-frame image (for image2 and image2pipe).
591  */
592 static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
593 {
594 
596  int ret;
597  size_t bytes_written = 0;
598  const uint8_t *data;
599 
600  ret = libjxl_init_jxl_encoder(avctx);
601  if (ret < 0) {
602  av_log(avctx, AV_LOG_ERROR, "Error frame-initializing JxlEncoder\n");
603  return ret;
604  }
605 
606  ret = libjxl_preprocess_stream(avctx, frame, 0);
607  if (ret < 0)
608  return ret;
609 
611  if (ret < 0)
612  return ret;
613 
614  if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, ctx->jxl_fmt.align * frame->height)
615  != JXL_ENC_SUCCESS) {
616  av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
617  return AVERROR_EXTERNAL;
618  }
619 
620  /*
621  * Run this after the last frame in the image has been passed.
622  */
623  JxlEncoderCloseInput(ctx->encoder);
624 
625  ret = libjxl_process_output(avctx, &bytes_written);
626  if (ret < 0)
627  return ret;
628 
629  ret = ff_get_encode_buffer(avctx, pkt, bytes_written, 0);
630  if (ret < 0)
631  return ret;
632 
633  memcpy(pkt->data, ctx->buffer, bytes_written);
634  *got_packet = 1;
635 
636  return 0;
637 }
638 
639 /**
640  * Encode one frame of the animation. libjxl requires us to set duration of the frame
641  * but we're only promised the PTS, not the duration. As a result we have to buffer
642  * a frame and subtract the PTS from the last PTS. The last frame uses the previous
643  * frame's calculated duration as a fallback if its duration field is unset.
644  *
645  * We also need to tell libjxl if our frame is the last one, which we won't know upon
646  * receiving a single frame, so we have to buffer a frame as well and send the "last frame"
647  * upon receiving the special EOF frame.
648  */
650 {
652  int ret = 0;
653  JxlFrameHeader frame_header;
654  size_t bytes_written = 0;
655  const uint8_t *data;
656 
657  if (!ctx->prev) {
658  ctx->prev = av_frame_alloc();
659  if (!ctx->prev)
660  return AVERROR(ENOMEM);
661  ret = ff_encode_get_frame(avctx, ctx->prev);
662  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
663  return ret;
664  ret = libjxl_preprocess_stream(avctx, ctx->prev, 1);
665  if (ret < 0)
666  return ret;
667  }
668 
669  ret = ff_encode_get_frame(avctx, ctx->frame);
670  if (ret == AVERROR_EOF) {
671  av_frame_free(&ctx->frame);
672  ret = 0;
673  }
674  if (ret == AVERROR(EAGAIN))
675  return ret;
676 
677  JxlEncoderInitFrameHeader(&frame_header);
678 
679  ctx->duration = ctx->prev->duration ? ctx->prev->duration :
680  ctx->frame ? ctx->frame->pts - ctx->prev->pts :
681  ctx->duration ? ctx->duration :
682  1;
683 
684  frame_header.duration = ctx->duration;
685  pkt->duration = ctx->duration;
686  pkt->pts = ctx->prev->pts;
687  pkt->dts = pkt->pts;
688 
689  if (JxlEncoderSetFrameHeader(ctx->options, &frame_header) != JXL_ENC_SUCCESS) {
690  av_log(avctx, AV_LOG_ERROR, "Failed to set JxlFrameHeader\n");
691  return AVERROR_EXTERNAL;
692  }
693 
694  ret = libjxl_preprocess_frame(avctx, ctx->prev, &data);
695  if (ret < 0)
696  return ret;
697 
698  if (JxlEncoderAddImageFrame(ctx->options, &ctx->jxl_fmt, data, ctx->jxl_fmt.align * ctx->prev->height)
699  != JXL_ENC_SUCCESS) {
700  av_log(avctx, AV_LOG_ERROR, "Failed to add Image Frame\n");
701  return AVERROR_EXTERNAL;
702  }
703 
704  if (!ctx->frame)
705  JxlEncoderCloseInput(ctx->encoder);
706 
707  ret = libjxl_process_output(avctx, &bytes_written);
708  if (ret < 0)
709  return ret;
710 
711  ret = ff_get_encode_buffer(avctx, pkt, bytes_written, 0);
712  if (ret < 0)
713  return ret;
714 
715  memcpy(pkt->data, ctx->buffer, bytes_written);
716 
717  if (ctx->frame) {
718  av_frame_unref(ctx->prev);
719  av_frame_move_ref(ctx->prev, ctx->frame);
720  } else {
721  av_frame_free(&ctx->prev);
722  }
723 
724  return ret;
725 }
726 
728 {
730 
731  if (ctx->runner)
732  JxlThreadParallelRunnerDestroy(ctx->runner);
733  ctx->runner = NULL;
734 
735  /*
736  * destroying the encoder also frees
737  * ctx->options so we don't need to
738  */
739  if (ctx->encoder)
740  JxlEncoderDestroy(ctx->encoder);
741  ctx->encoder = NULL;
742 
743  av_freep(&ctx->buffer);
744  av_frame_free(&ctx->prev);
745  av_frame_free(&ctx->frame);
746 
747  return 0;
748 }
749 
750 #define OFFSET(x) offsetof(LibJxlEncodeContext, x)
751 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
752 
753 static const AVOption libjxl_encode_options[] = {
754  { "effort", "Encoding effort", OFFSET(effort), AV_OPT_TYPE_INT, { .i64 = 7 }, 1, 9, VE },
755  { "distance", "Maximum Butteraugli distance (quality setting, "
756  "lower = better, zero = lossless, default 1.0)", OFFSET(distance), AV_OPT_TYPE_FLOAT, { .dbl = -1.0 }, -1.0, 15.0, VE },
757  { "modular", "Force modular mode", OFFSET(modular), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
758  { "xyb", "Use XYB-encoding for lossy images", OFFSET(xyb),
759  AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
760  { NULL },
761 };
762 
763 static const AVClass libjxl_encode_class = {
764  .class_name = "libjxl",
765  .item_name = av_default_item_name,
766  .option = libjxl_encode_options,
767  .version = LIBAVUTIL_VERSION_INT,
768 };
769 
778 };
779 
781  .p.name = "libjxl",
782  CODEC_LONG_NAME("libjxl JPEG XL"),
783  .p.type = AVMEDIA_TYPE_VIDEO,
784  .p.id = AV_CODEC_ID_JPEGXL,
785  .priv_data_size = sizeof(LibJxlEncodeContext),
788  .close = libjxl_encode_close,
789  .p.capabilities = AV_CODEC_CAP_OTHER_THREADS |
792  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
796  .alpha_modes = (const enum AVAlphaMode[]) {
798  },
799  .p.priv_class = &libjxl_encode_class,
800  .p.wrapper_name = "libjxl",
801 };
802 
804  .p.name = "libjxl_anim",
805  CODEC_LONG_NAME("libjxl JPEG XL animated"),
806  .p.type = AVMEDIA_TYPE_VIDEO,
807  .p.id = AV_CODEC_ID_JPEGXL_ANIM,
808  .priv_data_size = sizeof(LibJxlEncodeContext),
811  .close = libjxl_encode_close,
812  .p.capabilities = AV_CODEC_CAP_OTHER_THREADS |
815  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
819  .alpha_modes = (const enum AVAlphaMode[]) {
821  },
822  .p.priv_class = &libjxl_encode_class,
823  .p.wrapper_name = "libjxl",
824 };
LibJxlEncodeContext::effort
int effort
Definition: libjxlenc.c:55
LibJxlEncodeContext::options
JxlEncoderFrameSettings * options
Definition: libjxlenc.c:54
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_CODEC_ID_JPEGXL_ANIM
@ AV_CODEC_ID_JPEGXL_ANIM
Definition: codec_id.h:331
LibJxlEncodeContext::buffer_size
size_t buffer_size
Definition: libjxlenc.c:60
ff_libjxl_get_threadcount
size_t ff_libjxl_get_threadcount(int threads)
Transform threadcount in ffmpeg to one used by libjxl.
Definition: libjxl.c:33
AVCodecContext::alpha_mode
enum AVAlphaMode alpha_mode
Indicates how the alpha channel of the video is represented.
Definition: avcodec.h:1932
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:42
libjxl_process_output
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 ...
Definition: libjxlenc.c:547
AVERROR
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
opt.h
AV_PIX_FMT_YA8
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
Definition: pixfmt.h:140
AVALPHA_MODE_STRAIGHT
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
Definition: pixfmt.h:822
AVALPHA_MODE_PREMULTIPLIED
@ AVALPHA_MODE_PREMULTIPLIED
Alpha channel is multiplied into color values.
Definition: pixfmt.h:821
av_exif_parse_buffer
int av_exif_parse_buffer(void *logctx, const uint8_t *buf, size_t size, AVExifMetadata *ifd, enum AVExifHeaderMode header_mode)
Decodes the EXIF data provided in the buffer and writes it into the struct *ifd.
Definition: exif.c:767
libm.h
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:659
VE
#define VE
Definition: libjxlenc.c:751
AVExifEntry
Definition: exif.h:85
av_exif_write
int av_exif_write(void *logctx, const AVExifMetadata *ifd, AVBufferRef **buffer, enum AVExifHeaderMode header_mode)
Allocates a buffer using av_malloc of an appropriate size and writes the EXIF data represented by ifd...
Definition: exif.c:706
AVExifMetadata
Definition: exif.h:76
AVColorPrimariesDesc
Struct that contains both white point location and primaries location, providing the complete descrip...
Definition: csp.h:78
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3585
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
LibJxlEncodeContext::runner
void * runner
Definition: libjxlenc.c:52
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:689
frame_header
static int FUNC() frame_header(CodedBitstreamContext *ctx, RWContext *rw, APVRawFrameHeader *current)
Definition: cbs_apv_syntax_template.c:142
matrix
Definition: vc1dsp.c:43
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:213
int64_t
long long int64_t
Definition: coverity.c:34
av_exif_orientation_to_matrix
int av_exif_orientation_to_matrix(int32_t *matrix, int orientation)
Convert an orientation constant used by EXIF's orientation tag into a display matrix used by AV_FRAME...
Definition: exif.c:1197
AV_PIX_FMT_FLAG_FLOAT
#define AV_PIX_FMT_FLAG_FLOAT
The pixel format contains IEEE-754 floating point values.
Definition: pixdesc.h:158
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
pixdesc.h
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:652
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:786
av_display_matrix_flip
void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
Flip the input matrix horizontally and/or vertically.
Definition: display.c:66
AVPacket::data
uint8_t * data
Definition: packet.h:558
AVOption
AVOption.
Definition: opt.h:429
encode.h
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:683
data
const char data[16]
Definition: mxf.c:149
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:34
FFCodec
Definition: codec_internal.h:127
libjxl.h
AV_FRAME_DATA_DISPLAYMATRIX
@ AV_FRAME_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
Definition: frame.h:85
av_get_bits_per_pixel
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:3537
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:576
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:655
quality
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
Definition: rate_distortion.txt:12
libjxl_anim_encode_init
static av_cold int libjxl_anim_encode_init(AVCodecContext *avctx)
Initializer for the animation encoder.
Definition: libjxlenc.c:181
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCOL_TRC_IEC61966_2_1
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:694
libjxl_anim_encode_frame
static int libjxl_anim_encode_frame(AVCodecContext *avctx, AVPacket *pkt)
Encode one frame of the animation.
Definition: libjxlenc.c:649
OFFSET
#define OFFSET(x)
Definition: libjxlenc.c:750
LibJxlEncodeContext::prev
AVFrame * prev
Definition: libjxlenc.c:65
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1561
AVCOL_TRC_GAMMA28
@ AVCOL_TRC_GAMMA28
also ITU-R BT470BG
Definition: pixfmt.h:686
av_exif_free
void av_exif_free(AVExifMetadata *ifd)
Frees all resources associated with the given EXIF metadata struct.
Definition: exif.c:612
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:488
LibJxlEncodeContext::xyb
int xyb
Definition: libjxlenc.c:58
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:535
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:358
AVRational::num
int num
Numerator.
Definition: rational.h:59
libjxl_populate_colorspace
static int libjxl_populate_colorspace(AVCodecContext *avctx, const AVFrame *frame, const AVPixFmtDescriptor *pix_desc, const JxlBasicInfo *info)
Definition: libjxlenc.c:252
LibJxlEncodeContext::modular
int modular
Definition: libjxlenc.c:57
AVCOL_TRC_GAMMA22
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:685
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:645
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
AVFrameSideData::size
size_t size
Definition: frame.h:285
av_cold
#define av_cold
Definition: attributes.h:106
AVCodecContext::global_quality
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1217
av_csp_primaries_desc_from_id
const AVColorPrimariesDesc * av_csp_primaries_desc_from_id(enum AVColorPrimaries prm)
Retrieves a complete gamut description from an enum constant describing the color primaries.
Definition: csp.c:90
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
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:109
info
MIPS optimizations info
Definition: mips.txt:2
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:144
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1553
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_RGBF32
#define AV_PIX_FMT_RGBF32
Definition: pixfmt.h:645
AV_PIX_FMT_GRAYF32
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:601
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:658
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
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:542
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
av_exif_get_tag_id
int32_t av_exif_get_tag_id(const char *name)
Retrieves the tag ID associated with the provided tag string name.
Definition: exif.c:225
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
exif_internal.h
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:669
CODEC_PIXFMTS_ARRAY
#define CODEC_PIXFMTS_ARRAY(array)
Definition: codec_internal.h:392
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_EXIF_TIFF_HEADER
@ AV_EXIF_TIFF_HEADER
The TIFF header starts with 0x49492a00, or 0x4d4d002a.
Definition: exif.h:63
AVPixFmtDescriptor::nb_components
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
LibJxlEncodeContext::buffer
uint8_t * buffer
Definition: libjxlenc.c:59
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:366
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:657
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
AV_FRAME_DATA_ICC_PROFILE
@ AV_FRAME_DATA_ICC_PROFILE
The data contains an ICC profile as an opaque octet buffer following the format described by ISO 1507...
Definition: frame.h:144
libjxl_init_jxl_encoder
static int libjxl_init_jxl_encoder(AVCodecContext *avctx)
Initialize the encoder on a per-file basis.
Definition: libjxlenc.c:104
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
AVPixFmtDescriptor::flags
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:94
av_exif_remove_entry
int av_exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags)
Remove an entry from the provided EXIF metadata struct.
Definition: exif.c:1143
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:752
LibJxlEncodeContext
Definition: libjxlenc.c:50
error.h
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:666
AVExifEntry::value
union AVExifEntry::@120 value
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:535
AVAlphaMode
AVAlphaMode
Correlation between the alpha channel and color values.
Definition: pixfmt.h:819
AVCOL_TRC_SMPTE2084
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition: pixfmt.h:697
AVCOL_PRI_SMPTE431
@ AVCOL_PRI_SMPTE431
SMPTE ST 431-2 (2011) / DCI P3.
Definition: pixfmt.h:669
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_preprocess_stream
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,...
Definition: libjxlenc.c:323
codec_internal.h
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:592
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:538
AVFrameSideData::data
uint8_t * data
Definition: frame.h:284
libjxl_preprocess_frame
static int libjxl_preprocess_frame(AVCodecContext *avctx, const AVFrame *frame, const uint8_t **data)
Sends frame information to libjxl on a per-frame basis.
Definition: libjxlenc.c:503
ff_libjxl_encoder
const FFCodec ff_libjxl_encoder
Definition: libjxlenc.c:780
frame.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:557
csp.h
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
quality_to_distance
static float quality_to_distance(float quality)
Map a quality setting for -qscale roughly from libjpeg quality numbers to libjxl's butteraugli distan...
Definition: libjxlenc.c:83
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:682
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
LibJxlEncodeContext::distance
float distance
Definition: libjxlenc.c:56
libjxl_populate_primaries
static int libjxl_populate_primaries(void *avctx, JxlColorEncoding *jxl_color, enum AVColorPrimaries prm)
Populate a JxlColorEncoding with the given enum AVColorPrimaries.
Definition: libjxlenc.c:205
AV_PIX_FMT_YA16
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:537
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:551
libjxl_encode_init
static av_cold int libjxl_encode_init(AVCodecContext *avctx)
Global encoder initialization.
Definition: libjxlenc.c:131
ff_libjxl_anim_encoder
const FFCodec ff_libjxl_anim_encoder
Definition: libjxlenc.c:803
available
if no frame is available
Definition: filter_design.txt:166
display.h
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:727
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:523
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:496
LibJxlEncodeContext::jxl_fmt
JxlPixelFormat jxl_fmt
Definition: libjxlenc.c:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
libjxl_supported_pixfmts
static enum AVPixelFormat libjxl_supported_pixfmts[]
Definition: libjxlenc.c:770
AVExifEntry::uint
uint64_t * uint
Definition: exif.h:109
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:769
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:81
libjxl_encode_class
static const AVClass libjxl_encode_class
Definition: libjxlenc.c:763
avcodec.h
version.h
tag
uint32_t tag
Definition: movenc.c:1995
ret
ret
Definition: filter_design.txt:187
pixfmt.h
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
AVALPHA_MODE_UNSPECIFIED
@ AVALPHA_MODE_UNSPECIFIED
Unknown alpha handling, or no alpha channel.
Definition: pixfmt.h:820
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AVCodecContext
main external API structure.
Definition: avcodec.h:431
AVCOL_TRC_ARIB_STD_B67
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition: pixfmt.h:701
frame_header
Definition: truemotion1.c:88
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:104
libjxl_encode_options
static const AVOption libjxl_encode_options[]
Definition: libjxlenc.c:753
AVRational::den
int den
Denominator.
Definition: rational.h:60
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
temp
else temp
Definition: vf_mcdeint.c:271
AV_PIX_FMT_RGBAF32
#define AV_PIX_FMT_RGBAF32
Definition: pixfmt.h:646
AVFormatContext::duration
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1399
ff_exif_sanitize_ifd
int ff_exif_sanitize_ifd(void *logctx, const AVFrame *frame, AVExifMetadata *ifd)
Compares values in the IFD with data in the provided AVFrame and sets the values in that IFD to match...
Definition: exif.c:1235
LibJxlEncodeContext::frame
AVFrame * frame
Definition: libjxlenc.c:64
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
avutil.h
mem.h
ff_encode_get_frame
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:203
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:72
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:282
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVCOL_PRI_SMPTE432
@ AVCOL_PRI_SMPTE432
SMPTE ST 432-1 (2010) / P3 D65 / Display P3.
Definition: pixfmt.h:670
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_libjxl_init_memory_manager
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...
Definition: libjxl.c:65
av_exif_get_entry
int av_exif_get_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags, AVExifEntry **value)
Get an entry with the tagged ID from the EXIF metadata struct.
Definition: exif.c:1054
AV_FRAME_DATA_EXIF
@ AV_FRAME_DATA_EXIF
Extensible image file format metadata.
Definition: frame.h:262
int32_t
int32_t
Definition: audioconvert.c:56
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:231
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCOL_TRC_SMPTE428
@ AVCOL_TRC_SMPTE428
SMPTE ST 428-1.
Definition: pixfmt.h:699
LibJxlEncodeContext::encoder
JxlEncoder * encoder
Definition: libjxlenc.c:53
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:226
av_exif_matrix_to_orientation
int av_exif_matrix_to_orientation(const int32_t *matrix)
Convert a display matrix used by AV_FRAME_DATA_DISPLAYMATRIX into an orientation constant used by EXI...
Definition: exif.c:1184
LibJxlEncodeContext::duration
int64_t duration
Definition: libjxlenc.c:66
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155