This article was first published on the wechat official account Byteflow
FFmpeg development series serial:
FFmpeg Development (01) : FFmpeg compilation and integration
FFmpeg development (02) : FFmpeg + ANativeWindow video decoding playback
FFmpeg development (03) : FFmpeg + OpenSLES audio decoding playback
FFmpeg development (04) : FFmpeg + OpenGLES audio visual playback
FFmpeg development (05) : FFmpeg + OpenGLES video decoding playback and video filter
FFmpeg development (06) : FFmpeg player to achieve audio and video synchronization in three ways
FFmpeg development (07) : FFmpeg + OpenGLES implementation of 3D panorama player
FFmpeg Development (08) : FFmpeg player video rendering optimization
Recently, ACCORDING to the needs of the project, CRAZY OpenCL, FFmpeg related articles fell behind a lot, behind also prepare to introduce the application of OpenCL on Android, in addition, OpenCL can be combined with OpenGL, very interesting.
I’ve covered decoding audio and video with FFmpeg and rendering video with filters. I’ll cover audio and video coding, including adding fun filters to video and funny sound changes to audio.
About X264, FDK-AAC, FFmpeg open source library compilation introduction online “full street” yes, can refer to a lot of information. This article just uses a higher VERSION of NDK and uses CLang to compile to prevent compilation problems in later secondary development.
Compile environment
Compile environment:
- android-ndk-r20b-linux-x86_64
- Ffmpeg – 4.2.2
- Libfdk – aac version: 0.1.5
- x264
Compile the FDK – aac
Fdk-aac stands for: Fraunhofer FDK AAC Codec Library is an open source software library for encoding and decoding audio in the advanced audio encoding format AAC.
Create the build_fdk_aac_clang.sh script in the fdk-aac decompression directory:
#! /bin/bash
NDK=/root/workspace/android-ndk-r20b # you need to replace it with your local NDK path
HOST_TAG=linux-x86_64
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$HOST_TAG
ANDROID_LIB_PATH="$(pwd)/android"
API=21
function build_android
{
echo "Build fdk-aac for android $CPU"
./configure \
--host=$HOST \
--disable-shared \
--enable-static \
--prefix="$ANDROID_LIB_PATH/$CPU" \
CPPFLAGS="-fPIC"
make clean
make -j8
make install
echo "Build fdk-aac for android $CPU completed"
}
# armeabi-v7a
CPU=armv7-a
HOST=arm-linux-android
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export AS=$TOOLCHAIN/bin/arm-linux-androideabi-as
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
export STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip
export CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
export CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
build_android
# arm64-v8a
CPU=armv8-a
HOST=aarch64-linux-android
export AR=$TOOLCHAIN/bin/aarch64-linux-android-ar
export AS=$TOOLCHAIN/bin/aarch64-linux-android-as
export LD=$TOOLCHAIN/bin/aarch64-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/aarch64-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/aarch64-linux-android-strip
export CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
export CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
build_android
Copy the code
To change the permission to compile the script, perform the compile:
# change build_fdk_aac_clang.sh executable permission
chmod +x build_fdk_aac_clang.sh
Run the compile script
bash build_fdk_aac_clang.sh
Copy the code
After compiling, the Android folder is automatically created in the current directory and the 32 & 64 bit libfDK-aac. a library is generated.
- the FDK - aac - 0.1.5 - android - lib/armeabi - v7a -- -- -- -- -- -- -- libfdk aac. -- -- -- -- -- a lib/arm64 - v8a -- -- -- -- -- -- -- libfdk aac. ACopy the code
Compile the x264
X264 is a GPL-licensed open source video coding library, mainly used for H.264/MPEG-4 AVC video coding, only used as encoders.
Build_x264_clang. sh:
#! /bin/bash
export NDK=/root/workspace/android-ndk-r20b # you need to replace it with your local NDK path
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
export API=21
function build_android
{
./configure \
--prefix=$PREFIX \
--disable-cli \
--disable-shared \
--enable-static \
--enable-pic \
--host=$my_host \
--cross-prefix=$CROSS_PREFIX \
--sysroot=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot \
make clean
make -j8
make install
}
#arm64-v8a
PREFIX=./android/arm64-v8a
my_host=aarch64-linux-android
export TARGET=aarch64-linux-android
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
build_android
#armeabi-v7a
PREFIX=./android/armeabi-v7a
my_host=armv7a-linux-android
export TARGET=armv7a-linux-androideabi
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
build_android
Copy the code
Also change the permission to compile the script, execute the compile:
# change build_x264_clang.sh executable permission
chmod +x build_x264_clang.sh
Run the compile script
bash build_x264_clang.sh
Copy the code
After compiling, the Android folder is automatically created in the current directory and the 32 & 64 bit libx264.a library is generated.
-x264
---android
-----lib/armeabi-v7a
-------libx264.a
-----lib/arm64-v8a
-------libx264.a
Copy the code
Compile FFmpeg links fdK-AAC and X264
The introduction, compilation, and integration of FFmpeg can be found in the previous article: FFmpeg integration Compilation. This section directly introduces linking fdK-AAC and X264 libraries when compiling FFmpeg.
On Android, when we want to link to a third-party library, we need the header file of the third-party library and the corresponding.a or.so library. Previously, we have compiled fdK-AAC and X264 libraries, and can obtain their corresponding static libraries and header files.
FFmpeg, FDK-AAC, and X264 directory structures
Build_ffmpeg_link_fdk_aac_x264_clang. sh:
#! /bin/bash
export NDK=/root/workspace/android-ndk-r20b # you need to replace it with your local NDK path
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
function build_android
{
./configure \
--prefix=$PREFIX \
--enable-neon \
--enable-hwaccels \
--enable-gpl \
--disable-postproc \
--enable-static \
--enable-shared \
--disable-debug \
--enable-small \
--enable-jni \
--enable-mediacodec \
--disable-doc \
--enable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--enable-libx264 \
--enable-libfdk-aac \
--enable-encoder=libx264 \
--enable-encoder=libfdk-aac \
--enable-nonfree \
--enable-muxers \
--enable-decoders \
--enable-demuxers \
--enable-parsers \
--enable-protocols \
--cross-prefix=$CROSS_PREFIX \
--target-os=android \
--arch=$ARCH \
--cpu=$CPU \
--cc=$CC \
--cxx=$CXX \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-I$X264_INCLUDE -I$FDK_INCLUDE -Os -fpic $OPTIMIZE_CFLAGS" \
--extra-ldflags="-lm -L$FDK_LIB -L$X264_LIB $ADDI_LDFLAGS"
make clean
make -j8
make install
echo "The Compilation of FFmpeg with x264,fdk-aac for $CPU is completed"
}
#armv8-a
ARCH=arm64
CPU=armv8-a
API=21
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
BASE_PATH=/root/workspace
LIB_TARGET_ABI=arm64-v8a
# specify fdK-aAC header files and static library directories
FDK_INCLUDE=$BASE_PATH/ FDK - aac - 0.1.5 / android /$LIB_TARGET_ABI/include
FDK_LIB=$BASE_PATH/ FDK - aac - 0.1.5 / android /$LIB_TARGET_ABI/lib
# specify x264 header files and static library directories
X264_INCLUDE=$BASE_PATH/x264/android/$LIB_TARGET_ABI/include
X264_LIB=$BASE_PATH/x264/android/$LIB_TARGET_ABI/lib
build_android
cp $FDK_LIB/libfdk-aac.a $PREFIX/lib
cp $X264_LIB/libx264.a $PREFIX/lib
#armv7-a
ARCH=arm
CPU=armv7-a
API=21
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
PREFIX=$(pwd)/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
BASE_PATH=/root/workspace
LIB_TARGET_ABI=armeabi-v7a
FDK_INCLUDE=$BASE_PATH/ FDK - aac - 0.1.5 / android /$LIB_TARGET_ABI/include
FDK_LIB=$BASE_PATH/ FDK - aac - 0.1.5 / android /$LIB_TARGET_ABI/lib
X264_INCLUDE=$BASE_PATH/x264/android/$LIB_TARGET_ABI/include
X264_LIB=$BASE_PATH/x264/android/$LIB_TARGET_ABI/lib
build_android
cp $FDK_LIB/libfdk-aac.a $PREFIX/lib
cp $X264_LIB/libx264.a $PREFIX/lib
Copy the code
There are a few key points to note:
# Enable two encoders --enable-libx264 \ --enable-libfdk-aac \ --enable-encoder=libx264 \ --enable-encoder=libfdk-aac \...... --extra-cflags="-I$X264_INCLUDE -i $fdk_include-os-fpic $OPTIMIZE_CFLAGS" \ --extra-ldflags="-lm -L$FDK_LIB -L$X264_LIB $ADDI_LDFLAGS"Copy the code
There is also a link to the standard Mathematics library:
--extra-ldflags="-lm -l $FDK_LIB -l $X264_LIB $ADDI_LDFLAGSCopy the code
FFmpeg link to fdK-AAC;
Here comes another question: how do I check for common FFmpeg compilation errors?
The answer is to look at the config.log file in the ffbuild folder. You can skip to the end of the file to see the error.
Finally, by viewing the compilation and configuration information of FFmpeg library after the compilation is successful, to see whether our compilation is successful, this article will print out the compilation and configuration information of FFmpeg through the program.
The source code
LearnFFmpeg source code
Technical communication
Technical exchange/get source code can add my wechat: byte-flow