Introduction
Android is becoming a popular mobile platform. With the help of the NDK component in the Android Developer Tools, open source libraries that are written in native programming languages such as C and C++ are theoretically available on the Android platform. In this article, we’ll show you how to compile some well-known open source libraries for the X86 Android platform.
FFmpeg Compiling
FFmpeg is a free open source solution for cross-platform audio and video streaming, using LGPL or GPL (depending on your choice of components). FFmpeg provides recording, conversion, and streaming of audio and video functions.
Most open source solutions support cross-compiling; but FFmpeg makes it even easier. Here is a script file you can use to build FFmpeg on Linux*:
#!/bin/bash NDK=$ANDROID_NDK_ROOT #your ndk root path PLATFORM=$NDK/platforms/android-14/arch-x86 PREBUILT=$NDK/toolchains/x86-4.4.3/prebuilt/linux-x86 function build_one { ./configure --target-os=linux --prefix=$PREFIX --enable-cross-compile --extra-libs="-lgcc" --arch=x86 --cc=$PREBUILT/bin/i686-android-linux-gcc --cross-prefix=$PREBUILT/bin/i686-android-linux- --nm=$PREBUILT/bin/i686-android-linux-nm --sysroot=$PLATFORM --extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -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" --disable-ffplay --disable-avfilter --disable-avdevice --disable-ffprobe --enable-asm --enable-yasm $ADDITIONAL_CONFIGURE_FLAG make clean make -j4 install } #x86 CPU=x86 OPTIMIZE_CFLAGS="-march=atom -ffast-math -msse3 -mfpmath=sse" PREFIX=./android/$CPU ADDITIONAL_CONFIGURE_FLAG= build_one
After running this script, you will get libavcode.a, libavformat.a, libavutil.a, libswresample.a, and libswscale.a files. Link these libraries to your project as a prelink static library to get a FFmpeg dynamic library.
For the best performance, use “enable-asm” and “enable-yasm”. You must install yasm on your Linux system. Note! A compiling error occurs on old FFmpeg versions when yasm is enabled. Change mpegaudiodsp.c and delete these lines of code:
#if HAVE_SSE2_INLINE if (cpu_flags & AV_CPU_FLAG_SSE2) { s->apply_window_float = apply_window_mp3; } #endif /* HAVE_SSE2_INLINE */
Speex Compiling
Speex is a free speech codec, widely used in VoIP and sound recorder applications. Most developers use the NDK to build speex. Please read http://wang-peng1.iteye.com/blog/2040718 for more detail ( This blog is in Chinese, but can be easily read using Google Translator). This page shows an ARM* build script. To build an x86 library, you must add “-DFLOATING_POINT=1 -D_USE_SSE -D_USE_SSE2”. We don’t think this is the best solution for building speex libraries. It’s better if you write the build script yourself, which is not easy for most developers. Thus, cross-compiling on a Linux system is a better choice. In this article we will tell you how to write a cross-compiling script step by step.
First, visit http://www.speex.org/downloads/ and http://www.linuxfromscratch.org/blfs/view/svn/multimedia/libogg.html to download the source files. Speex needs the libogg library, so download the libogg file too. The build script for libogg is as follows:
#!/bin/bash export ANDROID_NDK=/home/lym/android-ndk-r9d PREBUILT=$ANDROID_NDK/toolchains/x86-4.6/prebuilt/linux-x86_64 PLATFORM=$ANDROID_NDK/platforms/android-9/arch-x86 export CFLAGS="-L$PLATFORM/usr/lib -I$PLATFORM/usr/include -I$PREBUILT/lib/gcc/i686-linux-android/4.6/include-fixed --sysroot=$PLATFORM -std=c99" export CC=$PREBUILT/bin/i686-linux-android-gcc export AR=$PREBUILT/bin/i686-linux-android-ar export AS=$PREBUILT/bin/i686-linux-android-as export LD=$PREBUILT/bin/i686-linux-android-ld export RANLIB=$PREBUILT/bin/i686-linux-android-ranlib export NM=$PREBUILT/bin/i686-linux-android-nm export STRIP=$PREBUILT/bin/i686-linux-android-strip ./configure --host=x86-linux --with-sysroot=$PLATFORM/ --prefix=/home/lym/work/Third_party_lib/speex-1.2rc1/build/x86_ogg/ echo "config sucess..." make clean make install
It’s a common build script for compiling most open source projects. Please change the ANDROID_NDK in your path and add “--sysroot=$PLATFORM -std=c99”, so that gcc can find the right library and header files. “std=c99” allows you to use a more flexible syntax, for example, “for(int i = ...)”.
Please see the libogg web site for how to write “./configure”. “--host=x86-linux” must be added to support x86 library compiling. “--prefix" will set the final output location. When you run this script, you will get the “lib” and “include” folders in the prefix path.
Now to build speex, edit and reuse the common build script shown above. First, you must add libogg library and header file paths in CFLAGS. After running the compile command line, you may get errors that some header files cannot be found. Add “-I$PLATFORM/usr/include -I$PREBUILT/lib/gcc/i686-linux-android/4.6/include-fixed -I/usr/include --sysroot=$PLATFORM” to CFLAGS. For any header files that cannot be found, just add their corresponding paths.
Next, as suggested on speex official web site, Add the following lines to the configure command:
--disable-oggtest --disable-fixed-point --enable-float-api --enable-sse
“enable-sse” will get the best performance on X86 devices, and x86 processors support hardware float, so use “enable-float-api”. Also add “-DM_PI=3.14159263” to CFLAGS. The macro define is missing in the scal.c. Finally, the last error is “stderr” cannot be linked. You must change the source code one by one and delete “stderr” in os_support.h and some other files. libspeex.a will be generated in the prefix location.
WebP Compiling
WebP is a new image format that provides lossless and lossy compression for images on the web. Please go to https://developers.google.com/speed/webp/download to get the source code of WebP library and utilities. Because WebP provides an Android.mk file, compiling WebP for Android is easy. Copy WebP under the jni of your project and make sure abi=x86 is added to application.mk. libwebp.so will be generated after the application is built.
LAME Compiling
LAME is a high-quality MPEG Audio Layer III (MP3) encoder licensed under the LGPL. To compile LAME for X86 Android, please follow these steps:
- Download and decompress the latest LAME source code from this web site: http://lame.sourceforge.net/download.php
- Copy all source code into the “jni” subfolder of your Android project.
- Create an Android.mk file in the “jni” folder with this content:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libmp3lame LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/ LOCAL_SRC_FILES := ./libmp3lame/bitstream.c ./libmp3lame/encoder.c ./libmp3lame/fft.c ./libmp3lame/gain_analysis.c ./libmp3lame/id3tag.c ./libmp3lame/lame.c ./libmp3lame/mpglib_interface.c ./libmp3lame/newmdct.c ./libmp3lame/presets.c ./libmp3lame/psymodel.c ./libmp3lame/quantize.c ./libmp3lame/quantize_pvt.c ./libmp3lame/reservoir.c ./libmp3lame/set_get.c ./libmp3lame/tables.c ./libmp3lame/takehiro.c ./libmp3lame/util.c ./libmp3lame/vbrquantize.c ./libmp3lame/VbrTag.c ./libmp3lame/version.c LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY)
Create an application.mk file in the “jni” folder with the following content (just one line, specifying the X86 ABI):
APP_ABI := x86
Edit the util.h file in the jni\libmp3lame\ folder, replacing line 574
extern ieee754_float32_t fast_log2(ieee754_float32_t x);
with:
extern float fast_log2(float x);
Run the ndk-build command in the Android project root folder. The libmp3lame.so will then be generated in the libs/x86/ folder of the Android project.
Conclusion
From the above examples, we can see that compiling open source libs for the X86 Android platform usually doesn’t take much effort. In some cases, it’s just as easy as adding the X86 ABI in the application.mk file; while in other cases, some changes may be needed, such as adapting the X86-related compile flags to gain better performance, writing a cross-compile script, etc.
Related Articles
Creating a Hardware Decoder Integrating FFmpeg with MediaCodec for Intel® Atom™-Based Android* Platforms:https://software.intel.com/en-us/android/articles/creating-a-hardware-decoder-integrating-ffmpeg-with-mediacodec-for-intel-atom-based-android
Open source project - LAME mp3 encoder optimization:https://software.intel.com/en-us/blogs/2008/10/06/open-source-project-lame-mp3-encoder-optimization
References
[1] FFmpeg: http://www.ffmpeg.org/index.html [2] Speex: http://www.speex.org/ [3] WebP: https://developers.google.com/speed/webp/ [4] LAME: http://lame.sourceforge.net/
About the Authors
Yuming Li and Fanjiang Pei are application engineers in the Intel® Atom™ Processor Mobile Enabling Team, Developer Relations Division, Software and Solutions Group (SSG). They are responsible for Android app enabling on Intel Atom processors. They focus on enabling 3rd party SDKs and open source libraries for the X86 Android platforms.
Notices
INFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS OTHERWISE AGREED IN WRITING BY INTEL, THE INTEL PRODUCTS ARE NOT DESIGNED NOR INTENDED FOR ANY APPLICATION IN WHICH THE FAILURE OF THE INTEL PRODUCT COULD CREATE A SITUATION WHERE PERSONAL INJURY OR DEATH MAY OCCUR.
Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined." Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information.
The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request.
Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order.
Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm
Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark* and MobileMark*, are measured using specific computer systems, components, software, operations, and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products.
Any software source code reprinted in this document is furnished under a software license and may only be used or copied in accordance with the terms of that license.
Intel, the Intel logo, and Atom are trademarks of Intel Corporation in the U.S. and/or other countries.
Copyright © 2014 Intel Corporation. All rights reserved.
*Other names and brands may be claimed as the property of others.