Files
libva-v4l2-request-fourier/src/request.c
T
claude-noether 6df2159dd3 fresnel-fourier iter7 Phase 7 fix-forward: data links connect pads not entities directly
Empirical Phase 7 verification revealed the algorithm bug: data links
in MEDIA_IOC_G_TOPOLOGY connect PAD IDs, not entity IDs directly.
My iter7 Phase 6 commit compared link source_id/sink_id against
the proc entity_id, never matched → io_entity_ids stayed empty →
interface lookup never fired → returns -1 → falls back to legacy
hardcoded path.

Topology dump on fresnel /dev/media0 (rkvdec) confirmed:
- Entity 3 (rkvdec-proc) has function=0x4008 (DECODER) ✓
- Data link src=16777218 sink=16777220 — these are PAD ids
  (0x01000002, 0x01000004), NOT entity 3.
- Interface link src=50331660 (interface) sink=1 (entity) — for
  interface links source/sink ARE entity IDs.

Fix: resolve pads → entities via the topo.pads[] array.
1. Collect pads belonging to proc entity (via pads[].entity_id).
2. For each data link touching those pads, the OTHER pad's
   entity_id is an IO neighbor.
3. Find interface link to those IO entities (unchanged from prev).

Also allocate topo.pads[] in the 2-call ioctl pattern.

Signed-off-by: claude-noether <claude-noether@reauktion.de>
2026-05-13 11:00:20 +00:00

