FFmpeg Transplant Android

Source: Internet
Author: User

Recent projects need to parse Apple's HLS streaming protocol, and FFmpeg has added support for HLS protocols since the 0.11.1 "happiness" version. There are many articles on the internet about ffmpeg compiling and porting, but most of them are descriptions of the old versions. The new version of the source code has made changes to many places, including some variable names, constant names, function names, function parameters, structure, etc., so in the old version of the JNI interface, you need to make corresponding changes. This compilation porting work really took me a lot of time, now summed up, I hope to give you some help. Note that this article is not necessarily correct, everyone in the compilation process, will inevitably encounter a variety of errors, can be proposed to discuss together.

Compilation environment: Ubuntu 12.04, Android NDK R8

First, download the source code, new project

: http://ffmpeg.org/download.html, download the latest ffmpeg-0.11.1 compression pack.

Under the $ndk_home/samples/directory (mine is/root/develop/android-ndk-r8/samples/ Create a FFMPEG-0.11 directory, in which a new JNI directory, and then the FFmpeg source decompression in the JNI, the source folder renamed to Libffmpeg, so the final source of the path is:

/root/develop/android-ndk-r8/samples/ffmpeg-0.11/jni/libffmpeg

Second, configure the source code

1. Create a config_common.sh script

Create a config_common.sh in the JNI directory with the following content:

Shellscript Code
  1. #!/bin/bash
  2. Ffconfig_common= '
  3. --enable-version3
  4. --disable-gpl
  5. --disable-nonfree
  6. --disable-debug
  7. --disable-ffmpeg
  8. --disable-ffplay
  9. --disable-ffserver
  10. --disable-ffprobe
  11. --disable-encoders
  12. --disable-muxers
  13. --disable-devices
  14. --disable-protocols
  15. --enable-protocol=file
  16. --disable-swscale
  17. --disable-avfilter
  18. --enable-network
  19. --disable-avdevice
  20. --enable-parsers
  21. --disable-decoders
  22. --disable-demuxers
  23. --enable-demuxer=matroska
  24. --disable-demuxer=rm
  25. --disable-demuxer=avi
  26. --disable-demuxer=flv
  27. --disable-demuxer=mov
  28. --enable-demuxer=mp3
  29. --enable-demuxer=mpegps
  30. --enable-demuxer=mpegvideo
  31. --enable-demuxer=asf
  32. --enable-decoder=aac
  33. --enable-decoder=ac3
  34. --enable-decoder=dca
  35. --enable-decoder=flv
  36. --disable-decoder=h263
  37. --disable-decoder=h264
  38. --enable-decoder=svq3
  39. --enable-decoder=amrnb
  40. --enable-decoder=nellymoser
  41. --enable-decoder=pcm_alaw
  42. --enable-decoder=pcm_dvd
  43. --enable-decoder=pcm_mulaw
  44. --enable-decoder=pcm_s16be
  45. --enable-decoder=pcm_s16le
  46. --enable-decoder=pcm_s16le_planar
  47. --enable-decoder=pcm_s24be
  48. --enable-decoder=pcm_s24daud
  49. --enable-decoder=pcm_s24le
  50. --enable-decoder=pcm_s32be
  51. --enable-decoder=pcm_s32le
  52. --enable-decoder=pcm_s8
  53. --enable-decoder=pcm_u16be
  54. --enable-decoder=pcm_u16le
  55. --enable-decoder=pcm_u24be
  56. --enable-decoder=pcm_u24le
  57. --enable-decoder=pcm_u32be
  58. --enable-decoder=pcm_u32le
  59. --enable-decoder=atrac3
  60. --enable-decoder=pcm_u8
  61. --enable-decoder=sipr
  62. --enable-decoder=mjpeg
  63. --enable-decoder=mp1
  64. --enable-decoder=mp2
  65. --enable-decoder=mp3
  66. --enable-decoder=tscc
  67. --enable-decoder=mpeg1video
  68. --enable-decoder=mpeg2video
  69. --enable-decoder=mpeg4
  70. --enable-decoder=ra_144
  71. --enable-decoder=ra_288
  72. --enable-decoder=cook
  73. --enable-decoder=rv10
  74. --enable-decoder=rv20
  75. --enable-decoder=rv30
  76. --enable-decoder=rv40
  77. --enable-decoder=wmav1
  78. --enable-decoder=wmav2
  79. --enable-decoder=wmapro
  80. --disable-decoder=wmv1
  81. --disable-decoder=wmv2
  82. --disable-decoder=wmv3
  83. --enable-decoder=vc1
  84. --enable-decoder=msmpeg4v1
  85. --enable-decoder=msmpeg4v2
  86. --enable-decoder=msmpeg4v3

You can configure the above parameters yourself as needed.

2. Create a config.sh script

Create a config.sh in the JNI directory with the following content:

Shellscript Code
  1. #!/bin/bash
  2. SOURCE config_common.sh/* Execute the above config_common.sh script here */
  3. CD libffmpeg/* Enter the LIBFFMPEG directory */
  4. prebuilt=/root/develop/android-ndk-r8/toolchains/arm-linux-androideabi-4.4. 3/prebuilt/linux-x86
  5. platform=/root/develop/android-ndk-r8/platforms/android-14/arch-arm
  6. ./configure--target-os=linux \
  7. --arch=arm \
  8. --enable-shared \
  9. --enable-cross-compile \
  10. --cc= $PREBUILT/BIN/ARM-LINUX-ANDROIDEABI-GCC \
  11. --cross-prefix= $PREBUILT/bin/arm-linux-androideabi-\
  12. --strip= $PREBUILT/bin/arm-linux-androideabi-strip \
  13. --extra-cflags="-fpic-dandroid" \
  14. --extra-ldflags="-wl,-t, $PREBUILT/arm-linux-androideabi/lib/ldscripts/armelf_linux_eabi.x-wl,-rpath-link=$ Platform/usr/lib-l$platform/usr/lib-nostdlib $PREBUILT/LIB/GCC/ARM-LINUX-ANDROIDEABI/4.4.3/CRTBEGIN.O $PREBUILT/ Lib/gcc/arm-linux-androideabi/4.4.3/crtend.o-lc-lm-ldl "\
  15. $FFCONFIG _common
  16. Cd.. /* Return to JNI directory */
  17. Ndk-build Clean

3. Execute the config.sh script

Then open the terminal and execute the following command:

$chmod +x config.sh/* Give config.sh Execute permission */
$./config.sh

If configured correctly, the last two lines shown are:

LICENSE:LGPL version 3 or later
Creating Config.mak and Config.h ...

If Warning:/root/develop/android-ndk-8/.............../arm-linux-androideabi-pkg-config not found appears, the library Detection may fail. can be ignored.

4. Modify Config.h

Enter the jni/libffmpeg/directory and find this sentence in the newly generated config.h:

#define Restrict Restrict

Because the Android GCC does not recognize the Restrict keyword, it is rewritten as:

#define Restrict

If you need to be networked, you also need to set the following definition in Config.h to 1:

#define Config_network 1
#define CONFIG_HTTP_PROTOCOL 1
#define CONFIG_HTTPPROXY_PROTOCOL 1
#define CONFIG_HTTPS_PROTOCOL 1
#define CONFIG_TCP_PROTOCOL 1
#define CONFIG_UDP_PROTOCOL 1
#define HAVE_STRUCT_SOCKADDR_STORAGE 1
#define HAVE_STRUCT_ADDRINFO 1
#define HAVE_SOCKLEN_T 1

#define Have_inet_aton 0

Accordingly, the Config.mak also needs to be modified to:

Config_network=yes
Config_http_protocol=yes
Config_httpproxy_protocol=yes
Config_https_protocol=yes
Config_tcp_protocol=yes
Config_udp_protocol=yes
Have_struct_sockaddr_storage=yes
Have_struct_addrinfo=yes
Have_socklen_t=yes

! Have_inet_aton=yes

5. Modify the Libm.h file

All of the static functions in the/libavutil/libm.h are commented out.

6. Modify the Makefile file

Modify the Libpostproc file in the Libavcodec, Libavfilter, Libavformat, Libavutil, Libswscale, and makefile directories and comment out the following sentence:

Include $ (subdir): /config.mak

7. Create a new AV.MK script

Under/jni/libffmpeg, create a script file av.mk with the following content:

# Local_path is one of Libavutil, Libavcodec, Libavformat, or Libswscale

#include $ (Local_path)/.. /config-$ (target_arch). Mak
Include $ (local_path)/. /config.mak

OBJS: =
Objs-yes: =
Mmx-objs-yes: =
Include $ (local_path)/makefile
Ifdef addition_inc
Include $ (addition_inc)
endif

# Collect objects
objs-$ (HAVE_MMX) + = $ (mmx-objs-yes)
OBJS + = $ (objs-yes)

Ffname: = lib$ (NAME)
Fflibs: = $ (foreach,name,$ (fflibs), lib$ (NAME))
Ffcflags =-dhave_av_config_h $ (CFLAGS)
Ffcflags + =-dtarget_config=\ "config-$ (target_arch). H\"

All_s_files: = $ (wildcard $ (local_path)/$ (target_arch)/*. S
All_s_files: = $ (Addprefix $ (target_arch)/, $ (Notdir $ (all_s_files)))

Ifneq ($ (all_s_files),)
ALL_S_OBJS: = $ (patsubst%). s,%.o,$ (All_s_files))
C_OBJS: = $ (Filter-out $ (ALL_S_OBJS), $ (OBJS))
S_OBJS: = $ (Filter $ (ALL_S_OBJS), $ (OBJS))
Else
C_OBJS: = $ (OBJS)
S_OBJS: =
endif

C_files: = $ (Patsubst%.o,%.c,$ (C_OBJS))
S_files: = $ (Patsubst%.o,%. s,$ (S_OBJS))

Fffiles: = $ (sort $ (s_files)) $ (sort $ (c_files))

8. Create a new ANDROID.MK script

Create a new android.mk in the JNI directory with the following content:

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_src_files: = ExportJNI.cpp ExportPlayer.cpp packetqueue.cpp/* where ExportJNI.cpp is the JNI interface I wrote */
Local_static_libraries: = Libavformat libavcodec libswscale libavutil libpostproc ffmpeg libsubtitle/* Note the order of LIBRARIES */
Local_module: = export/* final Generation libexport.so*/
Local_arm_mode: = ARM
Local_cflags: =-d__stdc_constant_macros-i$ (Local_path)/libffmpeg/-i$ (Local_path)/libsubtitle/
Local_ldlibs: =-llog
Include $ (build_shared_library)
Include $ (call all-makefiles-under,$ (Local_path))

Create a new android.mk under the Jni/libffmpeg directory:

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_src_files: = SETTINGS.C MyLog.cpp/* If you do not need auxiliary functions such as print output, this line can be deleted */
Local_module: = FFmpeg
Local_arm_mode: = ARM
Include $ (build_static_library)
Include $ (call all-makefiles-under,$ (Local_path))


Create a new android.mk under the Jni/libffmpeg/libavformat directory:

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Include $ (local_path)/. /av.mk
Local_src_files: = $ (fffiles)
Local_c_includes: =\
$ (Local_path) \
$ (Local_path)/..
Local_cflags + = $ (ffcflags)
Local_cflags + =-include "String.h"-dipv6mr_interface=ipv6mr_ifindex
Local_ldlibs: =-lz
Local_static_libraries: = $ (fflibs)
Local_module: = $ (Ffname)
Local_arm_mode: = ARM
Include $ (build_static_library)

Create a new android.mk under the Jni/libffmpeg/libavcodec directory:

Local_path: = $ (call My-dir)
Include $ (clear_vars)
Addition_inc: = $ (Local_path)/arm/makefile
Include $ (local_path)/. /av.mk
Addition_inc: =

Local_src_files: = $ (fffiles)

Local_c_includes: =\
$ (Local_path) \
$ (Local_path)/..
Local_cflags + = $ (ffcflags)
Ifeq ($ (Target_arch_abi), Armeabi)
Local_cflags + =-duse_int
endif

Local_ldlibs: =-lz
Local_static_libraries: = $ (fflibs)
Local_module: = $ (Ffname)
Local_arm_mode: = ARM
Include $ (build_static_library)

Create new android.mk in the Libavfilter, Libavutil, Libpostproc, and Libswscale directories:

Local_path:= $ (call My-dir)
Include $ (clear_vars)
include$ (Local_path)/. /av.mk
Local_src_files:= $ (fffiles)
local_c_includes:= \
$ (Local_path) \
$ (Local_path)/..
local_cflags+= $ (ffcflags)
Local_static_libraries:= $ (fflibs)
Local_module:= $ (ffname)
include$ (Build_static_library)

9. Create a new APPLICATION.MK script

Create a application.mk in the JNI directory with the following content:

App_stl: = stlport_static
App_abi: = Armeabi

Iii. NDK Compilation

Execute the Ndk-build under ffmpeg-0.11/:

$/ffmpeg-0.11# Ndk-build

The compilation process takes about 10 minutes, and finally generates LIBAVCODEC.A, LIBAVFORMAT.A, LIBAVUTIL.A, libffmpeg.a, ffmpeg-0.11/obj/local/armeabi/directory, Libswscale.a and LIBPOSTPROC.A, and libexport.so, generate libexport.so in the ffmpeg-0.11/libs/armeabi/directory.

Reprint: http://superonion.iteye.com/blog/1609777

FFmpeg Transplant Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.