Files
libva-v4l2-request-fourier/src/request.c
T
claude-noether 7f8fa93213 fresnel-fourier iter4 Phase 6 commit Z: device-path auto-detect via media controller topology
Pre-iter4 backend hardcoded /dev/video0 + /dev/media0 as defaults
when no env override was set. Linux 7.0 udev/probe order changed,
rockchip-rga (RGB color converter, no codec) now claims
/dev/video0 — legacy default returns empty profile list.

Discovery is driven by the media controller graph (the canonical
v4l2-request approach). NOT a /dev/video* walk by enumeration
order — that mispairs video and media nodes when one driver
registers multiple media devices, and depends on probe-order
luck.

Algorithm:
  1. Walk /dev/media0..15. MEDIA_IOC_DEVICE_INFO names the driver.
     Match against {rkvdec, hantro-vpu, cedrus, sun4i_csi}.
  2. MEDIA_IOC_G_TOPOLOGY enumerates the entity/interface graph.
     The MEDIA_INTF_T_V4L_VIDEO interface carries major:minor of
     the V4L2 video node owned by THIS media controller — paired
     by the kernel, not by /dev/* enumeration order.
  3. Resolve major:minor to /dev/videoN via /sys/dev/char/<M>:<N>
     (the kernel's char-device sysfs symlink whose basename is
     the device node name).

LIBVA_V4L2_REQUEST_NO_AUTODETECT=1 escape hatch reverts to legacy
/dev/video0 + /dev/media0 hardcoded behavior for callers that
depended on it.

Phase 5 C4 amendment: walk-and-pick-first selects rkvdec on RK3399
(rkvdec's media controller enumerates before hantro's). H.264 /
HEVC / VP9 (rkvdec codecs) work without env override after this
commit. MPEG-2 / VP8 (hantro) still require explicit
LIBVA_V4L2_REQUEST_VIDEO_PATH=/dev/video3 override; full
multi-decoder dispatch is iter4-B1 backlog item.

Verified empirically on fresnel (linux-fresnel-fourier 7.0-1):
- vainfo (no env) -> "auto-selected codec device: /dev/video1 +
  /dev/media0", enumerates H264*5 + HEVCMain (rkvdec) — paired
  via topology graph, not /dev/video* enumeration.
- vainfo NO_AUTODETECT=1 -> empty list (legacy /dev/video0 = rga).
- vainfo with explicit /dev/video3 + /dev/media1 -> MPEG2*2 + VP8.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 06:05:41 +00:00

427 lines
14 KiB
C

/*
* Copyright (C) 2007 Intel Corporation
* Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
* Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "buffer.h"
#include "config.h"
#include "context.h"
#include "image.h"
#include "picture.h"
#include "subpicture.h"
#include "surface.h"
#include "autoconfig.h"
#include <va/va_backend.h>
#include "request.h"
#include "utils.h"
#include "v4l2.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/media.h>
#include <linux/videodev2.h>
/*
* fresnel-fourier iter4 Phase 6 commit Z: device-path auto-detect via media controller topology
*
* Pre-iter4 the backend hardcoded /dev/video0 + /dev/media0 as defaults
* when no env override was set. On Linux 7.0 the udev/probe order
* changed and rockchip-rga (an RGB color converter, no codec support)
* now claims /dev/video0 — the legacy default returns an empty profile
* list.
*
* Discovery is driven by the media controller graph (NOT by walking
* /dev/video* in enumeration order — that approach can mispair the
* video and media nodes when one driver registers multiple media
* devices, and depends on probe-order luck):
*
* 1. Walk /dev/media0..N. For each, MEDIA_IOC_DEVICE_INFO names the
* driver. Match against the known-decoder list.
* 2. MEDIA_IOC_G_TOPOLOGY returns the entity/interface graph. The
* MEDIA_INTF_T_V4L_VIDEO interface entries carry major:minor of
* the V4L2 video node owned by THIS media controller — guaranteed
* paired by the kernel.
* 3. Resolve major:minor to /dev/videoN via /sys/dev/char/<M>:<N>
* (the kernel's char-device sysfs symlink whose basename is the
* device node name).
*
* Phase 5 C4: walk picks rkvdec on RK3399 (rkvdec's media controller
* enumerates before hantro's). MPEG-2/VP8 (hantro) still need explicit
* LIBVA_V4L2_REQUEST_VIDEO_PATH override; full multi-decoder dispatch
* is iter4-B1 backlog.
*
* Escape hatch: LIBVA_V4L2_REQUEST_NO_AUTODETECT=1 reverts to legacy
* hardcoded /dev/video0 + /dev/media0 for callers that relied on it.
*/
static const char * const known_decoder_drivers[] = {
"rkvdec",
"hantro-vpu",
"cedrus",
"sun4i_csi",
NULL
};
static int resolve_dev_node(uint32_t major, uint32_t minor, char *out, size_t out_sz)
{
char sysfs_path[64], target[256];
ssize_t n;
const char *base;
snprintf(sysfs_path, sizeof sysfs_path, "/sys/dev/char/%u:%u", major, minor);
n = readlink(sysfs_path, target, sizeof target - 1);
if (n < 0)
return -1;
target[n] = '\0';
base = strrchr(target, '/');
base = base ? base + 1 : target;
snprintf(out, out_sz, "/dev/%s", base);
return 0;
}
static int find_video_node_via_topology(int media_fd, char *video_out, size_t video_out_sz)
{
struct media_v2_topology topo;
struct media_v2_interface *interfaces = NULL;
int ret = -1;
unsigned int i;
memset(&topo, 0, sizeof topo);
if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topo) < 0)
return -1;
if (topo.num_interfaces == 0)
return -1;
interfaces = calloc(topo.num_interfaces, sizeof *interfaces);
if (!interfaces)
return -1;
memset(&topo, 0, sizeof topo);
if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topo) < 0)
goto out;
topo.ptr_interfaces = (uintptr_t)interfaces;
if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topo) < 0)
goto out;
for (i = 0; i < topo.num_interfaces; i++) {
if (interfaces[i].intf_type != MEDIA_INTF_T_V4L_VIDEO)
continue;
if (resolve_dev_node(interfaces[i].devnode.major,
interfaces[i].devnode.minor,
video_out, video_out_sz) == 0) {
ret = 0;
break;
}
}
out:
free(interfaces);
return ret;
}
static int find_codec_device(char *video_out, size_t video_out_sz,
char *media_out, size_t media_out_sz)
{
struct media_device_info info;
char path[32];
const char * const *kd;
int fd, i;
for (i = 0; i < 16; i++) {
snprintf(path, sizeof path, "/dev/media%d", i);
fd = open(path, O_RDWR | O_NONBLOCK);
if (fd < 0)
continue;
memset(&info, 0, sizeof info);
if (ioctl(fd, MEDIA_IOC_DEVICE_INFO, &info) == 0) {
for (kd = known_decoder_drivers; *kd; kd++) {
if (strcmp(info.driver, *kd) != 0)
continue;
if (find_video_node_via_topology(fd, video_out,
video_out_sz) == 0) {
snprintf(media_out, media_out_sz, "%s", path);
close(fd);
return 0;
}
break;
}
}
close(fd);
}
return -1;
}
/* Set default visibility for the init function only. */
VAStatus __attribute__((visibility("default")))
VA_DRIVER_INIT_FUNC(VADriverContextP context);
VAStatus VA_DRIVER_INIT_FUNC(VADriverContextP context)
{
struct request_data *driver_data;
struct VADriverVTable *vtable = context->vtable;
VAStatus status;
unsigned int capabilities;
unsigned int capabilities_required;
int video_fd = -1;
int media_fd = -1;
char *video_path;
char *media_path;
int rc;
context->version_major = VA_MAJOR_VERSION;
context->version_minor = VA_MINOR_VERSION;
context->max_profiles = V4L2_REQUEST_MAX_PROFILES;
context->max_entrypoints = V4L2_REQUEST_MAX_ENTRYPOINTS;
context->max_attributes = V4L2_REQUEST_MAX_CONFIG_ATTRIBUTES;
context->max_image_formats = V4L2_REQUEST_MAX_IMAGE_FORMATS;
context->max_subpic_formats = V4L2_REQUEST_MAX_SUBPIC_FORMATS;
context->max_display_attributes = V4L2_REQUEST_MAX_DISPLAY_ATTRIBUTES;
context->str_vendor = V4L2_REQUEST_STR_VENDOR;
vtable->vaTerminate = RequestTerminate;
vtable->vaQueryConfigEntrypoints = RequestQueryConfigEntrypoints;
vtable->vaQueryConfigProfiles = RequestQueryConfigProfiles;
vtable->vaQueryConfigEntrypoints = RequestQueryConfigEntrypoints;
vtable->vaQueryConfigAttributes = RequestQueryConfigAttributes;
vtable->vaCreateConfig = RequestCreateConfig;
vtable->vaDestroyConfig = RequestDestroyConfig;
vtable->vaGetConfigAttributes = RequestGetConfigAttributes;
vtable->vaCreateSurfaces = RequestCreateSurfaces;
vtable->vaCreateSurfaces2 = RequestCreateSurfaces2;
vtable->vaDestroySurfaces = RequestDestroySurfaces;
vtable->vaExportSurfaceHandle = RequestExportSurfaceHandle;
vtable->vaCreateContext = RequestCreateContext;
vtable->vaDestroyContext = RequestDestroyContext;
vtable->vaCreateBuffer = RequestCreateBuffer;
vtable->vaBufferSetNumElements = RequestBufferSetNumElements;
vtable->vaMapBuffer = RequestMapBuffer;
vtable->vaUnmapBuffer = RequestUnmapBuffer;
vtable->vaDestroyBuffer = RequestDestroyBuffer;
vtable->vaBufferInfo = RequestBufferInfo;
vtable->vaAcquireBufferHandle = RequestAcquireBufferHandle;
vtable->vaReleaseBufferHandle = RequestReleaseBufferHandle;
vtable->vaBeginPicture = RequestBeginPicture;
vtable->vaRenderPicture = RequestRenderPicture;
vtable->vaEndPicture = RequestEndPicture;
vtable->vaSyncSurface = RequestSyncSurface;
vtable->vaQuerySurfaceAttributes = RequestQuerySurfaceAttributes;
vtable->vaQuerySurfaceStatus = RequestQuerySurfaceStatus;
vtable->vaPutSurface = RequestPutSurface;
vtable->vaQueryImageFormats = RequestQueryImageFormats;
vtable->vaCreateImage = RequestCreateImage;
vtable->vaDeriveImage = RequestDeriveImage;
vtable->vaDestroyImage = RequestDestroyImage;
vtable->vaSetImagePalette = RequestSetImagePalette;
vtable->vaGetImage = RequestGetImage;
vtable->vaPutImage = RequestPutImage;
vtable->vaQuerySubpictureFormats = RequestQuerySubpictureFormats;
vtable->vaCreateSubpicture = RequestCreateSubpicture;
vtable->vaDestroySubpicture = RequestDestroySubpicture;
vtable->vaSetSubpictureImage = RequestSetSubpictureImage;
vtable->vaSetSubpictureChromakey = RequestSetSubpictureChromakey;
vtable->vaSetSubpictureGlobalAlpha = RequestSetSubpictureGlobalAlpha;
vtable->vaAssociateSubpicture = RequestAssociateSubpicture;
vtable->vaDeassociateSubpicture = RequestDeassociateSubpicture;
vtable->vaQueryDisplayAttributes = RequestQueryDisplayAttributes;
vtable->vaGetDisplayAttributes = RequestGetDisplayAttributes;
vtable->vaSetDisplayAttributes = RequestSetDisplayAttributes;
vtable->vaLockSurface = RequestLockSurface;
vtable->vaUnlockSurface = RequestUnlockSurface;
driver_data = malloc(sizeof(*driver_data));
memset(driver_data, 0, sizeof(*driver_data));
context->pDriverData = driver_data;
object_heap_init(&driver_data->config_heap,
sizeof(struct object_config), CONFIG_ID_OFFSET);
object_heap_init(&driver_data->context_heap,
sizeof(struct object_context), CONTEXT_ID_OFFSET);
object_heap_init(&driver_data->surface_heap,
sizeof(struct object_surface), SURFACE_ID_OFFSET);
object_heap_init(&driver_data->buffer_heap,
sizeof(struct object_buffer), BUFFER_ID_OFFSET);
object_heap_init(&driver_data->image_heap, sizeof(struct object_image),
IMAGE_ID_OFFSET);
static char auto_video[32], auto_media[32];
bool auto_media_set = false;
video_path = getenv("LIBVA_V4L2_REQUEST_VIDEO_PATH");
if (video_path == NULL) {
if (getenv("LIBVA_V4L2_REQUEST_NO_AUTODETECT")) {
video_path = "/dev/video0";
} else if (find_codec_device(auto_video, sizeof auto_video,
auto_media, sizeof auto_media) == 0) {
video_path = auto_video;
auto_media_set = true;
request_log("auto-selected codec device: %s + %s\n",
auto_video, auto_media);
} else {
video_path = "/dev/video0";
}
}
video_fd = open(video_path, O_RDWR | O_NONBLOCK);
if (video_fd < 0)
return VA_STATUS_ERROR_OPERATION_FAILED;
rc = v4l2_query_capabilities(video_fd, &capabilities);
if (rc < 0) {
status = VA_STATUS_ERROR_OPERATION_FAILED;
goto error;
}
capabilities_required = V4L2_CAP_STREAMING;
if ((capabilities & capabilities_required) != capabilities_required) {
request_log("Missing required driver capabilities\n");
status = VA_STATUS_ERROR_OPERATION_FAILED;
goto error;
}
media_path = getenv("LIBVA_V4L2_REQUEST_MEDIA_PATH");
if (media_path == NULL) {
if (auto_media_set)
media_path = auto_media;
else
media_path = "/dev/media0";
}
media_fd = open(media_path, O_RDWR | O_NONBLOCK);
if (media_fd < 0)
return VA_STATUS_ERROR_OPERATION_FAILED;
driver_data->video_fd = video_fd;
driver_data->media_fd = media_fd;
status = VA_STATUS_SUCCESS;
goto complete;
error:
status = VA_STATUS_ERROR_OPERATION_FAILED;
if (video_fd >= 0)
close(video_fd);
if (media_fd >= 0)
close(media_fd);
complete:
return status;
}
VAStatus RequestTerminate(VADriverContextP context)
{
struct request_data *driver_data = context->pDriverData;
struct object_buffer *buffer_object;
struct object_image *image_object;
struct object_surface *surface_object;
struct object_context *context_object;
struct object_config *config_object;
int iterator;
/*
* Tear down the OUTPUT buffer pool before closing video_fd so
* the munmap calls in request_pool_destroy() can still touch the
* mmap regions (which are tied to that fd's lifetime).
*/
request_pool_destroy(&driver_data->output_pool);
close(driver_data->video_fd);
close(driver_data->media_fd);
/* Cleanup leftover buffers. */
image_object = (struct object_image *)
object_heap_first(&driver_data->image_heap, &iterator);
while (image_object != NULL) {
RequestDestroyImage(context, (VAImageID)image_object->base.id);
image_object = (struct object_image *)
object_heap_next(&driver_data->image_heap, &iterator);
}
object_heap_destroy(&driver_data->image_heap);
buffer_object = (struct object_buffer *)
object_heap_first(&driver_data->buffer_heap, &iterator);
while (buffer_object != NULL) {
RequestDestroyBuffer(context,
(VABufferID)buffer_object->base.id);
buffer_object = (struct object_buffer *)
object_heap_next(&driver_data->buffer_heap, &iterator);
}
object_heap_destroy(&driver_data->buffer_heap);
surface_object = (struct object_surface *)
object_heap_first(&driver_data->surface_heap, &iterator);
while (surface_object != NULL) {
RequestDestroySurfaces(context,
(VASurfaceID *)&surface_object->base.id, 1);
surface_object = (struct object_surface *)
object_heap_next(&driver_data->surface_heap, &iterator);
}
object_heap_destroy(&driver_data->surface_heap);
context_object = (struct object_context *)
object_heap_first(&driver_data->context_heap, &iterator);
while (context_object != NULL) {
RequestDestroyContext(context,
(VAContextID)context_object->base.id);
context_object = (struct object_context *)
object_heap_next(&driver_data->context_heap, &iterator);
}
object_heap_destroy(&driver_data->context_heap);
config_object = (struct object_config *)
object_heap_first(&driver_data->config_heap, &iterator);
while (config_object != NULL) {
RequestDestroyConfig(context,
(VAConfigID)config_object->base.id);
config_object = (struct object_config *)
object_heap_next(&driver_data->config_heap, &iterator);
}
object_heap_destroy(&driver_data->config_heap);
free(context->pDriverData);
context->pDriverData = NULL;
return VA_STATUS_SUCCESS;
}