Android libX265库的交叉编译

基于x265_2.5(2.5版本),Android-170.提取Android ndk r10交叉编译工具链

运行如下命令:

/home/xxxx/project/test/ndk/android-ndk-r9d/build/tools/make-standalone-toolchain.sh --platform=android-17 --install-dir=/home/xxxx/project/test/ndk/android-ndk-r9d/tmp/android-17-toolchain

make-standalone-toolchain.sh --platform=android-18 --install-dir=/home/yuhao/android-18-toolchain

1.交叉编译

下载libx265源码
下载地址:https://bitbucket.org/multicoreware/x265/downloads/

1.修改文件libx265/x265_2.5/build/arm-linux/crosscompile.cmake
源文件

5 set(CROSS_COMPILE_ARM 1)
  6 set(CMAKE_SYSTEM_NAME Linux)
  7 set(CMAKE_SYSTEM_PROCESSOR armv6l)
  8 
  9 # specify the cross compiler
 10 set(CMAKE_C_COMPILER arm-linux-androideabi-gcc)
 11 set(CMAKE_CXX_COMPILER arm-linux-androideabi-g++)
 12 
 13 # specify the target environment
 14 SET(CMAKE_FIND_ROOT_PATH  /home/yuhao/android-16-toolchain)
 15 

修改为

5 set(CROSS_COMPILE_ARM 1)
  6 set(CMAKE_SYSTEM_NAME Linux)
  7 set(CMAKE_SYSTEM_PROCESSOR armv6l)
  8 
  9 # specify the cross compiler
 10 set(CMAKE_C_COMPILER arm-linux-androideabi-gcc)
 11 set(CMAKE_CXX_COMPILER arm-linux-androideabi-g++)
 12 
 13 # specify the target environment
 14 SET(CMAKE_FIND_ROOT_PATH  /home/yuhao/android-18-toolchain)
 15 
 16 set(CMAKE_SYSROOT /home/yuhao/android-18-toolchain/sysroot)

1.2修改libx265/x265_2.5/source/CMakeLists.txt
因为android的ndk虽然有pthread.h,但是没有libpthread.a,集成到libc.a里了所以去掉-lpthread

619 if(X265_LATEST_TAG)
620     # convert lists of link libraries into -lstdc++ -lm etc..
621     fore::ach(LIB ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES} ${PLATFORM_LIBS})
622         if(IS_ABSOLUTE ${LIB} AND EXISTS ${LIB})
623             list(APPEND PLIBLIST "${LIB}")
624         else()
625             list(APPEND PLIBLIST "-l${LIB}")
626         endif()
627     endforeach()
628     if(PLIBLIST)
629         # blacklist of libraries that should not be in Libs.private
630         list(REMOVE_ITEM PLIBLIST "-lc" "-lpthread")  //修改点
631         string(REPLACE ";" " " PRIVATE_LIBS "${PLIBLIST}")
632     else()
633         set(PRIVATE_LIBS "")
634     endif(PLIBLIST)

修改为

619 if(X265_LATEST_TAG)
620     # convert lists of link libraries into -lstdc++ -lm etc..
621     foreach(LIB ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES} ${PLATFORM_LIBS})
622         if(IS_ABSOLUTE ${LIB} AND EXISTS ${LIB})
623             list(APPEND PLIBLIST "${LIB}")
624         else()
625             list(APPEND PLIBLIST "-l${LIB}")
626         endif()
627     endforeach()
628     if(PLIBLIST)
629         # blacklist of libraries that should not be in Libs.private
630         list(REMOVE_ITEM PLIBLIST "-lc")  //修改点
631         string(REPLACE ";" " " PRIVATE_LIBS "${PLIBLIST}")
632     else()
633         set(PRIVATE_LIBS "")
634     endif(PLIBLIST)

当前文件libx265/x265_2.5/source/CMakeLists.txt

 81 if(UNIX)
 82     list(APPEND PLATFORM_LIBS pthread)  //修改点
 83     find_library(LIBRT rt)
 84     if(LIBRT)
 85         list(APPEND PLATFORM_LIBS rt)
 86     endif()

修改为

 81 if(UNIX)
 82 #    list(APPEND PLATFORM_LIBS pthread)  //修改点
 83     find_library(LIBRT rt)
 84     if(LIBRT)
 85         list(APPEND PLATFORM_LIBS rt)
 86     endif()

