# Maintainer: Markus Fritsche <fritsche.markus@gmail.com>
#
# libva-v4l2-request-fourier — VA-API backend for V4L2 stateless decoders,
# multiplanar fork. Successor to the predecessor experimental package
# libva-v4l2-request-ohm-gl-fix (tarball + 18-patch stack); this package
# tracks the campaign fork's git history directly, so iteration sweeps
# (DEBUG removal, follow-up bugfixes) land in a clean linear log.
#
# Campaign: ~/src/libva-multiplanar/ (iter8 close 2026-05-06) plus
# ~/src/fresnel-fourier/ which carried the fork to iter38b — multi-device
# probe so a single libva session serves all 5 codecs (rkvdec H.264 +
# HEVC + VP9, hantro MPEG-2 + VP8) plus a bounds-check fix for
# MAX_PROFILES. Pinned below.
# Fork repo: https://git.reauktion.de/marfrit/libva-v4l2-request-fourier
# Bootlin upstream: https://github.com/bootlin/libva-v4l2-request
#
# Build target: fermi LXC actrunner-aarch64-hertz via marfrit-packages
# Gitea Actions (path=arch/libva-v4l2-request-fourier triggers the
# libva-v4l2-request-fourier-aarch64 job — wire up alongside the existing
# ffmpeg-v4l2-request-git job in .gitea/workflows/build.yml).
# Alternative: boltzmann via his subagent + marfrit-publish.

pkgname=libva-v4l2-request-fourier
epoch=1
_upstreampkg=libva-v4l2-request

# Pin the fork tip. 77f9236 = PR #12 merge "av1: populate
# V4L2_CID_STATELESS_AV1_SEQUENCE in codec_set_controls (#11 libva side)"
# — addresses the libva-side portion of marfrit/libva-v4l2-request-fourier#11.
# Adds src/av1.{c,h} with av1_set_controls that maps VAAPI's
# VAPictureParameterBufferAV1.seq_info_fields onto struct
# v4l2_ctrl_av1_sequence and queues V4L2_CID_STATELESS_AV1_SEQUENCE via
# S_EXT_CTRLS. The daedalus_v4l2 daemon track is the consumer that turns
# the ctrl into an OBU_SEQUENCE_HEADER and prepends it to the slice
# bitstream so libdav1d can parse the (sequence-header-stripped) OUTPUT
# buffer that ffmpeg-vaapi delivers. Until the daemon side lands the ctrl
# is just sitting in the request unused — no-cost no-op for HW decoders
# (vpu981 parses OBU bytes directly).
#
# Prior pin (c1bb444) = PR #9 merge — h264_set_controls
# max_num_ref_frames fallback + libva-boundary instrumentation for the
# daedalus consumer-strict path (issue #8 libva side).
_commit=77f92364661419f6e5a7bd827c1b845b4e426569

# Project version from meson.build (1.0.0) + commit count + short sha,
# matching the ffmpeg-v4l2-request-fourier convention. Recomputed at
# build time by pkgver() below; the static value here is a placeholder
# so AUR-style consumers see something coherent before src/ exists.
pkgver=1.0.0.r386.77f9236
pkgrel=1
pkgdesc="VA-API backend for V4L2 stateless decoders (multiplanar fork — fourier umbrella)"
arch=('aarch64')
url="https://git.reauktion.de/marfrit/libva-v4l2-request-fourier"
license=('LGPL2.1' 'MIT')
depends=('libva' 'libdrm' 'systemd-libs')
makedepends=('meson' 'ninja' 'pkgconf' 'git')
provides=("${_upstreampkg}=${pkgver}" 'libva-driver')
conflicts=("${_upstreampkg}" 'libva-v4l2-request-ohm-gl-fix')
replaces=("${_upstreampkg}" 'libva-v4l2-request-ohm-gl-fix')

source=("git+https://git.reauktion.de/marfrit/libva-v4l2-request-fourier.git#commit=${_commit}")
sha256sums=('SKIP')

pkgver() {
    cd "${srcdir}/${_upstreampkg}-fourier"
    printf '1.0.0.r%s.%s' \
        "$(git rev-list --count HEAD)" \
        "$(git rev-parse --short=7 HEAD)"
}

build() {
    cd "${srcdir}/${_upstreampkg}-fourier"
    # meson_options.txt only exposes 'kernel_headers' — leave it empty to
    # use system /usr/include kernel UAPI headers. No per-codec toggles.
    #
    # b_lto=false: override arch-meson's wrapper default of `-D b_lto=true`,
    # which the makepkg.conf OPTIONS=(..., !lto, ...) line does NOT actually
    # override (arch-meson hard-codes b_lto=true). The hand-built reproducer
    # from issue #17 shows: LTO/ICF kernel-folds per-codec helpers and HEVC's
    # multi-control-struct chain (SPS+PPS+DECODE_PARAMS+SLICE_PARAMS) gets a
    # wrong helper-instance pulled in at vaEndPicture → segfault. The 4 other
    # codecs (single-control-struct) tolerate the folding by accident.
    arch-meson build --buildtype=release -Db_lto=false
    meson compile -C build
}

package() {
    cd "${srcdir}/${_upstreampkg}-fourier"
    meson install -C build --destdir "${pkgdir}"

    install -Dm644 COPYING       "${pkgdir}/usr/share/licenses/${pkgname}/COPYING"
    install -Dm644 COPYING.LGPL  "${pkgdir}/usr/share/licenses/${pkgname}/COPYING.LGPL"
    install -Dm644 COPYING.MIT   "${pkgdir}/usr/share/licenses/${pkgname}/COPYING.MIT"
}
