voelin-media: record the AVFrame offsets of libavutil 61 instead of guessing

The DMA-BUF import reads AVFrame.buf[0] and hw_frames_ctx at offsets the
loader keeps in a table per libavutil major. The table ended in an open
range that answered every major from 60 up with 60's offsets, so on this
machine (libavutil 61) the import ran on an unverified guess, and a future
major would get the same treatment.

Compiled offsetof from the headers installed next to the library: libavutil
61 puts buf at 184 and hw_frames_ctx at 328, which is what 60 uses, so the
guess happened to hold. Record 61 explicitly and stop answering unknown
majors: AVFrame has shrunk between majors (424 bytes in 61 against 536 in
56), so an offset guessed for an unknown layout can point past the end of
the struct. An unlisted major now turns the import off with a reason and
leaves everything else working.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5 committed 2026-09-29 13:33:52 +03:00
1 parent 2f19a340f7
commit 4fac682cae
1 file changed
+55 -8
+55 -8
View File
@@ -13,11 +13,14 @@
//! | `AVFrame.pict_type`, `AVFrame.pts` | forcing a keyframe, timestamps | right after the leading fields: `key_frame` (removed in libavutil 60), `pict_type`, `sample_aspect_ratio`, `pts` | a fresh frame must read `pict_type` 0, `sample_aspect_ratio` {0, 1}, `pts` `AV_NOPTS_VALUE` in exactly one of the two layouts |
//! | `AVCodecContext.hw_frames_ctx` | VA-API input frames | next to fields that have AVOptions, whose offsets `av_opt_find` reports: before `hw_device_ctx`, `hwaccel_flags` (libavcodec 61 and later, after `err_recognition`), or two fields before `max_pixels` (up to 60) | the neighbours' option offsets must match the layout, and the field must be NULL in a new context |
//! | `AVHWFramesContext` `initial_pool_size`, `format`, `sw_format`, `width`, `height` | creating a frame pool | after seven pointers (eight up to libavutil 58: `internal`) | `device_ctx` must equal the device and both formats must be `AV_PIX_FMT_NONE` in a new context |
//! | `AVFrame.buf[0]`, `AVFrame.hw_frames_ctx` | importing DMA-BUFs (zero-copy) | a table per libavutil major (56-60, 64-bit only) | `buf[0]` of a frame from `av_frame_get_buffer` must hold that frame's `data[0]`; `hw_frames_ctx` of a frame from `av_hwframe_get_buffer` must reference the pool |
//! | `AVFrame.buf[0]`, `AVFrame.hw_frames_ctx` | importing DMA-BUFs (zero-copy) | a table per libavutil major (56-61, 64-bit only; an unlisted major disables the import) | `buf[0]` of a frame from `av_frame_get_buffer` must hold that frame's `data[0]`; `hw_frames_ctx` of a frame from `av_hwframe_get_buffer` must reference the pool |
//!
//! A check that fails disables only what needs the field (VA-API, or the
//! DMA-BUF import), with the reason in the probe results; a new major whose
//! layout matches keeps working without a change here.
//! DMA-BUF import), with the reason in the probe results. The first three
//! rows are found by searching, so a new major whose layout matches keeps
//! working without a change here; the last one is a table, because there is
//! nothing to anchor those offsets to, and an unlisted major turns the
//! DMA-BUF import off rather than guess (see [`frame_refs_table`]).
#![allow(unsafe_code)]
use std::ffi::c_int;
@@ -324,8 +327,19 @@ pub struct FrameRefs {
pub hw_frames_ctx: usize,
}
/// The table of [`FrameRefs`] per libavutil major (64-bit targets). A newer
/// major is tried with the newest entry and must pass the checks.
/// The table of [`FrameRefs`] per libavutil major (64-bit targets).
///
/// Every entry is `offsetof` compiled from that release's own
/// `libavutil/frame.h`; libavutil 61's was measured against the headers
/// installed next to the library it describes (FFmpeg 9.0.1: `buf` 184,
/// `hw_frames_ctx` 328, `sizeof(AVFrame)` 424).
///
/// A major that is not listed returns `None`, which disables the DMA-BUF
/// import (and nothing else) with the reason in the probe results. It
/// deliberately does not fall back to the newest entry: `AVFrame` has both
/// grown and shrunk between majors (424 bytes in 61 against 536 in 56), so
/// an offset guessed for an unknown layout could read past the end of the
/// struct. Adding a major here needs its `offsetof` values, not a guess.
fn frame_refs_table(avutil_major: u32) -> Option<FrameRefs> {
if size_of::<Ptr>() != 8 {
return None;
@@ -334,7 +348,7 @@ fn frame_refs_table(avutil_major: u32) -> Option<FrameRefs> {
56 => (288, 480),
57 | 58 => (224, 392),
59 => (200, 352),
60.. => (184, 328),
60 | 61 => (184, 328),
_ => return None,
};
Some(FrameRefs { buf, hw_frames_ctx })
@@ -346,8 +360,12 @@ fn frame_refs_table(avutil_major: u32) -> Option<FrameRefs> {
pub fn frame_refs(api: &Api, yuv420p: c_int) -> Result<FrameRefs, String> {
// SAFETY: no arguments.
let major = unsafe { (api.avutil_version)() } >> 16;
let refs = frame_refs_table(major)
.ok_or_else(|| format!("AVFrame references not known for libavutil {major} here"))?;
let refs = frame_refs_table(major).ok_or_else(|| {
format!(
"AVFrame.buf / hw_frames_ctx offsets are not recorded for libavutil {major} (see \
frame_refs_table); the import stays off until they are measured"
)
})?;
// SAFETY: a frame of 16x16 yuv420p pixels; the head fields are set
// before av_frame_get_buffer as its documentation asks; the offsets are
// inside AVFrame for this major (the table), pointer-aligned.
@@ -410,4 +428,33 @@ mod tests {
assert_eq!(frame_refs_table(58), Some(FrameRefs { buf: 224, hw_frames_ctx: 392 }));
assert_eq!(frame_refs_table(55), None);
}
/// libavutil 61 (FFmpeg 9), compiled from the installed headers on
/// x86-64: `AVFrame` `pict_type` 120, `pts` 136, `buf` 184,
/// `hw_frames_ctx` 328, `sizeof` 424; `AVCodecContext`
/// `err_recognition` 528, `hw_frames_ctx` 552, `hw_device_ctx` 560,
/// `hwaccel_flags` 568, `extra_hw_frames` 572; `AVHWFramesContext`
/// `pool` 48, `initial_pool_size` 56, `format` 60, `sw_format` 64,
/// `width` 68, `height` 72.
#[cfg(target_pointer_width = "64")]
#[test]
fn libavutil_61_matches_the_headers() {
assert_eq!(frame_refs_table(61), Some(FrameRefs { buf: 184, hw_frames_ctx: 328 }));
assert_eq!(offset_of!(FrameWithoutKeyFrame, pict_type), 120);
assert_eq!(offset_of!(FrameWithoutKeyFrame, pts), 136);
let base = 528;
assert_eq!(base + offset_of!(HwFieldsNew, hw_frames_ctx), 552);
assert_eq!(base + offset_of!(HwFieldsNew, hw_device_ctx), 560);
assert_eq!(base + offset_of!(HwFieldsNew, hwaccel_flags), 568);
assert_eq!(base + offset_of!(HwFieldsNew, extra_hw_frames), 572);
assert_eq!(offset_of!(HwFramesPublic, pool), 48);
assert_eq!(offset_of!(HwFramesPublic, initial_pool_size), 56);
assert_eq!(offset_of!(HwFramesPublic, sw_format), 64);
assert_eq!(offset_of!(HwFramesPublic, height), 72);
// A major nobody measured must disable the import, not reuse 61's
// offsets: AVFrame has shrunk between majors (424 bytes in 61
// against 536 in 56), so reading at a guessed offset could leave
// the struct.
assert_eq!(frame_refs_table(62), None);
}
}