2.安装编译所需要的软件
执行如下命令

sudo apt-get install cmake-curses-gui

3.配置编译信息
在build/arm-linux/目录下执行配置命令

cmake -DCMAKE_TOOLCHAIN_FILE=crosscompile.cmake -G "Unix Makefiles" ../../source && ccmake ../../source

执行如上命令会出现下图信息

 CHECKED_BUILD                    OFF        
 CMAKE_BUILD_TYPE                 Release    
 CMAKE_INSTALL_PREFIX             /home/yuhao/ytxPlayer/android/contrib/jni/jniLibs/x265
 DETAILED_CU_STATS                OFF  
 ENABLE_AGGRESSIVE_CHECKS         OFF  
 ENABLE_ASSEMBLY                  OFF   //关闭汇编
 ENABLE_CLI                       ON   
 ENABLE_HDR10_PLUS                OFF  
 ENABLE_LIBNUMA                   ON   
 ENABLE_PIC                       ON  //打开PIC 位置无关
 ENABLE_PPA                       OFF  
 ENABLE_SHARED                    ON   
 ENABLE_TESTS                     OFF   
 ENABLE_VTUNE                     OFF   
 FSANITIZE                              
 HIGH_BIT_DEPTH                   OFF   
 LIBDL                            /home/yuhao/android-18-toolchain/sysroot/usr/lib/libdl.so
 LIB_INSTALL_DIR                  lib    
 NO_ATOMICS                       OFF    
 NUMA_ROOT_DIR                    /usr   
 STATIC_LINK_CRT                  OFF  
 WARNINGS_AS_ERRORS               OFF    
 YASM_EXECUTABLE                  /home/yuhao/android-18-toolchain/bin/yasm                     

输入c
然后输入g

修改文件/build/arm-linux/CMakeFiles/x265-shared.dir/link.txt

arm-linux-androideabi-g++  -fPIC -O3 -DNDEBUG -Wl,-Bsymbolic,-znoexecstack -shared -Wl,-soname,libx265.so.130 -o libx265.so.130

修改为(****后续编译如有问题 则删除最后的 -lrt 编译选项)
因为android的ndk没有librt.a,集成到libc.a里了所以去掉-lrt ???需要验证一下 懒得弄了 累了!!!

arm-linux-androideabi-g++  -fPIC -O3 -DNDEBUG -Wl,-Bsymbolic,-znoexecstack -shared -o libx265.so

修改文件/build/arm-linux/CMakeFiles/cli.dir/link.txt

-o x265 -rdynamic libx265.so.130

修改为

-o x265 -rdynamic libx265.so

修改文件/build/arm-linux/CMakeFiles/x265-shared.dir/build.make

