# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Contributor: Jeff Han <hanjinfei@foxmail.com>, DongZhengDong <dzhengdong10@gmail.com>, wupingyuan <wupingyuan2@h-partners.com>
# Maintainer: Jeff Han <hanjinfei@foxmail.com>

pkgname=FFmpeg_n4.3.8
pkgver=n4.3.8
pkgrel=0
pkgdesc="FFmpeg is a collection of libraries and tools to process multimedia content such as audio, video, subtitles and related metadata."
url="https://github.com/FFmpeg/FFmpeg/"
archs=("armeabi-v7a" "arm64-v8a" "x86_64")
license=("GPL-2.0" "GPL-3.0" "LGPL-3.0" "LGPL-2.1")
depends=("rtmpdump" "openssl_1_0_2u" "lame" "openh264")
makedepends=()
source="https://gitee.com/mirrors/ffmpeg/repository/archive/$pkgver.zip"

autounpack=true
downloadpackage=true
buildtools="configure"

builddir=${pkgname:0:6}-${pkgver}
packagename=$builddir.zip
patchflag=true
source envset.sh
buildhost=true
arch=
ldflags=

prepare() {
    mv ffmpeg-${pkgver} $builddir
    if $patchflag
    then
        cd $builddir
        patch -p1 < $PKGBUILD_ROOT/0001-CVE-2025-0518.patch
        patch -p1 < $PKGBUILD_ROOT/0002-CVE-2023-6605.patch
        patch -p1 < $PKGBUILD_ROOT/0003-CVE-2025-59728.patch
        patch -p1 < $PKGBUILD_ROOT/0004-CVE-2025-22919.patch
        patch -p1 < $PKGBUILD_ROOT/0005-CVE-2025-10256.patch
        patch -p1 < $PKGBUILD_ROOT/0006-CVE-2025-7700.patch
        patchflag=false
        cd $OLDPWD
    fi

    cp -rf $builddir $builddir-$ARCH-build
    if [ $ARCH == "armeabi-v7a" ]
    then
        setarm32ENV
        arch=arm
        ldflags="-L${OHOS_SDK}/native/sysroot/usr/lib/arm-linux-ohos"
    elif [ $ARCH == "arm64-v8a" ]
    then
        setarm64ENV
        arch=aarch64
        ldflags="-L${OHOS_SDK}/native/sysroot/usr/lib/aarch64-linux-ohos"
	elif [ $ARCH == "x86_64" ]
    then
        setx86_64ENV
        arch=x86_64
        ldflags="-L${OHOS_SDK}/native/sysroot/usr/lib/x86_64-linux-ohos"
    else
        echo "${ARCH} not support"
        return -1
    fi
    return $ret
}

build() {
    cd $builddir-$ARCH-build
    PKG_CONFIG_LIBDIR="${pkgconfigpath}" ./configure "$@" --enable-neon --enable-asm --enable-network \
    --enable-cross-compile --enable-librtmp --disable-x86asm --enable-openssl --enable-protocols --enable-libmp3lame --enable-libopenh264 \
    --disable-static --enable-shared --disable-doc --disable-htmlpages --target-os=linux --arch=$arch \
    --cc=${CC} --ld=${CC} --strip=${STRIP} --host-cc="${CC}" --host-ld="${CC}" --host-os=linux \
    --host-ldflags=${ldflags} --sysroot=${OHOS_SDK}/native/sysroot --extra-cflags="-I${LYCIUM_ROOT}/usr/lame/${ARCH}/include" --extra-ldflags="-L${LYCIUM_ROOT}/usr/lame/${ARCH}/lib"  > $buildlog 2>&1
    $MAKE >> $buildlog 2>&1
    ret=$?
    cd $OLDPWD
    return $ret
}

package() {
    cd $builddir-$ARCH-build
    $MAKE install >> $buildlog 2>&1
    cd $OLDPWD
}


check() {
    unset arch
    unset ldflags
    if [ $ARCH == "armeabi-v7a" ]
    then
        unsetarm32ENV
    elif [ $ARCH == "arm64-v8a" ]
    then
        unsetarm64ENV
	elif [ $ARCH == "x86_64" ]
    then
        unsetx86_64ENV
    else
        echo "${ARCH} not support"
        return -1
    fi
    echo "The test must be on an OpenHarmony device!"
}

# 清理环境
cleanbuild() {
    rm -rf ${PWD}/${builddir} ${PWD}/$builddir-arm64-v8a-build ${PWD}/$builddir-armeabi-v7a-build #${PWD}/$packagename
}
