From 266cda3c3b6079c2cfb2f1972d64710f08ff8273 Mon Sep 17 00:00:00 2001 From: Suyog Pawar Date: Tue, 17 Sep 2024 11:29:03 +0530 Subject: [PATCH] libavc : Enable support for MacOS Test: ./avcenc ./avcdec Change-Id: If03196cf979d7f6638b99d5b13afba6df3364178 --- CMakeLists.txt | 23 +++++++++++++++- README.md | 26 ++++++++++++++++++ cmake/toolchains/aarch32_toolchain.cmake | 4 +-- cmake/toolchains/aarch64_toolchain.cmake | 4 +-- cmake/toolchains/android_toolchain.cmake | 34 ++++++++++++++++++++++++ cmake/utils.cmake | 18 ++++++++++--- common/common.cmake | 4 +-- decoder/arm/ih264d_function_selector.c | 5 ++++ decoder/ih264d_dpb_mgr.c | 13 ++++++++- decoder/libavcdec.cmake | 4 +-- encoder/arm/ih264e_function_selector.c | 5 ++++ encoder/libavcenc.cmake | 4 +-- fuzzer/avc_dec_fuzzer.cmake | 4 ++- fuzzer/avc_enc_fuzzer.cmake | 4 ++- 14 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 cmake/toolchains/android_toolchain.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index d0c37d3a..a32ae8ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,17 @@ cmake_minimum_required(VERSION 3.9.1) project(libavc C CXX) -enable_language(ASM) + +if(NOT DEFINED SYSTEM_NAME) + set(SYSTEM_NAME ${CMAKE_HOST_SYSTEM_NAME}) +endif() + +if(NOT DEFINED SYSTEM_PROCESSOR) + set(SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR}) +endif() + +if(NOT "${SYSTEM_NAME}" STREQUAL "Darwin") + enable_language(ASM) +endif() set(AVC_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") set(AVC_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}") @@ -19,6 +30,16 @@ if("${AVC_ROOT}" STREQUAL "${AVC_CONFIG_DIR}") "And re-run CMake from the build directory.") endif() +if("${SYSTEM_NAME}" STREQUAL "Android") + find_library(log-lib log QUIET) + if(NOT log-lib) + message(FATAL_ERROR "Could NOT find log library, retry after installing \ + log library at sysroot.") + else() + message(STATUS "Found log-lib: ${log-lib}") + endif() +endif() + set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) diff --git a/README.md b/README.md index 5b0663ad..84665b84 100644 --- a/README.md +++ b/README.md @@ -51,3 +51,29 @@ $ make $ cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/aarch32_toolchain.cmake $ make ``` + +### Building for android +NOTE: This assumes that you are building on a machine that has + [Android NDK](https://developer.android.com/ndk/downloads). + +``` +$ cd external/libavc +$ mkdir build +$ cd build +``` + +#### Armv7 (32-bit) + + cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/android_toolchain.cmake\ + -DAVC_ANDROID_NDK_PATH=/opt/android-ndk-r26d/\ + -DANDROID_ABI=armeabi-v7a\ + -DANDROID_PLATFORM=android-23 ../ + make + +#### Armv8 (64-bit) + + cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/android_toolchain.cmake\ + -DAVC_ANDROID_NDK_PATH=/opt/android-ndk-r26d/\ + -DANDROID_ABI=arm64-v8a\ + -DANDROID_PLATFORM=android-23 ../ + make \ No newline at end of file diff --git a/cmake/toolchains/aarch32_toolchain.cmake b/cmake/toolchains/aarch32_toolchain.cmake index 5fabde2b..a5562230 100644 --- a/cmake/toolchains/aarch32_toolchain.cmake +++ b/cmake/toolchains/aarch32_toolchain.cmake @@ -1,5 +1,5 @@ -set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSTEM_PROCESSOR aarch32) +set(SYSTEM_NAME Linux) +set(SYSTEM_PROCESSOR aarch32) # Modify these variables with paths to appropriate compilers that can produce # armv7 targets diff --git a/cmake/toolchains/aarch64_toolchain.cmake b/cmake/toolchains/aarch64_toolchain.cmake index 5efbf77a..b5dab9d0 100644 --- a/cmake/toolchains/aarch64_toolchain.cmake +++ b/cmake/toolchains/aarch64_toolchain.cmake @@ -1,5 +1,5 @@ -set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSTEM_PROCESSOR aarch64) +set(SYSTEM_NAME Linux) +set(SYSTEM_PROCESSOR aarch64) # Modify these variables with paths to appropriate compilers that can produce # armv8 targets diff --git a/cmake/toolchains/android_toolchain.cmake b/cmake/toolchains/android_toolchain.cmake new file mode 100644 index 00000000..f725a049 --- /dev/null +++ b/cmake/toolchains/android_toolchain.cmake @@ -0,0 +1,34 @@ +set(SYSTEM_NAME Android) +set(CMAKE_SYSTEM_NAME Android) + +if(NOT ANDROID_PLATFORM) + set(ANDROID_PLATFORM android-23) +endif() + +# Choose target architecture with: +# -DANDROID_ABI={armeabi-v7a, arm64-v8a, x86, x86_64} +if(NOT ANDROID_ABI) + set(ANDROID_ABI arm64-v8a) +endif() + +if(ANDROID_ABI MATCHES "^armeabi") + set(SYSTEM_PROCESSOR aarch32) +else() + set(SYSTEM_PROCESSOR aarch64) +endif() + +# Toolchain files don't have access to cached variables: +# https://gitlab.kitware.com/cmake/cmake/issues/16170. Set an intermediate +# environment variable when loaded the first time. +if(AVC_ANDROID_NDK_PATH) + set(ENV{AVC_ANDROID_NDK_PATH} "${AVC_ANDROID_NDK_PATH}") +else() + set(AVC_ANDROID_NDK_PATH "$ENV{AVC_ANDROID_NDK_PATH}") +endif() + +if(NOT AVC_ANDROID_NDK_PATH) + message(FATAL_ERROR "AVC_ANDROID_NDK_PATH not set.") + return() +endif() + +include("${AVC_ANDROID_NDK_PATH}/build/cmake/android.toolchain.cmake") \ No newline at end of file diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 468e9d18..85f98ac5 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -3,9 +3,9 @@ set(CMAKE_C_STANDARD 90) # Adds compiler options for all targets function(libavc_add_compile_options) - if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") + if("${SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${SYSTEM_PROCESSOR}" STREQUAL "arm64") add_compile_options(-march=armv8-a) - elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32") + elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32") add_compile_options(-march=armv7-a -mfpu=neon) else() add_compile_options(-msse4.2 -mno-avx) @@ -34,9 +34,15 @@ endfunction() # Adds defintions for all targets function(libavc_add_definitions) - if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") + if("${SYSTEM_NAME}" STREQUAL "Darwin") + if("${SYSTEM_PROCESSOR}" STREQUAL "arm64") + add_definitions(-DARMV8 -DDARWIN -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC) + else() + add_definitions(-DX86 -DDARWIN -DDISABLE_AVX2 -DDEFAULT_ARCH=D_ARCH_X86_GENERIC) + endif() + elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch64") add_definitions(-DARMV8 -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC) - elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32") + elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32") add_definitions(-DARMV7 -DDEFAULT_ARCH=D_ARCH_ARM_A9Q) else() add_definitions(-DX86 -DX86_LINUX=1 -DDISABLE_AVX2 @@ -83,6 +89,10 @@ function(libavc_add_executable NAME LIB) add_dependencies(${NAME} ${LIB} ${ARG_LIBS}) target_link_libraries(${NAME} ${LIB} ${ARG_LIBS}) + if("${SYSTEM_NAME}" STREQUAL "Android") + target_link_libraries(${NAME} ${log-lib}) + endif() + if(ARG_FUZZER) target_compile_options(${NAME} PRIVATE $<$:-std=c++17>) diff --git a/common/common.cmake b/common/common.cmake index 47ca54b4..4b3e8bbf 100644 --- a/common/common.cmake +++ b/common/common.cmake @@ -27,7 +27,7 @@ include_directories(${AVC_ROOT}/common) include_directories(${AVC_ROOT}/common/mvc) # arm/x86 sources -if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") +if("${SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${SYSTEM_PROCESSOR}" STREQUAL "arm64") list( APPEND LIBAVC_COMMON_ASMS @@ -59,7 +59,7 @@ if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") include_directories(${AVC_ROOT}/common/arm) include_directories(${AVC_ROOT}/common/armv8) -elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch32") +elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32") list( APPEND LIBAVC_COMMON_ASMS diff --git a/decoder/arm/ih264d_function_selector.c b/decoder/arm/ih264d_function_selector.c index 925043bc..d111973c 100644 --- a/decoder/arm/ih264d_function_selector.c +++ b/decoder/arm/ih264d_function_selector.c @@ -64,8 +64,13 @@ void ih264d_init_function_ptr(dec_struct_t *ps_codec) #if defined(ARMV8) case ARCH_ARMV8_GENERIC: default: +#ifdef DARWIN + ih264d_init_function_ptr_generic(ps_codec); + break; +#else ih264d_init_function_ptr_av8(ps_codec); break; +#endif #elif !defined(DISABLE_NEON) case ARCH_ARM_A5: case ARCH_ARM_A7: diff --git a/decoder/ih264d_dpb_mgr.c b/decoder/ih264d_dpb_mgr.c index f5974ba0..0fa62c37 100644 --- a/decoder/ih264d_dpb_mgr.c +++ b/decoder/ih264d_dpb_mgr.c @@ -18,7 +18,7 @@ * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore */ #ifdef __ANDROID__ -#include +#include #endif #include "ih264_typedefs.h" #include "ih264_macros.h" @@ -39,6 +39,17 @@ #include "ih264_buf_mgr.h" #include "assert.h" +#ifdef __ANDROID__ +#ifndef ALOG +#define ALOG(priority, tag, ...) ((void)__android_log_print(ANDROID_##priority, tag, __VA_ARGS__)) +#define ALOGE(...) ALOG(LOG_ERROR, NULL, __VA_ARGS__) +inline int android_errorWriteLog(int tag, const char* subTag) { + ALOGE("android_errorWriteLog(%x, %s)", tag, subTag); + return 0; +} +#endif +#endif + /*! *************************************************************************** * \file ih264d_dpb_mgr.c diff --git a/decoder/libavcdec.cmake b/decoder/libavcdec.cmake index 911c3281..1b72dd56 100644 --- a/decoder/libavcdec.cmake +++ b/decoder/libavcdec.cmake @@ -36,8 +36,8 @@ list( include_directories(${AVC_ROOT}/decoder) -if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${CMAKE_SYSTEM_PROCESSOR}" - STREQUAL "aarch32") +if("${SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${SYSTEM_PROCESSOR}" STREQUAL "arm64" + OR "${SYSTEM_PROCESSOR}" STREQUAL "aarch32") list( APPEND LIBAVCDEC_ASMS "${AVC_ROOT}/decoder/arm/ih264d_function_selector.c" "${AVC_ROOT}/decoder/arm/ih264d_function_selector_a9q.c" diff --git a/encoder/arm/ih264e_function_selector.c b/encoder/arm/ih264e_function_selector.c index 2b155cac..fb57e384 100644 --- a/encoder/arm/ih264e_function_selector.c +++ b/encoder/arm/ih264e_function_selector.c @@ -119,8 +119,13 @@ void ih264e_init_function_ptr(void *pv_codec) case ARCH_ARM_A57: case ARCH_ARM_V8_NEON: default: +#ifdef DARWIN + ih264e_init_function_ptr_generic(ps_codec); + break; +#else ih264e_init_function_ptr_neon_av8(ps_codec); break; +#endif #elif !defined(DISABLE_NEON) case ARCH_ARM_A9Q: case ARCH_ARM_A9A: diff --git a/encoder/libavcenc.cmake b/encoder/libavcenc.cmake index cf7be9cf..a8b525c7 100644 --- a/encoder/libavcenc.cmake +++ b/encoder/libavcenc.cmake @@ -44,7 +44,7 @@ list( include_directories(${AVC_ROOT}/encoder) -if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") +if("${SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${SYSTEM_PROCESSOR}" STREQUAL "arm64") list( APPEND LIBAVCENC_ASMS @@ -57,7 +57,7 @@ if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") "${AVC_ROOT}/encoder/armv8/ime_distortion_metrics_av8.s") include_directories(${AVC_ROOT}/encoder/armv8) -elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32") +elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32") list( APPEND LIBAVCENC_ASMS diff --git a/fuzzer/avc_dec_fuzzer.cmake b/fuzzer/avc_dec_fuzzer.cmake index c20aaed7..2d8fc3ca 100644 --- a/fuzzer/avc_dec_fuzzer.cmake +++ b/fuzzer/avc_dec_fuzzer.cmake @@ -1,2 +1,4 @@ -libavc_add_fuzzer(avc_dec_fuzzer libavcdec SOURCES +if(NOT "${SYSTEM_NAME}" STREQUAL "Darwin") + libavc_add_fuzzer(avc_dec_fuzzer libavcdec SOURCES ${AVC_ROOT}/fuzzer/avc_dec_fuzzer.cpp) +endif() \ No newline at end of file diff --git a/fuzzer/avc_enc_fuzzer.cmake b/fuzzer/avc_enc_fuzzer.cmake index d6cb5516..89e70faf 100644 --- a/fuzzer/avc_enc_fuzzer.cmake +++ b/fuzzer/avc_enc_fuzzer.cmake @@ -1,2 +1,4 @@ -libavc_add_fuzzer(avc_enc_fuzzer libavcenc SOURCES +if(NOT "${SYSTEM_NAME}" STREQUAL "Darwin") + libavc_add_fuzzer(avc_enc_fuzzer libavcenc SOURCES ${AVC_ROOT}/fuzzer/avc_enc_fuzzer.cpp) +endif() \ No newline at end of file