Skip to content

Allow FFmpeg linking on Android - #13726

Merged
opencv-pushbot merged 1 commit into
opencv:3.4from
nicolaerosia:android-ffmpeg
Feb 14, 2019
Merged

opencv-pushbot merged 1 commit into
opencv:3.4from
nicolaerosia:android-ffmpeg

Conversation

@nicolaerosia

@nicolaerosia nicolaerosia commented Jan 30, 2019 •

Copy link
Copy Markdown
Contributor

In order to help CMake and PkgConfig find FFmpeg you need to:

  1. export PKG_CONFIG_LIBDIR=${SYSROOT}
    where SYSROOT=${ANDROID_NDK}/sysroot
  2. Add -DCMAKE_PREFIX_PATH=${FFMPEG_INSTALL_PREFIX}
  3. Append -DCMAKE_FIND_ROOT_PATH=${FFMPEG_INSTALL_PREFIX}

Sample scripts are below in comments.

Fixes: #4451 #5236 #12600

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you build FFmpeg for Android? (some instructions which are used)


# rewrite libraries to absolute paths
foreach(lib ${FFMPEG_LIBRARIES})
find_library(FFMPEG_ABSOLUTE_${lib} "${lib}" PATHS "${FFMPEG_lib${lib}_LIBDIR}" NO_DEFAULT_PATH)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dump values of:

  • CMAKE_FIND_ROOT_PATH
  • ${FFMPEG_lib${lib}_LIBDIR}
  • your FFMPEG_INSTALL_PREFIX

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMAKE_FIND_ROOT_PATH: /home/user/Android/android-ndk-r18b
-- FFMPEG_LIBRARY_DIRS: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavformat_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavcodec_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libz_LIBDIR: 
-- FFMPEG_libswscale_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavutil_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libm_LIBDIR: 
-- FFMPEG_libavresample_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavutil_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libm_LIBDIR: 
FFMPEG_INSTALL_PREFIX /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3
tree of FFMPEG_INSTALL_PREFIX:
/home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/
├── include
│   ├── libavcodec
│   │   ├── ac3_parser.h
│   │   ├── adts_parser.h
│   │   ├── avcodec.h
│   │   ├── avdct.h
│   │   ├── avfft.h
│   │   ├── d3d11va.h
│   │   ├── dirac.h
│   │   ├── dv_profile.h
│   │   ├── dxva2.h
│   │   ├── jni.h
│   │   ├── mediacodec.h
│   │   ├── qsv.h
│   │   ├── vaapi.h
│   │   ├── vdpau.h
│   │   ├── version.h
│   │   ├── videotoolbox.h
│   │   ├── vorbis_parser.h
│   │   └── xvmc.h
│   ├── libavdevice
│   │   ├── avdevice.h
│   │   └── version.h
│   ├── libavfilter
│   │   ├── avfilter.h
│   │   ├── buffersink.h
│   │   ├── buffersrc.h
│   │   └── version.h
│   ├── libavformat
│   │   ├── avformat.h
│   │   ├── avio.h
│   │   └── version.h
│   ├── libavresample
│   │   ├── avresample.h
│   │   └── version.h
│   ├── libavutil
│   │   ├── adler32.h
│   │   ├── aes_ctr.h
│   │   ├── aes.h
│   │   ├── attributes.h
│   │   ├── audio_fifo.h
│   │   ├── avassert.h
│   │   ├── avconfig.h
│   │   ├── avstring.h
│   │   ├── avutil.h
│   │   ├── base64.h
│   │   ├── blowfish.h
│   │   ├── bprint.h
│   │   ├── bswap.h
│   │   ├── buffer.h
│   │   ├── camellia.h
│   │   ├── cast5.h
│   │   ├── channel_layout.h
│   │   ├── common.h
│   │   ├── cpu.h
│   │   ├── crc.h
│   │   ├── des.h
│   │   ├── dict.h
│   │   ├── display.h
│   │   ├── downmix_info.h
│   │   ├── encryption_info.h
│   │   ├── error.h
│   │   ├── eval.h
│   │   ├── ffversion.h
│   │   ├── fifo.h
│   │   ├── file.h
│   │   ├── frame.h
│   │   ├── hash.h
│   │   ├── hmac.h
│   │   ├── hwcontext_cuda.h
│   │   ├── hwcontext_d3d11va.h
│   │   ├── hwcontext_drm.h
│   │   ├── hwcontext_dxva2.h
│   │   ├── hwcontext.h
│   │   ├── hwcontext_mediacodec.h
│   │   ├── hwcontext_qsv.h
│   │   ├── hwcontext_vaapi.h
│   │   ├── hwcontext_vdpau.h
│   │   ├── hwcontext_videotoolbox.h
│   │   ├── imgutils.h
│   │   ├── intfloat.h
│   │   ├── intreadwrite.h
│   │   ├── lfg.h
│   │   ├── log.h
│   │   ├── lzo.h
│   │   ├── macros.h
│   │   ├── mastering_display_metadata.h
│   │   ├── mathematics.h
│   │   ├── md5.h
│   │   ├── mem.h
│   │   ├── motion_vector.h
│   │   ├── murmur3.h
│   │   ├── opt.h
│   │   ├── parseutils.h
│   │   ├── pixdesc.h
│   │   ├── pixelutils.h
│   │   ├── pixfmt.h
│   │   ├── random_seed.h
│   │   ├── rational.h
│   │   ├── rc4.h
│   │   ├── replaygain.h
│   │   ├── ripemd.h
│   │   ├── samplefmt.h
│   │   ├── sha512.h
│   │   ├── sha.h
│   │   ├── spherical.h
│   │   ├── stereo3d.h
│   │   ├── tea.h
│   │   ├── threadmessage.h
│   │   ├── timecode.h
│   │   ├── time.h
│   │   ├── timestamp.h
│   │   ├── tree.h
│   │   ├── twofish.h
│   │   ├── version.h
│   │   └── xtea.h
│   ├── libswresample
│   │   ├── swresample.h
│   │   └── version.h
│   └── libswscale
│       ├── swscale.h
│       └── version.h
├── lib
│   ├── libavcodec.a
│   ├── libavdevice.a
│   ├── libavfilter.a
│   ├── libavformat.a
│   ├── libavresample.a
│   ├── libavutil.a
│   ├── libswresample.a
│   ├── libswscale.a
│   └── pkgconfig
│       ├── libavcodec.pc
│       ├── libavdevice.pc
│       ├── libavfilter.pc
│       ├── libavformat.pc
│       ├── libavresample.pc
│       ├── libavutil.pc
│       ├── libswresample.pc
│       └── libswscale.pc
└── share
    └── ffmpeg
        └── examples
            ├── avio_dir_cmd.c
            ├── avio_reading.c
            ├── decode_audio.c
            ├── decode_video.c
            ├── demuxing_decoding.c
            ├── encode_audio.c
            ├── encode_video.c
            ├── extract_mvs.c
            ├── filter_audio.c
            ├── filtering_audio.c
            ├── filtering_video.c
            ├── http_multiclient.c
            ├── hw_decode.c
            ├── Makefile
            ├── metadata.c
            ├── muxing.c
            ├── qsvdec.c
            ├── README
            ├── remuxing.c
            ├── resampling_audio.c
            ├── scaling_video.c
            ├── transcode_aac.c
            ├── transcoding.c
            ├── vaapi_encode.c
            └── vaapi_transcode.c

