Compiling libjpeg turbo of Android environment using cmake

Posted by AffApprentice on Fri, 03 Jan 2020 15:57:59 +0100

1. libjpeg-turbo

   compiled 1.5.9 before [1] Versions libjpeg-turbo , now upgrade to 2.0.0 and compile based on CMake.

Still, according to the official website, libjpeg turbo is 2-6 times faster than libjpeg, thanks to its highly optimized Huffman algorithm. In many cases, the performance of libjpeg turbo is comparable to the proprietary high-speed JPEG codec

On such systems, libjpeg-turbo is generally 2-6x as fast as libjpeg, all else being equal. On other types of systems, libjpeg-turbo can still outperform libjpeg by a significant amount, by virtue of its highly-optimized Huffman coding routines. In many cases, the performance of libjpeg-turbo rivals that of proprietary high-speed JPEG codecs

2. Compilation process

2.1 compilation environment

Compile in 'Ubuntu' on virtual machine VM of Windows 10

ubuntu

2.2 compilation requirements

We will compile with reference to BUILDING.md in the source code, github address BUILDING.md

NDK
: Linux 64 bit (x86) / android-ndk-r15c

CMake
: cmake-3.12.1-Linux-x86_64

NASM or YASM
: if you need to compile x86 or x86-64 libraries, you need to install NASM 2.10 or more or YASM 2.10 and above.

tool

2.3 configure environment variables

It is not necessary to configure Cmake into the system environment variable.


env.jpg

2.4 compilation

2.4.1 download source code

Download and extract the source code https://github.com/libjpeg-turbo/libjpeg-turbo

2.4.2 compile script

# lib-name
MY_LIBS_NAME=libjpeg-turbo
MY_SOURCE_DIR=$(pwd)/libjpeg-turbo-master
#The storage directory of middleware generated during compilation. In order to distinguish compilation directory, source directory and install directory
MY_BUILD_DIR=binary

export PATH=/home/as/Android/cmake-3.12.1-Linux-x86_64/bin:$PATH

NDK_PATH=/home/as/Android/android-ndk-r15c
BUILD_PLATFORM=linux-x86_64
TOOLCHAIN_VERSION=4.9
ANDROID_VERSION=24

ANDROID_ARMV5_CFLAGS="-march=armv5te"
ANDROID_ARMV7_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"  # -mfpu=vfpv3-d16  -fexceptions -frtti
ANDROID_ARMV8_CFLAGS="-march=armv8-a"   # -mfloat-abi=softfp -mfpu=neon -fexceptions -frtti
ANDROID_X86_CFLAGS="-march=i386 -mtune=intel -mssse3 -mfpmath=sse -m32"
ANDROID_X86_64_CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"

# params($1:arch,$2:arch_abi,$3:host,$4:compiler,$5:cflags,$6:processor)
build_bin() {

    echo "-------------------star build $2-------------------------"

    ARCH=$1                # arm arm64 x86 x86_64
    ANDROID_ARCH_ABI=$2    # armeabi armeabi-v7a x86 mips
    # Final compiled installation directory
    PREFIX=$(pwd)/dist/${MY_LIBS_NAME}/${ANDROID_ARCH_ABI}/
    HOST=$3
    COMPILER=$4
    PROCESSOR=$6
    SYSROOT=${NDK_PATH}/platforms/android-${ANDROID_VERSION}/arch-${ARCH}
    CFALGS="$5"
    TOOLCHAIN=${NDK_PATH}/toolchains/${HOST}-${TOOLCHAIN_VERSION}/prebuilt/${BUILD_PLATFORM}
    
    # build Middleware
    BUILD_DIR=./${MY_BUILD_DIR}/${ANDROID_ARCH_ABI}

    export CFLAGS="$5 -O3 -D__ANDROID_API__=${ANDROID_VERSION} --sysroot=${SYSROOT} \
                   -isystem ${NDK_PATH}/sysroot/usr/include \
                   -isystem ${NDK_PATH}/sysroot/usr/include/${HOST} "
    export LDFLAGS=-pie

    echo "path==>$PATH"
    echo "build_dir==>$BUILD_DIR"
    echo "ARCH==>$ARCH"
    echo "ANDROID_ARCH_ABI==>$ANDROID_ARCH_ABI"
    echo "HOST==>$HOST"
    echo "CFALGS==>$CFALGS"
    echo "COMPILER==>$COMPILER-gcc"
    echo "PROCESSOR==>$PROCESSOR"

    mkdir -p ${BUILD_DIR}   #Create the compilation directory of the current arch GUI, such as binary/armeabi-v7a
    cd ${BUILD_DIR}         #Here is the 2-level compilation directory of the current arch GUI


cat >toolchain.cmake << EOF 
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR $6)
set(CMAKE_C_COMPILER ${TOOLCHAIN}/bin/${COMPILER}-gcc)
set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN}/${COMPILER})
EOF # An error will be reported if the script is not written at the top

    cmake -G"Unix Makefiles" \
          -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake \
          -DCMAKE_POSITION_INDEPENDENT_CODE=1 \
          -DCMAKE_INSTALL_PREFIX=${PREFIX} \
          -DWITH_JPEG8=1 \
          ${MY_SOURCE_DIR}

    make clean
    make
    make install

    #Jump out of the current arch ABCD compilation directory, corresponding to the above CD ${build \ dir}, so that the function can be executed multiple times
    cd ../../

    echo "-------------------$2 build end-------------------------"
}

# build armeabi
build_bin arm armeabi arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV5_CFLAGS" arm

#build armeabi-v7a
build_bin arm armeabi-v7a arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV7_CFLAGS" arm

#build arm64-v8a
build_bin arm64 arm64-v8a aarch64-linux-android aarch64-linux-android "$ANDROID_ARMV8_CFLAGS" aarch64

#build x86
build_bin x86 x86 x86 i686-linux-android "$ANDROID_X86_CFLAGS" i386

#build x86_64
build_bin x86_64 x86_64 x86_64 x86_64-linux-android "$ANDROID_X86_64_CFLAGS" x86_64

2.4.3 execute compilation script

sudo sh build.sh

x86_64.jpg

2.4.4 possible problems in compilation

  1. Syntax error: end of file unexpected (expecting "}")
    Cat < < EOF > file name and content EOF in compilation script. It is better to write in the top case, otherwise the above error may occur

  2. Unrecognized cflags parameter
    Some cflags parameters may lead to compilation recognition. Modify the parameters with compilation errors

2.4.5 install results

dist/libjpeg-turbo

dist/libjpeg-turbo/arm64-v8a

dist/libjpeg-turbo/arm64-v8a/lib
  1. Compiling libjpeg turbo for Android

Topics: Linux Android cmake Ubuntu