Skip to content

External libraries integration

Alexander Berezhnoi edited this page Apr 24, 2020 · 6 revisions

It is possible to extend the script with a functionality that downloads and builds an additional library that will be integrated into FFmpeg's result binaries.

An actual list of libraries that are supported can be found in the FFmpeg's configure script near the 'External library support' section.

To add a new library to the compilation process several steps should be done:

1. Allow new argument parsing

In scripts/parse-arguments.sh file you need to add a block similar to this:

--enable-libdav1d|-dav1d)
      EXTERNAL_LIBRARIES+=( "libdav1d" )
    ;;

The actual value of the --enable-xxx has to be picked from the FFmpeg's configure script arguments list. Additional shorter version can be specified too.

The libdav1d in this case is a name of a library and it is used in several other places. Stay tuned.

2. Add 2 scripts for downloading and building the library

They both have to be placed in a directory with the name of an external library, like scripts/libdav1d, for example.

download.sh

This script has to download the source code of an appropriate library. By any means - that is up to this script. Currently all of existed scripts download an archive with the source code of a library of a certain version. The FFmpeg's source code can come from Git repository also.

This script also need to check if the source code is already downloaded to eliminate downloading it each time the script is executed.

This script is executed inside a directory where the source code of this library can be placed. Like sources/libdav1d for libdav1d library. Inside the script is free to create subdirectories and write the actual source code there. This possibility is done to easily switch between different versions of a library.

After the script knows that the source code is available, it has to export a variable with the absolute path to the actual source code. Like .../sources/libdav1d/dav1d-0.5.2 for the 0.5.2 version of the libdav1d. If it isn't done, the .../sources/libdav1d will be considered such a directory. The name of this variable to export has to follow this pattern: SOURCES_DIR_${library_name}, so for libdav1d it has to be strictly SOURCES_DIR_libdav1d.

Here are examples of how this script can be implemented:

build.sh

This script has to compile a static library from the source code downloaded with download.sh script and place it to a certain place. Only FFmpeg's build.sh produces shared libraries.

The script is executed in the source code directory that was exported as SOURCES_DIR_${library_name}.

This script will be executed for each ABI to build. By default, it will be executed 4 times: for armeabi-v7a, arm64-v8a, x86 and x86_64.

This script relies on a bunch of already exported variables that point to toolchain and sysroot directory, the cross compiler for the specified ABI, the linker, the path to place the result binary and many other stuff. The full list of ABI-specific variables can be found in scripts/export-build-variables.sh.

Also there are some variables specific to the host machine, like a number of cores in the CPU. Those variables are listed in scripts/export-host-variables.sh. Certain variables contain a path to a binary that has to be installed separately, like meson and ninja for libdav1d building.

Basically, a build.sh script has to execute library-specific mechanism that produces the static library and pass all currently defined variables to that mechanism.

Here are quite different examples of how it can be done:

One more thing. Sometimes an external library needs to add a linker flag to FFmpeg building process. Such a flag can be added via a special variable. The scripts/libopus is an example of it:

export FFMPEG_EXTRA_LD_FLAGS="${FFMPEG_EXTRA_LD_FLAGS} -lm"

The FFMPEG_EXTRA_LD_FLAGS variable will be passed to FFmpeg's configure script later.

Beware that if at least one external library is licensed under GPL, then the whole FFmpeg library will be also under GPL license. Currently the ffmpeg-android-maker ignores license check during the assembling and supports only free libraries.