Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build FFmpeg for Android without GCC #562

Closed
anonym24 opened this issue May 19, 2018 · 11 comments
Closed

Build FFmpeg for Android without GCC #562

anonym24 opened this issue May 19, 2018 · 11 comments

Comments

@anonym24
Copy link

anonym24 commented May 19, 2018

I edited https://github.com/bytedeco/javacpp-presets/blob/master/ffmpeg/cppbuild.sh script a little bit to build FFmpeg without x264/265 and other extra libs:

#!/bin/bash
# This file is meant to be included by the parent cppbuild.sh script
if [[ -z "$PLATFORM" ]]; then
    pushd ..
    bash cppbuild.sh "$@" ffmpeg
    popd
    exit
fi

DISABLE="--disable-static --disable-iconv --disable-opencl --disable-sdl2 --disable-bzlib --disable-lzma --disable-linux-perf"
ENABLE="--enable-shared --enable-version3 --enable-runtime-cpudetect"

FFMPEG_VERSION=4.0

download http://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.bz2 ffmpeg-$FFMPEG_VERSION.tar.bz2

mkdir -p $PLATFORM
cd $PLATFORM
INSTALL_PATH=`pwd`
echo "Decompressing archives..."
tar --totals -xjf ../ffmpeg-$FFMPEG_VERSION.tar.bz2

patch -Np1 -d ffmpeg-$FFMPEG_VERSION < ../../ffmpeg.patch

cd ffmpeg-$FFMPEG_VERSION

case $PLATFORM in
    android-arm)
        export AR="$ANDROID_BIN-ar"
        export CPP="$ANDROID_BIN-cpp"
        export CC="$ANDROID_BIN-gcc"
        export CXX="$ANDROID_BIN-g++"
        export RANLIB="$ANDROID_BIN-ranlib"
        export STRIP="$ANDROID_BIN-strip"
        export CPPFLAGS="$ANDROID_FLAGS -D_FILE_OFFSET_BITS=32"
        export CFLAGS="$ANDROID_FLAGS -D_FILE_OFFSET_BITS=32"
        export CXXFLAGS="$ANDROID_FLAGS -D_FILE_OFFSET_BITS=32"
        export LDFLAGS="-Wl,--no-undefined -Wl,--fix-cortex-a8 -z text"
        export LIBS="-lgcc -ldl -lz -lm -lc"
        patch -Np1 < ../../../ffmpeg-android.patch
        sed -i="" 's/_FILE_OFFSET_BITS=64/_FILE_OFFSET_BITS=32/g' configure
        ./configure --prefix=.. $DISABLE $ENABLE --enable-pthreads --enable-cross-compile --cross-prefix="$ANDROID_BIN-" --ranlib="$ANDROID_BIN-ranlib" --sysroot="$ANDROID_ROOT" --target-os=linux --arch=arm --extra-cflags="-I../include/ $CFLAGS" --extra-ldflags="-L../lib/ -L$ANDROID_CPP/libs/armeabi/ $LDFLAGS" --extra-libs="-lgnustl_static $LIBS" --disable-symver --disable-programs
        make -j $MAKEJ
        make install
        ;;

    android-arm64)
        export AR="$ANDROID_BIN-ar"
        export CPP="$ANDROID_BIN-cpp"
        export CC="$ANDROID_BIN-gcc"
        export CXX="$ANDROID_BIN-g++"
        export RANLIB="$ANDROID_BIN-ranlib"
        export STRIP="$ANDROID_BIN-strip"
        export CPPFLAGS="$ANDROID_FLAGS"
        export CFLAGS="$ANDROID_FLAGS"
        export CXXFLAGS="$ANDROID_FLAGS"
        export LDFLAGS="-Wl,--no-undefined -z text"
        export LIBS="-lgcc -ldl -lz -lm -lc"
        patch -Np1 < ../../../ffmpeg-android.patch
        ./configure --prefix=.. $DISABLE $ENABLE --enable-pthreads --enable-cross-compile --cross-prefix="$ANDROID_BIN-" --ranlib="$ANDROID_BIN-ranlib" --sysroot="$ANDROID_ROOT" --target-os=linux --arch=aarch64 --extra-cflags="-I../include/ $CFLAGS" --extra-ldflags="-L../lib/ -L$ANDROID_CPP/libs/arm64-v8a/ $LDFLAGS" --extra-libs="-lgnustl_static $LIBS" --disable-symver --disable-programs
        make -j $MAKEJ
        make install
        ;;
    *)
        echo "Error: Platform \"$PLATFORM\" is not supported"
        ;;
