Building FFMPEG for Android on x86

FFMPEG is a popular cross platform open source multimedia framework used in open source media applications such as Handbrake.  The default builds for FFMPEG are Linux, Windows, and OSX.  This blog shows a method for building the FFMPEG shared libraries for Android x86.  

The setup used for this build was a VirtualBox Virtual Machine running Ubuntu Linux 12.04 64-bit.

To build FFMPEG for Android x86:
1.  Download the latest Android NDK from http://developer.android.com/tools/sdk/ndk/index.html

2.  Download the latest FFMPEG source code from http://www.ffmpeg.org/download.html

3.  Open a Terminal and cd into the FFMPEG directory

4.  Configure ANDROID_NDK environment variable
    ANDROID_NDK=<your ndk path>
    PATH=$PATH:$ANDROID_NDK

5.  Setup the toolchain
    DEST=`pwd`
    PREFIX=$DEST/build/android/x86
    TOOLCHAIN=/tmp/vplayer
    $ANDROID_NDK/build/tools/make-standalone-toolchain.sh --toolchain=x86-4.8 --arch=x86 --system=linux-x86_64 --platform=android-14 --install-dir=/tmp/vplayer

    export PATH=$TOOLCHAIN/bin:$PATH
    export CC="ccache i686-linux-android-gcc-4.8"
    export LD=i686-linux-android-ld
    export AR=i686-linux-android-ar

6.  Configure FFMPEG
./configure --target-os=linux --arch=x86 --cpu=i686 --cross-prefix=i686-linux-android- --enable-cross-compile --enable-shared --disable-static --disable-symver --disable-doc --disable-ffplay --disable-ffmpeg --disable-ffprobe --disable-ffserver --disable-avdevice --disable-postproc --disable-encoders --disable-muxers --disable-devices --disable-demuxer=sbg --disable-demuxer=dts --disable-parser=dca --disable-decoder=dca --disable-decoder=svq3 --enable-network --enable-version3 --disable-amd3dnow --disable-amd3dnowext --enable-asm --enable-yasm --enable-pic --prefix=$PREFIX --extra-cflags='-std=c99 -O3 -Wall -fpic -pipe   -DANDROID -DNDEBUG  -march=atom -msse3 -ffast-math -mfpmath=sse' --extra-ldflags='-lm -lz -Wl,--no-undefined -Wl,-z,noexecstack'


7.  Build FFMPEG for Android x86.  The include and shared library files will be in the build/android/x86 folder
make clean
make
make install

8.  Generate libffmpeg.so shared library file in the build/android/x86 folder

rm libavcodec/log2_tab.o libavformat/log2_tab.o libswresample/log2_tab.o

