#!/bin/sh
# One architecture of the Media extension's Windows ffmpeg, inside the container
# build-windows.ps1 runs: zlib and dav1d (static, dav1d with its assembly), then
# ffmpeg and ffprobe, LGPL only, Media Foundation's h264_mf as the H.264 encoder,
# linked statically so the two programs import nothing but Windows' own DLLs
# (Media Foundation itself is loaded at run time). Run build-windows.ps1, not this.
#
#   sh /src/ffmpeg/build-windows.sh x64|arm64
#
# /src is mediaext (read-only); /build is mediaext/.build/ffmpeg, holding the
# pinned tarballs in /build/src and receiving /build/<FFMPEG_VERSION>/windows-<arch>.
# A copy of this file is published beside the Media page as
# website/extensions/media/build-ffmpeg-windows.sh (kept identical by a test).
set -eu
case "${1:-}" in
  x64) triple=x86_64-w64-mingw32; cpu=x86_64 ;;
  arm64) triple=aarch64-w64-mingw32; cpu=aarch64 ;;
  *) echo "usage: build-windows.sh x64|arm64" >&2; exit 2 ;;
esac
. /src/ffmpeg/versions.env
. /src/ffmpeg/versions-windows.env
. /src/ffmpeg/components.env
# csv NAMES...: the names as configure's comma-separated list.
csv() { echo "$*" | tr -s ' ' ','; }
llvm="llvm-mingw-$LLVM_MINGW_VERSION-ucrt-ubuntu-22.04-x86_64.tar.xz"

cd /build/src
printf '%s  %s\n' "$FFMPEG_SHA256" "ffmpeg-$FFMPEG_VERSION.tar.xz" "$DAV1D_SHA256" "dav1d-$DAV1D_VERSION.tar.xz" \
  "$ZLIB_SHA256" "zlib-$ZLIB_VERSION.tar.xz" "$LLVM_MINGW_SHA256" "$llvm" | sha256sum -c -

mkdir -p /opt/llvm-mingw
tar -xJf "$llvm" -C /opt/llvm-mingw --strip-components 1
export PATH="/opt/llvm-mingw/bin:$PATH"

work="/tmp/$1"
prefix="$work/prefix"
out="/build/$FFMPEG_VERSION/windows-$1"
mkdir -p "$work/ffmpeg" "$work/dav1d" "$work/zlib" "$prefix/include" "$prefix/lib" "$out"
tar -xJf "ffmpeg-$FFMPEG_VERSION.tar.xz" -C "$work/ffmpeg" --strip-components 1
tar -xJf "dav1d-$DAV1D_VERSION.tar.xz" -C "$work/dav1d" --strip-components 1
tar -xJf "zlib-$ZLIB_VERSION.tar.xz" -C "$work/zlib" --strip-components 1

echo "==> zlib $ZLIB_VERSION ($1)"
make -C "$work/zlib" -f win32/Makefile.gcc PREFIX="$triple-" CC="$triple-clang" libz.a
cp "$work/zlib/zlib.h" "$work/zlib/zconf.h" "$prefix/include/"
cp "$work/zlib/libz.a" "$prefix/lib/"

echo "==> dav1d $DAV1D_VERSION ($1)"
cat > "$work/cross.ini" <<EOF
[binaries]
c = '$triple-clang'
ar = '$triple-ar'
strip = '$triple-strip'
windres = '$triple-windres'
nasm = 'nasm'

[host_machine]
system = 'windows'
cpu_family = '$cpu'
cpu = '$cpu'
endian = 'little'
EOF
meson setup "$work/dav1d-build" "$work/dav1d" --cross-file "$work/cross.ini" --prefix "$prefix" --libdir lib \
  --default-library=static --buildtype=release -Denable_tools=false -Denable_tests=false \
  -Denable_examples=false -Denable_asm=true
ninja -C "$work/dav1d-build" install

# The components are components.env's and nothing else; check-components.sh holds the
# configuration to it. zlib serves PNG and compressed movie headers; lavfi is the one
# input device, for the encoder check's source; --disable-autodetect makes the result
# depend on these flags alone, so each external piece is named. The Media Foundation
# encoder does not compile without D3D11VA (which, like Media Foundation, loads its
# DLLs at run time).
echo "==> ffmpeg $FFMPEG_VERSION ($1)"
mkdir -p "$work/ffmpeg-build"
cd "$work/ffmpeg-build"
PKG_CONFIG_LIBDIR="$prefix/lib/pkgconfig" PKG_CONFIG_PATH="" "$work/ffmpeg/configure" \
  --prefix="$prefix" \
  --enable-cross-compile --target-os=mingw32 --arch="$cpu" --cross-prefix="$triple-" \
  --cc="$triple-clang" --cxx="$triple-clang++" \
  --pkg-config=pkg-config --pkg-config-flags=--static \
  --disable-gpl --disable-nonfree \
  --disable-doc --disable-debug \
  --disable-everything \
  --enable-protocol="$(csv $PROTOCOLS $PROTOCOLS_WINDOWS)" --enable-demuxer="$(csv $DEMUXERS)" \
  --enable-decoder="$(csv $DECODERS)" --enable-parser="$(csv $PARSERS)" --enable-bsf="$(csv $BSFS)" \
  --enable-muxer="$(csv $MUXERS)" --enable-encoder="$(csv $ENCODERS $ENCODERS_WINDOWS)" \
  --enable-filter="$(csv $FILTERS)" --enable-hwaccel="$(csv $HWACCELS_WINDOWS)" \
  --disable-shared --enable-static \
  --disable-ffplay --disable-indevs --enable-indev=lavfi --disable-outdevs \
  --disable-autodetect --enable-w32threads --enable-mediafoundation --enable-d3d11va \
  --enable-libdav1d --enable-zlib \
  --extra-cflags="-I$prefix/include" \
  --extra-ldflags=-static --extra-ldflags="-L$prefix/lib"
sh /src/ffmpeg/check-components.sh windows config_components.h
make -j"$(nproc)"
make install
cp "$prefix/bin/ffmpeg.exe" "$prefix/bin/ffprobe.exe" "$out/"
echo "built $out/ffmpeg.exe and ffprobe.exe (ffmpeg $FFMPEG_VERSION, dav1d $DAV1D_VERSION, zlib $ZLIB_VERSION)"