esac

cd ../..

build works fine for arm64

but for just arm (armv7a) I get an error:

user@user-PC:~/Android/JavaCPP/javacpp-presets$ ANDROID_NDK=$NDK bash cppbuild.sh -platform android-arm install ffmpeg
Detected platform "linux-x86_64"
Building for platform "android-arm"
Installing "ffmpeg"
~/Android/JavaCPP/javacpp-presets/ffmpeg/cppbuild ~/Android/JavaCPP/javacpp-presets
Decompressing archives...
Total bytes read: 68341760 (66MiB, 32MiB/s)
patching file configure
patching file ffbuild/library.mak
/home/user/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc is unable to create an executable file.
C compiler test failed.

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.

@anonym24
Copy link
Author

anonym24 commented May 19, 2018

x86 and x86_64 also work fine

 android-x86)
#        ANDROID_ROOT=${ANDROID_ROOT//14/21}
#        ANDROID_FLAGS=${ANDROID_FLAGS//14/21}
        export AR="$ANDROID_BIN-ar"
        export CPP="$ANDROID_BIN-cpp"
        export CC="$ANDROID_BIN-gcc"
        export CXX="$ANDROID_BIN-g++"
        export RANLIB="$ANDROID_BIN-ranlib"
        export STRIP="$ANDROID_BIN-strip"
        export CPPFLAGS="$ANDROID_FLAGS -D_FILE_OFFSET_BITS=32"
        export CFLAGS="$ANDROID_FLAGS -D_FILE_OFFSET_BITS=32"
        export CXXFLAGS="$ANDROID_FLAGS -D_FILE_OFFSET_BITS=32"
        export LDFLAGS="-Wl,--no-undefined -z text"
        export LIBS="-lgcc -ldl -lz -lm -lc"
        patch -Np1 < ../../../ffmpeg-android.patch
        sed -i="" 's/_FILE_OFFSET_BITS=64/_FILE_OFFSET_BITS=32/g' configure
        ./configure --prefix=.. $DISABLE $ENABLE --enable-pthreads --enable-cross-compile --cross-prefix="$ANDROID_BIN-" --ranlib="$ANDROID_BIN-ranlib" --sysroot="$ANDROID_ROOT" --target-os=linux --arch=atom --extra-cflags="-I../include/ $CFLAGS" --extra-ldflags="-L../lib/ -L$ANDROID_CPP/libs/x86/ $LDFLAGS" --extra-libs="-lgnustl_static $LIBS" --disable-symver --disable-programs
        make -j $MAKEJ
        make install
        ;;

     android-x86_64)
        export AR="$ANDROID_BIN-ar"
        export CPP="$ANDROID_BIN-cpp"
        export CC="$ANDROID_BIN-gcc"
        export CXX="$ANDROID_BIN-g++"
        export RANLIB="$ANDROID_BIN-ranlib"
        export STRIP="$ANDROID_BIN-strip"
        export CPPFLAGS="$ANDROID_FLAGS"
        export CFLAGS="$ANDROID_FLAGS"
        export CXXFLAGS="$ANDROID_FLAGS"
        export LDFLAGS="-Wl,--no-undefined -z text"
        export LIBS="-lgcc -ldl -lz -lm -lc"
        patch -Np1 < ../../../ffmpeg-android.patch
        ./configure --prefix=.. $DISABLE $ENABLE --enable-pthreads --enable-cross-compile --cross-prefix="$ANDROID_BIN-" --ranlib="$ANDROID_BIN-ranlib" --sysroot="$ANDROID_ROOT" --target-os=linux --arch=atom --extra-cflags="-I../include/ $CFLAGS" --extra-ldflags="-L../lib/ -L$ANDROID_CPP/libs/x86_64/ $LDFLAGS" --extra-libs="-lgnustl_static $LIBS" --disable-symver --disable-programs
        make -j $MAKEJ
        make install
        ;;
    *)

