Skip to content

Commit

Permalink
avcodec/libjxl: add Jpeg XL decoding via libjxl
Browse files Browse the repository at this point in the history
This commit adds decoding support to libavcodec
for Jpeg XL images via the external library libjxl.
  • Loading branch information
Traneptora authored and cyanreg committed Apr 23, 2022
1 parent 0008c15 commit 458cc7e
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 0 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS
Expand Up @@ -194,6 +194,7 @@ Codecs:
libcodec2.c Tomas Härdin
libdirac* David Conrad
libdavs2.c Huiwen Ren
libjxl*.c, libjxl.h Leo Izen
libgsm.c Michel Bardiaux
libkvazaar.c Arttu Ylä-Outinen
libopenh264enc.c Martin Storsjo, Linjie Fu
Expand Down
5 changes: 5 additions & 0 deletions configure
Expand Up @@ -240,6 +240,7 @@ External library support:
--enable-libiec61883 enable iec61883 via libiec61883 [no]
--enable-libilbc enable iLBC de/encoding via libilbc [no]
--enable-libjack enable JACK audio sound server [no]
--enable-libjxl enable JPEG XL decoding via libjxl [no]
--enable-libklvanc enable Kernel Labs VANC processing [no]
--enable-libkvazaar enable HEVC encoding via libkvazaar [no]
--enable-liblensfun enable lensfun lens correction [no]
Expand Down Expand Up @@ -1833,6 +1834,7 @@ EXTERNAL_LIBRARY_LIST="
libiec61883
libilbc
libjack
libjxl
libklvanc
libkvazaar
libmodplug
Expand Down Expand Up @@ -3331,6 +3333,7 @@ libgsm_ms_decoder_deps="libgsm"
libgsm_ms_encoder_deps="libgsm"
libilbc_decoder_deps="libilbc"
libilbc_encoder_deps="libilbc"
libjxl_decoder_deps="libjxl libjxl_threads"
libkvazaar_encoder_deps="libkvazaar"
libmodplug_demuxer_deps="libmodplug"
libmp3lame_encoder_deps="libmp3lame"
Expand Down Expand Up @@ -6545,6 +6548,8 @@ enabled libgsm && { for gsm_hdr in "gsm.h" "gsm/gsm.h"; do
check_lib libgsm "${gsm_hdr}" gsm_create -lgsm && break;
done || die "ERROR: libgsm not found"; }
enabled libilbc && require libilbc ilbc.h WebRtcIlbcfix_InitDecode -lilbc $pthreads_extralibs
enabled libjxl && require_pkg_config libjxl "libjxl >= 0.7.0" jxl/decode.h JxlDecoderVersion &&
require_pkg_config libjxl_threads "libjxl_threads >= 0.7.0" jxl/thread_parallel_runner.h JxlThreadParallelRunner
enabled libklvanc && require libklvanc libklvanc/vanc.h klvanc_context_create -lklvanc
enabled libkvazaar && require_pkg_config libkvazaar "kvazaar >= 0.8.1" kvazaar.h kvz_api_get
enabled liblensfun && require_pkg_config liblensfun lensfun lensfun.h lf_db_new
Expand Down
7 changes: 7 additions & 0 deletions doc/general_contents.texi
Expand Up @@ -171,6 +171,13 @@ Go to @url{https://github.com/TimothyGu/libilbc} and follow the instructions for
installing the library. Then pass @code{--enable-libilbc} to configure to
enable it.

@section libjxl

JPEG XL is an image format intended to fully replace legacy JPEG for an extended
period of life. See @url{https://jpegxl.info/} for more information, and see
@url{https://github.com/libjxl/libjxl} for the library source. You can pass
@code{--enable-libjxl} to configure in order enable the libjxl wrapper.

@section libvpx

FFmpeg can make use of the libvpx library for VP8/VP9 decoding and encoding.
Expand Down
1 change: 1 addition & 0 deletions libavcodec/Makefile
Expand Up @@ -1062,6 +1062,7 @@ OBJS-$(CONFIG_LIBGSM_MS_DECODER) += libgsmdec.o
OBJS-$(CONFIG_LIBGSM_MS_ENCODER) += libgsmenc.o
OBJS-$(CONFIG_LIBILBC_DECODER) += libilbc.o
OBJS-$(CONFIG_LIBILBC_ENCODER) += libilbc.o
OBJS-$(CONFIG_LIBJXL_DECODER) += libjxldec.o libjxl.o
OBJS-$(CONFIG_LIBKVAZAAR_ENCODER) += libkvazaar.o
OBJS-$(CONFIG_LIBMP3LAME_ENCODER) += libmp3lame.o
OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER) += libopencore-amr.o
Expand Down
1 change: 1 addition & 0 deletions libavcodec/allcodecs.c
Expand Up @@ -751,6 +751,7 @@ extern const FFCodec ff_libgsm_ms_encoder;
extern const FFCodec ff_libgsm_ms_decoder;
extern const FFCodec ff_libilbc_encoder;
extern const FFCodec ff_libilbc_decoder;
extern const FFCodec ff_libjxl_decoder;
extern const FFCodec ff_libmp3lame_encoder;
extern const FFCodec ff_libopencore_amrnb_encoder;
extern const FFCodec ff_libopencore_amrnb_decoder;
Expand Down
70 changes: 70 additions & 0 deletions libavcodec/libjxl.c
@@ -0,0 +1,70 @@
/*
* JPEG XL de/encoding via libjxl, common support implementation
* Copyright (c) 2021 Leo Izen <leo.izen@gmail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/**
* @file
* JPEG XL via libjxl common support implementation
*/

#include "libavutil/cpu.h"
#include "libavutil/mem.h"

#include <jxl/memory_manager.h>
#include "libjxl.h"

size_t ff_libjxl_get_threadcount(int threads)
{
if (threads <= 0)
return av_cpu_count();
if (threads == 1)
return 0;
return threads;
}

/**
* Wrapper around av_malloc used as a jpegxl_alloc_func.
*
* @param opaque opaque pointer for jpegxl_alloc_func, always ignored
* @param size Size in bytes for the memory block to be allocated
* @return Pointer to the allocated block, or `NULL` if it cannot be allocated
*/
static void *libjxl_av_malloc(void *opaque, size_t size)
{
return av_malloc(size);
}

/**
* Wrapper around av_free used as a jpegxl_free_func.
*
* @param opaque opaque pointer for jpegxl_free_func, always ignored
* @param address Pointer to the allocated block, to free. `NULL` permitted as a no-op.
*/
static void libjxl_av_free(void *opaque, void *address)
{
av_free(address);
}

void ff_libjxl_init_memory_manager(JxlMemoryManager *manager)
{
manager->opaque = NULL;
manager->alloc = &libjxl_av_malloc;
manager->free = &libjxl_av_free;
}
48 changes: 48 additions & 0 deletions libavcodec/libjxl.h
@@ -0,0 +1,48 @@
/*
* JPEG XL de/encoding via libjxl, common support header
* Copyright (c) 2021 Leo Izen <leo.izen@gmail.com>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/**
* @file
* JPEG XL via libjxl common support header
*/

#ifndef AVCODEC_LIBJXL_H
#define AVCODEC_LIBJXL_H

#include <jxl/memory_manager.h>

/**
* Transform threadcount in ffmpeg to one used by libjxl.
*
* @param threads ffmpeg's threads AVOption
* @return thread count for libjxl's parallel runner
*/
size_t ff_libjxl_get_threadcount(int threads);

/**
* Initialize and populate a JxlMemoryManager
* with av_malloc() and av_free() so libjxl will use these
* functions.
* @param manager a pointer to a JxlMemoryManager struct
*/
void ff_libjxl_init_memory_manager(JxlMemoryManager *manager);

#endif /* AVCODEC_LIBJXL_H */

0 comments on commit 458cc7e

Please sign in to comment.