Build FFMPEG 4.1 for Android (latest version)

preface

As mentioned above, the main content of this article is to summarize the problems encountered in compiling FFmpeg. One thing to note here is that different versions of the NDK will encounter different pits. This article focuses on the compilation process, encountered pits. The configure configuration will vary depending on the needs of the user. The configure configuration will vary depending on the needs of the user.


The compilation process

Just to give you an overview of the compilation process, we don’t consider cross-compilation for the sake of illustration.

  1. Go to FFmpeg or Github to download the source code locally. Download address: (1) ffMPEG official website download address (2) Github ffMPEG download address

  2. R17 is recommended (including R17C). Why not use the latest version of R19B? I’ll answer that question later. Android NDK download address

  3. Go to the FFmpeg directory and find a configure script./configure –help Take your time and enable or disable FFmpeg compilation depending on your project needs. One thing to note here is that if you want to compile the FFmpeg dynamic library, which is the.so library, you need to modify the configure script. Modify before:

SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)' LIB_INSTALL_EXTRA_CMD='? (RANLIB) "$(LIBDIR)/$(LIBNAME)"' SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)' SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR)  $(SLIBNAME)'Copy the code

Revised:

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)' LIB_INSTALL_EXTRA_CMD='? (RANLIB) "$(LIBDIR)/$(LIBNAME)"' SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)' SLIB_INSTALL_LINKS='$(SLIBNAME)'Copy the code

The purpose of the modification is to generate a dynamic library naming way can be understood and recognized by the Android platform. If you are compiling the FFmpeg static library, there is no need to make the above changes.

Create a build_android.sh script to help build ffmpeg.build_android. sh in the same directory as the configure script:

#! /bin/bash set-x API=14 NDK=/NDK installation location /android-ndk-r15c SYSROOT=$NDK/platforms/android-$API/arch-arm/ TOOLCHAIN=$NDK/ toolChains /arm-linux-androideabi-4.9/prebuilt/linux-x86_64 function function_one {./configure \ --prefix=$PREFIX \ --disable-shared \ --enable-static \ --disable-doc \ --disable-ffplay \ --disable-ffprobe \ --disable-symver \ --disable-ffmpeg \ --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --target-os=linux \ --arch=arm \ --enable-cross-compile \ --sysroot=$SYSROOT \ --extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a" \ --enable-neon make clean all make make install } CPU=armv7-a PREFIX=$(pwd)/android/$CPU function_oneCopy the code

Libavresample and libpostProc are deprecated and deprecated by default. If you want to compile these two libraries, you need to set: – enable – avresample – enable the GPL – enable – nonfree – enable – postproc The above compilation script compiled FFmpeg library is still very complete, quite large, we do not need so complete in the real project, only need to use part of the function, here can be tailored to FFmpeg according to need, custom. To customize the content, run./configure.sh –help to view the research by yourself. I won’t go into details here.


Ideally, the android/armv7-a/ file will be successfully generated after the above steps, including the lib,include directory,lib folder is required to compile, suitable for the Android platform library files. But it doesn’t always work out so well. As it says, ideally… Then came the pit…


In the pit of

The build_android.sh script used in the NDK version is R15C, so in FFmpeg 4.1 you should get this error:

libavformat/udp.c: In function 'udp_set_multicast_sources':
libavformat/udp.c:290:28: error: request for member 's_addr' in something not a structure or union
Copy the code

There was little online description of the error, and the official response was of little value.

trac.ffmpeg.org/ticket/7741

Solution: There are two solutions: 1. Upgrade the NDK version to R17C 2. If you don’t want to upgrade the NDK, you can change the libavformat/udp.c file and comment out the error code. If you don’t use this feature in your project. This problem should occur in the lower version of the NDK, I have tried the R9B version, also have this problem.

(2) if the NDK version is R17C, the above problem will not be encountered, but will encounter new problems.