14 directories, 154 files

@nicolaerosia

nicolaerosia commented Jan 30, 2019 •

Copy link
Copy Markdown
Contributor Author

Grab Android NDK 18 and create a standalone toolchain (optional, you can comment it in script)

export ANDROID_NDK_HOME=${HOME}/Android/android-ndk-r18b

${ANDROID_NDK_HOME}/build/tools/make_standalone_toolchain.py \
--arch arm64 \
--api 26 \
-v \
--install-dir=${HOME}/Android/android-ndk-r18b_arm64_api26

checkout ffmpeg git, tags/n4.0.3

#!/bin/bash

if [ $# -ne 1 ]; then
	echo "usage: ./build.sh <ARCH>"
	echo "ARCH: arm64-v8a x86_64"
	exit 1
fi;

set -e
set -o xtrace

ARCH=$1
# make sure this level is the same as with OpenCV
# Android 8.0 26 (https://source.android.com/setup/start/build-numbers#platform-code-names-versions-api-levels-and-ndk-releases)
ANDROID_NATIVE_API_LEVEL=26

case $ARCH in
	"arm64-v8a")
		TARGET="aarch64-linux-android"
		CC_FLAGS="-target aarch64-none-linux-android -march=armv8-a -mfpu=neon -mfloat-abi=soft"
		PLATFORM_ARCH="arm64"
		LIB_FOLDER="lib"
		CONFIGURATION_ARCH="--disable-pthreads"
		TOOLCHAIN_FOLDER=$TARGET
		FFMPEG_ARCH=aarch64
	;;

	"x86_64")
		TARGET="x86_64-linux-android"
		CC_FLAGS="-target x86_64-none-linux-androideabi -msse4.2 -mpopcnt -m64 -mtune=intel"
		PLATFORM_ARCH="x86_64"
		LIB_FOLDER="lib64"
		CONFIGURATION_ARCH=""
		TOOLCHAIN_FOLDER=$PLATFORM_ARCH
		FFMPEG_ARCH=x86_64
	;;

	*)
		echo "unsupported platform"
		exit 1
	;;
