# What the amdgpu kernel driver says the video block can do (AMDGPU_INFO_VIDEO_CAPS).
import ctypes, os
from ctypes import POINTER, byref, c_uint32, c_void_p, sizeof

class CodecInfo(ctypes.Structure):
    _fields_ = [(f, c_uint32) for f in
                ("valid", "max_width", "max_height", "max_pixels_per_frame", "max_level", "pad")]

class VideoCaps(ctypes.Structure):
    _fields_ = [("codec_info", CodecInfo * 8)]

CODECS = ["MPEG2", "MPEG4", "VC1", "H264", "HEVC", "JPEG", "VP9", "AV1"]
lib = ctypes.CDLL("libdrm_amdgpu.so.1")
lib.amdgpu_device_initialize.argtypes = [ctypes.c_int, POINTER(c_uint32), POINTER(c_uint32), POINTER(c_void_p)]
lib.amdgpu_query_video_caps_info.argtypes = [c_void_p, ctypes.c_uint, ctypes.c_uint, c_void_p]

fd = os.open("/dev/dri/renderD128", os.O_RDWR)
major, minor, dev = c_uint32(), c_uint32(), c_void_p()
assert lib.amdgpu_device_initialize(fd, byref(major), byref(minor), byref(dev)) == 0

for kind, cap_type in (("decode", 0), ("encode", 1)):
    caps = VideoCaps()
    assert lib.amdgpu_query_video_caps_info(dev, cap_type, sizeof(caps), byref(caps)) == 0
    print(f"{kind}:")
    for name, c in zip(CODECS, caps.codec_info):
        if c.valid:
            print(f"  {name:5} {c.max_width}x{c.max_height} level {c.max_level}")
