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
27#ifndef AVCODEC_LIBJXL_H27#ifndef AVCODEC_LIBJXL_H
28#define AVCODEC_LIBJXL_H28#define AVCODEC_LIBJXL_H
29 29
30#include <jxl/decode.h>
30#include <jxl/memory_manager.h>31#include <jxl/memory_manager.h>
31 32
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
32/**44/**
33 * Transform threadcount in ffmpeg to one used by libjxl.45 * Transform threadcount in ffmpeg to one used by libjxl.
34 *46 *
libavcodec/libjxldec.c+13−6
@@ -47,7 +47,9 @@ typedef struct LibJxlDecodeContext { · 46 unmodified lines
47 JxlDecoder *decoder;47 JxlDecoder *decoder;
48 JxlBasicInfo basic_info;48 JxlBasicInfo basic_info;
49 JxlPixelFormat jxl_pixfmt;49 JxlPixelFormat jxl_pixfmt;
50#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
50 JxlBitDepth jxl_bit_depth;51 JxlBitDepth jxl_bit_depth;
52#endif
51 JxlDecoderStatus events;53 JxlDecoderStatus events;
52 AVBufferRef *iccp;54 AVBufferRef *iccp;
53} LibJxlDecodeContext;55} LibJxlDecodeContext;
@@ -94,14 +96,17 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx) · 40 unmodified lines
94 return libjxl_init_jxl_decoder(avctx);96 return libjxl_init_jxl_decoder(avctx);
95}97}
96 98
97static enum AVPixelFormat libjxl_get_pix_fmt(AVCodecContext *avctx, const JxlBasicInfo *basic_info,99static enum AVPixelFormat libjxl_get_pix_fmt(AVCodecContext *avctx, LibJxlDecodeContext *ctx)
98 JxlPixelFormat *format, JxlBitDepth *depth)
99{100{
101 const JxlBasicInfo *basic_info = &ctx->basic_info;
102 JxlPixelFormat *format = &ctx->jxl_pixfmt;
100 format->endianness = JXL_NATIVE_ENDIAN;103 format->endianness = JXL_NATIVE_ENDIAN;
101 format->num_channels = basic_info->num_color_channels + (basic_info->alpha_bits > 0);104 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;105#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
103 depth->type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;106 ctx->jxl_bit_depth.bits_per_sample = avctx->bits_per_raw_sample = basic_info->bits_per_sample;
104 depth->exponent_bits_per_sample = basic_info->exponent_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
105 /* Gray */110 /* Gray */
106 if (basic_info->num_color_channels == 1) {111 if (basic_info->num_color_channels == 1) {
107 if (basic_info->bits_per_sample <= 8) {112 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
372 av_log(avctx, AV_LOG_ERROR, "Bad libjxl basic info event\n");377 av_log(avctx, AV_LOG_ERROR, "Bad libjxl basic info event\n");
373 return AVERROR_EXTERNAL;378 return AVERROR_EXTERNAL;
374 }379 }
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);
376 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {381 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
377 av_log(avctx, AV_LOG_ERROR, "Bad libjxl pixel format\n");382 av_log(avctx, AV_LOG_ERROR, "Bad libjxl pixel format\n");
378 return AVERROR_EXTERNAL;383 return AVERROR_EXTERNAL;
@@ -395,10 +400,12 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f · 16 unmodified lines
395 av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec need image out buffer event\n");400 av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec need image out buffer event\n");
396 return AVERROR_EXTERNAL;401 return AVERROR_EXTERNAL;
397 }402 }
403#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
398 if (JxlDecoderSetImageOutBitDepth(ctx->decoder, &ctx->jxl_bit_depth) != JXL_DEC_SUCCESS) {404 if (JxlDecoderSetImageOutBitDepth(ctx->decoder, &ctx->jxl_bit_depth) != JXL_DEC_SUCCESS) {
399 av_log(avctx, AV_LOG_ERROR, "Error setting output bit depth\n");405 av_log(avctx, AV_LOG_ERROR, "Error setting output bit depth\n");
400 return AVERROR_EXTERNAL;406 return AVERROR_EXTERNAL;
401 }407 }
408#endif
402 continue;409 continue;
403 case JXL_DEC_FULL_IMAGE:410 case JXL_DEC_FULL_IMAGE:
404 /* full image is one frame, even if animated */411 /* 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
250 JxlBasicInfo info;250 JxlBasicInfo info;
251 JxlColorEncoding jxl_color;251 JxlColorEncoding jxl_color;
252 JxlPixelFormat jxl_fmt;252 JxlPixelFormat jxl_fmt;
253 int bits_per_sample;
254#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
253 JxlBitDepth jxl_bit_depth;255 JxlBitDepth jxl_bit_depth;
256#endif
254 JxlEncoderStatus jret;257 JxlEncoderStatus jret;
255 int ret;258 int ret;
256 size_t available = ctx->buffer_size;259 size_t available = ctx->buffer_size;
@@ -270,22 +273,26 @@ static int libjxl_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFra · 13 unmodified lines
270 info.ysize = frame->height;273 info.ysize = frame->height;
271 info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;274 info.num_extra_channels = (jxl_fmt.num_channels + 1) % 2;
272 info.num_color_channels = jxl_fmt.num_channels - info.num_extra_channels;275 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;
274 info.bits_per_sample = avctx->bits_per_raw_sample > 0 && !(pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT)277 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;
276 info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;279 info.alpha_bits = (info.num_extra_channels > 0) * info.bits_per_sample;
277 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {280 if (pix_desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
278 info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;281 info.exponent_bits_per_sample = info.bits_per_sample > 16 ? 8 : 5;
279 info.alpha_exponent_bits = info.alpha_bits ? info.exponent_bits_per_sample : 0;282 info.alpha_exponent_bits = info.alpha_bits ? info.exponent_bits_per_sample : 0;
280 jxl_fmt.data_type = info.bits_per_sample > 16 ? JXL_TYPE_FLOAT : JXL_TYPE_FLOAT16;283 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;
282 } else {284 } else {
283 info.exponent_bits_per_sample = 0;285 info.exponent_bits_per_sample = 0;
284 info.alpha_exponent_bits = 0;286 info.alpha_exponent_bits = 0;
285 jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;287 jxl_fmt.data_type = info.bits_per_sample <= 8 ? JXL_TYPE_UINT8 : JXL_TYPE_UINT16;
286 jxl_bit_depth.exponent_bits_per_sample = 0;
287 }288 }
289
290#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
291 jxl_bit_depth.bits_per_sample = bits_per_sample;
288 jxl_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT;292 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
289 296
290 /* JPEG XL format itself does not support limited range */297 /* JPEG XL format itself does not support limited range */
291 if (avctx->color_range == AVCOL_RANGE_MPEG ||298 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
362 av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");369 av_log(avctx, AV_LOG_WARNING, "Could not set ICC Profile\n");
363 if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)370 if (JxlEncoderSetColorEncoding(ctx->encoder, &jxl_color) != JXL_ENC_SUCCESS)
364 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");371 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlColorEncoding\n");
372
373#if JPEGXL_NUMERIC_VERSION >= JPEGXL_COMPUTE_NUMERIC_VERSION(0, 8, 0)
365 if (JxlEncoderSetFrameBitDepth(ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)374 if (JxlEncoderSetFrameBitDepth(ctx->options, &jxl_bit_depth) != JXL_ENC_SUCCESS)
366 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlBitDepth\n");375 av_log(avctx, AV_LOG_WARNING, "Failed to set JxlBitDepth\n");
376#endif
367 377
368 /* depending on basic info, level 10 might378 /* depending on basic info, level 10 might
369 * be required instead of level 5 */379 * 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