libyuv 是谷歌开源的对 YUV 数据进行缩放、旋转和转换的跨平台库,可在 Windows、Linux、Mac、Android 等操作系统,x86、x64、arm 架构上进行编译运行,支持 AVX2、NEON 等 SIMD 指令加速。

官方介绍的几个功能:

  • 可以使用 point,bilinear 或 box 三种方法进行 YUV 的压缩
  • 可以转换为 RGB 来实现渲染/特效
  • 旋转 90/180/270 的角度以适配设备的竖屏模式
  • 可以把 webcam 转换为 YUV 来进行压缩
  • 还有一系列的平台性能优化等

接下来让我们编译 libyuv,编译步骤在 Windows 和 Mac 上都验证成功,下面以 Mac 为例。

1. clone libyuv 源码

既然要编译库,怎么少得了源码呢?

git clone https://chromium.googlesource.com/libyuv/libyuv

2. 重命名文件夹

clone 的文件夹叫做 libyuv,这里要把它修改为 jni。

3. 修改 Android.mk

注释 jpeg 相关的代码,其他地方不用改动。

fix-jpeg1
fix-jpeg2
fix-jpeg3

下面是修改后的 Android.mk 文件内容:

# This is the Android makefile for libyuv for NDK.
LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_CPP_EXTENSION := .cc

LOCAL_SRC_FILES := \
    source/compare.cc           \
    source/compare_common.cc    \
    source/compare_gcc.cc       \
    source/compare_mmi.cc       \
    source/compare_msa.cc       \
    source/compare_neon.cc      \
    source/compare_neon64.cc    \
    source/convert.cc           \
    source/convert_argb.cc      \
    source/convert_from.cc      \
    source/convert_from_argb.cc \
    source/convert_to_argb.cc   \
    source/convert_to_i420.cc   \
    source/cpu_id.cc            \
    source/planar_functions.cc  \
    source/rotate.cc            \
    source/rotate_any.cc        \
    source/rotate_argb.cc       \
    source/rotate_common.cc     \
    source/rotate_gcc.cc        \
    source/rotate_mmi.cc        \
    source/rotate_msa.cc        \
    source/rotate_neon.cc       \
    source/rotate_neon64.cc     \
    source/row_any.cc           \
    source/row_common.cc        \
    source/row_gcc.cc           \
    source/row_mmi.cc           \
    source/row_msa.cc           \
    source/row_neon.cc          \
    source/row_neon64.cc        \
    source/scale.cc             \
    source/scale_any.cc         \
    source/scale_argb.cc        \
    source/scale_common.cc      \
    source/scale_gcc.cc         \
    source/scale_mmi.cc         \
    source/scale_msa.cc         \
    source/scale_neon.cc        \
    source/scale_neon64.cc      \
    source/video_common.cc

common_CFLAGS := -Wall -fexceptions
# ifneq ($(LIBYUV_DISABLE_JPEG), "yes")
# LOCAL_SRC_FILES += \
#     source/convert_jpeg.cc      \
#     source/mjpeg_decoder.cc     \
#     source/mjpeg_validate.cc
# common_CFLAGS += -DHAVE_JPEG
# LOCAL_SHARED_LIBRARIES := libjpeg
# endif

LOCAL_CFLAGS += $(common_CFLAGS)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include

LOCAL_MODULE := libyuv_static
LOCAL_MODULE_TAGS := optional

include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_WHOLE_STATIC_LIBRARIES := libyuv_static
# 生成库的名称
LOCAL_MODULE := libyuv
# ifneq ($(LIBYUV_DISABLE_JPEG), "yes")
# LOCAL_SHARED_LIBRARIES := libjpeg
# endif

# 生成静态库还是动态库
# BUILD_STATIC_LIBRSRY 静态库
# BUILD_SHARED_LIBRARY 动态库
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libyuv_static
# LOCAL_SHARED_LIBRARIES := libjpeg
LOCAL_MODULE_TAGS := tests
LOCAL_CPP_EXTENSION := .cc
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
    unit_test/unit_test.cc        \
    unit_test/basictypes_test.cc  \
    unit_test/color_test.cc       \
    unit_test/compare_test.cc     \
    unit_test/convert_test.cc     \
    unit_test/cpu_test.cc         \
    unit_test/cpu_thread_test.cc  \
    unit_test/math_test.cc        \
    unit_test/planar_test.cc      \
    unit_test/rotate_argb_test.cc \
    unit_test/rotate_test.cc      \
    unit_test/scale_argb_test.cc  \
    unit_test/scale_test.cc       \
    unit_test/video_common_test.cc

LOCAL_MODULE := libyuv_unittest
include $(BUILD_NATIVE_TEST)

4. 新建 Application.mk

在 jni 文件夹下,新建 Application.mk 文件,指定架构平台和 Android 版本,内容如下:

APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
APP_PLATFORM := android-18

5. ndk-build 编译

编译前要确保本机配置好了 NDK 环境变量,如果不知道怎么做请自行百度。

cd 到 jni 文件夹的父级文件夹,然后执行 ndk 相关的命令。每次编译前都要先执行 ndk-build clean 清理缓存,最后执行 ndk-build 编译库,在 jni 父文件夹下就会出现两个文件夹: libs 文件夹下是动态库,obj 文件夹下是静态库。

ndk-build

参考文章: