Compiling FFmpeg 2.3 with Android NDK r10

2019-04-21 21:35发布

问题:

I want to compile FFMpeg 2.3 with Android NDK r10 by following this tutorial: http://www.roman10.net/how-to-build-ffmpeg-for-android/

After getting several errors i modified the build script like this:

  #!/bin/bash
    ######################################################
# Usage:
# put this script in top of FFmpeg source tree
# ./build_android
# It generates binary for following architectures:
# ARMv6 
# ARMv6+VFP 
# ARMv7+VFPv3-d16 (Tegra2) 
# ARMv7+Neon (Cortex-A8)
# Customizing:
# 1. Feel free to change ./configure parameters for more features
# 2. To adapt other ARM variants
# set $CPU and $OPTIMIZE_CFLAGS 
# call build_one
######################################################
NDK=/usr/local/src/android-ndk-r10
PLATFORM=$NDK/platforms/android-8/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64
function build_one
{
./configure --target-os=linux \
    --prefix=$PREFIX \
    --enable-cross-compile \
    --extra-libs="-lgcc" \
    --arch=arm \
    --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
    --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
    --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
    --sysroot=$PLATFORM \
    --extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS " \
    --disable-shared \
    --enable-static \
    --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
    --disable-everything \
    --enable-demuxer=mov \
    --enable-demuxer=h264 \
    --disable-ffplay \
    --enable-protocol=file \
    --enable-avformat \
    --enable-avcodec \
    --enable-decoder=rawvideo \
    --enable-decoder=mjpeg \
    --enable-decoder=h263 \
    --enable-decoder=h265 \
    --enable-decoder=mpeg4 \
    --enable-decoder=h264 \
    --enable-parser=h264 \
    --enable-parser=h265
    --enabled-network \
    --enable-zlib \
    --disable-avfilter \
    --disable-avdevice \
    $ADDITIONAL_CONFIGURE_FLAG

make clean
make  -j4 install
$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -nostdlib,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.8/libgcc.a
}

#arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7vfpv3
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
build_one

#arm v7vfp
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
#PREFIX=./android/$CPU-vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7n
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=--enable-neon
#build_one

#arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

But unfortunatly I get the following error:

    INSTALL ffmpeg
INSTALL ffprobe
INSTALL ffserver
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(eventfd.o): multiple definition of 'eventfd'
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(eventfd.o): previous definition here
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): multiple definition of '__dorand48'
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): previous definition here
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): multiple definition of '__rand48_mult'
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): previous definition here
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): multiple definition of '__rand48_add'
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): previous definition here
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): multiple definition of '__rand48_seed'
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(_rand48.o): previous definition here
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libm.a(s_scalbn.o): multiple definition of 'ldexp'
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: /usr/local/src/android-ndk-r10/platforms/android-8/arch-arm//usr/lib/libc.a(ldexp.o): previous definition here
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: cannot find -lz
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: cannot find -ldl
/usr/local/src/android-ndk-r10/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld: error: cannot find -llog
libavformat/http.c:384: error: undefined reference to 'inflateEnd'
libavformat/http.c:385: error: undefined reference to 'inflateInit2_'
libavformat/http.c:390: error: undefined reference to 'zlibCompileFlags'
libavformat/http.c:1029: error: undefined reference to 'inflateEnd'
libavformat/http.c:867: error: undefined reference to 'inflate'
libavformat/id3v2.c:840: error: undefined reference to 'uncompress'
libavformat/mov.c:2934: error: undefined reference to 'uncompress'
bionic/libc/bionic/drand48.c:24: error: undefined reference to '_rand48_seed'
bionic/libc/bionic/erand48.c:21: error: undefined reference to '_dorand48'

Any help would be appreciated. Thank you!

回答1:

In your build, ld looks for static system libs (libc.a, libz.a, ...) while it should be looking for the shared versions of these libraries.

There is a -Bshared flag that can help, but to have full control of what is happening, I'd suggest the following linking step:

$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib \
-soname libffmpeg.so -nostdlib,noexecstack -Bsymbolic \
--whole-archive --no-undefined \
-o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a \
libavutil/libavutil.a libswscale/libswscale.a \
$PLATFORM/usr/lib/libc.so $PLATFORM/usr/lib/libm.so \
$PLATFORM/usr/lib/libz.so \
$PLATFORM/usr/lib/libdl.so $PLATFORM/usr/lib/liblog.so \
--dynamic-linker=/system/bin/linker \
$PREBUILT/lib/gcc/arm-linux-androideabi/4.8/libgcc.a

You seem to not need the executables, so you can add to your ./configure call

--disable-ffmpeg
--disable-ffprobe
--disable-ffserver

and remove the following lines:

--extra-ldflags
--extra-libs


回答2:

This worked for me:

First, make a standalone toolchain:

android-ndk-r10/build/tools/make-standalone-toolchain.sh --platform=android-{your target here} --install-dir={pick a toolchain install directory}

Then configure ffmpeg using the standalone toolchain you made:

./configure                                                                  \
--prefix={where you want to install ffmpeg to}                               \
--cross-prefix={standalone toolchain install dir}/bin/arm-linux-androideabi- \
--target-os=linux                                                            \
--arch=arm                                                                   \
--enable-cross-compile                                                       \
--sysroot={standaline toolchain install dir}/sysroot                         \
--disable-yasm

I think you need to disable yasm since it doesn't work for ARM.

From there you can just "make install" and it should work.