交叉编译支持多线程的Android版X264库

来源:互联网 发布:视频会议软件 编辑:程序博客网 时间:2024/05/17 02:42

好文章要转!

博客原文:http://bashell.sinaapp.com/archives/cross-complie-pthread-android-x264-library.html


第一步,制作独立交叉编译链,我使用ndkr9制作的, 使用API 9平台,gcc4.6
进入ndk目录,执行

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ ./build/tools/make-standalone-toolchain.sh --platform=android-9 --install-dir=/home/aliang/arm-linux-androideabi  

第二部,修改x264的configure
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. libpthread=""  
  2. if [ "$thread" = "auto" ]; then  
  3.     thread="no"  
  4.     case $SYS in  
  5.         BEOS)  
  6.             thread="beos"  
  7.             define HAVE_BEOSTHREAD  
  8.             ;;  
  9.         WINDOWS)  
  10.             if cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then  
  11.                 thread="posix"  
  12.                 libpthread="-lpthread"  
  13.             elif cc_check pthread.h -lpthreadGC2 "pthread_create(0,0,0,0);" ; then  
  14.                 thread="posix"  
  15.                 libpthread="-lpthreadGC2"  
  16.             elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then  
  17.                 thread="posix"  
  18.                 libpthread="-lpthreadGC2 -lwsock32"  
  19.                 define PTW32_STATIC_LIB  
  20.             elif cc_check pthread.h "-lpthreadGC2 -lws2_32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then  
  21.                 thread="posix"  
  22.                 libpthread="-lpthreadGC2 -lws2_32"  
  23.                 define PTW32_STATIC_LIB  
  24.             else  
  25.                 # default to native threading if pthread-win32 is unavailable  
  26.                 thread="win32"  
  27.             fi  
  28.             ;;  
  29.         QNX)  
  30.             cc_check pthread.h -lc && thread="posix" && libpthread="-lc"  
  31.             ;;  
  32.         *)  
  33.             //这里  
  34.             cc_check pthread.h -lc && thread="posix" && libpthread="-lc"  
  35.             ;;  
  36.     esac  
  37. 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-
可以看到,已经支持多线程了

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. platform:      ARM  
  2. system:        LINUX  
  3. cli:           yes  
  4. libx264:       internal  
  5. shared:        no  
  6. static:        no  
  7. asm:           yes  
  8. interlaced:    yes  
  9. avs:           avxsynth  
  10. lavf:          no  
  11. ffms:          no  
  12. gpac:          no  
  13. gpl:           yes  
  14. thread:        posix  
  15. opencl:        yes  
  16. filters:       crop select_every  
  17. debug:         no  
  18. gprof:         no  
  19. strip:         no  
  20. PIC:           no  
  21. visualize:     no  
  22. bit depth:     8  
  23. chroma format: all  

效果如何,我测试下看看

*************呃 不用试了,NDK头文件里虽然申明了方法,c库里却没有实现****************

好吧 解决方法当然是有的,修改x264/common/cpu.c
通过读取/sys/devices/system/cpu/present文件判断cpu核心数
0 单核
0-1 双核
0-3 四核
sched.h还是用以前的吧

博主注:

int x264_cpu_num_processors( void )

这个函数在ffmpeg2.1里面的原型已经不是上面那么简单了。

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. int x264_cpu_num_processors( void )  
  2. {  
  3. #if !HAVE_THREAD  
  4.     return 1;  
  5.   
  6. #elif SYS_WINDOWS  
  7.     return x264_pthread_num_processors_np();  
  8.   
  9. #elif SYS_CYGWIN || SYS_SunOS  
  10.     return sysconf( _SC_NPROCESSORS_ONLN );  
  11.   
  12. #elif SYS_LINUX  
  13.     cpu_set_t p_aff;  
  14.     memset( &p_aff, 0, sizeof(p_aff) );  
  15.     if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) )  
  16.         return 1;  
  17. #if HAVE_CPU_COUNT  
  18.     return CPU_COUNT(&p_aff);  
  19. #else  
  20.     int np = 0;  
  21.     for( unsigned int bit = 0; bit < 8 * sizeof(p_aff); bit++ )  
  22.         np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;  
  23.     return np;  
  24. #endif  
  25.   
  26. #elif SYS_BEOS  
  27.     system_info info;  
  28.     get_system_info( &info );  
  29.     return info.cpu_count;  
  30.   
  31. #elif SYS_MACOSX || SYS_FREEBSD || SYS_OPENBSD  
  32.     int ncpu;  
  33.     size_t length = sizeof( ncpu );  
  34. #if SYS_OPENBSD  
  35.     int mib[2] = { CTL_HW, HW_NCPU };  
  36.     if( sysctl(mib, 2, &ncpu, &length, NULL, 0) )  
  37. #else  
  38.     if( sysctlbyname("hw.ncpu", &ncpu, &length, NULL, 0) )  
  39. #endif  
  40.     {  
  41.         ncpu = 1;  
  42.     }  
  43.     return ncpu;  
  44.   
  45. #else  
  46.     return 1;  
  47. #endif  
  48. }  


替换其中的这段代码代码就行了。

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1.     cpu_set_t p_aff;  
  2.     memset( &p_aff, 0, sizeof(p_aff) );  
  3.     if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) )  
  4.         return 1;  
  5. #if HAVE_CPU_COUNT  
  6.     return CPU_COUNT(&p_aff);  
  7. #else  
  8.     int np = 0;  
  9.     for( unsigned int bit = 0; bit < 8 * sizeof(p_aff); bit++ )  
  10.         np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;  
  11.     return np; 
0 0
原创粉丝点击