编译android用的x264

来源:互联网 发布:上海浦东机场有没有mac 编辑:程序博客网 时间:2024/05/25 20:01

本文很多内容转载自:

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

因为编译过程中根据自己的需要修改了部分内容,记录下以供自己及其它人以后参考。


编译工具为:ndkr9c, API9, GCC4.6

x264版本是:x264-20120827-2245

原文中第一步需制作自己的交叉编译工具链,因为发现制作前后的一样,所以直接跳过第一步,进行第二步:修改x264中的configure文件

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
            ;;
        OPENBSD)
            cc_check pthread.h -pthread && thread="posix" && libpthread="-pthread"
            ;;
        *)
            cc_check pthread.h -lc && thread="posix" && libpthread="-lc"
            ;;
    esac
fi

蓝色字体部分即为修改部分;


修改完configure并make,发现确实已经提示支持posix多线程,在此再次感谢原博文作者;

但是配置完编译的过程中,却发现在x264/common/cpu.c中的x264_cpu_num_processors函数中有未声明类型cpu_set_t p_aff,导致编译不过,

不过原博文已经提供了解决办法,通过读取sys/devices/system/cpu/present文件来读取cpu核心数量,代码如下:

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();
}

修改cpu.c之后,x264顺利编译完成,生成 libx264.a库;


可能原文并没有遇到下述问题,在后续的调用x264过程中出现问题:

cannot scan executable section 1 of libx264.a(dct-a.o) for Cortex-A8 erratum because it has no mapping symbols

查找后从http://code.google.com/p/android/issues/detail?id=40794#c4得出答案,在makefile文件的182及186行需要修改

-@ $(if $(STRIP), $(STRIP) -x $@) # delete local/anonymous symbols, so they don't show up in oprofile修改为以下蓝字

-@ $(if $(STRIP), $(STRIP) -x -K \$$a -K \$$t -K \$$d $@) #delete local/anonymous symbols, so they don't show up in oprofile, except the mapping symbols

接下来就可以顺利对视频进行转码压缩了;


之后的一段时间因为需要提高转码效率,读了原博文作者的http://bashell.sinaapp.com/archives/x264-encoding-live-stream-config-and-optimization-on-android-ios-platform.html,受益匪浅,非常感谢!

0 0