zoukankan      html  css  js  c++  java
  • FFmpeg在Android上的移植之第一步

    http://blog.sina.com.cn/s/blog_69a04cf40100x1fr.html

    从事多媒体软件开发的人几乎没有不知道FFmpeg的,很多视频播放器都是基于FFmpeg开发的。如今最火的智能手机操作系统Android上的很多第三方视频播放器也是基于FFmpeg实现全格式支持。由于Android通常跑在ARM处理器上,而且Android使用了自己的libc库(即bionic),因此要在Android上编译和使用FFmpeg需要做一些移植工作,好在FFmpeg本身用C写成,很好地支持跨平台移植,实现这个目的并不难,事实上已经有很多前辈做过这方面的工作并公开了他们的成果,大概列一下:

    1.     Rockplayer:

    http://rockplayer.freecoder.org/index_cn.html

    http://blog.csdn.net/harry_helei/article/details/6322767

    2.     havlenapetr:

    http://github.com/havlenapetr/FFMpeg

    http://blog.csdn.net/scut1135/article/details/6536157

    http://hi.baidu.com/eefolks/blog/item/e0329e4682859129cefca351.html

    http://ajavn.com/anzhuotuandui/4351.html

    3.     halfninja:

    https://github.com/halfninja/android-ffmpeg-x264

    4.     olvaffe's:

    http://gitorious.org/~olvaffe/ffmpeg/ffmpeg-android

    http://www.nujk.com/ffmpeg-on-android

    5.     流媒体开发论坛 - 罗索工作室:

    http://www.rosoo.net/a/201108/14834.html

    http://bbs.rosoo.net/thread-6252-1-1.html

     

    最近本人工作中也需要在Android上使用FFmpeg,有了前人的基础就不必自己从头做起,我选择了Rockplayer的方案进行实践。RockplayerAndroid上最有名的第三方视频播放器之一,其开发者根据LGPL协议公开了所使用的FFmpeg源码,该方案用Android NDKFFmpeg源码编译生成一个单独的共享库libffmpeg.so,其中静态链接了libavformatlibavcodeclibavutil等模块,要使用FFmpeg只需要调用这一个.so即可。当然有了这个库还只是第一步,要利用FFmpeg开发一个自己的播放器或者把FFmpeg集成到Android本身的播放引擎stagefright中还需要很多其他工作,包括针对硬件平台进行优化。目前我做的只是编译出libffmpeg.so以及将FFmpeg自带的工具ffmpegadb shell中跑起来。我是在Android 3.2Honeycomb)上编译FFmpeg,所用NDK版本为r7,在Android的其他版本上编译也大同小异。下面把具体编译步骤描述如下,假定NDK安装在~/android-ndk-r7

    1.   首先从FFmpeg官网下载最新的release版本源码ffmpeg-0.10.tar.gz解压缩到Android源码树的external/ffmpeg/下(通常第三方源码都放在external这个文件夹下)。

    2.   修改$ANDROID_BUILD_TOP/external/ffmpeg/libavdevice/v 4l.c,在
    #include <linux/videodev.h>
     之上增加2行:

    typedef __signed__ long long __s64;

    typedef unsigned long long __u64;

    否则编译不过。

    3.   准备一个编译脚本build_android.sh并放在$ANDROID_BUILD_TOP/external/ffmpeg/下面,这个脚本也是Rockplayer提供的,需做一些修改,其内容附在后面。

    4.   $ANDROID_BUILD_TOP/external/ffmpeg/下面运行./build_android.sh开始编译FFmpeg,编译好的libffmpeg.so会放在文件夹android里面,一共有3个版本分别对应3ARM体系结构,包括armv7-aarmv7-a-vfparmv6_vfp,根据所运行的硬件平台选取其中一个版本。为了编译使用FFmpeg的程序时可以方便地找到libffmpeg.so,可将它复制到$OUT/system/lib/$OUT/obj/lib/,当然这一步也可以加在build_android.sh中做。

    5.   接下来就是编译可执行文件ffmpeg了,这个工具可以在命令行下完成FFmpeg提供的几乎所有功能包括编码、解码、转码等,也是用来调试和验证很有用的工具。其实上述编译完后在$ANDROID_BUILD_TOP/external/ffmpeg/下也会生成ffmpeg,但是在设备上无法运行。为了编出能在设备上运行的ffmpeg,可以写一个简单的Android.mk,其内容如下:

    LOCAL_PATH:= $(call my-dir)

    include $(CLEAR_VARS)

     

    LOCAL_SRC_FILES:=

           cmdutils.c

           ffmpeg.c

     

    LOCAL_C_INCLUDES :=

     

    LOCAL_SHARED_LIBRARIES :=

           libffmpeg

     

    LOCAL_PRELINK_MODULE := false

     

    LOCAL_MODULE := ffmpeg

     

    include $(BUILD_EXECUTABLE)

     

    将这个Android.mk放在$ANDROID_BUILD_TOP/external/ffmpeg/下面,运行mm,编好的ffmpeg$OUT/system/bin/中。

    6.   将上面编好的libffmpeg.soffmpeg分别用adb push复制到设备的/system/lib//system/bin/push之前可先对libffmpeg.so做一下strip否则太大。现在就可以在adb shell中运行ffmpeg了,使用方法与在PC上相同。当然如果要用到其他开源库例如x264那么还需要把它们也移植到Android

    以上是基本的步骤,仅供参考,可以根据具体情况修改build_android.sh里面的配置选项。如前所述,编译FFmpeg只是第一步,后续还有很多工作需要做,今后如果有进展再贴上来。

    最后附上build_android.sh供参考,在此也感谢Rockplayer的开发者:

     

    #!/bin/bash

     

    ######################################################

    # FFmpeg builds script for Android+ARM platform

    #

    # This script is released under term of

    #   CDDL (http://www.opensource.org/licenses/cddl1)

    # Wrote by pinxue (~@gmail.com) from RockPlayer.com

    #                                   2010-8 ~ 2011-4

    ######################################################

     

    ######################################################

    # Usage:

    #   put this script in top of FFmpeg source tree

    #   ./build_android

    #

    # It generates binary for following architectures:

    #     ARMv6

    #     ARMv6+VFP

    #     ARMv7+VFPv3-d16 (Tegra2)

    #     ARMv7+Neon (Cortex-A8)

    #

    # Customizing:

    # 1. Feel free to change ./configure parameters for more features

    # 2. To adapt other ARM variants

    #       set $CPU and $OPTIMIZE_CFLAGS

    #       call build_one

    ######################################################

     

    NDK=~/android-ndk-r7

    PLATFORM=$NDK/platforms/android-8/arch-arm/

    PREBUILT=../../prebuilt/linux-x86/toolchain/arm-eabi-4.4.3

     

    function build_one

    {

     

    # -fasm : required. Android header file uses asm keyword instead of __asm__ , but most of c dialect (like ansi,c99,gnu99) implies -fno-asm.

    #   ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h: In function '___arch__swab32':

    #   ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h:25: error: expected ')' before ':' token

     

    # -fno-short-enums : optimized.  Else FFmpeg obj will generate a huge number of warning for variable-size enums,

    #   though we may suppress them by --no-enum-size-warning, it would be better to avoid it.

    #   .../ld: warning: cmdutils.o uses variable-size enums yet the output is to use 32-bit enums; use of enum values across objects may fail

     

    # --extra-libs="-lgcc" : required. Else cannot solve some runtime function symbols

    #   ... undefined reference to `__aeabi_f2uiz'

     

    # --enable-protocols : required. Without this option, the file open always fails mysteriously.

    #   FFmpeg's av_open_input_file will invoke file format probing functions, but because most of useful demuxers has flag of zero

    #   which cause them are ignored during file format probling and fall to url stream parsing,

    #   if protocols are disabled, the file:// url cannot be opened as well.

     

    # $PREBUILT/bin/arm-eabi-ar d libavcodec/libavcodec.a inverse.o : required.

    #   FFmpeg includes two copies of inverse.c both in libavutil and libavcodec for performance consideration (not sure the benifit yet)

    #   Without this step, final ld of generating libffmpeg.so will fail silently, if invoke ld through gcc, gcc will collect more reasonable error message.

     

    # -llog: debug only, FFmpeg itself doesn't require it at all.

    #   With this option, we may simply includes "utils/Log.h" and use LOGx() to observe FFmpeg's behavior

    #   PS, it seems the toolchain implies -DNDEBUG somewhere, it would be safer to use following syntax

    #    #ifdef NDEBUG

    #        #undef NDEBUG

    #        #define HAVE_NDEBUG

    #    #endif

    #    #include "utils/Log.h"

    #    #ifdef HAVE_NDEBUG

    #        #define NDEBUG

    #        #undef HAVE_NDEBUG

    #    #endif

     

    # --whole-archive : required. Else ld generate a small .so file (about 15k)

     

    # --no-stdlib : required. Android doesn't use standard c runtime but invited its own wheal (bionic libc) because of license consideration.

     

    # space before of configure lines: required for some options. Else next line will be merged into previous lines's content and cause problem.

    #   Especially the --extra-cflags, the next line will pass to gcc in this case and configure will say gcc cannot create executable.

     

    # many options mentioned by articles over internet are implied by -O2 or -O3 already, need not repeat at all.

     

    # two or three common optimization cflags are omitted because not sure about the trade off yet. invoke NDK build system with V=1 to find them.

     

    # -Wl,-T,$PREBUILT/arm-eabi/lib/ldscripts/armelf.x mentioned by almost every articles over internet, but it is not required to specify at all.

     

    # -Dipv6mr_interface=ipv6mr_ifindex : required. Android inet header doesn't use ipv6mr_interface which is required by rfc, seems it generate this user space header file directly from kernel header file, but Linux kernel has decided to keep its own name for ever and ask user space header to use rfc name.

     

    # HAVE_SYS_UIO_H : required. Else:

    # In file included from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/socket.h:29,

    #                 from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/sys/socket.h:33,

    #                 from libavformat/network.h:35,

    #                 from libavformat/utils.c:46:

    #~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/uio.h:19: error: redefinition of 'struct iovec'

    #

     

    # --disable-doc : required because of strange bug of toolchain.

     

    ./configure --target-os=linux

        --prefix=$PREFIX

        --enable-cross-compile

        --extra-libs="-lgcc"

        --arch=arm

        --cc=$PREBUILT/bin/arm-eabi-gcc

        --cross-prefix=$PREBUILT/bin/arm-eabi-

        --nm=$PREBUILT/bin/arm-eabi-nm

        --sysroot=$PLATFORM

        --extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums  -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS "

        --disable-shared

        --enable-static

        --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -nostdlib -lc -lm -ldl -llog"

        --enable-parsers

        --enable-encoders  

        --enable-decoders

        --enable-muxers

        --enable-demuxers

        --enable-swscale  

        --enable-swscale-alpha

        --disable-ffplay

        --disable-ffprobe

        --disable-ffserver

        --enable-network

        --enable-indevs

        --disable-bsfs

        --enable-filters

        --enable-avfilter

        --enable-protocols  

        --enable-asm

        $ADDITIONAL_CONFIGURE_FLAG

     

     

    #make clean

    make  -j4 install

     

    $PREBUILT/bin/arm-eabi-ar d libavcodec/libavcodec.a inverse.o

     

    $PREBUILT/bin/arm-eabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libavfilter/libavfilter.a libswscale/libswscale.a libavdevice/libavdevice.a libswresample/libswresample.a -lc -lm -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-eabi/4.4.3/libgcc.a

     

    }

     

    #arm v6

    CPU=armv6

    OPTIMIZE_CFLAGS="-marm -march=$CPU"

    PREFIX=./android/$CPU

    ADDITIONAL_CONFIGURE_FLAG=

    #build_one

     

    #arm v7vfpv3

    CPU=armv7-a

    OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "

    PREFIX=./android/$CPU

    ADDITIONAL_CONFIGURE_FLAG=

    build_one

     

    #arm v7vfp

    CPU=armv7-a

    OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "

    PREFIX=./android/$CPU-vfp

    ADDITIONAL_CONFIGURE_FLAG=

    build_one

     

    #arm v7n

    CPU=armv7-a

    OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"

    PREFIX=./android/$CPU

    ADDITIONAL_CONFIGURE_FLAG=--enable-neon

    build_one

     

    #arm v6+vfp

    CPU=armv6

    OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"

    PREFIX=./android/${CPU}_vfp

    ADDITIONAL_CONFIGURE_FLAG=

    build_one

  • 相关阅读:
    C# 读写 ini 配置文件
    [转]VB 读写ini 配置文件
    js实现隔行变色-------Day40
    Camel Games借助AWS在爆发式增长中提供优质游戏体验
    深入浅出--UNIX多进程编程之fork()函数
    【玩转微信公众平台之八】 演示样例代码分析
    jQuery 选择具有特殊属性的元素
    下载超星或读秀图书时,怎么搞定完整书签?
    意外的php之学习笔记
    POJ 1182 (经典食物链 /并查集扩展)
  • 原文地址:https://www.cnblogs.com/lost-in-code/p/3163507.html
Copyright © 2011-2022 走看看