History

avcodec/libjxl: add #ifdef guards for libjxl >= 0.8.0 features

Since many distributions ship libjxl 0.7.0 still, we'd still prefer to compile against that, but don't want to lose the features that require libjxl 0.8.0 or greater. For this reason I've added preprocessor #ifdef guards around the features that aren't necessarily in libjxl 0.7.0.

Leo Izen committed 3 years ago

Commit 00093481 parent c5aeb8f3 files · +39 −10Browse files

Unified · Split

3 files
  1. libavcodec/libjxl.h +12 −0
  2. libavcodec/libjxldec.c +13 −6
  3. libavcodec/libjxlenc.c +14 −4
libavcodec/libjxl.h+12−0
@@ -27,8 +27,20 @@ · 26 unmodified lines
2727 #ifndef AVCODEC_LIBJXL_H
2828 #define AVCODEC_LIBJXL_H
2929
30+#include <jxl/decode.h>
3031 #include <jxl/memory_manager.h>
3132
33+/*
34+ * libjxl version 0.7.0 and earlier doesn't contain these macros at all
35+ * so to detect version 0.7.0 versus 0.8.0 we need to define them ourselves
36+ */
37+#ifndef JPEGXL_COMPUTE_NUMERIC_VERSION
38+ #define JPEGXL_COMPUTE_NUMERIC_VERSION(major,minor,patch) ((major<<24) | (minor<<16) | (patch<<8) | 0)
39+#endif
40+#ifndef JPEGXL_NUMERIC_VERSION
41+ #define JPEGXL_NUMERIC_VERSION JPEGXL_COMPUTE_NUMERIC_VERSION(0, 7, 0)
42+#endif
43+
3244 /**
3345 * Transform threadcount in ffmpeg to one used by libjxl.
3446 *
libavcodec/libjxldec.c+13−6
@@ -47,7 +47,9 @@ typedef struct LibJxlDecodeContext { · 46 unmodified lines
4747 JxlDecoder *decoder;
4848 JxlBasicInfo basic_info;
4949 JxlPixelFormat jxl_pixfmt;
50+#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
5051 JxlBitDepth jxl_bit_depth;
52+#endif
5153 JxlDecoderStatus events;
5254 AVBufferRef *iccp;
5355 } LibJxlDecodeContext;
@@ -94,14 +96,17 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx) · 40 unmodified lines
9496 return libjxl_init_jxl_decoder(avctx);
9597 }
9698
97−static enum AVPixelFormat libjxl_get_pix_fmt(AVCodecContext *avctx, const JxlBasicInfo *basic_info,
98− JxlPixelFormat *format, JxlBitDepth *depth)
99+static enum AVPixelFormat libjxl_get_pix_fmt(AVCodecContext *avctx, LibJxlDecodeContext *ctx)
99100 {
101+ const JxlBasicInfo *basic_info = &ctx->basic_info;
102+ JxlPixelFormat *format = &ctx->jxl_pixfmt;
100103 format->endianness = JXL_NATIVE_ENDIAN;
101104 format->num_channels = basic_info->num_color_channels + (basic_info->alpha_bits > 0);
102− depth->bits_per_sample = avctx->bits_per_raw_sample = basic_info->bits_per_sample;
103− depth->type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
104− depth->exponent_bits_per_sample = basic_info->exponent_bits_per_sample;
105+#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
106+ ctx->jxl_bit_depth.bits_per_sample = avctx->bits_per_raw_sample = basic_info->bits_per_sample;
107+ ctx->jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
108+ ctx->jxl_bit_depth.exponent_bits_per_sample = basic_info->exponent_bits_per_sample;
109+#endif
105110 /* Gray */
106111 if (basic_info->num_color_channels == 1) {
107112 if (basic_info->bits_per_sample <= 8) {
@@ -372,7 +377,7 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f · 264 unmodified lines
372377 av_log(avctx, AV_LOG_ERROR, "Bad libjxl basic info event\n");
373378 return AVERROR_EXTERNAL;
374379 }
375− avctx->pix_fmt = libjxl_get_pix_fmt(avctx, &ctx->basic_info, &ctx->jxl_pixfmt, &ctx->jxl_bit_depth);
380+ avctx->pix_fmt = libjxl_get_pix_fmt(avctx, ctx);
376381 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
377382 av_log(avctx, AV_LOG_ERROR, "Bad libjxl pixel format\n");
378383 return AVERROR_EXTERNAL;
@@ -395,10 +400,12 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f · 16 unmodified lines
395400 av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec need image out buffer event\n");
396401 return AVERROR_EXTERNAL;
397402 }
403+#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
398404 if (JxlDecoderSetImageOutBitDepth(ctx->decoder, &ctx->jxl_bit_depth) != JXL_DEC_SUCCESS) {
399405 av_log(avctx, AV_LOG_ERROR, "Error setting output bit depth\n");
400406 return AVERROR_EXTERNAL;
401407 }
408+#endif
402409 continue;
403410 case JXL_DEC_FULL_IMAGE:
404411 /* full image is one frame, even if animated */
libavcodec/libjxlenc.c+14−4
@@ -250,7 +250,10 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra · 249 unmodified lines
250250 JxlBasicInfo info;
251251 JxlColorEncoding jxl_color;
252252 JxlPixelFormat jxl_fmt;
253+ int bits_per_sample;
254+#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
253255 JxlBitDepth jxl_bit_depth;
256+#endif
254257 JxlEncoderStatus jret;
255258 int ret;
256259 size_t available = ctx->buffer_size;
@@ -270,22 +273,26 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra · 13 unmodified lines
270273 info.ysize = frame->height;
271274 info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
272275 info.num_color_channels = jxl_fmt.num_channels - info.num_extra_channels;
273− jxl_bit_depth.bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
276+ bits_per_sample = av_get_bits_per_pixel(pix_desc) / jxl_fmt.num_channels;
274277 info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)
275− ? avctx->bits_per_raw_sample : jxl_bit_depth.bits_per_sample;
278+ ? avctx->bits_per_raw_sample : bits_per_sample;
276279 info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
277280 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
278281 info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
279282 info.alpha_exponent_bits = info.alpha_bits ? info.exponent_bits_per_sample : 0;
280283 jxl_fmt.data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : JXL_TYPE_FLOAT16;
281− jxl_bit_depth.exponent_bits_per_sample = info.exponent_bits_per_sample;
282284 } else {
283285 info.exponent_bits_per_sample = 0;
284286 info.alpha_exponent_bits = 0;
285287 jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;
286− jxl_bit_depth.exponent_bits_per_sample = 0;
287288 }
289+
290+#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
291+ jxl_bit_depth.bits_per_sample = bits_per_sample;
288292 jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;
293+ jxl_bit_depth.exponent_bits_per_sample = pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT ?
294+ info.exponent_bits_per_sample : 0;
295+#endif
289296
290297 /* JPEG XL format itself does not support limited range */
291298 if (avctx->color_range == AVCOL_RANGE_MPEG ||
@@ -362,8 +369,11 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra · 70 unmodified lines
362369 av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
363370 if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)
364371 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");
372+
373+#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
365374 if (JxlEncoderSetFrameBitDepth(ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)
366375 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlBitDepth\n");
376+#endif
367377
368378 /* depending on basic info, level 10 might
369379 * be required instead of level 5 */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t