android-ndk-r7 编译 ffmpeg-0.10

看了这篇文章  http://blog.sina.com.cn/s/blog_69a04cf40100x1fr.html ,自己动手做的时候遇到了一些问题,记录一下。

1. 下载 android-ndk-r7-linux-x86.tar.bz2
解压到/works/sdk/ 目录
~/.bashrc中设定环境变量: 
export PATH=$PATH:/works/sdk/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
export PATH=$PATH:/works/sdk/android-sdk-linux/tools

2. 下载 ffmpeg-0.10.tar.gz
2.1 解压后 修改源文件
修改文件libavdevice/v4l.c,在
#include 之上增加2行:
typedef __signed__ long long __s64;
typedef unsigned long long __u64;
2.2 在源文件目录建立 build_android.sh 
2.3 将 ffmpeg-0.10 改名为 jni, 并在改名后的jni目录建立 Android.mk
ndk-build V=1
这时候会出错:arm-linux-androideabi-g++: /works/cong/obj/local/armeabi/ffmpeg: No such file or directory
将Android.mk 中 LOCAL_MODULE := ffmpeg 改为LOCAL_MODULE := ffmpeg_234
再次编译出现一大堆连接错误,Android.mk实在不熟悉只能手动执行: 在打印出来的 arm-linux-androideabi-g++ 的选项中加入 -L../libs/ -lffmpeg.
ok,编译通过