esac

FFMPEG_VERSION="n4.0.3"
# 4.1 is broken
#FFMPEG_VERSION="n4.1"
FFMPEG_SRC=$HOME/android-src/ffmpeg
FFMPEG_INSTALL_PREFIX=$HOME/android-install/ffmpeg-$ARCH-${FFMPEG_VERSION}

# if you want to use regular android ndk
#ANDROID_NDK_PREBUILT=0
#ANDROID_NDK=${HOME}/Android/android-ndk-r16b
#ANDROID_NDK=${HOME}/Android/android-ndk-r18b

# using prebuilt android ndk
ANDROID_NDK_PREBUILT=1
ANDROID_NDK=${HOME}/Android/android-ndk-r18b_arm64_api26

echo "ARCH: ${ARCH}"
echo "FFMPEG_SRC: ${FFMPEG_SRC}"
echo "FFMPEG_INSTALL_PREFIX: ${FFMPEG_INSTALL_PREFIX}"
echo "FFMPEG_VERSION: ${FFMPEG_VERSION}"
echo "ANDROID_NDK: ${ANDROID_NDK}"

if [ ! -d ${FFMPEG_SRC} ]; then
	git clone https://github.com/FFmpeg/FFmpeg.git $FFMPEG_SRC

	cd $FFMPEG_SRC
	git checkout -b local-${FFMPEG_VERSION} tags/${FFMPEG_VERSION}
fi

SYSROOT=${ANDROID_NDK}/sysroot
HOST=linux-x86_64
CFLAGS="-O3 -fPIC"
LDFLAGS="-lc"

if [ ${ANDROID_NDK_PREBUILT} -eq 1 ]; then
	LLVM_TOOLCHAIN=${ANDROID_NDK}/bin
	TOOLCHAIN=${ANDROID_NDK}/bin
else
	LLVM_TOOLCHAIN="${ANDROID_NDK}/toolchains/llvm/prebuilt/${HOST}/bin"
	TOOLCHAIN=${ANDROID_NDK}/toolchains/$TOOLCHAIN_FOLDER-4.9/prebuilt/$HOST/bin
fi

CC=${LLVM_TOOLCHAIN}/clang
CXX=${LLVM_TOOLCHAIN}/clang++
AS=$CC
AR=${TOOLCHAIN}/${TARGET}-ar
LD=${TOOLCHAIN}/${TARGET}-ld
STRIP=${TOOLCHAIN}/${TARGET}-strip

# WORKAROUND: v4l2 is disabled because on NDK r18b it emits an error related to ioctl
# --disable-indev=v4l2 \
# --disable-outdev=v4l2 \

CONFIGURATION_OPTS_1="--enable-small --disable-programs --enable-protocol=file \
--disable-outdevs \
--disable-indevs \
--disable-protocols \
--enable-protocol=file \
"

CONFIGURATION_OPTS_2="--enable-small --disable-programs \
--disable-protocols \
--enable-protocol=file \
--disable-protocol=http \
--disable-protocol=rtp \
--disable-protocol=tcp \
--disable-protocol=udp \
"

EXTRA_CFLAGS="$CC_FLAGS -DANDROID -I$SYSROOT/usr/include/$TARGET $CFLAGS"
if [ ${ANDROID_NDK_PREBUILT} -eq 1 ]; then
	EXTRA_LDFLAGS="-L${ANDROID_NDK}/lib/gcc/$TARGET/4.9.x -L${SYSROOT}/usr/${LIB_FOLDER} $LDFLAGS"
