#!/bin/sh
# LudicoAI's ffmpeg.exe and ffprobe.exe for 64-bit Windows: FFmpeg 6.1.6 under the LGPL 2.1 or later (built
# WITHOUT --enable-gpl and without --enable-nonfree), statically linked with LAME 3.100 (MP3 encoding, LGPL),
# dav1d 1.5.4 (AV1 decoding, BSD-2-Clause) and zlib 1.3.1 (zlib licence), cross-compiled with mingw-w64 in the
# image of ./Dockerfile. tools/build-ffmpeg.js runs it:
#
#   docker run --rm -v <the 4 tarballs>:/sources:ro -v <this folder>:/recipe:ro -v <output>:/out <image> \
#       sh /recipe/build.sh
#
# Nothing is downloaded here: the tarballs in /sources are the projects' own releases, checked by the SHA-256
# below. The build happens in /build (the same paths every time, so the same files come out: the paths end up
# in ffmpeg's configuration string). Out: /out/ffmpeg.exe, /out/ffprobe.exe, /out/BUILDINFO.txt.
#
# What LudicoAI uses ffmpeg for: a video's sound decoded to raw samples (transcription, speakers), one frame
# as a JPEG (the Library's pictures), MP3 (the development cloud path); ffprobe: a video's length. So: every
# decoder, demuxer, parser and filter of FFmpeg (videos of any kind open), only the encoders and muxers those
# uses need, the file and pipe protocols only (no network), no devices, no hardware acceleration.
set -eu

FFMPEG=ffmpeg-6.1.6;  FFMPEG_SHA=d4fcb164028dd3beee5d92c0ac72e46aac6973c75ea12dc14de07bf8f407370a
LAME=lame-3.100;      LAME_SHA=ddfe36cab873794038ae2c1210557ad34857a4b6bdc515785d1da9e175b1da1e
DAV1D=dav1d-1.5.4;    DAV1D_SHA=686616b7c69eb88d44459391ab25cac13b6647a3b288835c5784e71c1514a5c5
ZLIB=zlib-1.3.1;      ZLIB_SHA=38ef96b8dfe510d42707d9c781877914792541133e1870841463bfa73f883e32

HOST=x86_64-w64-mingw32
CC=$HOST-gcc-win32
PREFIX=/build/prefix
JOBS=$(nproc)
export LC_ALL=C TZ=UTC SOURCE_DATE_EPOCH=1767225600
export PKG_CONFIG_LIBDIR=$PREFIX/lib/pkgconfig PKG_CONFIG_PATH=

echo "== Sources (SHA-256)"
cd /sources
echo "$FFMPEG_SHA  $FFMPEG.tar.xz" | sha256sum -c -
echo "$LAME_SHA  $LAME.tar.gz" | sha256sum -c -
echo "$DAV1D_SHA  $DAV1D.tar.xz" | sha256sum -c -
echo "$ZLIB_SHA  $ZLIB.tar.xz" | sha256sum -c -

rm -rf /build
mkdir -p /build $PREFIX/include $PREFIX/lib
cd /build
for f in $FFMPEG.tar.xz $LAME.tar.gz $DAV1D.tar.xz $ZLIB.tar.xz; do tar -xf /sources/$f; done

echo "== zlib"
cd /build/$ZLIB
make -f win32/Makefile.gcc PREFIX=$HOST- CC=$CC libz.a
cp zlib.h zconf.h $PREFIX/include/
cp libz.a $PREFIX/lib/

echo "== LAME"
cd /build/$LAME
CC=$CC ./configure --host=$HOST --prefix=$PREFIX --enable-static --disable-shared \
    --disable-frontend --disable-decoder --disable-gtktest
make -j"$JOBS"
make install

echo "== dav1d"
cd /build/$DAV1D
meson setup out --cross-file /recipe/mingw64-cross.ini --prefix=$PREFIX --libdir=lib \
    --buildtype=release --default-library=static \
    -Denable_tools=false -Denable_tests=false -Denable_examples=false
ninja -C out -j"$JOBS"
ninja -C out install

