libheif

repository·master·Indexed 25 days ago

https://github.com/strukturag/libheif

A high-performance decoder and encoder for ISO/IEC 23008-12 HEIF and AVIF (AV1 Image File Format) files. It supports various compression codecs including HEVC, AV1, VVC, AVC, JPEG, and JPEG-2000. The library provides a C API and a header-only C++ wrapper, supporting advanced features such as image sequences, MP4 video, and high-resolution tiled images. It includes a suite of command-line tools like heif-dec, heif-enc, and heif-info.

Tokens
2.8K
Snippets
8
Records
18
Agent score
31%

What's inside libheif

  1. Overview of libheif

    master

    libheif is a decoder and encoder for ISO/IEC 23008-12 HEIF and AVIF (AV1 Image File Format) files. It supports high compression ratios using HEVC (H.265) or AV1 coding. Additionally, it supports HEIF images coded with VVC, AVC, JPEG, JPEG-2000, and uncompressed (ISO/IEC 23001-17) formats.

    The library uses external codec libraries (like libde265, x265, libaom, dav1d, etc.) and can be built with a subset of codecs or as dynamic plugins to manage dependencies and binary size.

  2. Manage libheif dynamic plugins

    master
    Codec backends can be compiled as dynamic plugins. These are loaded from directories specified in the LIBHEIF_PLUGIN_PATH environment variable (which should be a colon-separated list on Unix or semicolon-separated on Windows). If the variable is empty, libheif uses the directory specified by the PLUGIN_DIRECTORY CMake configuration.
  3. C++ API availability

    master
    In addition to the C API, libheif provides a C++ API which is a header-only wrapper around the C API. It is binary compatible with the C API but offers a significantly less verbose syntax for developers using C++.
  4. Advanced usage: Sequences and Tiled Images

    master

    libheif provides specialized APIs for advanced image types:

    • Image Sequences and MP4 Video: libheif can read and write HEIF image sequences and MP4 video (without audio) using all supported codecs. Refer to the image sequences API documentation.
    • High-resolution Tiled Images: For very large images that cannot be processed entirely in memory, libheif supports tile-by-tile decoding and encoding. Refer to the image tiling API documentation.
  5. Install libheif on Windows via vcpkg

    master

    Use the vcpkg dependency manager to build and install libheif on Windows:

    git clone https://github.com/Microsoft/vcpkg.git
    cd vcpkg
    ./bootstrap-vcpkg.bat
    ./vcpkg integrate install
    ./vcpkg install libheif
  6. Compile libheif using CMake

    master

    libheif uses CMake for its build system. For a minimal configuration, it is recommended to use libde265 and x265 for HEIC, and AOM for AVIF. Ensure libde265 is compiled and installed before building libheif.

    Basic build steps (requires CMake >= 3.21):

    mkdir build
    cd build
    cmake --preset=release ..
    make
  7. Use CMake presets for libheif builds

    master

    libheif provides several CMake presets to cover common use cases:

    • release: The preferred preset. Compiles all codecs as separate plugins.
    • release-noplugins: A smaller, self-contained build without the plugin system. Builds a single library with support for HEIC and AVIF.
    • testing: Builds and executes unit tests. Exposes internal library symbols. Do not use for distribution.
    • fuzzing: Configures all codecs into a self-contained library with enabled fuzzers. Do not use for distribution.
  8. Install libheif on macOS

    master

    To install libheif on macOS, use Homebrew for dependencies and then build from source:

    1. Install dependencies:
    brew install cmake make pkg-config x265 libde265 libjpeg libtool
    1. Configure and build (requires CMake >= 3.21):
    mkdir build
    cd build
    cmake --preset=release ..
    ./configure
    make
    brew install cmake make pkg-config x265 libde265 libjpeg libtool
    
    mkdir build
    cd build
    cmake --preset=release ..
    ./configure
    make
  9. Compile libheif to JavaScript / WASM

    master

    libheif can be compiled to JavaScript using Emscripten. Use the build-emscripten.sh script in the root directory:

    To build with standard JavaScript output:

    mkdir buildjs
    cd buildjs
    USE_WASM=0 ../build-emscripten.sh ..

    To build with WASM output, set USE_WASM=1.

  10. Configure libheif codec support and plugins

    master

    You can control which codecs are included and whether they are built-in or as dynamic plugins using CMake variables. To use dynamic plugins, ENABLE_PLUGIN_LOADING must be enabled.

    For each codec, use:

    • WITH_{codec}: Enables the codec.
    • WITH_{codec}_PLUGIN: Compiles the codec as a separate plugin.

    Supported {codec} placeholders: LIBDE265, X265, AOM_DECODER, AOM_ENCODER, SvtEnc, DAV1D, OpenH264, X264, FFMPEG_DECODER, JPEG_DECODER, JPEG_ENCODER, KVAZAAR, OpenJPEG_DECODER, OpenJPEG_ENCODER, OPENJPH_ENCODER, VVDEC, VVENC, UVG266, WEBCODECS.

  11. Handle libheif security limits

    master

    Libheif uses security limits to prevent memory exhaustion or DoS attacks from malicious files.

    • Programmatically: Use heif_security_limits to set individual limits when using the API.
    • CLI: Use the --disable-limits option with heif-dec to switch off limits.
    • Globally: Set the environment variable LIBHEIF_SECURITY_LIMITS=off to disable limits globally. Warning: Only do this if you are certain you are not processing malicious files.
  12. Load and decode a primary image from a HEIF file

    master

    To decode the primary image from a HEIF/AVIF file into an RGB buffer using the C API, follow these steps:

    1. Allocate a heif_context.
    2. Read the file into the context.
    3. Get a handle to the primary image.
    4. Decode the image specifying the desired colorspace (e.g., heif_colorspace_RGB) and chroma (e.g., heif_chroma_interleaved_RGB).
    5. Access the raw data using heif_image_get_plane_readonly.
    6. Release all allocated resources (heif_image, heif_image_handle, and heif_context).
    heif_context* ctx = heif_context_alloc();
    heif_context_read_from_file(ctx, input_filename, nullptr);
    
    // get a handle to the primary image
    heif_image_handle* handle;
    heif_context_get_primary_image_handle(ctx, &handle);
    
    // decode the image and convert colorspace to RGB, saved as 24bit interleaved
    heif_image* img;
    heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGB, nullptr);
    
    int stride;
    const uint8_t* data = heif_image_get_plane_readonly(img, heif_channel_interleaved, &stride);
    
    // ... process data as needed ...
    
    // clean up resources
    heif_image_release(img);
    heif_image_handle_release(handle);
    heif_context_free(ctx);