568 lines
18 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 + iter7 Phase 6 (B1a): device-path
* auto-detect via media controller topology with decoder-entity discrimination.
*
* Pre-iter4 the backend hardcoded /dev/video0 + /dev/media0. 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. iter4 commit Z replaced enumeration-order discovery with
* media-topology discovery.
*
* iter7 (B1a): the iter4 walk treated the hantro-vpu driver name as a single
* unit, but hantro-vpu registers BOTH encoder and decoder entities under one
* /dev/mediaN on RK3399. iter4's "pick the first V4L_VIDEO interface" could
* land on the encoder. iter7 walks ENTITIES looking for
* MEDIA_ENT_F_PROC_VIDEO_DECODER, then follows the kernel's link graph
* (data link from proc to IO entity, interface link from IO entity to V4L
* interface) to the correct /dev/videoN.
*
* Two-pass to prefer rkvdec: pass 1 accepts only "rkvdec" (multi-codec
* decoder, 3 of 5 codecs); pass 2 accepts any known decoder driver. On
* RK3399 this makes auto-detect always pick rkvdec when available.
*
* iter4-B1b (multi-decoder routing — open BOTH rkvdec AND hantro from one
* backend instance, dispatch per codec) is still deferred. Post-iter7 the
* backend opens one decoder per process; MPEG-2/VP8 (hantro) still need
* explicit LIBVA_V4L2_REQUEST_VIDEO_PATH override when iter7's pass-1
* lands on rkvdec.
*
* 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;
}
/*
* iter7 B1a: walk topology graph from decoder-proc entity to its V4L_VIDEO
* interface. Returns 0 + sets video_out on success, -1 if this media device
* has no decoder entity (e.g. encoder-only device).
*
* Algorithm (per Phase 5 review, empirically validated against
* boltzmann:~/src/linux-rockchip):
* 1. For each entity E with function == MEDIA_ENT_F_PROC_VIDEO_DECODER:
* 2. Find IO entity neighbors via DATA links (entity↔entity).
* 3. Find the V4L_VIDEO interface via INTERFACE links from those IO
* neighbors.
* 4. Resolve interface.devnode.major:minor to /dev/videoN.
*
* Two-call MEDIA_IOC_G_TOPOLOGY pattern (Phase 5 IMP-3): first call gets
* counts; second call fills the three arrays after we allocate them.
*
* Link discrimination via MEDIA_LNK_FL_INTERFACE_LINK (1U<<28):
* data links have flags & MEDIA_LNK_FL_INTERFACE_LINK == 0; interface
* links have it set. source_id/sink_id ordering is not guaranteed —
* check both endpoints.
*/
static int find_decoder_video_node_via_topology(int media_fd,
char *video_out,
size_t video_out_sz)
{
struct media_v2_topology topo;
struct media_v2_entity *entities = NULL;
struct media_v2_interface *interfaces = NULL;
struct media_v2_link *links = NULL;
struct media_v2_pad *pads = NULL;
int ret = -1;
unsigned int i, j;
memset(&topo, 0, sizeof topo);
if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topo) < 0)
return -1;
if (topo.num_entities == 0 || topo.num_interfaces == 0 ||
topo.num_links == 0 || topo.num_pads == 0)
return -1;
entities = calloc(topo.num_entities, sizeof *entities);
interfaces = calloc(topo.num_interfaces, sizeof *interfaces);
links = calloc(topo.num_links, sizeof *links);
pads = calloc(topo.num_pads, sizeof *pads);
if (!entities || !interfaces || !links || !pads)
goto out;
topo.ptr_entities = (uintptr_t)entities;
topo.ptr_interfaces = (uintptr_t)interfaces;
topo.ptr_links = (uintptr_t)links;
topo.ptr_pads = (uintptr_t)pads;
if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topo) < 0)
goto out;
for (i = 0; i < topo.num_entities; i++) {
uint32_t proc_id;
uint32_t proc_pad_ids[16];
uint32_t io_entity_ids[16];
unsigned int proc_pad_count = 0;
unsigned int io_count = 0;
if (entities[i].function != MEDIA_ENT_F_PROC_VIDEO_DECODER)
continue;
proc_id = entities[i].id;
/* Step 2a: collect pads belonging to the proc entity. Data
* links connect PADs, not entities directly. */
for (j = 0; j < topo.num_pads; j++) {
if (pads[j].entity_id != proc_id)
continue;
if (proc_pad_count < (sizeof proc_pad_ids /
sizeof proc_pad_ids[0]))
proc_pad_ids[proc_pad_count++] = pads[j].id;
}
/* Step 2b: walk data links. For each link with either endpoint
* in proc_pad_ids[], the other endpoint is a pad belonging to
* an IO neighbor. Resolve that pad's entity_id via pads[]. */
for (j = 0; j < topo.num_links; j++) {
uint32_t other_pad = 0;
unsigned int k;
if (links[j].flags & MEDIA_LNK_FL_INTERFACE_LINK)
continue;
for (k = 0; k < proc_pad_count; k++) {
if (links[j].source_id == proc_pad_ids[k])
other_pad = links[j].sink_id;
else if (links[j].sink_id == proc_pad_ids[k])
other_pad = links[j].source_id;
if (other_pad != 0)
break;
}
if (other_pad == 0)
continue;
/* Resolve other_pad to its entity_id. */
for (k = 0; k < topo.num_pads; k++) {
if (pads[k].id != other_pad)
continue;
if (io_count < (sizeof io_entity_ids /
sizeof io_entity_ids[0]))
io_entity_ids[io_count++] =
pads[k].entity_id;
break;
}
}
/* Step 3-4: find an interface link from any IO entity neighbor;
* resolve devnode for the linked V4L_VIDEO interface.
* Interface links connect interfaces↔entities directly (not
* via pads), so source_id/sink_id is an entity ID on one side
* and an interface ID on the other. */
for (j = 0; j < topo.num_links; j++) {
uint32_t intf_id = 0;
unsigned int k;
if (!(links[j].flags & MEDIA_LNK_FL_INTERFACE_LINK))
continue;
for (k = 0; k < io_count; k++) {
if (links[j].source_id == io_entity_ids[k])
intf_id = links[j].sink_id;
else if (links[j].sink_id == io_entity_ids[k])
intf_id = links[j].source_id;
if (intf_id != 0)
break;
}
if (intf_id == 0)
continue;
for (k = 0; k < topo.num_interfaces; k++) {
if (interfaces[k].id != intf_id)
continue;
if (interfaces[k].intf_type !=
MEDIA_INTF_T_V4L_VIDEO)
break;
if (resolve_dev_node(
interfaces[k].devnode.major,
interfaces[k].devnode.minor,
video_out, video_out_sz) == 0)
ret = 0;
break;
}
if (ret == 0)
goto out;
}
}
out:
free(entities);
free(interfaces);
free(links);
free(pads);
return ret;
}
/*
* iter7 B1a: two-pass walk of /dev/media0..N. Pass 1 accepts only "rkvdec"
* (multi-codec decoder serving 3 of 5 codecs). Pass 2 accepts any
* known_decoder_drivers entry. Within each pass, the chosen media device
* must ALSO contain at least one MEDIA_ENT_F_PROC_VIDEO_DECODER entity —
* guards against encoder-only devices that happen to share the same driver
* name (e.g. hantro-vpu encoder vs decoder inside one /dev/mediaN).
*/
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, pass;
for (pass = 0; pass < 2; pass++) {
for (i = 0; i < 16; i++) {
bool match;
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) {
close(fd);
continue;
}
if (pass == 0) {
/* Pass 1: rkvdec only. */
match = (strcmp(info.driver, "rkvdec") == 0);
} else {
/* Pass 2: any known decoder driver. */
match = false;
for (kd = known_decoder_drivers; *kd; kd++) {
if (strcmp(info.driver, *kd) == 0) {
match = true;
break;
}
}
}
if (!match) {
close(fd);
continue;
}
if (find_decoder_video_node_via_topology(
fd, video_out, video_out_sz) == 0) {
snprintf(media_out, media_out_sz, "%s", path);
close(fd);
return 0;
}
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;
}