echo "== FFmpeg"
cd /build/$FFMPEG
./configure \
    --prefix=$PREFIX --arch=x86_64 --target-os=mingw32 --cross-prefix=$HOST- --cc=$CC \
    --pkg-config=pkg-config --pkg-config-flags=--static \
    --enable-static --disable-shared \
    --disable-autodetect --enable-w32threads --enable-zlib --enable-libmp3lame --enable-libdav1d \
    --disable-debug --disable-doc --disable-avdevice --disable-postproc --disable-ffplay \
    --disable-network --disable-protocols --enable-protocol=file,pipe \
    --disable-encoders --enable-encoder=pcm_s16le,pcm_f32le,mjpeg,png,libmp3lame,wrapped_avframe,rawvideo \
    --disable-muxers --enable-muxer=pcm_s16le,pcm_f32le,wav,mp3,image2,image2pipe,null,md5,framemd5 \
    --extra-cflags="-I$PREFIX/include" \
    --extra-ldflags="-L$PREFIX/lib -static -static-libgcc -Wl,--no-insert-timestamp" \
    --extra-version=ludicoai > configure.out 2>&1 || { cat configure.out; exit 1; }
cat configure.out
# A component name that matches nothing is only a warning for configure: here it stops the build
# (the raw sound muxers are pcm_s16le / pcm_f32le, not s16le / f32le - a missing one breaks the app)
if grep -q 'did not match anything' configure.out; then echo "configure: an option matched nothing (above)"; exit 1; fi
grep -q '^#define FFMPEG_LICENSE "LGPL version 2.1 or later"$' config.h || { echo "Not an LGPL configuration"; exit 1; }
for muxer in pcm_s16le pcm_f32le wav mp3 image2 null; do
    grep -q "^#define CONFIG_$(echo $muxer | tr a-z A-Z)_MUXER 1$" config_components.h || { echo "muxer $muxer is missing"; exit 1; }
done
for encoder in pcm_s16le pcm_f32le mjpeg libmp3lame; do
    grep -q "^#define CONFIG_$(echo $encoder | tr a-z A-Z)_ENCODER 1$" config_components.h || { echo "encoder $encoder is missing"; exit 1; }
done
make -j"$JOBS" ffmpeg.exe ffprobe.exe

cp ffmpeg.exe ffprobe.exe /out/
{
    echo "LudicoAI's ffmpeg.exe and ffprobe.exe for 64-bit Windows (native/ffmpeg/build.sh)"
    echo
    echo "Licence: LGPL version 2.1 or later (FFmpeg built without --enable-gpl and without --enable-nonfree)"
    echo
    echo "Sources (SHA-256):"
    echo "  $FFMPEG.tar.xz  $FFMPEG_SHA  https://ffmpeg.org/releases/$FFMPEG.tar.xz"
    echo "    (git tag n6.1.6, commit f1e3a2bf7a2f2cde936d1ed97f09a26853d20125)"
    echo "  $LAME.tar.gz  $LAME_SHA  https://downloads.sourceforge.net/project/lame/lame/3.100/$LAME.tar.gz"
    echo "  $DAV1D.tar.xz  $DAV1D_SHA  https://downloads.videolan.org/pub/videolan/dav1d/1.5.4/$DAV1D.tar.xz"
    echo "  $ZLIB.tar.xz  $ZLIB_SHA  https://github.com/madler/zlib/releases/download/v1.3.1/$ZLIB.tar.xz"
    echo
    echo "FFmpeg configuration:"
    sed -n 's/^#define FFMPEG_CONFIGURATION "\(.*\)"$/  \1/p' config.h
    echo
    echo "Toolchain (Debian packages):"
    dpkg-query -W -f='  ${Package} ${Version}\n' gcc-mingw-w64-x86-64-win32 gcc-mingw-w64-base binutils-mingw-w64-x86-64 \
        mingw-w64-x86-64-dev nasm meson ninja-build pkgconf make
    echo
    echo "Results (SHA-256):"
    cd /out && sha256sum ffmpeg.exe ffprobe.exe | sed 's/^/  /'
    echo
    echo "Windows DLLs they load:"
    $HOST-objdump -p ffmpeg.exe | sed -n 's/^\tDLL Name: /  ffmpeg.exe: /p'
    $HOST-objdump -p ffprobe.exe | sed -n 's/^\tDLL Name: /  ffprobe.exe: /p'
} > /out/BUILDINFO.txt
cat /out/BUILDINFO.txt
