[FFmpeg-devel] [PATCH] avfilter: Proof of Concept: enable out-of-tree filters
Leandro Santiago
leandrosansilva at gmail.com
Mon Mar 24 18:20:02 EET 2025
In this iteration I've made the following changes, based on the received feedback:
- No external tools are needed by the build system.
- The external filters should be put in ext/libavfilter. Anything there will be included.
- The build system will execute a file called `configure.sh` in the directory of the external filter. This script has access to the functions and variables defined on `./configure`, as it's included via `.` on sh.
- I will document the "API" for the external filters as soon the approach is approved, but an example of filter can be found at https://gitlab.com/leandrosansilva/ffmpeg-extra-filter-example . If you are unhappy with the example code hosted on gitlab.com, I can move it elsewhere where it gets easier for you to have access to the code.
- Essentially, an external filter is composed by at least a `configure.sh` and a `Makefile`.
I really hope this can be the last iteration, as I ran out of ideas on how to simplify the process, so please let me know your thoughts :-)
Leandro
On 3/24/25 16:56, Leandro Santiago wrote:
> This is a POC/prototype that aims to enable out of tree filters on
> FFmpeg.
>
> Here is how to test it, with an example filter:
>
> ```
> mkdir -p ext/libavfilter
> pushd ext/libavfilter
> git clone https://gitlab.com/leandrosansilva/ffmpeg-extra-filter-example example
> popd
> ```
>
> Then compile ffmpeg as usual:
>
> ```
> ./configure && make && make install
> ```
>
> Now you can use the new filters:
>
> ```
> ffplay /path/to/file.webm -vf 'foo,bar'
> ```
>
> What works:
>
> - Building C based filters with no extra, or simple dependencies.
> - Multiple filters in the same object file.
>
> What was not implemented:
>
> - I believe it would be useful to check if the license of the filter is
> compatible with the license used to build FFmpeg.
>
> - Only extra filters written in C (maybe C++?) are supported for now.
> One of my goals is to enable Rust as well.
>
> - Documentation
>
> - There should be a way to have optional dependencies, for filters that
> can have multiple "backends", for instance, to be chosen at build time.
>
> Signed-off-by: Leandro Santiago <leandrosansilva at gmail.com>
> ---
> .gitignore | 3 ++
> configure | 60 ++++++++++++++++++++++++++++++++++++++--
> libavfilter/Makefile | 4 +++
> libavfilter/allfilters.c | 1 +
> 4 files changed, 66 insertions(+), 2 deletions(-)
>
> diff --git a/.gitignore b/.gitignore
> index 430abaf91b..7d66dee516 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -44,3 +44,6 @@
> /tools/python/__pycache__/
> /libavcodec/vulkan/*.c
> /libavfilter/vulkan/*.c
> +/ffbuild/external-filters.mak
> +/libavfilter/external_filters_extern.h
> +/ext
> diff --git a/configure b/configure
> index 2fdbe8cbbe..317862fa1a 100755
> --- a/configure
> +++ b/configure
> @@ -1798,6 +1798,7 @@ AVDEVICE_COMPONENTS="
>
> AVFILTER_COMPONENTS="
> filters
> + external_filters
> "
>
> AVFORMAT_COMPONENTS="
> @@ -4086,7 +4087,7 @@ swscale_deps="avutil"
> swscale_suggest="libm stdatomic"
>
> avcodec_extralibs="pthreads_extralibs iconv_extralibs dxva2_extralibs liblcevc_dec_extralibs lcms2_extralibs"
> -avfilter_extralibs="pthreads_extralibs"
> +avfilter_extralibs="pthreads_extralibs external_filters_extralibs"
> avutil_extralibs="d3d11va_extralibs d3d12va_extralibs mediacodec_extralibs nanosleep_extralibs pthreads_extralibs vaapi_drm_extralibs vaapi_x11_extralibs vaapi_win32_extralibs vdpau_x11_extralibs"
>
> # programs
> @@ -4324,10 +4325,15 @@ ALL_COMPONENTS="
> $AVFORMAT_COMPONENTS_LIST
> "
>
> -for n in $COMPONENT_LIST; do
> +enable_component() {
> + n="$1"
> v=$(toupper ${n%s})_LIST
> eval enable \$$v
> eval ${n}_if_any="\$$v"
> +}
> +
> +for n in $COMPONENT_LIST; do
> + enable_component "$n"
> done
>
> enable $ARCH_EXT_LIST
> @@ -7463,6 +7469,56 @@ elif enabled iconv; then
> check_func_headers iconv.h iconv || check_lib iconv iconv.h iconv -liconv
> fi
>
> +# Handle external filters from the directory ext/libavfilter
> +
> +echo "" > libavfilter/external_filters_extern.h
> +echo "" > ffbuild/external-filters.mak
> +
> +EXTERNAL_FILTER_LIST=""
> +
> +# This function is used by the external filters' configure.sh script to register the filters
> +register_external_filter_symbol() {
> + EXT_FILTER_SYMBOL="$1"
> + EXTERNAL_FILTER_LIST="$EXTERNAL_FILTER_LIST $(echo $EXT_FILTER_SYMBOL | sed 's/^ff_[avfsinkrc]\{2,5\}_\([[:alnum:]_]\{1,\}\)/\1_filter/')"
> +
> + eval "$(echo "$EXT_FILTER_SYMBOL" | sed 's/^ff_\([avfsinkrc]\{2,5\}\)_\([[:alnum:]]\{1,\}\)$/full_filter_name_\2=\1_\2/')"
> +
> + echo "extern const FFFilter $EXT_FILTER_SYMBOL;" >> libavfilter/external_filters_extern.h
> +
> + echo "EXTERNAL_FILTER_$(\
> + echo $EXT_FILTER_SYMBOL \
> + | sed 's/^ff_[avfsinkrc]\{2,5\}_\([[:alnum:]]\{1,\}\)$/\1/' \
> + | tr a-z A-Z \
> + )_LOCATION = $CURRENT_EXT_FILTER_LOCATION" >> ffbuild/external-filters.mak
> +}
> +
> +if [ -d "ext/libavfilter" ]; then
> + for f in ext/libavfilter/*; do
> + [ -f "$f/configure.sh" ] || die "Mandatory file $f/configure.sh not found"
> + [ -f "$f/Makefile" ] || die "Mandatory file $f/Makefile not found"
> + CURRENT_EXT_FILTER_LOCATION="$f"
> + . "$f/configure.sh" || die "Could not configure external filter at $f"
> + echo "include $f/Makefile" >> ffbuild/external-filters.mak
> + done
> +
> + enable_component external_filters
> +fi
> +
> +FILTER_LIST="
> + $FILTER_LIST
> + $EXTERNAL_FILTER_LIST
> +"
> +
> +AVFILTER_COMPONENTS_LIST="
> + $AVFILTER_COMPONENTS_LIST
> + $EXTERNAL_FILTER_LIST
> +"
> +
> +ALL_COMPONENTS="
> + $ALL_COMPONENTS
> + $EXTERNAL_FILTER_LIST
> +"
> +
> enabled debug && add_cflags -g"$debuglevel" && add_asflags -g"$debuglevel"
>
> # add some useful compiler flags if supported
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 7c0d879ec9..877b24c30f 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -27,6 +27,10 @@ OBJS = allfilters.o \
> include $(SRC_PATH)/libavfilter/dnn/Makefile
> include $(SRC_PATH)/libavfilter/vulkan/Makefile
>
> +# external filters handling
> +include $(SRC_PATH)/ffbuild/external-filters.mak
> +EXTERNAL_FILTER_FLAGS = -I$(PWD) -I$(PWD)/libavfilter
> +
> OBJS-$(HAVE_LIBC_MSVCRT) += file_open.o
> OBJS-$(HAVE_THREADS) += pthread.o
>
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 740d9ab265..c2d576e4be 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -621,6 +621,7 @@ extern const FFFilter ff_vsrc_buffer;
> extern const FFFilter ff_asink_abuffer;
> extern const FFFilter ff_vsink_buffer;
>
> +#include "libavfilter/external_filters_extern.h"
> #include "libavfilter/filter_list.c"
>
>
More information about the ffmpeg-devel
mailing list