187 libx265.so: encoder/CMakeFiles/encoder.dir/__/x265-extras.cpp.o
188 libx265.so: common/CMakeFiles/common.dir/arm/asm-primitives.cpp.o //修改点
189 libx265.so: common/CMakeFiles/common.dir/primitives.cpp.o
190 libx265.so: common/CMakeFiles/common.dir/pixel.cpp.o
191 libx265.so: common/CMakeFiles/common.dir/dct.cpp.o
192 libx265.so: common/CMakeFiles/common.dir/ipfilter.cpp.o
193 libx265.so: common/CMakeFiles/common.dir/intrapred.cpp.o
194 libx265.so: common/CMakeFiles/common.dir/loopfilter.cpp.o
195 libx265.so: common/CMakeFiles/common.dir/constants.cpp.o
196 libx265.so.130: common/CMakeFiles/common.dir/cpu.cpp.o
197 libx265.so.130: common/CMakeFiles/common.dir/version.cpp.o
198 libx265.so.130: common/CMakeFiles/common.dir/threading.cpp.o
199 libx265.so.130: common/CMakeFiles/common.dir/threadpool.cpp.o
200 libx265.so.130: common/CMakeFiles/common.dir/wavefront.cpp.o
201 libx265.so.130: common/CMakeFiles/common.dir/md5.cpp.o
202 libx265.so.130: common/CMakeFiles/common.dir/bitstream.cpp.o
203 libx265.so.130: common/CMakeFiles/common.dir/yuv.cpp.o
204 libx265.so.130: common/CMakeFiles/common.dir/shortyuv.cpp.o
205 libx265.so.130: common/CMakeFiles/common.dir/picyuv.cpp.o
206 libx265.so.130: common/CMakeFiles/common.dir/common.cpp.o
207 libx265.so.130: common/CMakeFiles/common.dir/param.cpp.o
208 libx265.so.130: common/CMakeFiles/common.dir/frame.cpp.o
209 libx265.so.130: common/CMakeFiles/common.dir/framedata.cpp.o
210 libx265.so.130: common/CMakeFiles/common.dir/cudata.cpp.o
211 libx265.so.130: common/CMakeFiles/common.dir/slice.cpp.o
212 libx265.so.130: common/CMakeFiles/common.dir/lowres.cpp.o
213 libx265.so.130: common/CMakeFiles/common.dir/piclist.cpp.o
214 libx265.so.130: common/CMakeFiles/common.dir/predict.cpp.o
215 libx265.so.130: common/CMakeFiles/common.dir/scalinglist.cpp.o
216 libx265.so.130: common/CMakeFiles/common.dir/quant.cpp.o
217 libx265.so.130: common/CMakeFiles/common.dir/deblock.cpp.o
218 libx265.so.130: CMakeFiles/x265-shared.dir/build.make
219 libx265.so.130: x265.def
220 libx265.so.130: CMakeFiles/x265-shared.dir/link.txt
221         @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/yuhao/ytxPlayer/android/contrib/libx265/tmp/x265_2.5/build/arm-lin
    ux/CMakeFiles --progress-num=$(CMAKE_PROGRESS_10) "Linking CXX shared library libx265.so"
222         $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/x265-shared.dir/link.txt --verbose=$(VERBOSE)
223         $(CMAKE_COMMAND) -E cmake_symlink_library libx265.so.130 libx265.so.130 libx265.so
224 
225 libx265.so: libx265.so.130
226         @$(CMAKE_COMMAND) -E touch_nocreate libx265.so

改为

187 libx265.so: encoder/CMakeFiles/encoder.dir/__/x265-extras.cpp.o
188 #libx265.so: common/CMakeFiles/common.dir/arm/asm-primitives.cpp.o //修改点
189 libx265.so: common/CMakeFiles/common.dir/primitives.cpp.o
190 libx265.so: common/CMakeFiles/common.dir/pixel.cpp.o
191 libx265.so: common/CMakeFiles/common.dir/dct.cpp.o
192 libx265.so: common/CMakeFiles/common.dir/ipfilter.cpp.o //将libx265.so.130改为libx265.so
193 libx265.so: common/CMakeFiles/common.dir/intrapred.cpp.o
194 libx265.so: common/CMakeFiles/common.dir/loopfilter.cpp.o
195 libx265.so: common/CMakeFiles/common.dir/constants.cpp.o
196 libx265.so: common/CMakeFiles/common.dir/cpu.cpp.o
197 libx265.so: common/CMakeFiles/common.dir/version.cpp.o
198 libx265.so: common/CMakeFiles/common.dir/threading.cpp.o
199 libx265.so: common/CMakeFiles/common.dir/threadpool.cpp.o
200 libx265.so: common/CMakeFiles/common.dir/wavefront.cpp.o
201 libx265.so: common/CMakeFiles/common.dir/md5.cpp.o
202 libx265.so: common/CMakeFiles/common.dir/bitstream.cpp.o
203 libx265.so: common/CMakeFiles/common.dir/yuv.cpp.o
204 libx265.so: common/CMakeFiles/common.dir/shortyuv.cpp.o
205 libx265.so: common/CMakeFiles/common.dir/picyuv.cpp.o
206 libx265.so: common/CMakeFiles/common.dir/common.cpp.o
207 libx265.so: common/CMakeFiles/common.dir/param.cpp.o
208 libx265.so: common/CMakeFiles/common.dir/frame.cpp.o
209 libx265.so: common/CMakeFiles/common.dir/framedata.cpp.o
210 libx265.so: common/CMakeFiles/common.dir/cudata.cpp.o
211 libx265.so: common/CMakeFiles/common.dir/slice.cpp.o
212 libx265.so: common/CMakeFiles/common.dir/lowres.cpp.o
213 libx265.so: common/CMakeFiles/common.dir/piclist.cpp.o
214 libx265.so: common/CMakeFiles/common.dir/predict.cpp.o
215 libx265.so: common/CMakeFiles/common.dir/scalinglist.cpp.o
216 libx265.so: common/CMakeFiles/common.dir/quant.cpp.o
217 libx265.so: common/CMakeFiles/common.dir/deblock.cpp.o
218 libx265.so: CMakeFiles/x265-shared.dir/build.make
219 libx265.so: x265.def
220 libx265.so: CMakeFiles/x265-shared.dir/link.txt
221         @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/yuhao/ytxPlayer/android/contrib/libx265/x265_2.5/build/arm-linux/C
    MakeFiles --progress-num=$(CMAKE_PROGRESS_10) "Linking CXX shared library libx265.so"
