File intel-gpu-max-10-bit.sh of Package kodi

#!/bin/bash

# Workaround for Intel Alder Lake (and later) GPU HDMI bandwidth issue
# that causes HD audio passthrough (DTS-HD/TrueHD) dropouts at 4K.
# Limits max bits-per-channel to 10 to free HDMI bandwidth for HBR audio.
#
# Based on: https://github.com/LibreELEC/LibreELEC.tv/pull/8588
# Upstream Intel bug: https://gitlab.freedesktop.org/drm/intel/-/issues/10199
#
# Usage:
#   intel-gpu-max-10-bit.sh [--vgaport <port>]
#
# Examples:
#   intel-gpu-max-10-bit.sh                        # auto-detect single connected port
#   intel-gpu-max-10-bit.sh --vgaport HDMI-A-2     # specify port explicitly

set -euo pipefail

KODI_SERVICE="kodi-gbm.service"
BIT=10
NOT="max ${BIT} bit not activated"

# --- help ---
if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then
    echo "Usage: $0 [--vgaport <port>]"
    echo ""
    echo "Limits Intel GPU max bits-per-channel to ${BIT} on the connected"
    echo "HDMI port, freeing bandwidth for HD audio passthrough."
    echo ""
    echo "Options:"
    echo "  --vgaport <port>   Specify the DRM connector name (e.g. HDMI-A-2)"
    echo "  --help, -h         Show this help"
    exit 0
fi

# --- opt-out file ---
if [ -f /etc/dont-activate-10-bit-max ]; then
    echo "INFO: /etc/dont-activate-10-bit-max exists, skipping"
    exit 0
fi

# --- check for Intel GPU ---
if ! lspci | grep -qEi 'VGA .* Intel '; then
    echo "No Intel GPU found, ${NOT}"
    exit 0
fi

# --- check for modetest ---
if ! command -v modetest &>/dev/null; then
    echo "ERROR: 'modetest' not found. Install libdrm-tests or drm-utils:"
    echo "  Debian/Ubuntu: sudo apt install libdrm-tests"
    echo "  Arch:          sudo pacman -S libdrm"
    echo "  Fedora:        sudo dnf install libdrm-utils"
    exit 1
fi

# --- parse --vgaport argument ---
PORT=""
if [ "${1:-}" = "--vgaport" ] && [ -n "${2:-}" ]; then
    VGAPORT="$2"
    shift 2

    # Check if specified port exists (connected or disconnected)
    if ls /sys/class/drm/card*-"${VGAPORT}"/status &>/dev/null; then
        if ! grep -q '^connected$' /sys/class/drm/card*-"${VGAPORT}"/status 2>/dev/null; then
            echo "WARN: VGAPORT=${VGAPORT} is not connected, trying anyway"
        fi
        PORT="${VGAPORT}"
    else
        echo "ERROR: port ${VGAPORT} not found"
        NAMEs="$(grep -l 'connected$' /sys/class/drm/card*/status 2>/dev/null | awk -F/ '{ print $5 }' | sed -e 's|^card[^-]*-||' | tr '\n' ' ')"
        echo "  available ports: ${NAMEs}"
        exit 1
    fi
fi

# --- auto-detect port if not specified ---
if [ -z "${PORT}" ]; then
    PORTs=$(grep -l '^connected$' /sys/class/drm/card*/status 2>/dev/null | wc -l)
    if [ "${PORTs}" = "1" ]; then
        PORT=$(grep -l '^connected$' /sys/class/drm/card*/status | awk -F/ '{ print $5 }' | sed -e 's|^card[^-]*-||')
    elif [ "${PORTs}" = "0" ]; then
        echo "ERROR: no connected display ports found, ${NOT}"
        exit 1
    else
        NAMEs="$(grep -l '^connected$' /sys/class/drm/card*/status | awk -F/ '{ print $5 }' | sed -e 's|^card[^-]*-||' | tr '\n' ' ')"
        echo "ERROR: found ${PORTs} connected ports: ${NAMEs}"
        echo "  auto-detect works only with 1 connected port."
        echo "  specify the port explicitly:"
        echo "    $0 --vgaport <port>"
        exit 1
    fi
fi

if [ -z "${PORT}" ]; then
    echo "ERROR: could not determine VGA port to use"
    NAMEs="$(grep -l 'connected$' /sys/class/drm/card*/status 2>/dev/null | awk -F/ '{ print $5 }' | sed -e 's|^card[^-]*-||' | tr '\n' ' ')"
    echo "  available ports: ${NAMEs}"
    echo "  run: $0 --vgaport <port>"
    exit 1
fi

# --- find the modetest connector ID ---
CONNECTOR=$(modetest -c 2>/dev/null | grep -E "[[:space:]]${PORT}[[:space:]]" | awk '{ print $1 }')
if [ -z "${CONNECTOR}" ]; then
    echo "ERROR: connector ID for port ${PORT} not found via modetest"
    exit 1
fi

echo "Using VGA port ${PORT} with connector ID ${CONNECTOR}"

# --- check if Kodi is running ---
if systemctl is-active "${KODI_SERVICE}" --quiet 2>/dev/null; then
    echo "ERROR: ${KODI_SERVICE} is running, ${NOT}"
    echo "  try:"
    echo "  sudo systemctl stop ${KODI_SERVICE} && sudo $0 $* && sudo systemctl start ${KODI_SERVICE}"
    exit 1
fi

# --- apply the limit ---
echo "Setting max bpc to ${BIT} on connector ${CONNECTOR} (${PORT})"
modetest -w "${CONNECTOR}":"max bpc":"${BIT}"
echo "Done."
openSUSE Build Service is sponsored by