# VA-API driver with Apple Video Decoder fixes from the sofus13 fork.
# The asahi alias lets libva discover it on Apple Silicon.

_fork=sofus13
pkgname=libva-v4l2_request-avd
pkgver=1.3
pkgrel=1
pkgdesc='VA-API driver for V4L2 stateless decoders, with Apple Video Decoder (AVD) support'
arch=('aarch64')
url="https://github.com/$_fork/libva-v4l2_request"
license=('GPL-3.0-or-later')
# The driver is a dlopen()ed plugin: it links only libc and exports the VA
# backend entry point (__vaDriverInit_1_24), so libva is a runtime rather than
# a link-time dependency -- and that symbol is versioned, so this needs a
# rebuild whenever libva's driver ABI is bumped. libdrm is only used for
# drm_fourcc.h constants at compile time.
depends=('glibc' 'libva')
makedepends=('meson' 'libdrm')
provides=('libva-v4l2_request')
conflicts=('libva-v4l2_request')
install="$pkgname.install"
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/$pkgver.tar.gz")
sha256sums=('3670a1712f9f0a5a61bd44a781eba81dd9b2e0fd8271417ca8a9fbbed9dccfe9')

build() {
  # driverdir is left unset so meson takes it from libva's pkg-config
  # (/usr/lib/dri); the codec set is decided by configure-time checks against
  # the kernel uapi headers, so linux-api-headers governs what gets built in.
  # Plain meson rather than arch-meson: the latter lives in devtools, which is
  # not a safe assumption outside a clean chroot. buildtype=plain hands the
  # compiler makepkg's own CFLAGS/LDFLAGS, which is what arch-meson does too.
  meson setup \
    --prefix=/usr \
    --buildtype=plain \
    --wrap-mode=nodownload \
    "libva-v4l2_request-$pkgver" build
  meson compile -C build
}

package() {
  meson install -C build --destdir "$pkgdir"

  # libva picks its driver from the DRM driver name of the render node, which
  # on Apple Silicon is "asahi", so it looks for asahi_drv_video.so. Nothing
  # else ships that name (mesa provides no VA driver for asahi), so aliasing it
  # here makes every VA-API application find the decoder with no environment
  # variable at all. If mesa ever ships its own asahi_drv_video.so this will
  # collide and pacman will say so at install time.
  ln -sf v4l2_request_drv_video.so "$pkgdir/usr/lib/dri/asahi_drv_video.so"

  install -Dm644 "libva-v4l2_request-$pkgver/COPYING" \
    "$pkgdir/usr/share/licenses/$pkgname/COPYING"
  install -Dm644 "libva-v4l2_request-$pkgver/README" \
    "$pkgdir/usr/share/doc/$pkgname/README"
}