only arm fails

p.s. last lines from config.log:

/home/user/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -Wl,--no-undefined -Wl,--fix-cortex-a8 -z text -L../lib/ -L/home/user/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9//libs/armeabi/ -Wl,--no-undefined -Wl,--fix-cortex-a8 -z text --sysroot=/home/user/Android/Sdk/ndk-bundle/platforms/android-19/arch-arm/ -march=armv7-a -o /tmp/ffconf.pWXlyIs5/test /tmp/ffconf.pWXlyIs5/test.o -lgnustl_static -lgcc -ldl -lz -lm -lc
/home/user/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lgnustl_static
collect2: error: ld returned 1 exit status
C compiler test failed.

it says error: cannot find -lgnustl_static

My OS: Linux 64 (Ubuntu 18.0), NDK: v17

@anonym24
Copy link
Author

solved I have to use NDK r15c instead of r17 to build for arm, though r17 works ok for other archs

@saudet
Copy link
Member

saudet commented May 19, 2018

Well, let's keep that opened since Google will apparently be removing GCC from in NDK r18:

  • GCC is no longer supported. It will be removed in NDK r18.

https://github.com/android-ndk/ndk/wiki/Changelog-r17

So we'll need to figure out something for that eventually.
Hopefully someone somewhere will keep GCC alive for Android...

@saudet saudet reopened this May 19, 2018
@saudet saudet changed the title Build FFmpeg for Android without x264 and other libs Build FFmpeg for Android without GCC May 19, 2018
@zkrige
Copy link

zkrige commented Sep 5, 2018

Is there a solution for this on NDK r17? I'm getting the same issue with a javacpp project im working on.

the 64bit armv8 builds fine, but the armv-7a fails with this error

@anonym24
Copy link
Author

anonym24 commented Oct 6, 2018

@zkrige these building scripts are supposed to work with NDK 15, no one at that time tested/optimized them for newer version of NDK, there is no need to downvote my comments, have some respect

@saudet
Copy link
Member

saudet commented Dec 14, 2018

It's done! Now all presets for Android get built with Clang from NDK r18b: http://bytedeco.org/builds/

@saudet
Copy link
Member

saudet commented Dec 16, 2018

There is no Gradle build for Android, if that's what you mean. It wouldn't be portable to any other platform, so I'm not going to spend time on this personally, but as usual contributions are welcome! Please send a pull request.

@anonym24
Copy link
Author

anonym24 commented Dec 16, 2018

@saudet I see you have changed minimum API version from 14 to 21 in the script (for 32 cpu architectures) 9f279b6#diff-1e00f893b0beb998acb8357f4f0163a1R35
why so? or it doesn't matter and is it still going to work on 19 API for example? (19 - KitKat is still quite popular)

@saudet
Copy link
Member

saudet commented Dec 17, 2018

It might work, yes, could you give it a try? We need to cut somewhere at some point, so unless there is a good reason to not do it now, might as well be now. It only creates more maintenance for me to adjust the builds all the time, but again, if you have time to spend on this, any and all contributions are welcome!

@anonym24
Copy link
Author

anonym24 commented Dec 19, 2018

Tested with script from https://github.com/IljaKosynkin/FFmpeg-Development-Kit to Build FFmpeg 4.0.3

I can't get it work on my KitKat device (19 API, armeabi-v7a) - failed: dlopen failed: cannot locate symbol "mmap64" referenced by "libavutil.so"... when I try to use it in my Android app

