第一步,制作独立交叉编译链,我使用ndkr9制作的, 使用API 9平台,gcc4.6
进入ndk目录,执行
$ ./build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/home/aliang/arm-linux-androideabi
第二部,修改x264的configure
libpthread=""
if [ "$thread" = "auto" ]; then
thread="no"
case $SYS in
BEOS)
thread="beos"
define HAVE_BEOSTHREAD
;;
WINDOWS)
if cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then
thread="posix"
libpthread="-lpthread"
elif cc_check pthread.h -lpthreadGC2 "pthread_create(0,0,0,0);" ; then
thread="posix"
libpthread="-lpthreadGC2"
elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
thread="posix"
libpthread="-lpthreadGC2 -lwsock32"
define PTW32_STATIC_LIB
elif cc_check pthread.h "-lpthreadGC2 -lws2_32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then
thread="posix"
libpthread="-lpthreadGC2 -lws2_32"
define PTW32_STATIC_LIB
else
# default to native threading if pthread-win32 is unavailable
thread="win32"
fi
;;
QNX)
cc_check pthread.h -lc && thread="posix" && libpthread="-lc"
;;
*)
cc_check pthread.h -lc && thread="posix" && libpthread="-lc"
;;
esac
fi
改为红色行的内容,因为android的ndk虽然有pthread.h,但是没有libpthread.a,集成到libc.a里了
第三步,定位到制作的独立编译链的头文件目录 /home/aliang/arm-linux-androideabi/sysroot/usr/include/
使用这个sched.h替换原有的sched.h文件
ok
./configure –host=arm-linux-androideabi –cross-prefix=arm-linux-androideabi-
可以看到,已经支持多线程了
platform: ARM
system: LINUX
cli: yes
libx264: internal
shared: no
static: no
asm: yes
interlaced: yes
avs: avxsynth
lavf: no
ffms: no
gpac: no
gpl: yes
thread: posix
opencl: yes
filters: crop select_every
debug: no
gprof: no
strip: no
PIC: no
visualize: no
bit depth: 8
chroma format: all
效果如何,我测试下看看
*************呃 不用试了,NDK头文件里虽然申明了方法,c库里却没有实现****************
好吧 解决方法当然是有的,修改x264/common/cpu.c
通过读取/sys/devices/system/cpu/present文件判断cpu核心数
0 单核
0-1 双核
0-3 四核
sched.h还是用以前的吧
int getNrOfCPUs()
{
FILE* fp;
int res, i = -1, j = -1;
/* open file */
fp = fopen("/sys/devices/system/cpu/present", "r");
if (fp == 0)
{
return -1; /* failure */
}
/* read and interpret line */
res = fscanf(fp, "%d-%d", &i, &j);
/* close file */
fclose(fp);
/* interpret result */
if (res == 1 && i == 0) /* single-core? */
{
return 1;
}
if (res == 2 && i == 0) /* 2+ cores */
{
return j+1;
}
return 1; /* failure */
}
int x264_cpu_num_processors( void )
{
return getNrOfCPUs();
}
测试结果请移步这里Android环境 多核CPU x264编码性能测试
X264性能调优,请看Android,IOS平台上x264编码实时视频参数设置与优化
原创文章,转载请注明: 转载自贝壳博客
本文链接地址: 交叉编译支持多线程的Android版X264库
感谢作者,学习了!