else
	EXTRA_LDFLAGS="-L${ANDROID_NDK}/toolchains/$TOOLCHAIN_FOLDER-4.9/prebuilt/$HOST/lib/gcc/$TARGET/4.9.x -L${ANDROID_NDK}/platforms/android-${ANDROID_NATIVE_API_LEVEL}/arch-$PLATFORM_ARCH/usr/${LIB_FOLDER} $LDFLAGS"
fi

CONFIGURATION_OPTS=${CONFIGURATION_OPTS_1}
mkdir -p ${FFMPEG_INSTALL_PREFIX}
cd ${FFMPEG_SRC}
# this may fail if this is a clean build already
make clean || true
./configure \
--arch=${FFMPEG_ARCH} \
--prefix=${FFMPEG_INSTALL_PREFIX} \
--disable-asm \
--enable-cross-compile \
--disable-doc \
${CONFIGURATION_OPTS} \
${CONFIGURATION_ARCH} \
--enable-pic \
--disable-shared \
--enable-static \
--enable-avresample \
--disable-decoder=opus \
--disable-parser=opus \
--disable-muxer=opus \
--disable-encoder=opus \
--disable-encoder=libopus \
--disable-encoder=libcodec2 \
--disable-muxer=codec2 \
--disable-muxer=codec2raw \
--disable-muxer=spdif \
--disable-demuxer=codec2 \
--disable-demuxer=codec2raw \
--disable-demuxer=spdif \
--ar=$AR --strip=$STRIP --ld=$LD --cc=$CC --cxx=$CXX --as=$AS \
--target-os=android \
--extra-cflags="${EXTRA_CFLAGS}" \
--extra-ldflags="${EXTRA_LDFLAGS}" \
--sysroot=$SYSROOT --extra-libs=-lgcc \