on Marshmallow (23 API, 'arm64-v8a') device it works ok

The tiny script to build FFmpeg 4.0.3 with Clang:

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PARENT="$(dirname "${DIR}")"
NDK="$(dirname "${PARENT}")"
SYSROOT="$NDK/sysroot"
HOST="linux-x86_64"
LLVM_TOOLCHAIN="$NDK/toolchains/llvm/prebuilt/$HOST/bin"

CFLAGS="-O3 -fPIC"
LDFLAGS="-lc"

build () {
    ARCH=$1
    LEVEL=$2
    CONFIGURATION="--enable-cross-compile --disable-static --disable-programs --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --enable-shared --disable-protocols --enable-protocol=file --enable-pic --disable-hwaccels --disable-parsers --disable-bsfs --disable-indevs --disable-outdevs --disable-devices --disable-filters --disable-network $3"
    LIB_FOLDER="lib"

    case $ARCH in
        "armeabi-v7a")
            TARGET="arm-linux-androideabi"
            CC_FLAGS="-target thumbv7-none-linux-androideabi -mfpu=vfpv3-d16 -mfloat-abi=soft"
            LDFLAGS="--fix-cortex-a8 $LDFLAGS"
            PLATFORM_ARCH="arm"
            TOOLCHAIN_FOLDER=$TARGET
        ;;
        "arm64-v8a")
            TARGET="aarch64-linux-android"
            CC_FLAGS="-target aarch64-none-linux-android -mfpu=neon -mfloat-abi=soft"
            PLATFORM_ARCH="arm64"
            TOOLCHAIN_FOLDER=$TARGET
        ;;
        "x86")
            TARGET="i686-linux-android"
            CC_FLAGS="-target i686-none-linux-androideabi -mtune=intel -mssse3 -mfpmath=sse -m32"
            PLATFORM_ARCH="x86"
            TOOLCHAIN_FOLDER=$PLATFORM_ARCH
        ;;
        "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"
            TOOLCHAIN_FOLDER=$PLATFORM_ARCH
        ;;
    esac

    TOOLCHAIN=$NDK/toolchains/$TOOLCHAIN_FOLDER-4.9/prebuilt/$HOST/bin

    CC=$LLVM_TOOLCHAIN/clang
    CXX=$LLVM_TOOLCHAIN/clang++
    AS=$CC

    AR=$TOOLCHAIN/$TARGET-ar
    LD=$TOOLCHAIN/$TARGET-ld
    STRIP=$TOOLCHAIN/$TARGET-strip

    PREFIX="android/$ARCH"

    ./configure --prefix=$PREFIX  \
                $CONFIGURATION \
                --ar=$AR --strip=$STRIP --ld=$LD --cc=$CC --cxx=$CXX --as=$AS \
                --target-os=android \
                --extra-cflags="$CC_FLAGS -I$SYSROOT/usr/include/$TARGET $CFLAGS" \
                --extra-ldflags="-L$NDK/toolchains/$TOOLCHAIN_FOLDER-4.9/prebuilt/$HOST/lib/gcc/$TARGET/4.9.x -L$NDK/platforms/android-$LEVEL/arch-$PLATFORM_ARCH/usr/$LIB_FOLDER $LDFLAGS" \
                --sysroot=$SYSROOT --extra-libs=-lgcc

    $NDK/prebuilt/$HOST/bin/make clean
    $NDK/prebuilt/$HOST/bin/make -j4
    $NDK/prebuilt/$HOST/bin/make install
}

build "armeabi-v7a" "19" "--disable-asm"
build "arm64-v8a" "21" "--disable-asm --disable-pthreads"
build "x86" "19"
build "x86_64" "21" "--disable-asm"

@saudet
Copy link
Member

saudet commented Jan 13, 2019

Well, version 1.4.4 has been released and it requires Android 5.0. I'm assuming use of KitKat has dropped dramatically over the holidays, so I'm not concerned about that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants