Skip to content

Commit

Permalink
libmpeg2: Enable support for MacOS
Browse files Browse the repository at this point in the history
Test: ./mpeg2dec

Change-Id: If03196cf979d7f6638b99d5b13afba6df3367089
  • Loading branch information
sushant191k authored and harishdm committed Oct 18, 2024
1 parent c8de54c commit 93504b7
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 39 deletions.
118 changes: 91 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
cmake_minimum_required(VERSION 3.9.1)
project(libmpeg2 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(MPEG2_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
set(MPEG2_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}")
Expand All @@ -16,6 +27,16 @@ if("${MPEG2_ROOT}" STREQUAL "${MPEG2_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 ON)
find_package(Threads REQUIRED)
set(CMAKE_STATIC_LIBRARY_PREFIX "")
Expand Down Expand Up @@ -59,35 +80,78 @@ list(APPEND LIBMPEG2DEC_SRCS
"${MPEG2_ROOT}/common/ideint_function_selector_generic.c"
"${MPEG2_ROOT}/common/ideint_utils.c")

list(APPEND LIBMPEG2DEC_X86_SRCS
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector_avx2.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector_ssse3.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector_sse42.c"
"${MPEG2_ROOT}/common/x86/ideint_function_selector.c"
"${MPEG2_ROOT}/common/x86/ideint_function_selector_ssse3.c"
"${MPEG2_ROOT}/common/x86/ideint_function_selector_sse42.c"
"${MPEG2_ROOT}/common/x86/icv_variance_ssse3.c"
"${MPEG2_ROOT}/common/x86/icv_sad_ssse3.c"
"${MPEG2_ROOT}/common/x86/ideint_cac_ssse3.c"
"${MPEG2_ROOT}/common/x86/ideint_spatial_filter_ssse3.c"
"${MPEG2_ROOT}/common/x86/impeg2_idct_recon_sse42_intr.c"
"${MPEG2_ROOT}/common/x86/impeg2_inter_pred_sse42_intr.c"
"${MPEG2_ROOT}/common/x86/impeg2_mem_func_sse42_intr.c")

set(LIBMPEG2DEC_INCLUDES ${MPEG2_ROOT}/common ${MPEG2_ROOT}/decoder)

set(LIBMPEG2DEC_X86_C_FLAGS "-DX86 -DDISABLE_AVX2 -msse4.2 -mno-avx \
-DDEFAULT_ARCH=D_ARCH_X86_SSE42 -DMULTICORE")
set(LIBMPEG2DEC_X86_INCLUDES ${MPEG2_ROOT}/common/x86 ${MPEG2_ROOT}/decoder/x86)
set(LIBMPEG2DEC_C_FLAGS "${LIBMPEG2DEC_X86_C_FLAGS}")

include_directories(${LIBMPEG2DEC_INCLUDES} ${LIBMPEG2DEC_X86_INCLUDES})
add_library(libmpeg2dec ${LIBMPEG2DEC_SRCS} ${LIBMPEG2DEC_X86_SRCS})
include_directories(${MPEG2_ROOT}/common)
include_directories(${MPEG2_ROOT}/decoder)

if("${SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${SYSTEM_PROCESSOR}" STREQUAL "arm64")
list(APPEND LIBMPEG2DEC_SRCS
"${MPEG2_ROOT}/common/arm/ideint_function_selector.c"
"${MPEG2_ROOT}/common/arm/ideint_function_selector_av8.c"
"${MPEG2_ROOT}/common/armv8/icv_sad_av8.s"
"${MPEG2_ROOT}/common/armv8/icv_variance_av8.s"
"${MPEG2_ROOT}/common/armv8/ideint_cac_av8.s"
"${MPEG2_ROOT}/common/armv8/ideint_spatial_filter_av8.s"
"${MPEG2_ROOT}/common/armv8/impeg2_format_conv.s"
"${MPEG2_ROOT}/common/armv8/impeg2_idct.s"
"${MPEG2_ROOT}/common/armv8/impeg2_inter_pred.s"
"${MPEG2_ROOT}/common/armv8/impeg2_mem_func.s"
"${MPEG2_ROOT}/common/armv8/impeg2_neon_macros.s"
"${MPEG2_ROOT}/decoder/arm/impeg2d_function_selector.c"
"${MPEG2_ROOT}/decoder/arm/impeg2d_function_selector_av8.c")
include_directories(${MPEG2_ROOT}/common/armv8)
include_directories(${MPEG2_ROOT}/common/arm)

elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32")
list(APPEND LIBMPEG2DEC_SRCS
"${MPEG2_ROOT}/common/arm/ideint_function_selector.c"
"${MPEG2_ROOT}/decoder/arm/impeg2d_function_selector.c"
"${MPEG2_ROOT}/common/arm/icv_sad_a9.s"
"${MPEG2_ROOT}/common/arm/icv_variance_a9.s"
"${MPEG2_ROOT}/common/arm/ideint_cac_a9.s"
"${MPEG2_ROOT}/common/arm/ideint_function_selector_a9.c"
"${MPEG2_ROOT}/common/arm/ideint_spatial_filter_a9.s"
"${MPEG2_ROOT}/common/arm/impeg2_format_conv.s"
"${MPEG2_ROOT}/common/arm/impeg2_idct.s"
"${MPEG2_ROOT}/common/arm/impeg2_inter_pred.s"
"${MPEG2_ROOT}/common/arm/impeg2_mem_func.s"
"${MPEG2_ROOT}/decoder/arm/impeg2d_function_selector_a9q.c")

include_directories(${MPEG2_ROOT}/common/arm)
include_directories(${MPEG2_ROOT}/decoder/arm)

else()
list(APPEND LIBMPEG2DEC_SRCS
"${MPEG2_ROOT}/common/x86/icv_sad_ssse3.c"
"${MPEG2_ROOT}/common/x86/icv_variance_ssse3.c"
"${MPEG2_ROOT}/common/x86/ideint_cac_ssse3.c"
"${MPEG2_ROOT}/common/x86/ideint_function_selector.c"
"${MPEG2_ROOT}/common/x86/ideint_function_selector_sse42.c"
"${MPEG2_ROOT}/common/x86/ideint_function_selector_ssse3.c"
"${MPEG2_ROOT}/common/x86/ideint_spatial_filter_ssse3.c"
"${MPEG2_ROOT}/common/x86/impeg2_idct_recon_sse42_intr.c"
"${MPEG2_ROOT}/common/x86/impeg2_inter_pred_sse42_intr.c"
"${MPEG2_ROOT}/common/x86/impeg2_mem_func_sse42_intr.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector_avx2.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector_sse42.c"
"${MPEG2_ROOT}/decoder/x86/impeg2d_function_selector_ssse3.c")

include_directories(${MPEG2_ROOT}/common/x86)
include_directories(${MPEG2_ROOT}/decoder/x86)
set(LIBMPEG2DEC_C_FLAGS "${LIBMPEG2DEC_X86_C_FLAGS}")

endif()

include_directories(${LIBMPEG2DEC_INCLUDES})

add_library(libmpeg2dec ${LIBMPEG2DEC_SRCS})

set_target_properties(libmpeg2dec
PROPERTIES COMPILE_FLAGS "${LIBMPEG2DEC_C_FLAGS}")

libmpeg2_add_executable(mpeg2dec libmpeg2dec SOURCES ${MPEG2_ROOT}/test/decoder/main.c)
target_compile_definitions(mpeg2dec PRIVATE PROFILE_ENABLE MD5_DISABLE)

libmpeg2_add_fuzzer(mpeg2_dec_fuzzer libmpeg2dec SOURCES ${MPEG2_ROOT}/fuzzer/mpeg2_dec_fuzzer.cpp)
if(NOT "${SYSTEM_NAME}" STREQUAL "Darwin")
libmpeg2_add_fuzzer(mpeg2_dec_fuzzer libmpeg2dec SOURCES ${MPEG2_ROOT}/fuzzer/mpeg2_dec_fuzzer.cpp)
endif()
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# LIBMPEG2
## Getting Started Document

# LibMPEG2 build steps

Supports:
- aarch32/aarch64 on Linux.
- aarch32/aarch64 on Android.
- x86_32/x86_64 on Linux.
- aarch64 on Mac.
- x86_64 on Mac.

## Native Builds
Use the following commands for building on the target machine

```
$ cd external/libmpeg2
$ mkdir build
$ cd build
$ cmake ..
$ make
```

## Cross-compiler based builds

### Building for x86_32 on a x86_64 Linux machine
```
$ cd external/libmpeg2
$ mkdir build
$ cd build
$ CFLAGS="-m32" CXXFLAGS="-m32" LDFLAGS="-m32" cmake ..
$ make
```

### Building for aarch32/aarch64
Update 'CMAKE_C_COMPILER', 'CMAKE_CXX_COMPILER', 'CMAKE_C_COMPILER_AR', and
'CMAKE_CXX_COMPILER_AR' in CMAKE_TOOLCHAIN_FILE passed below

```
$ cd external/libmpeg2
$ mkdir build
$ cd build
```

#### For aarch64
```
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/aarch64_toolchain.cmake
$ make
```

#### For aarch32
```
$ 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/libmpeg2
$ mkdir build
$ cd build
```

#### Armv7 (32-bit)

cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/android_toolchain.cmake\
-DMPEG2_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\
-DMPEG2_ANDROID_NDK_PATH=/opt/android-ndk-r26d/\
-DANDROID_ABI=arm64-v8a\
-DANDROID_PLATFORM=android-23 ../
make
7 changes: 7 additions & 0 deletions cmake/toolchains/aarch32_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(SYSTEM_NAME Linux)
set(SYSTEM_PROCESSOR aarch32)

# Modify these variables with paths to appropriate compilers that can produce
# armv7 targets
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
13 changes: 13 additions & 0 deletions cmake/toolchains/aarch64_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(SYSTEM_NAME Linux)
set(SYSTEM_PROCESSOR aarch64)

# Modify these variables with paths to appropriate compilers that can produce
# armv8 targets
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)
set(CMAKE_C_COMPILER_AR
aarch64-linux-gnu-gcc-ar
CACHE FILEPATH "Archiver")
set(CMAKE_CXX_COMPILER_AR
aarch64-linux-gnu-gcc-ar
CACHE FILEPATH "Archiver")
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(MPEG2_ANDROID_NDK_PATH)
set(ENV{MPEG2_ANDROID_NDK_PATH} "${MPEG2_ANDROID_NDK_PATH}")
else()
set(MPEG2_ANDROID_NDK_PATH "$ENV{MPEG2_ANDROID_NDK_PATH}")
endif()

if(NOT MPEG2_ANDROID_NDK_PATH)
message(FATAL_ERROR "MPEG2_ANDROID_NDK_PATH not set.")
return()
endif()

include("${MPEG2_ANDROID_NDK_PATH}/build/cmake/android.toolchain.cmake")
26 changes: 19 additions & 7 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
include(CheckCXXCompilerFlag)
set(CMAKE_C_STANDARD 90)

# Adds compiler options for all targets
function(libmpeg2_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 @@ -32,13 +33,20 @@ endfunction()

# Adds defintions for all targets
function(libmpeg2_add_definitions)
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
add_definitions(-DARMV8 -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC -DENABLE_NEON)
elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch32")
add_definitions(-DARMV7 -DDEFAULT_ARCH=D_ARCH_ARM_A9Q -DENABLE_NEON)
if("${SYSTEM_NAME}" STREQUAL "Darwin")
if(${SYSTEM_PROCESSOR} STREQUAL "arm64")
add_definitions(-DARMV8 -DDARWIN -DDEFAULT_ARCH_ARMV8_GENERIC -DMULTICORE)
else()
add_definitions(-DX86 -DDARWIN -DDISABLE_AVX2
-DDEFAULT_ARCH=D_ARCH_X86_GENERIC -DMULTICORE)
endif()
elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch64")
add_definitions(-DARMV8 -DDEFAULT_ARCH=D_ARCH_ARMV8_GENERIC -DENABLE_NEON -DMULTICORE)
elseif("${SYSTEM_PROCESSOR}" STREQUAL "aarch32")
add_definitions(-DARMV7 -DDEFAULT_ARCH=D_ARCH_ARM_A9Q -DENABLE_NEON -DMULTICORE)
else()
add_definitions(-DX86 -DX86_LINUX=1 -DDISABLE_AVX2
-DDEFAULT_ARCH=D_ARCH_X86_SSE42)
-DDEFAULT_ARCH=D_ARCH_X86_SSE42 -DMULTICORE)
endif()
endfunction()

Expand Down Expand Up @@ -81,6 +89,10 @@ function(libmpeg2_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
5 changes: 5 additions & 0 deletions common/arm/ideint_function_selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ void ideint_init_function_ptr(ctxt_t *ps_ctxt)
{
#if defined(ARMV8)
default:
#ifdef DARWIN
ideint_init_function_ptr_generic(ps_ctxt);
break;
#else
ideint_init_function_ptr_av8(ps_ctxt);
break;
#endif
#elif !defined(DISABLE_NEON)
case ICV_ARM_NONEON:
break;
Expand Down
8 changes: 4 additions & 4 deletions common/arm/impeg2_format_conv.s
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ yuv420sp_uv_chroma:
ldr r3, [sp, #24] @// Load pu1_dest_uv from stack

ldr r4, [sp, #28] @// Load u2_height from stack
add r4, r4, 1
add r4, r4, #1

ldr r5, [sp, #32] @// Load u2_width from stack
add r5, r5, 1
add r5, r5, #1
bic r5, r5, #1

ldr r7, [sp, #40] @// Load u2_strideu from stack
Expand Down Expand Up @@ -334,10 +334,10 @@ yuv420sp_vu_chroma:
ldr r3, [sp, #24] @// Load pu1_dest_uv from stack

ldr r4, [sp, #28] @// Load u2_height from stack
add r4, r4, 1
add r4, r4, #1

ldr r5, [sp, #32] @// Load u2_width from stack
add r5, r5, 1
add r5, r5, #1
bic r5, r5, #1

ldr r7, [sp, #40] @// Load u2_strideu from stack
Expand Down
5 changes: 5 additions & 0 deletions decoder/arm/impeg2d_function_selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ void impeg2d_init_function_ptr(void *pv_codec)
#if defined(ARMV8)
case ARCH_ARMV8_GENERIC:
default:
#ifdef DARWIN
impeg2d_init_function_ptr_generic(ps_codec);
break;
#else
impeg2d_init_function_ptr_av8(ps_codec);
break;
#endif
#elif !defined(DISABLE_NEON)
case ARCH_ARM_A5:
case ARCH_ARM_A7:
Expand Down
Loading

0 comments on commit 93504b7

Please sign in to comment.