# Everstra Capture -- FFmpeg build recipe (LGPLv2.1+ source offer)
#
# Generated from the Dockerfile that builds the shipped Everstra Capture
# package (tools/gen_source_dockerfile.py --module capture); every instruction
# below is identical to that build. FFmpeg links the openh264 encoder
# (BSD-2-Clause), built here from its release tarball. Only the steps that
# build the closed-source application itself are left out.
#
#   docker build --build-arg ARCH=aarch64 -t everstra-capture-build .
#   docker create --name tmp everstra-capture-build
#   docker cp tmp:/opt/ffmpeg-bin ./ffmpeg-aarch64
#   docker rm tmp
#
# For armv7hf use --build-arg ARCH=armv7hf.

ARG ARCH=aarch64

ARG SDK_VERSION=1.15.3

ARG UBUNTU_VERSION=22.04

ARG REPO=axisecp

ARG SDK=acap-native-sdk

FROM ${REPO}/${SDK}:${SDK_VERSION}-${ARCH}-ubuntu${UBUNTU_VERSION}

ARG ARCH

ARG FFMPEG_VERSION=6.1.2

ARG OPENH264_VERSION=2.6.0

RUN apt-get update && \
    apt-get install -y --no-install-recommends wget xz-utils pkg-config gcc g++ libc6-dev make && \
    rm -rf /var/lib/apt/lists/*

RUN if [ "$ARCH" = "aarch64" ]; then \
      CROSS_PREFIX=aarch64-linux-gnu- ; \
      OH264_ARCH=arm64 ; \
      SYSROOT=/opt/axis/acapsdk/sysroots/aarch64 ; \
      CC_DIR=/opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux ; \
    else \
      CROSS_PREFIX=arm-linux-gnueabihf- ; \
      OH264_ARCH=arm ; \
      SYSROOT=/opt/axis/acapsdk/sysroots/armv7hf ; \
      CC_DIR=/opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi ; \
    fi && \
    export PATH="${CC_DIR}:/opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux/usr/bin:${PATH}" && \
    WORK=/opt/openh264-build && mkdir -p ${WORK} && cd ${WORK} && \
    wget -q "https://github.com/cisco/openh264/archive/refs/tags/v${OPENH264_VERSION}.tar.gz" -O openh264.tar.gz && \
    tar xf openh264.tar.gz && cd "openh264-${OPENH264_VERSION}" && \
    make -j$(nproc) OS=linux ARCH=${OH264_ARCH} \
        CC="${CROSS_PREFIX}gcc --sysroot=${SYSROOT}" \
        CXX="${CROSS_PREFIX}g++ --sysroot=${SYSROOT}" \
        AR=${CROSS_PREFIX}ar \
        STRIP=${CROSS_PREFIX}strip \
        PREFIX=${SYSROOT}/usr \
        install-static && \
    cd / && rm -rf ${WORK}

WORKDIR /opt/ffmpeg-build

RUN wget -q "https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.xz" && \
    tar xf "ffmpeg-${FFMPEG_VERSION}.tar.xz"

RUN if [ "$ARCH" = "aarch64" ]; then \
      CROSS_PREFIX=aarch64-linux-gnu- ; \
      FF_ARCH=aarch64 ; \
      SYSROOT=/opt/axis/acapsdk/sysroots/aarch64 ; \
      CC_DIR=/opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux/usr/bin/aarch64-poky-linux ; \
    else \
      CROSS_PREFIX=arm-linux-gnueabihf- ; \
      FF_ARCH=arm ; \
      SYSROOT=/opt/axis/acapsdk/sysroots/armv7hf ; \
      CC_DIR=/opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi ; \
    fi && \
    export PATH="${CC_DIR}:/opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux/usr/bin:${PATH}" && \
    export PKG_CONFIG_PATH="${SYSROOT}/usr/lib/pkgconfig" && \
    export PKG_CONFIG_LIBDIR="${SYSROOT}/usr/lib/pkgconfig" && \
    # MATROSKA (Capture 0.2.0): the recording export hands back MKV, and a
    # requested period can span several recording ids that have to be joined
    # into one file. Joining is a stream copy through the concat demuxer, so
    # ffmpeg must be able to READ and WRITE matroska — the 0.1.4 build could do
    # neither (muxers were mov/mp4, demuxers concat/jpeg_pipe only), which was
    # measured on a P3245-V before this flag was added, not assumed.
    # The h264 parser is what lets the copy find packet boundaries; the aac
    # parser does the same for the audio track when a recording carries one.
    # No decoder or encoder is added: the join must never re-encode.
    #
    # MP4 EXPORT (Phase 4): the mp4 muxer was already here for the timelapse and
    # intraday encoders, so remuxing MKV -> MP4 needs no new muxer. Note that
    # ffmpeg treats mov and mp4 as SEPARATE muxers and only mp4 is whitelisted,
    # which is why the remux names "-f mp4" explicitly.
    # aac_adtstoasc is added as insurance, not decoration. The mp4 muxer inserts
    # it ITSELF when an incoming AAC track is ADTS-framed, and if the filter is
    # not compiled in that auto-insertion fails at runtime with a muxing error
    # rather than at build time. Audio out of Matroska should already be ASC, so
    # this should never fire — but "should" is doing the work in that sentence
    # and the filter costs a couple of kilobytes.
    # h264_mp4toannexb is deliberately NOT used by the remux: it converts the
    # other direction, for MPEG-TS, and both MKV and MP4 carry H.264 as avcC.
    cd "ffmpeg-${FFMPEG_VERSION}" && \
    ./configure \
      --enable-cross-compile       \
      --cross-prefix=${CROSS_PREFIX} \
      --pkg-config=pkg-config      \
      --arch=${FF_ARCH}            \
      --target-os=linux            \
      --sysroot=${SYSROOT}         \
      --enable-small               \
      --enable-static              \
      --disable-shared             \
      --disable-doc                \
      --disable-htmlpages          \
      --disable-manpages           \
      --disable-podpages           \
      --disable-txtpages           \
      --disable-ffplay             \
      --disable-ffprobe            \
      --disable-debug              \
      --disable-postproc           \
      --disable-bzlib              \
      --disable-network            \
      --disable-everything         \
      --enable-swscale              \
      --enable-libopenh264         \
      --enable-protocol=file       \
      --enable-demuxer=concat      \
      --enable-demuxer=image_jpeg_pipe \
      --enable-decoder=mjpeg       \
      --enable-muxer=mp4           \
      --enable-encoder=libopenh264 \
      --enable-filter=scale        \
      --enable-demuxer=matroska    \
      --enable-muxer=matroska      \
      --enable-parser=h264         \
      --enable-parser=aac          \
      --enable-bsf=h264_mp4toannexb \
      --enable-bsf=aac_adtstoasc   \
      --extra-cflags="--sysroot=${SYSROOT} -I${SYSROOT}/usr/include" \
      --extra-ldflags="--sysroot=${SYSROOT} -L${SYSROOT}/usr/lib" \
      --extra-libs="-lopenh264 -lstdc++" \
      > /tmp/ffmpeg-configure.log 2>&1; \
    CONFIGURE_STATUS=$? ; \
    if grep -q "did not match anything" /tmp/ffmpeg-configure.log; then \
      echo "FATAL: ffmpeg configure silently ignored one or more --enable-*/--disable-* flags" >&2 ; \
      echo "(a component this build relies on would be silently MISSING from the binary --" >&2 ; \
      echo "this exact failure mode shipped once already: --enable-demuxer=jpeg_pipe was" >&2 ; \
      echo "silently dropped because FFmpeg 6.1.2 calls it image_jpeg_pipe at configure time," >&2 ; \
      echo "and only a runtime check on the actual binary caught it, not the build itself)." >&2 ; \
      grep "did not match anything" /tmp/ffmpeg-configure.log >&2 ; \
      exit 1 ; \
    fi ; \
    if [ "$CONFIGURE_STATUS" != "0" ]; then \
      cat /tmp/ffmpeg-configure.log >&2 ; \
      exit "$CONFIGURE_STATUS" ; \
    fi ; \
    sed -i "/^#define FFMPEG_CONFIGURATION /s#${SYSROOT}#<sysroot>#g" config.h && \
    make -j$(nproc) \
    && ${CROSS_PREFIX}strip ffmpeg \
    && if grep -qa -e "${SYSROOT}" -e /opt/ffmpeg-build -e /opt/tls-build ffmpeg; then \
         echo "FATAL: build path left in the ffmpeg binary" >&2 ; exit 1 ; \
       fi \
    && cp ffmpeg /opt/ffmpeg-bin
