# syntax=docker/dockerfile:1.7
#
# Reproducible build of the 20mb.net video engine: FFmpeg 9.1 libraries plus
# x264, libvpx, Opus, LAME, dav1d, zimg and zlib, compiled to WebAssembly
# (SIMD128, single-threaded) with Emscripten, linked into our own small
# transcoder (ffw/ffw.c).
#
#   docker build -f engine/Dockerfile engine --target dist --output type=local,dest=engine/dist
#
# Every source archive is read from engine/sources/ (the same files the site
# publishes for GPL compliance), so the build needs no network after the
# base image and apt packages.

ARG EMSDK_VERSION=7.0.1

############################################################ toolchain
FROM emscripten/emsdk:${EMSDK_VERSION} AS base
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
      autoconf automake libtool pkg-config meson ninja-build patch xz-utils \
 && rm -rf /var/lib/apt/lists/*
COPY scripts/env.sh /env.sh
WORKDIR /build

############################################################ zlib
FROM base AS zlib
COPY sources/zlib-1.3.2.tar.gz /tmp/
RUN . /env.sh && tar -xf /tmp/zlib-1.3.2.tar.gz && cd zlib-1.3.2 \
 && CFLAGS="$OPT_FLAGS" emconfigure ./configure --prefix="$PREFIX" --static \
 && emmake make -j"$JOBS" libz.a \
 && emmake make install

############################################################ x264
FROM base AS x264
COPY sources/x264-b35605a.tar.gz /tmp/
COPY scripts/x264-version.sh /x264-version.sh
# x264 adds -fno-tree-vectorize for its x86 assembly builds; there is no
# assembly here, so let clang vectorise for WebAssembly SIMD128 instead.
RUN . /env.sh && tar -xf /tmp/x264-b35605a.tar.gz && cd x264-b35605a \
 && cp /x264-version.sh version.sh && chmod +x version.sh \
 && emconfigure ./configure --prefix="$PREFIX" --host=i686-gnu \
      --enable-static --disable-cli --disable-asm --disable-thread --disable-opencl \
      --disable-avs --disable-swscale --disable-lavf --disable-ffms --disable-gpac --disable-lsmash \
      --bit-depth=8 --chroma-format=420 \
      --extra-cflags="$OPT_FLAGS" \
 && sed -i 's/ -fno-tree-vectorize//; s/ -m32//g' config.mak \
 && emmake make -j"$JOBS" \
 && emmake make install

############################################################ libvpx (VP8/VP9 encoders only)
FROM base AS libvpx
COPY sources/libvpx-1.17.0.tar.gz /tmp/
RUN . /env.sh && tar -xf /tmp/libvpx-1.17.0.tar.gz && cd libvpx-1.17.0 \
 && emconfigure ./configure --prefix="$PREFIX" --target=generic-gnu \
      --disable-install-bins --disable-examples --disable-tools --disable-docs \
      --disable-unit-tests --disable-decode-perf-tests --disable-encode-perf-tests \
      --disable-dependency-tracking --disable-multithread --disable-webm-io --disable-libyuv \
      --enable-vp8-encoder --enable-vp9-encoder --disable-vp8-decoder --disable-vp9-decoder \
      --enable-static --disable-shared \
      --extra-cflags="$OPT_FLAGS" --extra-cxxflags="$OPT_FLAGS" \
 && emmake make -j"$JOBS" \
 && emmake make install

############################################################ libopus
FROM base AS opus
COPY sources/opus-1.6.1.tar.gz /tmp/
RUN . /env.sh && tar -xf /tmp/opus-1.6.1.tar.gz && cd opus-1.6.1 \
 && CFLAGS="$OPT_FLAGS" emconfigure ./configure --prefix="$PREFIX" --host=i686-gnu \
      --enable-static --disable-shared --disable-doc --disable-extra-programs \
      --disable-asm --disable-rtcd --disable-intrinsics --disable-stack-protector \
 && emmake make -j"$JOBS" \
 && emmake make install

############################################################ LAME (MP3 encoder)
FROM base AS lame
COPY sources/lame-4.0.tar.gz /tmp/
RUN . /env.sh && tar -xf /tmp/lame-4.0.tar.gz && cd lame-4.0 \
 && CFLAGS="$OPT_FLAGS -DNDEBUG" emconfigure ./configure --prefix="$PREFIX" --host=i686-linux \
      --enable-static --disable-shared --disable-frontend --disable-decoder \
      --disable-analyzer-hooks --disable-dependency-tracking --disable-gtktest \
 && emmake make -j"$JOBS" \
 && emmake make install

############################################################ dav1d (AV1 decoder)
FROM base AS dav1d
COPY sources/dav1d-1.5.5.tar.xz /tmp/
COPY scripts/emscripten.cross /emscripten.cross
# dav1d asks Meson for the 'threads' dependency, which adds -pthread (shared
# memory) under Emscripten. The engine is single-threaded and dav1d runs with
# one thread, so use an empty dependency instead.
RUN . /env.sh && tar -xf /tmp/dav1d-1.5.5.tar.xz && cd dav1d-1.5.5 \
 && sed -i "s/dependency('threads')/declare_dependency()/" meson.build \
 && ! grep -q "dependency('threads')" meson.build \
 && meson setup build --cross-file /emscripten.cross --prefix="$PREFIX" --libdir=lib \
      --default-library=static --buildtype=release -Doptimization=3 \
      -Denable_tools=false -Denable_tests=false -Denable_asm=false -Dlogging=false \
 && ninja -C build -j"$JOBS" \
 && ninja -C build install \
 && ! grep -q -- '-pthread' "$PREFIX/lib/pkgconfig/dav1d.pc"

############################################################ zimg (HDR -> SDR conversion). zimg reports errors with C++ exceptions;
# -fwasm-exceptions uses WebAssembly's own (legacy, widely supported)
# exception instructions, which cost nothing when nothing is thrown.
# Newer libc++ no longer includes <exception> transitively, so force it, and
# drop the -pthread that zimg's configure adds (the engine has no threads).
FROM base AS zimg
COPY sources/zimg-3.0.6.tar.gz /tmp/
RUN . /env.sh && tar -xf /tmp/zimg-3.0.6.tar.gz && cd zimg-3.0.6 \
 && ./autogen.sh \
 && CXXFLAGS="$OPT_FLAGS -fwasm-exceptions -include exception" CFLAGS="$OPT_FLAGS" LDFLAGS="-fwasm-exceptions" \
    emconfigure ./configure --prefix="$PREFIX" --host=i686-gnu \
      --enable-static --disable-shared --disable-simd --disable-testapp --disable-example --disable-unit-test \
 && emmake make -j"$JOBS" PTHREAD_CFLAGS= PTHREAD_LIBS= \
 && emmake make install PTHREAD_CFLAGS= PTHREAD_LIBS= \
 && sed -i -E 's/ -pthread\b//g' "$PREFIX/lib/pkgconfig/zimg.pc"

############################################################ all dependencies
FROM base AS deps
COPY --from=zlib   /opt/deps /opt/deps
COPY --from=x264   /opt/deps /opt/deps
COPY --from=libvpx /opt/deps /opt/deps
COPY --from=opus   /opt/deps /opt/deps
COPY --from=lame   /opt/deps /opt/deps
COPY --from=dav1d  /opt/deps /opt/deps
COPY --from=zimg   /opt/deps /opt/deps

############################################################ FFmpeg 9.1 libraries
FROM deps AS ffmpeg
COPY sources/ffmpeg-9.1.tar.xz /tmp/
COPY scripts/components.sh /components.sh
COPY scripts/build-ffmpeg-wasm.sh /build-ffmpeg-wasm.sh
RUN bash /build-ffmpeg-wasm.sh

############################################################ our transcoder
FROM ffmpeg AS ffw
COPY ffw/ /ffw/
COPY scripts/build-ffw-wasm.sh /build-ffw-wasm.sh
RUN bash /build-ffw-wasm.sh

FROM scratch AS dist
COPY --from=ffw /out/ /
