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


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


第一步,制作独立交叉编译链,我使用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 x264_cpu_num_processors( void )

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

int x264_cpu_num_processors( void )
{
#if !HAVE_THREAD
    return 1;

#elif SYS_WINDOWS
    return x264_pthread_num_processors_np();

#elif SYS_CYGWIN || SYS_SunOS
    return sysconf( _SC_NPROCESSORS_ONLN );

#elif SYS_LINUX
    cpu_set_t p_aff;
    memset( &p_aff, 0, sizeof(p_aff) );
    if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) )
        return 1;
#if HAVE_CPU_COUNT
    return CPU_COUNT(&p_aff);
#else
    int np = 0;
    for( unsigned int bit = 0; bit < 8 * sizeof(p_aff); bit++ )
        np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
    return np;
#endif

#elif SYS_BEOS
    system_info info;
    get_system_info( &info );
    return info.cpu_count;

#elif SYS_MACOSX || SYS_FREEBSD || SYS_OPENBSD
    int ncpu;
    size_t length = sizeof( ncpu );
#if SYS_OPENBSD
    int mib[2] = { CTL_HW, HW_NCPU };
    if( sysctl(mib, 2, &ncpu, &length, NULL, 0) )
#else
    if( sysctlbyname("hw.ncpu", &ncpu, &length, NULL, 0) )
#endif
    {
        ncpu = 1;
    }
    return ncpu;

#else
    return 1;
#endif
}


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

    cpu_set_t p_aff;
    memset( &p_aff, 0, sizeof(p_aff) );
    if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) )
        return 1;
#if HAVE_CPU_COUNT
    return CPU_COUNT(&p_aff);
#else
    int np = 0;
    for( unsigned int bit = 0; bit < 8 * sizeof(p_aff); bit++ )
        np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
    return np;

你可能感兴趣的:(交叉编译支持多线程的Android版X264库)