build_andriod.sh
  1. #!/bin/bash
  2.  
  3. ######################################################
  4. # FFmpeg builds script for Android+ARM platform
  5. #
  6. # This script is released under term of
  7. # CDDL (http://www.opensource.org/licenses/cddl1)
  8. # Wrote by pinxue (~@gmail.com) from RockPlayer.com
  9. # 2010-~ 2011-4
  10. ######################################################
  11.  
  12. ######################################################
  13. # Usage:
  14. # put this script in top of FFmpeg source tree
  15. ./build_android
  16. #
  17. # It generates binary for following architectures:
  18. # ARMv6
  19. # ARMv6+VFP
  20. # ARMv7+VFPv3-d16 (Tegra2)
  21. # ARMv7+Neon (Cortex-A8)
  22. #
  23. # Customizing:
  24. # 1. Feel free to change ./configure parameters for more features
  25. # 2. To adapt other ARM variants
  26. set $CPU and $OPTIMIZE_CFLAGS
  27. call build_one
  28. ######################################################
  29.  
  30. #NDK=/work/sdk/android-ndk-r7
  31. NDK=/works/sdk/android-ndk-r7
  32. PLATFORM=$NDK/platforms/android-8/arch-arm/
  33. #PREBUILT=../../prebuilt/linux-x86/toolchain/arm-eabi-4.4.3
  34. PREBUILT=/works/sdk/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
  35.  
  36. function build_one
  37. {
  38.  
  39. -fasm : required. Android header file uses asm keyword instead of __asm__ , but most of c dialect (like ansi,c99,gnu99) implies -fno-asm.
  40. ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h: In function '___arch__swab32':
  41. ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/asm/byteorder.h:25: error: expected ')' before ':' token
  42.  
  43. -fno-short-enums : optimized. Else FFmpeg obj will generate a huge number of warning for variable-size enums,
  44. # though we may suppress them by --no-enum-size-warning, it would be better to avoid it.
  45. .../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
  46.  
  47. --extra-libs="-lgcc" : required. Else cannot solve some runtime function symbols
  48. ... undefined reference to `__aeabi_f2uiz'
  49.  
  50. --enable-protocols : required. Without this option, the file open always fails mysteriously.
  51. # FFmpeg's av_open_input_file will invoke file format probing functions, but because most of useful demuxers has flag of zero
  52. # which cause them are ignored during file format probling and fall to url stream parsing,
  53. if protocols are disabled, the file:// url cannot be opened as well.
  54.  
  55. # $PREBUILT/bin/arm-eabi-ar d libavcodec/libavcodec.a inverse.: required.
  56. # FFmpeg includes two copies of inverse.c both in libavutil and libavcodec for performance consideration (not sure the benifit yet)
  57. # Without this step, final ld of generating libffmpeg.so will fail silently, if invoke ld through gcc, gcc will collect more reasonable error message.
  58.  
  59. -llog: debug only, FFmpeg itself doesn't require it at all.
  60. # With this option, we may simply includes "utils/Log.h" and use LOGx() to observe FFmpeg's behavior
  61. # PS, it seems the toolchain implies -DNDEBUG somewhere, it would be safer to use following syntax
  62. # #ifdef NDEBUG
  63. # #undef NDEBUG
  64. # #define HAVE_NDEBUG
  65. # #endif
  66. # #include "utils/Log.h"
  67. # #ifdef HAVE_NDEBUG
  68. # #define NDEBUG
  69. # #undef HAVE_NDEBUG
  70. # #endif
  71.  
  72. --whole-archive : required. Else ld generate a small .so file (about 15k)
  73.  
  74. --no-stdlib : required. Android doesn't use standard c runtime but invited its own wheal (bionic libc) because of license consideration.
  75.  
  76. space before \ of configure lines: required for some options. Else next line will be merged into previous lines's content and cause problem.
  77. # Especially the --extra-cflags, the next line will pass to gcc in this case and configure will say gcc cannot create executable.
  78.  
  79. # many options mentioned by articles over internet are implied by -O2 or -O3 already, need not repeat at all.
  80.  
  81. # two or three common optimization cflags are omitted because not sure about the trade off yet. invoke NDK build system with V=to find them.
  82.  
  83. -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.
  84.  
  85. -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.
  86.  
  87. # HAVE_SYS_UIO_H : required. Else:
  88. In file included from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/socket.h:29,
  89. # from ~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/sys/socket.h:33,
  90. # from libavformat/network.h:35,
  91. # from libavformat/utils.c:46:
  92. #~/android/android-ndk-r4/build/platforms/android-5/arch-arm//usr/include/linux/uio.h:19: error: redefinition of 'struct iovec'
  93. #
  94.  
  95. --disable-doc : required because of strange bug of toolchain.
  96.  
  97. ./configure --target-os=linux \
  98.     --prefix=$PREFIX \
  99.     --enable-cross-compile \
  100.     --extra-libs="-lgcc" \
  101.     --arch=arm \
  102.     --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
  103.     --cross-prefix=$PREBUILT/bin/arm-linux-androideabi-\
  104.     --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
  105.     --sysroot=$PLATFORM \
  106.     --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 " \
  107.     --disable-shared \
  108.     --enable-static \
  109.     --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
  110.     --enable-parsers \
  111.     --enable-encoders \
  112.     --enable-decoders \
  113.     --enable-muxers \
  114.     --enable-demuxers \
  115.     --enable-swscale \
  116.     --enable-swscale-alpha \
  117.     --disable-ffplay \
  118.     --disable-ffprobe \
  119.     --disable-ffserver \
  120.     --enable-network \
  121.     --enable-indevs \
  122.     --disable-bsfs \
  123.     --enable-filters \
  124.     --enable-avfilter \
  125.     --enable-protocols \
  126.     --enable-asm \
  127.     $ADDITIONAL_CONFIGURE_FLAG
  128.  
  129.  
  130. #make clean
  131. make -j4 install
  132.  
  133. $PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.
  134. $PREBUILT/bin/arm-linux-androideabi-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.-lc -lm -lz -ldl -llog --warn-once --dynamic-linker=/system/bin/linker /works/sdk/android-ndk-r7/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
  135. }
  136. #$PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/armv7-a/libgcc.
  137.  
  138. #不需要编armv6
  139. #arm v7vfpv3
  140. CPU=armv7-a
  141. OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
  142. PREFIX=./android/$CPU ADDITIONAL_CONFIGURE_FLAG=
  143. build_one
  144.  
  145. #arm v7vfp
  146. CPU=armv7-a
  147. OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
  148. PREFIX=./android/$CPU-vfp
  149. ADDITIONAL_CONFIGURE_FLAG=
  150. build_one
这个 / works / sdk / android - ndk - r7 / toolchains / arm - linux - androideabi - 4 . 4 . 3 / prebuilt / linux - x86 / lib / gcc / arm - linux - androideabi / 4 . 4 . 3 / libgcc . a
用的是绝对路径,不太清这个   / works / sdk / android - ndk - r7 / toolchains / arm - linux - androideabi - 4 . 4 . 3 / prebuilt 路径下的两个 libgcc.a有什么区别,反正用上面的一个是可以编译成功,而用另一个编译不成功。
android.mk
  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)

  3. #LOCAL_SRC_FILES:= cmdutils.cpp ffmpeg.cpp
  4. LOCAL_SRC_FILES:= cmdutils.c ffmpeg.c

  5. LOCAL_C_INCLUDES :=

  6. LOCAL_SHARED_LIBRARIES := libffmpeg

  7. #LOCAL_PRELINK_MODULE := false

  8. LOCAL_MODULE := ffmpeg_234

  9. include $(BUILD_EXECUTABLE)
不改名为 ffmpeg_234 而一直用 ffmpeg是不能编译通过的,不清楚为什么。

你可能感兴趣的:(Android)