Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libavc : Enable support for MacOS #106

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
Expand All @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions cmake/toolchains/aarch32_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmake/toolchains/aarch64_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 34 additions & 0 deletions cmake/toolchains/android_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -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")
18 changes: 14 additions & 4 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 $<$<COMPILE_LANGUAGE:CXX>:-std=c++17>)
Expand Down
4 changes: 2 additions & 2 deletions common/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions decoder/arm/ih264d_function_selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion decoder/ih264d_dpb_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#ifdef __ANDROID__
#include <log/log.h>
#include <android/log.h>
#endif
#include "ih264_typedefs.h"
#include "ih264_macros.h"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions decoder/libavcdec.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions encoder/arm/ih264e_function_selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions encoder/libavcenc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion fuzzer/avc_dec_fuzzer.cmake
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion fuzzer/avc_enc_fuzzer.cmake
Original file line number Diff line number Diff line change
@@ -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()
Loading