222         $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/x265-shared.dir/link.txt --verbose=$(VERBOSE)
223         $(CMAKE_COMMAND) -E cmake_symlink_library libx265.so libx265.so libx265.so.130 //修改点
224 
225 libx265.so.130: libx265.so
226         @$(CMAKE_COMMAND) -E touch_nocreate libx265.so.130 //修改点

将libx265.so.130改为libx265.so 修改以后 libx265.so.130 一共剩下3个

修改文件source/common/cpu.cpp

331 uint32_t cpu_detect(void)
332 {   
333     int flags = 0;
334 
335 #if HAVE_ARMV6
336     flags |= X265_CPU_ARMV6;
337 
338     // don't do this hack if compiled with -mfpu=neon
339 #if !HAVE_NEON
340     static void (* oldsig)(int);
341     oldsig = signal(SIGILL, sigill_handler);
342     if (sigsetjmp(jmpbuf, 1))
343     {
344         signal(SIGILL, oldsig);
345         return flags;
346     }
347 
348     canjump = 1;
349     PFX(cpu_neon_test)();
350     canjump = 0;
351     signal(SIGILL, oldsig);
352 #endif // if !HAVE_NEON
353 
354     flags |= X265_CPU_NEON;
355 
356     // fast neon -> arm (Cortex-A9) detection relies on user access to the
357     // cycle counter; this assumes ARMv7 performance counters.
358     // NEON requires at least ARMv7, ARMv8 may require changes here, but
359     // hopefully this hacky detection method will have been replaced by then.
360     // Note that there is potential for a race condition if another program or
361     // x264 instance disables or reinits the counters while x264 is using them,
362     // which may result in incorrect detection and the counters stuck enabled.
363     // right now Apple does not seem to support performance counters for this test
364 #ifndef __MACH__
365     flags |= PFX(cpu_fast_neon_mrc_test)() ? X265_CPU_FAST_NEON_MRC : 0;
366 #endif
367     // TODO: write dual issue test? currently it's A8 (dual issue) vs. A9 (fast mrc)
368 #endif // if HAVE_ARMV6
369  
370     return flags;
371 } 

修改为

uint32_t cpu_detect(void)
{   
     int flags = 0;  
     return flags;
}

在目录/build/arm-linux/ 中执行make命令
/build/arm-linux/ cmake_install.cmake 这个文件可以查看那些库和头文件是必须的安装的!!!!!

编译如果出现如下错误 主要和ndk 的版本有关 可以直接在common.h 中修改 定义的宏
/home/luxin/下载/x265_2.5/source/common/common.h:246:29: error: ‘log2’ was not declared in this scope
#define X265_LOG2(x) log2(x)

//#if defined(_MSC_VER)
#define X265_LOG2F(x) (logf((float)(x)) * 1.44269504088896405f)
#define X265_LOG2(x) (log((double)(x)) * 1.4426950408889640513713538072172)
//#else
//#define X265_LOG2F(x) log2f(x)
//#define X265_LOG2(x)  log2(x)
//#endif

编译结束
下面给出一个H265编码和解码的测试程序
点击H265ENCODE按钮开始进行编码,通过log输出等待编码完成。完成后点击H265DECODE进行解码,生成YUV数据。
在测试时注意替换输入输出文件的路径
测试源码下载地址:https://github.com/ytxhao/androidH265Codec

你可能感兴趣的:(qt)