CC libavdevice/alldevices.o In file included from ./libavformat/internal.h:24:0, from libavdevice/alldevices.c:23: / opt/andorid_sdk/adt - bundle - Linux - x86-20131030 / android - the NDK - r17c/toolchains/arm - Linux - androideabi - 4.9 / prebuilt/Linux - x86_ 64 / lib/GCC/arm - Linux - androideabi / 4.9 x/include/stdint. H: thine: fatal error: stdint. H: No to the file or directoryCopy the code

This error occurred because the NDK R17C version separated the header files from the library files. The sysroot folder we specified had only the library files, while the header files were placed in the NDK directory sysroot. Solution: Add -isysroot $NDK/sysroot to -extra-cflags.

There are also separate headers for assemblies. Solution: according to the target platform to specify “-i $the NDK/sysroot/usr/include/arm – Linux – androideabi”, will be “arm – Linux – androideabi” platform can be changed to need

Build_android. sh after modification:

#! /bin/bash set -x API=14 NDK=/opt/andorid_sdk/adt-bundle-linux-x86-20131030/android-ndk-r17c SYSROOT = $the NDK/platforms/android - $API/arch - arm/TOOLCHAIN = $the NDK/toolchains/arm - Linux - androideabi - 4.9 / prebuilt/Linux - x86_64 function function_one { ./configure \ --prefix=$PREFIX \ --disable-shared \ --enable-static \ --disable-doc \ --disable-ffplay \ --disable-ffprobe \ --disable-symver \ --disable-ffmpeg \ --cc=$TOOLCHAIN/bin/arm-linux-androideabi-gcc \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --target-os=linux  \ --arch=arm \ --enable-cross-compile \ --sysroot=$SYSROOT \ --extra-cflags="-I$NDK/sysroot/usr/include/arm-linux-androideabi -isysroot $NDK/sysroot -fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated -mfloat-abi=softfp -marm -march=armv7-a" \ --enable-neon make clean all make make  install } CPU=armv7-a PREFIX=$(pwd)/android/$CPU function_oneCopy the code

(三)

libavcodec/aaccoder.c: In function 'search_for_ms':
libavcodec/aaccoder.c:803:25: error: expected identifier or '(' before numeric constant
                     int B0 = 0, B1 = 0;
                         ^

Copy the code

This is an error due to a definition conflict, related to the NDK version, and I didn’t encounter this problem when using r15C or R9B. Libavcodec /aaccoder. C: libavcodec/aaccoder. C: libavcodec/aaccoder. C: libavcodec/aaccoder.

(四)

libavcodec/hevc_mvs.c: In function 'derive_spatial_merge_candidates':
libavcodec/hevc_mvs.c:208:15: error: 'y0000000' undeclared (first use in this function)
             ((y ## v) >> s->ps.sps->log2_min_pu_size))
               ^

Copy the code

Solution: change libavcodec/hevc_mvs.c to B0, xB0 to xB0, and yB0 to yB0

(五)

libavcodec/opus_pvq.c: In function 'quant_band_template':
libavcodec/opus_pvq.c:498:9: error: expected identifier or '(' before numeric constant
     int B0 = blocks;
         ^

Copy the code

Solution: Change the libavcodec/opus_pvq.c variable B0 to B0

conclusion

[configure.sh] [configure.sh] [configure.sh] [configure.sh] [configure.sh] [configure.sh] [configure.sh] [configure.sh] [configure.sh] [configure.sh] For example, the -ffserver parameter is no longer used. GCC is used to build the script, but GCC is removed in NDK versions after R17C. So if you use the latest version of R19B now, there will be an error about GCC. Consider replacing the compiler with GCC, which should solve the problem, but I don’t know if there will be other problems… This still needs to be studied, and will be updated and supplemented as the findings come.

Refer to the article

Alientechlab.com/how-to-buil…

medium.com/@karthikcod…

Blog.csdn.net/luo0xue/art…