make V=1 -j1 && make install
ls -lh $FFMPEG_INSTALL_PREFIX/lib/*.a

OpenCV build:

#!/bin/bash

if [ $# -ne 1 ]; then
	echo "usage: ./build.sh <ARCH>"
	echo "ARCH: arm64-v8a x86_64"
	exit 1
fi;

set -e
set -o xtrace

ARCH=$1
ANDROID_NATIVE_API_LEVEL=26

case $ARCH in
	"arm64-v8a")
		echo "ARCH: $ARCH"
		ANDROID_PLATFORM_ID=3
	;;

	"x86_64")
		echo "ARCH: $ARCH"
		ANDROID_PLATFORM_ID=5
	;;

	*)
		echo "ARCH: $ARCH unsupported"
		exit 1
	;;
esac

FFMPEG_INSTALL_PREFIX=$HOME/android-install/ffmpeg-$ARCH-n4.0.3

# OBSOLETE NOT ANYMORE Android tools 25.2.5 is a must, check these changes:
# https://github.com/xuchong/android-opencv-ffmpeg/issues/2
# https://gist.github.com/xuchong/31426c624bffd8802ecf17a830bfef53
#export ANDROID_SDK=$HOME/Android/tools-r25.2.5
#export ANDROID_SDK=/home/user/Android/android-studio-ide-181.5056338
#export ANDROID_HOME=/home/user/Android/android-studio-ide-181.5056338
export ANDROID_SDK=/home/user/Android/android-studio-ide-182.5199772/Sdk
export ANDROID_HOME=/home/user/Android/android-studio-ide-182.5199772/Sdk
#export ANDROID_NDK=${HOME}/Android/android-ndk-r16b
export ANDROID_NDK=${HOME}/Android/android-ndk-r18b
SYSROOT=${ANDROID_NDK}/sysroot

# OBSOLETE NOT ANYMORE:
# linking with ffmpeg is broken - dunno why need to add lib dir
# go to ffmpeg, and copy *.a to /home/user/android-src/ffmpeg/android/arm64-v8a/lib
# cp /home/user/android-src/ffmpeg/android/arm64-v8a/lib/*.a /home/user/android-src/ffmpeg/android/arm64-v8a/lib
# Fixed now: you don't need to set PKG_CONFIG_SYSROOT_DIR
#export PKG_CONFIG_SYSROOT_DIR=$SYSROOT
export PKG_CONFIG_LIBDIR=$SYSROOT

# BUILD_TESTS && INSTALL_TESTS fail
# BUILD_ANDROID_EXAMPLES && INSTALL_ANDROID_EXAMPLES fail
OPENCV_SRC_DIR=$HOME/android-src/opencv
OPENCV_VERSION="4.0.0"

OPENCV_BUILD_DIR=$HOME/android-build/opencv-${OPENCV_VERSION}-api${ANDROID_NATIVE_API_LEVEL}-$ARCH
OPENCV_INSTALL_DIR=$HOME/android-install/opencv-${OPENCV_VERSION}-api${ANDROID_NATIVE_API_LEVEL}-$ARCH
OPENCV_CONTRIB_SRC_DIR=$HOME/android-src/opencv_contrib

# for x86_64 add IPP
# -DWITH_IPP=OFF \

rm -rf ${OPENCV_BUILD_DIR}

mkdir -p ${OPENCV_BUILD_DIR}
cd ${OPENCV_BUILD_DIR}
cmake \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_ABI=${ARCH} \
-DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL} \
-DANDROID_PLATFORM_ID=${ANDROID_PLATFORM_ID} \
-DANDROID_STL=c++_shared \
-DCMAKE_PREFIX_PATH=${FFMPEG_INSTALL_PREFIX} \
-DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_DIR} \
-DPKG_CONFIG_EXECUTABLE=/usr/bin/pkg-config \
-GNinja \
-DOPENCV_EXTRA_MODULES_PATH=${OPENCV_CONTRIB_SRC_DIR}/modules \
-DBUILD_TESTS=OFF \
-DINSTALL_TESTS=OFF \
-DBUILD_ANDROID_EXAMPLES=OFF \
-DINSTALL_ANDROID_EXAMPLES=OFF \
-DBUILD_PERF_TESTS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_DOCS=OFF \
-DWITH_TBB=ON \
-DWITH_OPENCL=OFF \
-DWITH_FFMPEG=ON \
-DOPENCV_FFMPEG_SKIP_DOWNLOAD=TRUE \
${OPENCV_SRC_DIR}

ninja-build install

@mshabunin

Copy link
Copy Markdown
Contributor

@nicolaerosia ,could you please check whether the following fix helps in your environment:

find_library(FFMPEG_ABSOLUTE_${lib} "${lib}" PATHS "${FFMPEG_lib${lib}_LIBDIR}" "${FFMPEG_LIBRARY_DIRS}" NO_DEFAULT_PATH)

These find_library calls have bee added to convert pkg_config results (-L<libdir> -lavcodec -l...) to absolute paths (-l<abs-path-to-libavcodec> -l...) to avoid conflicts with system locations.

@nicolaerosia

nicolaerosia commented Jan 31, 2019 •

Copy link
Copy Markdown
Contributor Author

@mshabunin that's not the issue, find_library fails to find the library because CMAKE_SYSROOT is set.
https://cmake.org/cmake/help/latest/command/find_library.html

The CMAKE_SYSROOT variable can also be used to specify exactly one directory to use as a prefix. Setting CMAKE_SYSROOT also has other effects. See the documentation for that variable for more.

So the find path ${FFMPEG_lib${lib}_LIBDIR} is being prefixed with CMAKE_SYSROOT as ${CMAKE_SYSROOT}/${FFMPEG_lib${lib}_LIBDIR}

Later Edit: with your change:

-- WARNING: Can't build ffmpeg test code
--   Video I/O:
--     FFMPEG:                      NO
--       avcodec:                   YES (58.18.100)
--       avformat:                  YES (58.12.100)
--       avutil:                    YES (56.14.100)
--       swscale:                   YES (5.1.100)
--       avresample:                YES (4.0.0)

I added some debug messages to demostrate that find_library invocation is right, and it still doesn't find the libs:

    	message(STATUS "FFMPEG_LIBRARY_DIRS: ${FFMPEG_LIBRARY_DIRS}")
    	message(STATUS "FFMPEG_lib${lib}_LIBDIR: ${FFMPEG_lib${lib}_LIBDIR}")
-- FFMPEG_LIBRARY_DIRS: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavformat_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavcodec_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libz_LIBDIR: 
-- FFMPEG_libswscale_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavutil_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libm_LIBDIR: 
-- FFMPEG_libavresample_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib
-- FFMPEG_libavutil_LIBDIR: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib

-- FFMPEG_LIBRARIES: avformat;avcodec;z;swscale;avutil;m;avresample;avutil;m

With my change:

-- FFMPEG_LIBRARIES: /home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib/libavformat.a;/home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib/libavcodec.a;z;/home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib/libswscale.a;/home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib/libavutil.a;m;/home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib/libavresample.a;/home/user/android-install/ffmpeg-arm64-v8a-n4.0.3/lib/libavutil.a;m

@mshabunin

mshabunin commented Jan 31, 2019 •

Copy link
Copy Markdown
Contributor

@nicolaerosia , we use NO_DEFAULT_PATH to search only in provided PATHS, then as documentation says:

By default at first the directories listed in CMAKE_FIND_ROOT_PATH are searched, then the CMAKE_SYSROOT directory is searched, and then the non-rooted directories will be searched.

So, non-rooted directories should be searched too.

Could you please provide a list of found/non-found libraries? Can CMAKE_PREFIX_PATH affect the search locations? Do you clean cmake cache after modifying build parameters?

@nicolaerosia

Copy link
Copy Markdown
Contributor Author

@mshabunin I always rm -rf my build dir, no worries.
You have a good point here, and I went to the toolchain definition to check what's happening.
Here's the revelant code snippet of android-ndk-r18b/build/cmake/android.toolchain.cmake

# For example, they may want to prevent the NDK's libz from being picked up so
# they can use their own.
# https://github.com/android-ndk/ndk/issues/517
if(NOT CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
endif()

if(NOT CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
endif()

if(NOT CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()

if(NOT CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
  set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
endif()

It will set CMAKE_FIND_ROOT_PATH_MODE_LIBRARY to ONLY.
So I went ahead and dropped my change to detect_ffmpeg.cmake, and modified my build script to add: -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=NEVER \ and now the libraries are found.

Should I update my PR to drop the change in detect_ffmpeg.cmake ?

@alalek

alalek commented Jan 31, 2019

Copy link
Copy Markdown
Member

@nicolaerosia Did you try to add ffmpeg path into CMake's "FIND_ROOT": cmake -DCMAKE_FIND_ROOT_PATH=$HOME/android-install ...?

@nicolaerosia

Copy link
Copy Markdown
Contributor Author

@alalek no, I just tried it now and it works
My final script:

#!/bin/bash

if [ $# -ne 1 ]; then
	echo "usage: ./build.sh <ARCH>"
	echo "ARCH: arm64-v8a x86_64"
	exit 1
fi;

set -e
set -o xtrace

ARCH=$1
ANDROID_NATIVE_API_LEVEL=26

case $ARCH in
	"arm64-v8a")
		echo "ARCH: $ARCH"
		ANDROID_PLATFORM_ID=3
	;;

	"x86_64")
		echo "ARCH: $ARCH"
		ANDROID_PLATFORM_ID=5
	;;

	*)
		echo "ARCH: $ARCH unsupported"
		exit 1
	;;
esac

FFMPEG_INSTALL_PREFIX=$HOME/android-install/ffmpeg-$ARCH-n4.0.3

# OBSOLETE NOT ANYMORE Android tools 25.2.5 is a must, check these changes:
# https://github.com/xuchong/android-opencv-ffmpeg/issues/2
# https://gist.github.com/xuchong/31426c624bffd8802ecf17a830bfef53
#export ANDROID_SDK=$HOME/Android/tools-r25.2.5
#export ANDROID_SDK=/home/user/Android/android-studio-ide-181.5056338
#export ANDROID_HOME=/home/user/Android/android-studio-ide-181.5056338
export ANDROID_SDK=/home/user/Android/android-studio-ide-182.5199772/Sdk
export ANDROID_HOME=/home/user/Android/android-studio-ide-182.5199772/Sdk
#export ANDROID_NDK=${HOME}/Android/android-ndk-r16b
export ANDROID_NDK=${HOME}/Android/android-ndk-r18b
SYSROOT=${ANDROID_NDK}/sysroot

# OBSOLETE NOT ANYMORE:
# linking with ffmpeg is broken - dunno why need to add lib dir
# go to ffmpeg, and copy *.a to /home/user/android-src/ffmpeg/android/arm64-v8a/lib
# cp /home/user/android-src/ffmpeg/android/arm64-v8a/lib/*.a /home/user/android-src/ffmpeg/android/arm64-v8a/lib
# Fixed now: you don't need to set PKG_CONFIG_SYSROOT_DIR
#export PKG_CONFIG_SYSROOT_DIR=$SYSROOT
export PKG_CONFIG_LIBDIR=$SYSROOT

# BUILD_TESTS && INSTALL_TESTS fail
# BUILD_ANDROID_EXAMPLES && INSTALL_ANDROID_EXAMPLES fail
OPENCV_SRC_DIR=$HOME/android-src/opencv
OPENCV_VERSION="4.0.0"

OPENCV_BUILD_DIR=$HOME/android-build/opencv-${OPENCV_VERSION}-api${ANDROID_NATIVE_API_LEVEL}-$ARCH
OPENCV_INSTALL_DIR=$HOME/android-install/opencv-${OPENCV_VERSION}-api${ANDROID_NATIVE_API_LEVEL}-$ARCH
OPENCV_CONTRIB_SRC_DIR=$HOME/android-src/opencv_contrib

# for x86_64 add IPP
# -DWITH_IPP=OFF \

rm -rf ${OPENCV_BUILD_DIR}

mkdir -p ${OPENCV_BUILD_DIR}
cd ${OPENCV_BUILD_DIR}
cmake \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_ABI=${ARCH} \
-DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL} \
-DANDROID_PLATFORM_ID=${ANDROID_PLATFORM_ID} \
-DANDROID_STL=c++_shared \
-DCMAKE_PREFIX_PATH=${FFMPEG_INSTALL_PREFIX} \
-DCMAKE_FIND_ROOT_PATH=${FFMPEG_INSTALL_PREFIX} \
-DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_DIR} \
-DPKG_CONFIG_EXECUTABLE=/usr/bin/pkg-config \
-GNinja \
-DOPENCV_EXTRA_MODULES_PATH=${OPENCV_CONTRIB_SRC_DIR}/modules \
-DBUILD_TESTS=OFF \
-DINSTALL_TESTS=OFF \
-DBUILD_ANDROID_EXAMPLES=OFF \
-DINSTALL_ANDROID_EXAMPLES=OFF \
-DBUILD_PERF_TESTS=OFF \
-DBUILD_EXAMPLES=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_DOCS=OFF \
-DWITH_TBB=ON \
-DWITH_OPENCL=OFF \
-DWITH_FFMPEG=ON \
-DOPENCV_FFMPEG_SKIP_DOWNLOAD=TRUE \
${OPENCV_SRC_DIR}

ninja-build install

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase patch (CMakeLists.txt changes only) onto 3.4 branch. We will merge changes from 3.4 into master regularly (weekly/bi-weekly).

Note: no needs to re-open PR, apply changes "inplace".

Comment thread CMakeLists.txt
OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" ON
VISIBLE_IF NOT ANDROID AND NOT IOS AND NOT WINRT
VISIBLE_IF NOT IOS AND NOT WINRT
VERIFY HAVE_FFMPEG)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ON => NOT ANDROID (disabled by default on Android).
Use WITH_FFMPEG=ON in build scripts.

OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" NOT ANDROID
  VISIBLE_IF NOT IOS AND NOT WINRT
  VERIFY HAVE_FFMPEG)

details: #13726

In order to help CMake and PkgConfig find FFmpeg you need to:
1) export `PKG_CONFIG_LIBDIR=${SYSROOT}`
where `SYSROOT=${ANDROID_NDK}/sysroot`
2) Add `-DCMAKE_PREFIX_PATH=${FFMPEG_INSTALL_PREFIX}`
3) Append `-DCMAKE_FIND_ROOT_PATH=${FFMPEG_INSTALL_PREFIX}`
@opencv-pushbot
opencv-pushbot merged commit 3aaf8df into opencv:3.4 Feb 14, 2019
@nicolaerosia

Copy link
Copy Markdown
Contributor Author

@alalek sorry for the delay, I was away for a while

@nicolaerosia
nicolaerosia deleted the android-ffmpeg branch February 15, 2019 08:49
@alalek alalek mentioned this pull request Feb 19, 2019
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
details: opencv#13726

In order to help CMake and PkgConfig find FFmpeg you need to:
1) export `PKG_CONFIG_LIBDIR=${SYSROOT}`
where `SYSROOT=${ANDROID_NDK}/sysroot`
2) Add `-DCMAKE_PREFIX_PATH=${FFMPEG_INSTALL_PREFIX}`
3) Append `-DCMAKE_FIND_ROOT_PATH=${FFMPEG_INSTALL_PREFIX}`
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants