Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

feature table

Preetham Pemmasani9ab01efc81a7
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 278d6bc..79efe3a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,13 +29,9 @@ option(BUI_BUILD_EXAMPLES "Build BUI example applications" OFF)
option(BUI_WARNINGS_AS_ERRORS "Treat compiler warnings as errors" OFF)
option(BUI_BUILD_PYTHON_BINDINGS "Build the buipy CPython bindings (needs Python3 dev files)" ON)
option(BUI_BUILD_QT_BACKEND "Build the Qt/QPainter backend (needs Qt5/Qt6 with Widgets)" OFF)
-# Dear ImGui on BUI (bindings/imgui): ON when its sources are here (not in the
-# C-only source copies other packages vendor) and C++ builds.
-set(_bui_imgui_default OFF)
-if(CMAKE_CXX_COMPILER AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/bindings/imgui/CMakeLists.txt")
- set(_bui_imgui_default ON)
-endif()
-option(BUI_BUILD_IMGUI "Build Dear ImGui on BUI (bui_imgui, its demo and test)" ${_bui_imgui_default})
+# The optional parts (image formats, video, GPU renderers, file browser, Dear
+# ImGui on BUI, ...): one option each, from features.json.
+include(features.cmake)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
diff --git a/features.cmake b/features.cmake
new file mode 100644
index 0000000..3015179
--- /dev/null
+++ b/features.cmake
@@ -0,0 +1,48 @@
+# The optional parts of BUI (features.json, the one table cargo and `bui build`
+# read too): an option each, ON by default unless the table says otherwise.
+# Off, a part's sources aren't compiled (it costs no binary size) and its
+# API stays, reporting it missing. bui_features(<target>) defines every
+# option that is ON for the target's sources (BUI_IMAGE_PNG, BUI_VIDEO, ...).
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+file(READ "${CMAKE_CURRENT_LIST_DIR}/features.json" _bui_features_json)
+set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/features.json")
+string(JSON _bui_nfeatures LENGTH "${_bui_features_json}" features)
+math(EXPR _bui_last "${_bui_nfeatures} - 1")
+set(BUI_FEATURE_OPTIONS "")
+foreach(_i RANGE ${_bui_last})
+ string(JSON _opt GET "${_bui_features_json}" features ${_i} option)
+ string(JSON _def GET "${_bui_features_json}" features ${_i} default)
+ string(JSON _doc GET "${_bui_features_json}" features ${_i} doc)
+ if(_opt STREQUAL "BUI_BUILD_IMGUI" AND NOT (CMAKE_CXX_COMPILER AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/bindings/imgui/CMakeLists.txt"))
+ set(_def OFF) # C-only source copies (the vendored crate sources) have no ImGui
+ endif()
+ option(${_opt} "${_doc}" ${_def})
+ list(APPEND BUI_FEATURE_OPTIONS ${_opt})
+ string(JSON _name GET "${_bui_features_json}" features ${_i} name)
+ set(_bui_option_of_${_name} ${_opt})
+endforeach()
+
+# a part that needs another (video-dav1d needs video): checked here, at the boundary
+foreach(_i RANGE ${_bui_last})
+ string(JSON _opt GET "${_bui_features_json}" features ${_i} option)
+ string(JSON _nreq ERROR_VARIABLE _noreq LENGTH "${_bui_features_json}" features ${_i} requires)
+ if(${_opt} AND NOT _noreq AND _nreq GREATER 0)
+ math(EXPR _rlast "${_nreq} - 1")
+ foreach(_k RANGE ${_rlast})
+ string(JSON _req GET "${_bui_features_json}" features ${_i} requires ${_k})
+ if(NOT ${_bui_option_of_${_req}})
+ message(FATAL_ERROR "${_opt} needs ${_bui_option_of_${_req}}")
+ endif()
+ endforeach()
+ endif()
+endforeach()
+
+function(bui_features target)
+ foreach(_opt ${BUI_FEATURE_OPTIONS})
+ if(${_opt})
+ target_compile_definitions(${target} PRIVATE ${_opt})
+ endif()
+ endforeach()
+endfunction()
diff --git a/features.json b/features.json
new file mode 100644
index 0000000..373e2a6
--- /dev/null
+++ b/features.json
@@ -0,0 +1,27 @@
+{
+ "about": "The optional parts of BUI: one table read by CMake (features.cmake: an option each), cargo (blenderui-sys/build.rs maps cargo features to those options and checks Cargo.toml lists them all) and `bui build` (bindings/node/features.js: which runtime an app needs). A part that is off costs no binary size; its API stays (it reports the part missing). One feature per line: build.rs reads it line by line.",
+ "features": [
+ {"name": "png", "option": "BUI_IMAGE_PNG", "default": true, "group": "images", "ext": ["png"], "doc": "PNG images (Wuffs)"},
+ {"name": "gif", "option": "BUI_IMAGE_GIF", "default": true, "group": "images", "ext": ["gif"], "doc": "GIF images and animations (Wuffs)"},
+ {"name": "bmp", "option": "BUI_IMAGE_BMP", "default": true, "group": "images", "ext": ["bmp"], "doc": "BMP images (Wuffs)"},
+ {"name": "tga", "option": "BUI_IMAGE_TGA", "default": true, "group": "images", "ext": ["tga"], "doc": "TGA images (Wuffs)"},
+ {"name": "qoi", "option": "BUI_IMAGE_QOI", "default": true, "group": "images", "ext": ["qoi"], "doc": "QOI images (Wuffs)"},
+ {"name": "jpeg", "option": "BUI_IMAGE_JPEG", "default": true, "group": "images", "ext": ["jpg", "jpeg", "jfif"], "doc": "JPEG images (libjpeg-turbo)"},
+ {"name": "webp", "option": "BUI_IMAGE_WEBP", "default": true, "group": "images", "ext": ["webp"], "doc": "WebP images and animations (libwebp)"},
+ {"name": "hdr", "option": "BUI_IMAGE_HDR", "default": true, "group": "images", "ext": ["hdr"], "doc": "Radiance HDR images"},
+ {"name": "pnm", "option": "BUI_IMAGE_PNM", "default": true, "group": "images", "ext": ["pnm", "ppm", "pgm"], "doc": "PNM (PGM, PPM) images"},
+ {"name": "svg", "option": "BUI_IMAGE_SVG", "default": true, "group": "images", "ext": ["svg"], "doc": "SVG images (nanosvg, which the icons use anyway: no extra code)"},
+ {"name": "video", "option": "BUI_VIDEO", "default": true, "group": "video", "ext": ["mp4", "m4v", "mov", "webm", "mkv", "avi", "wmv", "ogv"], "tags": ["video"], "doc": "<video> through the OS decoders (Media Foundation, AVFoundation, GStreamer), loaded at run time"},
+ {"name": "video-dav1d", "option": "BUI_VIDEO_DAV1D", "default": false, "group": "video", "requires": ["video"], "doc": "AV1 video with dav1d (BSD-2) and a small MP4/WebM demuxer, where the OS has no AV1 decoder"},
+ {"name": "video-ffmpeg", "option": "BUI_VIDEO_FFMPEG", "default": false, "group": "video", "requires": ["video"], "doc": "Every format FFmpeg reads (libavformat/libavcodec, LGPL or GPL by its build; patent-encumbered codecs)"},
+ {"name": "gpu", "option": "BUI_GPU", "default": true, "group": "rendering", "markers": ["gpu"], "doc": "GPU renderers: Direct3D 11 (Windows), OpenGL 3.3 (Linux); without it, the software raster only"},
+ {"name": "freetype", "option": "BUI_TEXT_FREETYPE", "default": true, "group": "rendering", "always": true, "doc": "Text through the vendored FreeType like Blender's BLF (else stb_truetype); every bui build runtime has it, so text looks the same"},
+ {"name": "filebrowser", "option": "BUI_FILEBROWSER", "default": true, "group": "ui", "tags": ["filebrowser"], "markers": ["fileDialog", "file_dialog", "filebrowser"], "doc": "Blender's File Browser (the filebrowser node and BK_file_dialog)"},
+ {"name": "imgui", "option": "BUI_BUILD_IMGUI", "default": true, "cargo_default": false, "group": "ui", "doc": "Dear ImGui on BUI (C++; the blenderui-imgui crate and Python's blenderui.imgui turn it on)"}
+ ],
+ "runtimes": [
+ {"name": "base", "features": ["svg", "freetype"], "doc": "UI and text, SVG images"},
+ {"name": "media", "features": ["svg", "freetype", "png", "gif", "bmp", "tga", "qoi", "jpeg", "webp", "hdr", "pnm", "video"], "doc": "base + every image format + video"},
+ {"name": "full", "features": ["svg", "freetype", "png", "gif", "bmp", "tga", "qoi", "jpeg", "webp", "hdr", "pnm", "video", "gpu", "filebrowser"], "doc": "everything a JavaScript app can use"}
+ ]
+}
diff --git a/gpu/CMakeLists.txt b/gpu/CMakeLists.txt
index 3d4f5b8..45784ce 100644
--- a/gpu/CMakeLists.txt
+++ b/gpu/CMakeLists.txt
@@ -8,11 +8,12 @@
set(SRC
intern/gpu_null.c
intern/gpu_raster.c
- intern/gpu_d3d11.c
+ intern/gpu_d3d11.c # without BUI_GPU (features.json), the stubs that keep rasters on the CPU
)
add_library(bui_gpu STATIC ${SRC})
add_library(bui::gpu ALIAS bui_gpu)
+bui_features(bui_gpu)
target_include_directories(bui_gpu PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
@@ -37,8 +38,8 @@ if(BUI_TEXT_GDI)
endif()
# FreeType (extern/freetype, 2.13.3 as Blender uses): loads, hints and renders
# glyphs the way Blender's BLF does (light hinting, the variable Inter at the
-# text's optical size, gamma 1.43). Compiled into bui_gpu itself.
-option(BUI_TEXT_FREETYPE "Render text with the vendored FreeType, like Blender" ON)
+# text's optical size, gamma 1.43). Compiled into bui_gpu itself
+# (BUI_TEXT_FREETYPE, features.json).
if(BUI_TEXT_FREETYPE AND NOT BUI_TEXT_GDI)
set(FT ${CMAKE_CURRENT_SOURCE_DIR}/extern/freetype)
set(FT_SRC
@@ -65,7 +66,7 @@ endif()
if(WIN32)
# gpu_raster.c: GDI glyph-run text engine + native window present.
target_link_libraries(bui_gpu PUBLIC gdi32 user32)
-elseif(NOT APPLE)
+elseif(NOT APPLE AND BUI_GPU)
# GPU renderer: OpenGL 3.3 (GLX windows, EGL offscreen) in gpu_gl.c instead
# of Direct3D 11. libGL / libEGL / libX11 are dlopen()ed at run time, never
# linked, so nothing GL is needed to build or import.
diff --git a/gpu/intern/gpu_d3d11.c b/gpu/intern/gpu_d3d11.c
index 6c1c9db..8e3321d 100644
--- a/gpu/intern/gpu_d3d11.c
+++ b/gpu/intern/gpu_d3d11.c
@@ -15,7 +15,7 @@
*
* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifdef _WIN32
+#if defined(_WIN32) && defined(BUI_GPU) /* BUI_GPU: features.json */
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
@@ -1655,8 +1655,8 @@ int bui_gpu_read(BuiGpu *g, uint32_t *dst, int w, int h)
/** \} */
-#elif !defined(BUI_GPU_GL) /* no GPU backend here (gpu_gl.c provides one on Linux):
- * every raster stays in software */
+#elif !defined(BUI_GPU_GL) /* no GPU backend here (gpu_gl.c provides one on Linux;
+ * built without BUI_GPU): every raster stays in software */
# include "gpu_d3d11.h"
diff --git a/kit/CMakeLists.txt b/kit/CMakeLists.txt
index 11e8e46..9652f48 100644
--- a/kit/CMakeLists.txt
+++ b/kit/CMakeLists.txt
@@ -22,31 +22,18 @@ add_library(bui_kit STATIC
intern/kit_regions.c
intern/kit_nodes.c
intern/kit_custom.c
- intern/kit_filebrowser.c
- intern/kit_filebrowser_paths.c
- intern/kit_filebrowser_posix.c
- intern/kit_filebrowser_win32.c
- intern/kit_filelist.c
- intern/kit_filelist_view.c
- intern/kit_fileops.c
- intern/kit_fsmenu.c
- # images (Blender's imbuf design): see intern/image/image.h
+ intern/kit_filebrowser.c # without BUI_FILEBROWSER: BK_file_dialog's stub only
+ # images and video (Blender's imbuf design): see intern/image/image.h
intern/image/imbuf.c
intern/image/filetype.c
intern/image/anim.c
intern/image/play.c
- intern/image/format_wuffs.c
- intern/image/format_jpeg.c
- intern/image/format_webp.c
- intern/image/format_hdr.c
- intern/image/format_pnm.c
intern/image/format_svg.c
intern/image/cache.c
intern/image/loader.c
intern/image/draw.c
intern/image/video.c
intern/image/movie.c
- intern/image/movie_mf.c
# Blender's UI sources copied and edited for the kit (C++; see blender/bk_blender.h)
intern/blender/interface_string_search.cc
intern/blender/interface_utils.cc
@@ -59,45 +46,78 @@ target_include_directories(bui_kit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/extern/na
set_source_files_properties(extern/nanosvg/blender_nanosvg.c extern/nanosvg/blender_raster.c
PROPERTIES COMPILE_OPTIONS "$<IF:$<C_COMPILER_ID:MSVC>,/w,-w>")
add_library(bui::kit ALIAS bui_kit)
+# the optional parts this build has (features.json): BUI_IMAGE_*, BUI_VIDEO*, ...
+bui_features(bui_kit)
+
+if(BUI_FILEBROWSER) # Blender's File Browser (editors/space_file)
+ target_sources(bui_kit PRIVATE
+ intern/kit_filebrowser_paths.c
+ intern/kit_filebrowser_posix.c
+ intern/kit_filebrowser_win32.c
+ intern/kit_filelist.c
+ intern/kit_filelist_view.c
+ intern/kit_fileops.c
+ intern/kit_fsmenu.c)
+endif()
# -- image decoders (third party, decoder parts only; see extern/*/README.bui) --
-option(BUI_IMAGE_SVG "SVG images through the kit's nanosvg (no extra code: the icons use it)" ON)
-if(BUI_IMAGE_SVG)
- target_compile_definitions(bui_kit PRIVATE BUI_IMAGE_SVG)
+set(_bui_quiet "$<IF:$<C_COMPILER_ID:MSVC>,/w,-w>")
+foreach(_f HDR PNM)
+ if(BUI_IMAGE_${_f})
+ string(TOLOWER ${_f} _lf)
+ target_sources(bui_kit PRIVATE intern/image/format_${_lf}.c)
+ endif()
+endforeach()
+# Wuffs: one translation unit (format_wuffs.c includes wuffs-v0.4.c) with the
+# modules of the formats this build has
+if(BUI_IMAGE_PNG OR BUI_IMAGE_GIF OR BUI_IMAGE_BMP OR BUI_IMAGE_TGA OR BUI_IMAGE_QOI)
+ target_sources(bui_kit PRIVATE intern/image/format_wuffs.c)
+ target_include_directories(bui_kit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/extern/wuffs)
+ set_source_files_properties(intern/image/format_wuffs.c PROPERTIES COMPILE_OPTIONS "${_bui_quiet}")
endif()
-if(WIN32)
- target_compile_definitions(bui_kit PRIVATE BUI_VIDEO_MF)
+# Video: the OS backend of this platform, loaded at run time; dav1d / FFmpeg
+# when asked for (they link those libraries)
+if(BUI_VIDEO)
+ if(WIN32)
+ target_sources(bui_kit PRIVATE intern/image/movie_mf.c)
+ target_compile_definitions(bui_kit PRIVATE BUI_VIDEO_MF)
+ elseif(APPLE)
+ target_sources(bui_kit PRIVATE intern/image/movie_avf.m)
+ set_source_files_properties(intern/image/movie_avf.m PROPERTIES COMPILE_OPTIONS "-fno-objc-arc")
+ target_compile_definitions(bui_kit PRIVATE BUI_VIDEO_AVF)
+ target_link_libraries(bui_kit PUBLIC "-weak_framework AVFoundation" "-weak_framework CoreMedia"
+ "-weak_framework CoreVideo")
+ else()
+ target_sources(bui_kit PRIVATE intern/image/movie_gst.c)
+ target_compile_definitions(bui_kit PRIVATE BUI_VIDEO_GST)
+ target_link_libraries(bui_kit PUBLIC ${CMAKE_DL_LIBS})
+ endif()
endif()
-set(_bui_quiet "$<IF:$<C_COMPILER_ID:MSVC>,/w,-w>")
-# Wuffs: one translation unit (format_wuffs.c includes wuffs-v0.4.c)
-target_include_directories(bui_kit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/extern/wuffs)
-set_source_files_properties(intern/image/format_wuffs.c PROPERTIES COMPILE_OPTIONS "${_bui_quiet}")
-# libjpeg-turbo 3.1.2: the 8-bit decompressor, no SIMD (it needs NASM)
-file(GLOB _bui_jpeg_src extern/libjpeg-turbo/src/*.c extern/libjpeg-turbo/src/wrapper/*.c)
-list(FILTER _bui_jpeg_src EXCLUDE REGEX "/(jdcolext|jdcol565|jdmrgext|jdmrg565|jstdhuff|jdapistd|jdcoefct|jdcolor|jddctmgr|jddiffct|jdlossls|jdmainct|jdmerge|jdpostct|jdsample|jidctflt|jidctfst|jidctint|jidctred|jutils)\.c$")
-add_library(bui_jpeg OBJECT ${_bui_jpeg_src}
- extern/libjpeg-turbo/bui/jmem_bui.c extern/libjpeg-turbo/bui/jdecode_8bit_only.c)
-target_include_directories(bui_jpeg PRIVATE extern/libjpeg-turbo/bui extern/libjpeg-turbo/src
- ${CMAKE_SOURCE_DIR}/core/include)
-target_compile_options(bui_jpeg PRIVATE ${_bui_quiet})
-# libwebp 1.6.0: decoder + demux
-file(GLOB _bui_webp_src extern/libwebp/src/dec/*.c extern/libwebp/src/dsp/*.c
- extern/libwebp/src/utils/*.c extern/libwebp/src/demux/*.c)
-add_library(bui_webp OBJECT ${_bui_webp_src})
-target_include_directories(bui_webp PRIVATE extern/libwebp)
-# the decode threads call libwebp at once: its DSP init takes a lock with pthreads
-# (upstream's Windows path is lock-free by design)
-if(NOT WIN32)
- target_compile_definitions(bui_webp PRIVATE WEBP_USE_THREAD)
+if(BUI_IMAGE_JPEG) # libjpeg-turbo 3.1.2: the 8-bit decompressor, no SIMD (it needs NASM)
+ file(GLOB _bui_jpeg_src extern/libjpeg-turbo/src/*.c extern/libjpeg-turbo/src/wrapper/*.c)
+ list(FILTER _bui_jpeg_src EXCLUDE REGEX "/(jdcolext|jdcol565|jdmrgext|jdmrg565|jstdhuff|jdapistd|jdcoefct|jdcolor|jddctmgr|jddiffct|jdlossls|jdmainct|jdmerge|jdpostct|jdsample|jidctflt|jidctfst|jidctint|jidctred|jutils)\\.c$")
+ add_library(bui_jpeg OBJECT ${_bui_jpeg_src}
+ extern/libjpeg-turbo/bui/jmem_bui.c extern/libjpeg-turbo/bui/jdecode_8bit_only.c)
+ target_include_directories(bui_jpeg PRIVATE extern/libjpeg-turbo/bui extern/libjpeg-turbo/src
+ ${CMAKE_SOURCE_DIR}/core/include)
+ target_compile_options(bui_jpeg PRIVATE ${_bui_quiet} $<$<NOT:$<C_COMPILER_ID:MSVC>>:-O2>)
+ target_sources(bui_kit PRIVATE intern/image/format_jpeg.c $<TARGET_OBJECTS:bui_jpeg>)
+ target_include_directories(bui_kit PRIVATE extern/libjpeg-turbo/bui extern/libjpeg-turbo/src)
endif()
-target_compile_options(bui_webp PRIVATE ${_bui_quiet})
-if(NOT MSVC)
- target_compile_options(bui_jpeg PRIVATE -O2)
- target_compile_options(bui_webp PRIVATE -O2)
+if(BUI_IMAGE_WEBP) # libwebp 1.6.0: decoder + demux
+ file(GLOB _bui_webp_src extern/libwebp/src/dec/*.c extern/libwebp/src/dsp/*.c
+ extern/libwebp/src/utils/*.c extern/libwebp/src/demux/*.c)
+ add_library(bui_webp OBJECT ${_bui_webp_src})
+ target_include_directories(bui_webp PRIVATE extern/libwebp)
+ # the decode threads call libwebp at once: its DSP init takes a lock with pthreads
+ # (upstream's Windows path is lock-free by design)
+ if(NOT WIN32)
+ target_compile_definitions(bui_webp PRIVATE WEBP_USE_THREAD)
+ endif()
+ target_compile_options(bui_webp PRIVATE ${_bui_quiet} $<$<NOT:$<C_COMPILER_ID:MSVC>>:-O2>)
+ target_sources(bui_kit PRIVATE intern/image/format_webp.c $<TARGET_OBJECTS:bui_webp>)
+ target_include_directories(bui_kit PRIVATE extern/libwebp)
endif()
-target_sources(bui_kit PRIVATE $<TARGET_OBJECTS:bui_jpeg> $<TARGET_OBJECTS:bui_webp>)
-target_include_directories(bui_kit PRIVATE extern/libjpeg-turbo/bui extern/libjpeg-turbo/src
- extern/libwebp)
# worker threads: bui_core links the platform's threads (Threads::Threads)
target_include_directories(bui_kit PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(bui_kit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/intern)
diff --git a/kit/intern/image/anim.c b/kit/intern/image/anim.c
index 0276998..c4fb1b2 100644
--- a/kit/intern/image/anim.c
+++ b/kit/intern/image/anim.c
@@ -33,13 +33,23 @@ struct BKImAnim {
BKImPlan plan; /* how frames are fitted */
};
+/* The in-memory animation codecs this build has (features.json). */
+static const BKImAnimCodec *const anim_codecs[] = {
+#ifdef BUI_IMAGE_GIF
+ &imb_gif_codec,
+#endif
+#ifdef BUI_IMAGE_WEBP
+ &imb_webp_codec,
+#endif
+ NULL,
+};
+
const BKImAnimCodec *imb_anim_codec(const uint8_t *mem, size_t size)
{
- if (imb_is_a_gif(mem, size)) {
- return &imb_gif_codec;
- }
- if (imb_webp_animated(mem, size)) {
- return &imb_webp_codec;
+ for (int i = 0; anim_codecs[i]; i++) {
+ if (anim_codecs[i]->is_a(mem, size)) {
+ return anim_codecs[i];
+ }
}
return NULL;
}
diff --git a/kit/intern/image/filetype.c b/kit/intern/image/filetype.c
index 4ff4d1f..7e931dd 100644
--- a/kit/intern/image/filetype.c
+++ b/kit/intern/image/filetype.c
@@ -19,22 +19,42 @@
#include "image.h"
-/* Order matters only for TGA, which has no signature: it goes last. */
+/* The formats this build has (features.json: BUI_IMAGE_*). Order matters
+ * only for TGA, which has no signature: it goes last. */
const BKImFileType BK_IMB_FILE_TYPES[] = {
+#ifdef BUI_IMAGE_PNG
{BK_IMAGE_PNG, "png", imb_is_a_png, imb_header_wuffs, imb_load_wuffs},
+#endif
+#ifdef BUI_IMAGE_JPEG
{BK_IMAGE_JPEG, "jpeg", imb_is_a_jpeg, imb_header_jpeg, imb_load_jpeg},
+#endif
+#ifdef BUI_IMAGE_WEBP
{BK_IMAGE_WEBP, "webp", imb_is_a_webp, imb_header_webp, imb_load_webp},
+#endif
+#ifdef BUI_IMAGE_GIF
{BK_IMAGE_GIF, "gif", imb_is_a_gif, imb_header_wuffs, imb_load_wuffs},
+#endif
+#ifdef BUI_IMAGE_BMP
{BK_IMAGE_BMP, "bmp", imb_is_a_bmp, imb_header_wuffs, imb_load_wuffs},
+#endif
+#ifdef BUI_IMAGE_QOI
{BK_IMAGE_QOI, "qoi", imb_is_a_qoi, imb_header_wuffs, imb_load_wuffs},
+#endif
+#ifdef BUI_IMAGE_HDR
{BK_IMAGE_HDR, "hdr", imb_is_a_hdr, imb_header_hdr, imb_load_hdr},
+#endif
+#ifdef BUI_IMAGE_PNM
{BK_IMAGE_PNM, "pnm", imb_is_a_pnm, imb_header_pnm, imb_load_pnm},
+#endif
#ifdef BUI_IMAGE_SVG
{BK_IMAGE_SVG, "svg", imb_is_a_svg, imb_header_svg, imb_load_svg},
#endif
+#ifdef BUI_IMAGE_TGA
{BK_IMAGE_TGA, "tga", imb_is_a_tga, imb_header_wuffs, imb_load_wuffs},
+#endif
+ {BK_IMAGE_NONE, NULL, NULL, NULL, NULL}, /* the end (a build may have no formats) */
};
-const int BK_IMB_FILE_TYPES_NUM = (int)(sizeof(BK_IMB_FILE_TYPES) / sizeof(BK_IMB_FILE_TYPES[0]));
+const int BK_IMB_FILE_TYPES_NUM = (int)(sizeof(BK_IMB_FILE_TYPES) / sizeof(BK_IMB_FILE_TYPES[0])) - 1;
const BKImFileType *imb_file_type(const uint8_t *mem, size_t size)
{
@@ -138,7 +158,11 @@ BKImBuf *imb_load(const uint8_t *mem, size_t size, const BKImageFit *target)
return target->frame > 0 ? imb_anim_frame(mem, size, target, target->frame - 1) :
imb_anim_all(mem, size, target);
}
+#ifdef BUI_IMAGE_JPEG
const int orient = t->ftype == BK_IMAGE_JPEG ? imb_jpeg_orientation(mem, size) : 1;
+#else
+ const int orient = 1; /* EXIF orientation: JPEG only */
+#endif
BKImPlan plan;
if (!imb_plan(target, info.width, info.height, orient, &plan)) {
return NULL;
diff --git a/kit/intern/image/format_wuffs.c b/kit/intern/image/format_wuffs.c
index d1cdd5c..a75d8b7 100644
--- a/kit/intern/image/format_wuffs.c
+++ b/kit/intern/image/format_wuffs.c
@@ -15,16 +15,27 @@
#define WUFFS_CONFIG__STATIC_FUNCTIONS
#define WUFFS_CONFIG__MODULES
#define WUFFS_CONFIG__MODULE__BASE
-#define WUFFS_CONFIG__MODULE__ADLER32
-#define WUFFS_CONFIG__MODULE__CRC32
-#define WUFFS_CONFIG__MODULE__DEFLATE
-#define WUFFS_CONFIG__MODULE__ZLIB
-#define WUFFS_CONFIG__MODULE__PNG
-#define WUFFS_CONFIG__MODULE__LZW
-#define WUFFS_CONFIG__MODULE__GIF
-#define WUFFS_CONFIG__MODULE__BMP
-#define WUFFS_CONFIG__MODULE__TARGA
-#define WUFFS_CONFIG__MODULE__QOI
+/* the modules of the formats this build has (features.json: BUI_IMAGE_*) */
+#ifdef BUI_IMAGE_PNG
+# define WUFFS_CONFIG__MODULE__ADLER32
+# define WUFFS_CONFIG__MODULE__CRC32
+# define WUFFS_CONFIG__MODULE__DEFLATE
+# define WUFFS_CONFIG__MODULE__ZLIB
+# define WUFFS_CONFIG__MODULE__PNG
+#endif
+#ifdef BUI_IMAGE_GIF
+# define WUFFS_CONFIG__MODULE__LZW
+# define WUFFS_CONFIG__MODULE__GIF
+#endif
+#ifdef BUI_IMAGE_BMP
+# define WUFFS_CONFIG__MODULE__BMP
+#endif
+#ifdef BUI_IMAGE_TGA
+# define WUFFS_CONFIG__MODULE__TARGA
+#endif
+#ifdef BUI_IMAGE_QOI
+# define WUFFS_CONFIG__MODULE__QOI
+#endif
#include "wuffs-v0.4.c"
/* -------------------------------------------------------------------- */
@@ -87,11 +98,21 @@ bool imb_is_a_tga(const uint8_t *mem, size_t size)
static wuffs_base__image_decoder *codec_new(const uint8_t *mem, size_t size)
{
+#ifdef BUI_IMAGE_PNG
if (imb_is_a_png(mem, size)) CODEC_NEW(png)
+#endif
+#ifdef BUI_IMAGE_GIF
if (imb_is_a_gif(mem, size)) CODEC_NEW(gif)
+#endif
+#ifdef BUI_IMAGE_BMP
if (imb_is_a_bmp(mem, size)) CODEC_NEW(bmp)
+#endif
+#ifdef BUI_IMAGE_QOI
if (imb_is_a_qoi(mem, size)) CODEC_NEW(qoi)
+#endif
+#ifdef BUI_IMAGE_TGA
if (imb_is_a_tga(mem, size)) CODEC_NEW(targa)
+#endif
return NULL;
}
@@ -111,9 +132,11 @@ static wuffs_base__image_decoder *open_image(const uint8_t *mem,
/* browsers ignore zlib/PNG checksums too; saves a pass over the data */
wuffs_base__image_decoder__set_quirk(dec, WUFFS_BASE__QUIRK_IGNORE_CHECKSUM, 1);
}
+#ifdef BUI_IMAGE_GIF
else if (imb_is_a_gif(mem, size)) {
wuffs_base__image_decoder__set_quirk(dec, WUFFS_GIF__QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA, 1);
}
+#endif
*src = wuffs_base__ptr_u8__reader((uint8_t *)mem, size, true);
memset(ic, 0, sizeof(*ic));
const wuffs_base__status st = wuffs_base__image_decoder__decode_image_config(dec, ic, src);
@@ -230,6 +253,8 @@ BKImBuf *imb_load_wuffs(const uint8_t *mem, size_t size, int want_w, int want_h)
return ib;
}
+#ifdef BUI_IMAGE_GIF
+
/* -------------------------------------------------------------------- */
/** \name GIF animation, one frame at a time (anim.c)
* \{ */
@@ -363,3 +388,5 @@ static size_t gif_bytes(const void *state)
const BKImAnimCodec imb_gif_codec = {"gif", false, imb_is_a_gif, gif_open, NULL, gif_next, gif_seek, gif_bytes, gif_close};
/** \} */
+
+#endif /* BUI_IMAGE_GIF */
diff --git a/kit/intern/image/movie_avf.m b/kit/intern/image/movie_avf.m
new file mode 100644
index 0000000..dc10ca7
--- /dev/null
+++ b/kit/intern/image/movie_avf.m
@@ -0,0 +1,209 @@
+/* Video on macOS: AVFoundation's AVAssetReader (the system demuxers and
+ * VideoToolbox decoders: H.264, HEVC, ProRes, VP9/AV1 where the OS has
+ * them), NV12 out of the decoder at the size drawn (kCVPixelBufferWidthKey
+ * / HeightKey on the track output: VideoToolbox scales).
+ *
+ * UNTESTED: written against the documented API without a Mac to run it on;
+ * it compiles only on Apple platforms (BUI_VIDEO_AVF) and the frameworks
+ * are weak-linked, so a failure here can't affect other platforms or
+ * start-up. Manual retain/release, as the Cocoa window host (-fno-objc-arc).
+ *
+ * AVAssetReader can't seek: a seek (or a new output size) starts a new
+ * reader at that time.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifdef BUI_VIDEO_AVF
+
+# import <AVFoundation/AVFoundation.h>
+# import <CoreMedia/CoreMedia.h>
+# import <CoreVideo/CoreVideo.h>
+
+# include <string.h>
+
+# include "MEM_guardedalloc.h"
+
+# include "image.h"
+
+typedef struct AvfMovie {
+ AVURLAsset *asset; /* retained */
+ AVAssetTrack *track; /* retained */
+ AVAssetReader *reader; /* retained; NULL until the first frame */
+ AVAssetReaderTrackOutput *out;
+ int w, h; /* output frame (NV12) */
+ double start; /* where the reader starts (seconds) */
+ double frame_dur;
+} AvfMovie;
+
+static void reader_drop(AvfMovie *m)
+{
+ if (m->reader) {
+ [m->reader cancelReading];
+ [m->reader release];
+ m->reader = nil;
+ }
+ m->out = nil; /* owned by the reader */
+}
+
+static void avf_close(void *state)
+{
+ AvfMovie *m = (AvfMovie *)state;
+ @autoreleasepool {
+ reader_drop(m);
+ [m->track release];
+ [m->asset release];
+ }
+ MEM_freeN(m);
+}
+
+/* A reader from m->start, decoding NV12 at m->w x m->h. */
+static bool reader_start(AvfMovie *m)
+{
+ reader_drop(m);
+ NSError *err = nil;
+ AVAssetReader *r = [[AVAssetReader alloc] initWithAsset:m->asset error:&err];
+ if (!r) {
+ return false;
+ }
+ NSDictionary *settings = @{
+ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange),
+ (id)kCVPixelBufferWidthKey : @(m->w),
+ (id)kCVPixelBufferHeightKey : @(m->h),
+ };
+ AVAssetReaderTrackOutput *out = [AVAssetReaderTrackOutput
+ assetReaderTrackOutputWithTrack:m->track
+ outputSettings:settings];
+ out.alwaysCopiesSampleData = NO;
+ if (![r canAddOutput:out]) {
+ [r release];
+ return false;
+ }
+ [r addOutput:out];
+ r.timeRange = CMTimeRangeMake(CMTimeMakeWithSeconds(m->start, 90000), kCMTimePositiveInfinity);
+ if (![r startReading]) {
+ [r release];
+ return false;
+ }
+ m->reader = r;
+ m->out = out;
+ return true;
+}
+
+static void *avf_open(const char *path, const uint8_t *mem, size_t size, BKImAnimInfo *r_info)
+{
+ (void)mem;
+ (void)size;
+ if (!path) {
+ return NULL;
+ }
+ AvfMovie *m = (AvfMovie *)MEM_callocN(sizeof(AvfMovie), "AvfMovie");
+ @autoreleasepool {
+ NSURL *url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:path]];
+ m->asset = url ? [[AVURLAsset alloc] initWithURL:url options:nil] : nil;
+# pragma clang diagnostic push
+# pragma clang diagnostic ignored "-Wdeprecated-declarations" /* the synchronous API */
+ NSArray<AVAssetTrack *> *tracks = [m->asset tracksWithMediaType:AVMediaTypeVideo];
+ m->track = tracks.count ? [tracks[0] retain] : nil;
+ const CGSize natural = m->track ? m->track.naturalSize : CGSizeZero;
+ const double duration = m->asset ? CMTimeGetSeconds(m->asset.duration) : 0.0;
+ const float fps = m->track ? m->track.nominalFrameRate : 0.0f;
+# pragma clang diagnostic pop
+ if (!m->track || natural.width < 2 || natural.height < 2) {
+ avf_close(m);
+ return NULL;
+ }
+ m->frame_dur = fps > 0 ? 1.0 / fps : 1.0 / 30.0;
+ r_info->src_w = (int)natural.width;
+ r_info->src_h = (int)natural.height;
+ r_info->duration = duration > 0.0 ? duration : 0.0;
+ r_info->loops = 1;
+ r_info->format = r_info->src_h >= 720 ? BUI_PIXMAP_NV12_BT709 : BUI_PIXMAP_NV12_BT601;
+ }
+ return m;
+}
+
+static bool avf_output(void *state, int w, int h, BKImAnimInfo *r_info)
+{
+ AvfMovie *m = (AvfMovie *)state;
+ m->w = BK_MAX(2, w & ~1);
+ m->h = BK_MAX(2, h & ~1);
+ bool ok;
+ @autoreleasepool {
+ ok = reader_start(m);
+ }
+ r_info->w = m->w;
+ r_info->h = m->h;
+ return ok;
+}
+
+static bool copy_frame(CVPixelBufferRef px, BKImBuf *dst)
+{
+ if (CVPixelBufferGetPlaneCount(px) != 2 || (int)CVPixelBufferGetWidth(px) != dst->x ||
+ (int)CVPixelBufferGetHeight(px) != dst->y ||
+ CVPixelBufferLockBaseAddress(px, kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess)
+ {
+ return false;
+ }
+ uint8_t *y = (uint8_t *)dst->rect, *uv = y + (size_t)dst->x * dst->y;
+ const uint8_t *sy = (const uint8_t *)CVPixelBufferGetBaseAddressOfPlane(px, 0);
+ const uint8_t *suv = (const uint8_t *)CVPixelBufferGetBaseAddressOfPlane(px, 1);
+ const size_t py = CVPixelBufferGetBytesPerRowOfPlane(px, 0);
+ const size_t puv = CVPixelBufferGetBytesPerRowOfPlane(px, 1);
+ for (int r = 0; r < dst->y; r++) {
+ memcpy(y + (size_t)r * dst->x, sy + r * py, (size_t)dst->x);
+ }
+ for (int r = 0; r < dst->y / 2; r++) {
+ memcpy(uv + (size_t)r * dst->x, suv + r * puv, (size_t)dst->x);
+ }
+ CVPixelBufferUnlockBaseAddress(px, kCVPixelBufferLock_ReadOnly);
+ return true;
+}
+
+static int avf_next(void *state, BKImBuf *dst, double *r_pts, double *r_dur)
+{
+ AvfMovie *m = (AvfMovie *)state;
+ int got = -1;
+ @autoreleasepool {
+ if (!m->reader && !reader_start(m)) {
+ return -1;
+ }
+ CMSampleBufferRef sb = [m->out copyNextSampleBuffer];
+ if (!sb) {
+ got = m->reader.status == AVAssetReaderStatusCompleted ? 0 : -1;
+ }
+ else {
+ CVPixelBufferRef px = CMSampleBufferGetImageBuffer(sb);
+ if (px && copy_frame(px, dst)) {
+ const CMTime pts = CMSampleBufferGetPresentationTimeStamp(sb);
+ const CMTime dur = CMSampleBufferGetDuration(sb);
+ *r_pts = CMTIME_IS_NUMERIC(pts) ? CMTimeGetSeconds(pts) : -1.0;
+ *r_dur = CMTIME_IS_NUMERIC(dur) ? CMTimeGetSeconds(dur) : m->frame_dur;
+ got = 1;
+ }
+ CFRelease(sb);
+ }
+ }
+ return got;
+}
+
+static bool avf_seek(void *state, double t)
+{
+ AvfMovie *m = (AvfMovie *)state;
+ m->start = t; /* readers start at a time (the keyframe before it: anim.c skips on) */
+ bool ok;
+ @autoreleasepool {
+ ok = reader_start(m);
+ }
+ return ok;
+}
+
+static size_t avf_bytes(const void *state)
+{
+ const AvfMovie *m = (const AvfMovie *)state;
+ return sizeof(*m) + (size_t)m->w * m->h * 3; /* decoded frames queued (an estimate) */
+}
+
+const BKImAnimCodec imb_movie_avf = {"avfoundation", true, imb_is_a_movie, avf_open, avf_output,
+ avf_next, avf_seek, avf_bytes, avf_close};
+
+#endif /* BUI_VIDEO_AVF */
diff --git a/kit/intern/image/movie_gst.c b/kit/intern/image/movie_gst.c
new file mode 100644
index 0000000..b036fd4
--- /dev/null
+++ b/kit/intern/image/movie_gst.c
@@ -0,0 +1,369 @@
+/* Video on Linux: GStreamer, loaded at run time (dlopen; no headers or
+ * libraries needed to build). GStreamer rather than raw VA-API: it brings
+ * the demuxers (MP4, WebM, MKV, ...) and every decoder the system has,
+ * picks VA-API hardware decoding through its va plugin where the GPU and
+ * driver offer it, and falls back to software (libav, openh264, dav1d)
+ * where not; raw VA-API would need our own demuxers and bitstream parsing
+ * per codec, and still a software fallback.
+ *
+ * The pipeline decodes, converts and scales to NV12 at the size drawn:
+ * filesrc ! decodebin ! videoconvert ! videoscale ! capsfilter ! appsink
+ * The appsink holds at most two frames and blocks the pipeline when they
+ * aren't pulled: a video that isn't shown costs nothing.
+ *
+ * The few GStreamer types used are mirrored here (stable 1.x ABI:
+ * GstBuffer timestamps, GstMapInfo, GstVideoMeta).
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifdef BUI_VIDEO_GST
+
+# include <dlfcn.h>
+# include <pthread.h>
+# include <stdio.h>
+# include <string.h>
+
+# include "MEM_guardedalloc.h"
+
+# include "image.h"
+
+/* -------------------------------------------------------------------- */
+/** \name GStreamer, loaded once
+ * \{ */
+
+typedef struct GstElement GstElement;
+typedef struct GstSample GstSample;
+typedef struct GstCaps GstCaps;
+typedef struct GstStructure GstStructure;
+typedef struct GstVideoMetaX GstVideoMetaX;
+typedef struct GstBufferX { /* GstBuffer: GstMiniObject (64 bytes), then: */
+ uint8_t mini_object[64];
+ void *pool;
+ uint64_t pts, dts, duration;
+} GstBufferX;
+typedef struct GstMapInfoX {
+ void *memory;
+ int flags;
+ uint8_t *data;
+ size_t size, maxsize;
+ void *user_data[4];
+ void *reserved[4];
+} GstMapInfoX;
+struct GstVideoMetaX { /* GstVideoMeta */
+ int meta_flags;
+ const void *info;
+ GstBufferX *buffer;
+ int flags, format;
+ unsigned id, width, height, n_planes;
+ size_t offset[4];
+ int stride[4];
+};
+
+enum { GST_STATE_NULL = 1, GST_STATE_PAUSED = 3, GST_STATE_PLAYING = 4 };
+enum { GST_STATE_CHANGE_FAILURE = 0 };
+enum { GST_FORMAT_TIME = 3 };
+enum { GST_SEEK_FLAG_FLUSH = 1 << 0, GST_SEEK_FLAG_KEY_UNIT = 1 << 2 };
+enum { GST_MAP_READ = 1 };
+# define GST_SECOND 1000000000ull
+# define GST_NONE ((uint64_t)-1)
+
+static struct {
+ pthread_once_t once;
+ bool ok;
+ int (*init_check)(int *, char ***, void **);
+ GstElement *(*parse_launch)(const char *, void **);
+ GstElement *(*bin_get_by_name)(GstElement *, const char *);
+ int (*set_state)(GstElement *, int);
+ int (*get_state)(GstElement *, int *, int *, uint64_t);
+ int (*query_duration)(GstElement *, int, int64_t *);
+ int (*seek_simple)(GstElement *, int, int, int64_t);
+ void (*object_unref)(void *);
+ void (*mini_object_unref)(void *);
+ GstBufferX *(*sample_get_buffer)(GstSample *);
+ GstCaps *(*sample_get_caps)(GstSample *);
+ GstCaps *(*caps_from_string)(const char *);
+ GstStructure *(*caps_get_structure)(const GstCaps *, unsigned);
+ int (*structure_get_int)(const GstStructure *, const char *, int *);
+ int (*structure_get_fraction)(const GstStructure *, const char *, int *, int *);
+ const char *(*structure_get_string)(const GstStructure *, const char *);
+ int (*buffer_map)(GstBufferX *, GstMapInfoX *, int);
+ void (*buffer_unmap)(GstBufferX *, GstMapInfoX *);
+ void (*object_set)(void *, const char *, ...);
+ void (*error_free)(void *);
+ GstSample *(*pull_sample)(GstElement *, uint64_t);
+ GstSample *(*pull_preroll)(GstElement *, uint64_t);
+ int (*is_eos)(GstElement *);
+ GstVideoMetaX *(*get_video_meta)(GstBufferX *);
+} G = {PTHREAD_ONCE_INIT};
+
+static void load(void)
+{
+ void *gst = dlopen("libgstreamer-1.0.so.0", RTLD_NOW | RTLD_LOCAL);
+ void *app = gst ? dlopen("libgstapp-1.0.so.0", RTLD_NOW | RTLD_LOCAL) : NULL;
+ void *video = gst ? dlopen("libgstvideo-1.0.so.0", RTLD_NOW | RTLD_LOCAL) : NULL;
+ if (!gst || !app) {
+ return; /* no GStreamer: no video, nothing else changes */
+ }
+# define SYM(lib, field, name) *(void **)&G.field = dlsym(lib, name)
+ SYM(gst, init_check, "gst_init_check");
+ SYM(gst, parse_launch, "gst_parse_launch");
+ SYM(gst, bin_get_by_name, "gst_bin_get_by_name");
+ SYM(gst, set_state, "gst_element_set_state");
+ SYM(gst, get_state, "gst_element_get_state");
+ SYM(gst, query_duration, "gst_element_query_duration");
+ SYM(gst, seek_simple, "gst_element_seek_simple");
+ SYM(gst, object_unref, "gst_object_unref");
+ SYM(gst, mini_object_unref, "gst_mini_object_unref");
+ SYM(gst, sample_get_buffer, "gst_sample_get_buffer");
+ SYM(gst, sample_get_caps, "gst_sample_get_caps");
+ SYM(gst, caps_from_string, "gst_caps_from_string");
+ SYM(gst, caps_get_structure, "gst_caps_get_structure");
+ SYM(gst, structure_get_int, "gst_structure_get_int");
+ SYM(gst, structure_get_fraction, "gst_structure_get_fraction");
+ SYM(gst, structure_get_string, "gst_structure_get_string");
+ SYM(gst, buffer_map, "gst_buffer_map");
+ SYM(gst, buffer_unmap, "gst_buffer_unmap");
+ SYM(gst, object_set, "g_object_set"); /* GLib, a dependency of GStreamer */
+ SYM(gst, error_free, "g_error_free");
+ SYM(app, pull_sample, "gst_app_sink_try_pull_sample");
+ SYM(app, pull_preroll, "gst_app_sink_try_pull_preroll");
+ SYM(app, is_eos, "gst_app_sink_is_eos");
+ if (video) {
+ SYM(video, get_video_meta, "gst_buffer_get_video_meta");
+ }
+# undef SYM
+ void **f = (void **)&G.init_check;
+ for (void **end = (void **)&G.get_video_meta; f < end; f++) {
+ if (!*f) {
+ return;
+ }
+ }
+ G.ok = G.init_check(NULL, NULL, NULL) != 0;
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name The codec
+ * \{ */
+
+typedef struct GstMovie {
+ GstElement *pipeline, *sink, *size;
+ int w, h; /* output frame (NV12) */
+ double frame_dur;
+ bool playing;
+} GstMovie;
+
+static void gst_close(void *state)
+{
+ GstMovie *m = (GstMovie *)state;
+ if (m->pipeline) {
+ G.set_state(m->pipeline, GST_STATE_NULL);
+ }
+ if (m->sink) {
+ G.object_unref(m->sink);
+ }
+ if (m->size) {
+ G.object_unref(m->size);
+ }
+ if (m->pipeline) {
+ G.object_unref(m->pipeline);
+ }
+ MEM_freeN(m);
+}
+
+/* The launch line with the path quoted (gst_parse_launch's escapes). */
+static char *pipeline_desc(const char *path)
+{
+ static const char head[] = "filesrc location=\"", tail[] =
+ "\" ! decodebin ! videoconvert ! videoscale ! capsfilter name=size "
+ "caps=video/x-raw,format=NV12 ! appsink name=sink sync=false max-buffers=2 "
+ "enable-last-sample=false";
+ const size_t n = strlen(path);
+ char *s = (char *)MEM_mallocN(sizeof(head) + 2 * n + sizeof(tail), "gst pipeline");
+ char *p = s;
+ memcpy(p, head, sizeof(head) - 1);
+ p += sizeof(head) - 1;
+ for (size_t i = 0; i < n; i++) {
+ if (path[i] == '"' || path[i] == '\\') {
+ *p++ = '\\';
+ }
+ *p++ = path[i];
+ }
+ memcpy(p, tail, sizeof(tail));
+ return s;
+}
+
+static void *gst_open(const char *path, const uint8_t *mem, size_t size, BKImAnimInfo *r_info)
+{
+ (void)mem;
+ (void)size;
+ pthread_once(&G.once, load);
+ if (!path || !G.ok) {
+ return NULL;
+ }
+ GstMovie *m = (GstMovie *)MEM_callocN(sizeof(GstMovie), "GstMovie");
+ char *desc = pipeline_desc(path);
+ void *err = NULL;
+ m->pipeline = G.parse_launch(desc, &err);
+ MEM_freeN(desc);
+ if (err) {
+ G.error_free(err);
+ }
+ m->sink = m->pipeline ? G.bin_get_by_name(m->pipeline, "sink") : NULL;
+ m->size = m->pipeline ? G.bin_get_by_name(m->pipeline, "size") : NULL;
+ /* preroll (paused): the first frame tells the size; no decoder, an error */
+ int st = 0;
+ if (!m->sink || !m->size || G.set_state(m->pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE ||
+ G.get_state(m->pipeline, &st, NULL, 10 * GST_SECOND) == GST_STATE_CHANGE_FAILURE ||
+ st != GST_STATE_PAUSED)
+ {
+ gst_close(m);
+ return NULL;
+ }
+ GstSample *pre = G.pull_preroll(m->sink, 0);
+ const GstStructure *s = pre ? G.caps_get_structure(G.sample_get_caps(pre), 0) : NULL;
+ int w = 0, h = 0, fn = 0, fd = 1, pn = 1, pd = 1;
+ const char *colorimetry = NULL;
+ if (s) {
+ G.structure_get_int(s, "width", &w);
+ G.structure_get_int(s, "height", &h);
+ G.structure_get_fraction(s, "framerate", &fn, &fd);
+ G.structure_get_fraction(s, "pixel-aspect-ratio", &pn, &pd);
+ colorimetry = G.structure_get_string(s, "colorimetry");
+ }
+ r_info->format = colorimetry && strstr(colorimetry, "709") ? BUI_PIXMAP_NV12_BT709 :
+ colorimetry && strstr(colorimetry, "601") ? BUI_PIXMAP_NV12_BT601 :
+ h >= 720 ? BUI_PIXMAP_NV12_BT709 :
+ BUI_PIXMAP_NV12_BT601;
+ if (pre) {
+ G.mini_object_unref(pre);
+ }
+ if (w <= 0 || h <= 0) {
+ gst_close(m);
+ return NULL;
+ }
+ m->w = w;
+ m->h = h;
+ m->frame_dur = fn > 0 && fd > 0 ? (double)fd / fn : 1.0 / 30.0;
+ int64_t dur = 0;
+ r_info->duration = G.query_duration(m->pipeline, GST_FORMAT_TIME, &dur) && dur > 0 ?
+ (double)dur / GST_SECOND :
+ 0.0;
+ r_info->src_w = pn > 0 && pd > 0 && pn != pd ? (int)((double)w * pn / pd + 0.5) : w;
+ r_info->src_h = h;
+ r_info->loops = 1;
+ return m;
+}
+
+static bool gst_output(void *state, int w, int h, BKImAnimInfo *r_info)
+{
+ GstMovie *m = (GstMovie *)state;
+ /* even, and rows of 4-byte multiples (videoconvert's NV12 stride) */
+ const int out_w = BK_MAX(4, w & ~3), out_h = BK_MAX(2, h & ~1);
+ char caps_s[128];
+ snprintf(caps_s, sizeof(caps_s),
+ "video/x-raw,format=NV12,width=%d,height=%d,pixel-aspect-ratio=1/1", out_w, out_h);
+ GstCaps *caps = G.caps_from_string(caps_s);
+ if (!caps) {
+ return false;
+ }
+ G.object_set(m->size, "caps", caps, NULL);
+ G.mini_object_unref(caps);
+ if (!m->playing) { /* the prerolled frame is at the old size: preroll again */
+ G.seek_simple(m->pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, 0);
+ }
+ m->w = out_w;
+ m->h = out_h;
+ r_info->w = out_w;
+ r_info->h = out_h;
+ return true;
+}
+
+/* The sample's planes into `dst` (NV12, its size); GstVideoMeta strides
+ * where the buffer has them. */
+static bool copy_frame(GstBufferX *buf, BKImBuf *dst)
+{
+ GstMapInfoX map;
+ if (!G.buffer_map(buf, &map, GST_MAP_READ)) {
+ return false;
+ }
+ const GstVideoMetaX *meta = G.get_video_meta ? G.get_video_meta(buf) : NULL;
+ size_t off[2] = {0, (size_t)dst->x * dst->y};
+ int stride[2] = {dst->x, dst->x};
+ if (meta && meta->n_planes == 2) {
+ off[0] = meta->offset[0];
+ off[1] = meta->offset[1];
+ stride[0] = meta->stride[0];
+ stride[1] = meta->stride[1];
+ }
+ const bool ok = stride[0] >= dst->x && stride[1] >= dst->x &&
+ off[0] + (size_t)stride[0] * (dst->y - 1) + dst->x <= map.size &&
+ off[1] + (size_t)stride[1] * (dst->y / 2 - 1) + dst->x <= map.size;
+ if (ok) {
+ uint8_t *y = (uint8_t *)dst->rect, *uv = y + (size_t)dst->x * dst->y;
+ for (int r = 0; r < dst->y; r++) {
+ memcpy(y + (size_t)r * dst->x, map.data + off[0] + (size_t)r * stride[0], (size_t)dst->x);
+ }
+ for (int r = 0; r < dst->y / 2; r++) {
+ memcpy(uv + (size_t)r * dst->x, map.data + off[1] + (size_t)r * stride[1], (size_t)dst->x);
+ }
+ }
+ G.buffer_unmap(buf, &map);
+ return ok;
+}
+
+static int gst_next(void *state, BKImBuf *dst, double *r_pts, double *r_dur)
+{
+ GstMovie *m = (GstMovie *)state;
+ if (!m->playing) {
+ m->playing = G.set_state(m->pipeline, GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE;
+ }
+ for (int stale = 0; stale < 8; stale++) {
+ GstSample *sample = G.pull_sample(m->sink, 5 * GST_SECOND);
+ if (!sample) {
+ return G.is_eos(m->sink) ? 0 : -1;
+ }
+ /* frames negotiated before the last size change: skip */
+ const GstStructure *s = G.caps_get_structure(G.sample_get_caps(sample), 0);
+ int w = 0, h = 0;
+ G.structure_get_int(s, "width", &w);
+ G.structure_get_int(s, "height", &h);
+ GstBufferX *buf = G.sample_get_buffer(sample);
+ const bool fits = w == dst->x && h == dst->y && buf;
+ const bool ok = fits && copy_frame(buf, dst);
+ if (ok) {
+ *r_pts = buf->pts != GST_NONE ? (double)buf->pts / GST_SECOND : -1.0;
+ *r_dur = buf->duration != GST_NONE ? (double)buf->duration / GST_SECOND : m->frame_dur;
+ }
+ G.mini_object_unref(sample);
+ if (ok) {
+ return 1;
+ }
+ if (fits) {
+ return -1;
+ }
+ }
+ return -1;
+}
+
+static bool gst_seek(void *state, double t)
+{
+ GstMovie *m = (GstMovie *)state;
+ return G.seek_simple(m->pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
+ (int64_t)(t * GST_SECOND)) != 0;
+}
+
+static size_t gst_bytes(const void *state)
+{
+ const GstMovie *m = (const GstMovie *)state;
+ return sizeof(*m) + (size_t)m->w * m->h * 3; /* the appsink's two frames (an estimate) */
+}
+
+const BKImAnimCodec imb_movie_gst = {
+ "gstreamer", true, imb_is_a_movie, gst_open, gst_output, gst_next, gst_seek, gst_bytes,
+ gst_close};
+
+/** \} */
+
+#endif /* BUI_VIDEO_GST */
diff --git a/kit/intern/kit_core.c b/kit/intern/kit_core.c
index 275af1f..9b6a9f4 100644
--- a/kit/intern/kit_core.c
+++ b/kit/intern/kit_core.c
@@ -159,7 +159,9 @@ static const BKClass *const g_classes[] = {
&bk_cls_gridview, &bk_cls_ramp, &bk_cls_curve, &bk_cls_normal,
&bk_cls_histogram, &bk_cls_frame, &bk_cls_radio, &bk_cls_canvas,
&bk_cls_textarea, &bk_cls_image, &bk_cls_meter, &bk_cls_custom, &bk_cls_video,
+#ifdef BUI_FILEBROWSER
&bk_cls_filebrowser,
+#endif
};
const BKClass *bk_class_find(const char *name)
diff --git a/kit/intern/kit_filebrowser.c b/kit/intern/kit_filebrowser.c
index 9e6d490..c0ebd1b 100644
--- a/kit/intern/kit_filebrowser.c
+++ b/kit/intern/kit_filebrowser.c
@@ -8,7 +8,8 @@
* - the execute region at the bottom: file name, file-type filter, Cancel and the execute
* button (file_panels.cc file_panel_execution_buttons_draw).
* This file builds the node, lays it out, maps keys and properties, and runs the blocking
- * dialog; kit_filebrowser_intern.h lists the other parts.
+ * dialog; kit_filebrowser_intern.h lists the other parts. Built without BUI_FILEBROWSER
+ * (features.json), only BK_file_dialog remains: it reports no browser (-1).
*
* SPDX-License-Identifier: GPL-2.0-or-later */
@@ -18,6 +19,8 @@
#include "kit_filebrowser_intern.h"
+#ifdef BUI_FILEBROWSER
+
static int u(void)
{
return bk_unit();
@@ -750,3 +753,32 @@ void BK_file_dialog_free(char **paths)
}
/** \} */
+
+#else /* !BUI_FILEBROWSER */
+
+int BK_file_dialog(const char *title,
+ int mode,
+ const char *dir,
+ const char *file,
+ const char *const *filters,
+ int nfilters,
+ int multiple,
+ char ***r_paths)
+{
+ (void)title;
+ (void)mode;
+ (void)dir;
+ (void)file;
+ (void)filters;
+ (void)nfilters;
+ (void)multiple;
+ *r_paths = NULL;
+ return -1; /* no file browser in this build */
+}
+
+void BK_file_dialog_free(char **paths)
+{
+ (void)paths;
+}
+
+#endif /* BUI_FILEBROWSER */