$CC -o $PREFIX/libffmpeg.so -shared libavutil/*.o libavutil/x86/*.o libavcodec/*.o libavcodec/x86/*.o libavformat/*.o libavfilter/*.o libavfilter/x86/*.o libswresample/*.o libswresample/x86/*.o libswscale/*.o libswscale/x86/*.o compat/*.o

 

To learn more about FFMPEG, please visit the link below:
http://www.ffmpeg.org/about.html    

To learn more about VirtualBox, please visit the link below:
https://www.virtualbox.org/

To learn more about Ubuntu Linux, please visit the link below:
http://www.ubuntu.com/download/desktop

To learn more about Handbrake, please visit the link below:
http://handbrake.fr/

 

++This sample source code is released under the Open Source Initiative OSI - The BSD License.

For more complete information about compiler optimizations, see our Optimization Notice.

8 comments

Top
vijayadhas D.'s picture

I am getting this error:

Could not find toolchain: /home/vijay/Android/Sdk/ndk-bundle/toolchains/x86-4.8/prebuilt/linux-x86_64 Please use --toolchain=<name> with the name of a toolchain supported by the source NDK

while executing this line

$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --toolchain=x86-4.8 --arch=x86 --platform=android-14 --install-dir=/tmp/vplayer

 

Please help me to resolve this issue. 

vijayadhas D.'s picture

Hi. Thanks for the tutorial. I am getting error in 5.  Setup the toolchain

While executing this line :-

$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --toolchain=x86-4.8 --arch=x86 --system=linux-x86_64 --platform=android-14 --install-dir=/tmp/vplayer

I am getting this error:-

HOST_OS=linux
HOST_EXE=
HOST_ARCH=x86_64
HOST_TAG=linux-x86_64
HOST_NUM_CPUS=1
BUILD_NUM_CPUS=2
ERROR: Unknown option '--system'. See --help for usage.

 

Please help me to solve this issue.

 

Thanks and Regards

C. Vijayadhas

cine.io developers's picture

ffmpeg uses autoconf as its build system. As of the time of this writing, no one has yet put together a Gradle build package for ffmpeg on Android. As far as I can tell, Gradle doesn't yet support autoconf, which makes it pretty difficult to do so

That said, after trying several of the tutorials that were out there, as well as some of the pre-built packages, we decided to roll our own Android build script wrapper for ffmpeg that includes support forOpenSSL and librtmp.

You can find it here: https://github.com/cine-io/android-ffmpeg-with-rtmp

Prerequisite: The Android NDK must be installed

Instructions:

$ git clone git@github.com:cine-io/android-ffmpeg-with-rtmp.git
$ cd android-ffmpeg-with-rtmp
$ ./build.sh

When the script is finished, the required shared libraries and ffmpeg executable will be put into the build/dist directory. You can include them in your project just like any other native binary.

Harrisson M.'s picture

Hi,  I have been working on ffmpeg for one of my projects. I am trying to utilize the ffmpeg static or shared libraries for my Android  x86 project. I have followed the steps provided on this page. When I try to build my project, I encountered three errors. I have been trying to rectify them from the past one week, I have tried all possible methods with no results. 

jni/jniplayer/jniplayer.cpp:1244: error: undefined reference to 'sws_freeContext'
jni/jniplayer/jniplayer.cpp:1384: error: undefined reference to 'sws_scale'
jni/jniplayer/jniplayer.cpp:1371: error: undefined reference to 'sws_getContext'

I suppose that the issue resides with the enabling of the macro for x86 platform.

#if ARCH_ARM
#define USE_SWSCALE 0
#else
#define USE_SWSCALE 1
#endif

Only when USE_SWSCALE is enabled, functions such as sws_getcontext get enabled in ffmpeg.

Kindly, please offer me some help with this issue. Thanks in advance for any answers.

MEA Mobile's picture

Hey Michael,

Any thoughts on " error: can't find a register in class 'GENERAL_REGS' while reloading 'asm'"

I'm trying to compile ffmpeg on a Mac. Some would you say its a bug in GCC, but what I'm asking here is if you think there's a workaround to this.

 

Pritam B.'s picture

Hi,

Thanks for the prompt reply, however, on executing the step 8, I am getting the following error:

/home/rajendra/Android/android-ndk-r9b/toolchains/x86-4.8/prebuilt/linux-x86/bin/../lib/gcc/i686-linux-android/4.8/../../../../i686-linux-android/bin/ld: error: cannot open crtbegin_so.o: No such file or directory
/home/rajendra/Android/android-ndk-r9b/toolchains/x86-4.8/prebuilt/linux-x86/bin/../lib/gcc/i686-linux-android/4.8/../../../../i686-linux-android/bin/ld: error: cannot open crtend_so.o: No such file or directory
/home/rajendra/Android/android-ndk-r9b/toolchains/x86-4.8/prebuilt/linux-x86/bin/../lib/gcc/i686-linux-android/4.8/../../../../i686-linux-android/bin/ld: error: cannot find -lc
/home/rajendra/Android/android-ndk-r9b/toolchains/x86-4.8/prebuilt/linux-x86/bin/../lib/gcc/i686-linux-android/4.8/../../../../i686-linux-android/bin/ld: error: cannot find -ldl
collect2: error: ld returned 1 exit status

Also could you please specify what should be the value of $LDFLAGS and $EXTRA_LDFLAGS?

Thanks in advance.

Pritam B

Rylee, Michael J (Intel)'s picture

Hi Pritam.  Thanks for the message.  I just added a step 8 for building the libffmpeg.so file.

Pritam B.'s picture

Hi Michael,

Thank you for the excellent tutorial. However, even after executing all the above steps, libffmpeg.so is not getting generated in the build folder. Could you please advice me regarding this problem? I am new to ndk and ffmpeg. My system info is as follows:

OS: Ubuntu 12.04.03 LTS 32-bit

Android NDK revision: 9b

I am trying to build for: --toolchain=x86-4.8 --arch=x86 --system=linux-x86 --platform=android-19

Also, if that file is not required, then could you please direct me to any sample android projects that can be run using the above build of FFMPEG? Any help will be greatly appreciated.

Thanks...

Pritam B

Add a Comment

Have a technical question? Visit our forums. Have site or software product issues? Contact support.