Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
11ba7ad
feat(macos): deliver the decoded CVPixelBuffer instead of transferrin…
Oct 25, 2026
17774af
feat(macos): add Decoder::last_frames()
Oct 25, 2026
06178e7
test(macos): pin the per-call output ordering of the GPU-frame path
Oct 25, 2026
95b7489
fix(ffmpeg_ram): let the app refuse GPU frames before decode drops th…
Oct 26, 2026
4aaed9f
fix(ffmpeg_ram): make GPU-frame delivery opt-in and reject by default
Oct 27, 2026
cdae54c
fix(ffmpeg_ram): keep no `&mut DecodeOutput` alive across the FFI call
Oct 28, 2026
d7db609
fix(ffmpeg_ram): declare the pixel-buffer callback as a function poin…
Oct 28, 2026
7140e16
fix(ffmpeg_ram): validate the planes before the decode callback reads…
Oct 28, 2026
94d44e6
fix(ffmpeg_ram): declare the pixel-buffer callback type on every plat…
Oct 29, 2026
6e56e91
test(ffmpeg_ram): move the transaction tests out of decode.rs
ShawnLiann Nov 1, 2026
4d55fc3
refactor(ffmpeg_ram)!: decide GPU delivery per call and return one De…
ShawnLiann Nov 1, 2026
7a22ccc
refactor(ffmpeg_ram): keep decode() as it was and make the GPU decisi…
ShawnLiann Nov 5, 2026
b02ccaa
fix(ffmpeg_ram): drop a GPU frame that a failed call had already acce…
ShawnLiann Nov 6, 2026
bd6f339
fix(ffmpeg_ram): keep decode() returning the mutable frame vector
ShawnLiann Nov 6, 2026
a8782a3
fix(ffmpeg_ram): reject an unknown pixel format before building the f…
ShawnLiann Nov 6, 2026
c360325
style(ffmpeg_ram): satisfy rustfmt and correct two comments
ShawnLiann Nov 6, 2026
eb7920b
test(ffmpeg_ram): pin that an unknown software pixfmt produces no frame
ShawnLiann Nov 6, 2026
3b3589e
refactor(ffmpeg_ram): narrow the pixel-format helpers to crate scope
ShawnLiann Nov 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 111 additions & 13 deletions cpp/ffmpeg_ram/ffmpeg_ram_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ extern "C" {
#include <libavutil/hwcontext_d3d11va.h>
#endif

/* macOS: delivering the decoded CVPixelBuffer directly to the renderer
* (zero copy). See the proposed design in the PR description. */
#if defined(__APPLE__)
#include <TargetConditionals.h>
#if TARGET_OS_OSX
#include <CoreVideo/CoreVideo.h>
#define HWCODEC_HAVE_PIXELBUFFER 1
#endif
#endif

#include "common.h"
#include "system.h"

Expand All @@ -30,6 +40,34 @@ typedef void (*RamDecodeCallback)(const void *obj, int width, int height,
int linesize[AV_NUM_DATA_POINTERS],
uint8_t *data[AV_NUM_DATA_POINTERS], int key);

/* Kept in sync with cpp/ffmpeg_ram/ffmpeg_ram_ffi.h. Declared locally because
* this translation unit has its own RamDecodeCallback typedef.
*
* The frame struct and the callback typedef are deliberately declared on every
* platform, not only under HWCODEC_HAVE_PIXELBUFFER: the exported setter below
* takes a RamPixelBufferCallback in its signature on all targets (its body is a
* no-op where no pixel buffer exists), and MSVC rejects that signature with
* C2061 when the type is only declared inside the Apple guard. Nothing here
* depends on CoreVideo - the pixfmt constants used to compare against live in
* is_direct_deliverable, which stays guarded. */
#define PIXELBUFFER_REJECTED 0
#define PIXELBUFFER_ACCEPTED 1
struct FfiPixelBufferFrame {
void *pixel_buffer; /* borrowed CVPixelBufferRef, valid only during the call */
int32_t width;
int32_t height;
uint32_t pixfmt;
};
typedef int32_t (*RamPixelBufferCallback)(void *obj,
const FfiPixelBufferFrame *frame);

#ifdef HWCODEC_HAVE_PIXELBUFFER
static bool is_direct_deliverable(uint32_t pixfmt) {
return pixfmt == (uint32_t)kCVPixelFormatType_420YpCbCr8BiPlanarFullRange ||
pixfmt == (uint32_t)kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
}
#endif

class FFmpegRamDecoder {
public:
AVCodecContext *c_ = NULL;
Expand All @@ -43,6 +81,9 @@ class FFmpegRamDecoder {
AVHWDeviceType device_type_ = AV_HWDEVICE_TYPE_NONE;
int thread_count_ = 1;
RamDecodeCallback callback_ = NULL;
#ifdef HWCODEC_HAVE_PIXELBUFFER
RamPixelBufferCallback pixelbuffer_cb_ = NULL;
#endif
DataFormat data_format_;

#ifdef CFG_PKG_TRACE
Expand All @@ -58,6 +99,13 @@ class FFmpegRamDecoder {
this->callback_ = callback;
}

#ifdef HWCODEC_HAVE_PIXELBUFFER
/* Owner-only and idle-only: see ffmpeg_ram_set_pixelbuffer_callback(). */
void set_pixelbuffer_callback(RamPixelBufferCallback callback) {
this->pixelbuffer_cb_ = callback;
}
#endif

~FFmpegRamDecoder() {}

void free_decoder() {
Expand Down Expand Up @@ -192,23 +240,47 @@ class FFmpegRamDecoder {
goto _exit;
}

/* Reset per iteration: a previous frame's software frame must never be
* delivered twice, and a direct frame must never replay it. */
tmp_frame = NULL;
bool delivered_by_pixelbuffer = false;

if (hwaccel_) {
if (!frame_->hw_frames_ctx) {
LOG_ERROR(std::string("hw_frames_ctx is NULL"));
goto _exit;
}
if ((sw_frame_->width != 0 || sw_frame_->height != 0) &&
(sw_frame_->width != frame_->width ||
sw_frame_->height != frame_->height)) {
av_frame_unref(sw_frame_);
#ifdef HWCODEC_HAVE_PIXELBUFFER
if (pixelbuffer_cb_ != NULL &&
frame_->format == AV_PIX_FMT_VIDEOTOOLBOX &&
frame_->data[3] != NULL) {
CVPixelBufferRef pb = (CVPixelBufferRef)frame_->data[3];
uint32_t pixfmt = (uint32_t)CVPixelBufferGetPixelFormatType(pb);
if (is_direct_deliverable(pixfmt)) {
FfiPixelBufferFrame f{pb, frame_->width, frame_->height, pixfmt};
/* Borrowed descriptor: the callee retains what it keeps, so a
* rejected frame needs no cleanup here. */
delivered_by_pixelbuffer =
pixelbuffer_cb_((void *)obj, &f) == PIXELBUFFER_ACCEPTED;
}
/* Unsupported format or rejection falls through to the software
* transfer below, on this very frame_ (no re-decode). */
}
if ((ret = av_hwframe_transfer_data(sw_frame_, frame_, 0)) < 0) {
LOG_ERROR(std::string("av_hwframe_transfer_data failed, ret = ") +
av_err2str(ret));
goto _exit;
#endif
if (!delivered_by_pixelbuffer) {
if ((sw_frame_->width != 0 || sw_frame_->height != 0) &&
(sw_frame_->width != frame_->width ||
sw_frame_->height != frame_->height)) {
av_frame_unref(sw_frame_);
}
if ((ret = av_hwframe_transfer_data(sw_frame_, frame_, 0)) < 0) {
LOG_ERROR(std::string("av_hwframe_transfer_data failed, ret = ") +
av_err2str(ret));
goto _exit;
}

tmp_frame = sw_frame_;
}

tmp_frame = sw_frame_;
} else {
tmp_frame = frame_;
}
Expand All @@ -223,9 +295,13 @@ class FFmpegRamDecoder {
int key_frame = frame_->key_frame;
#endif

callback_(obj, tmp_frame->width, tmp_frame->height,
(AVPixelFormat)tmp_frame->format, tmp_frame->linesize,
tmp_frame->data, key_frame);
/* The software callback is skipped whenever the frame was delivered as a
* pixel buffer, so a frame reaches the renderer exactly once. */
if (!delivered_by_pixelbuffer && tmp_frame != NULL) {
callback_(obj, tmp_frame->width, tmp_frame->height,
(AVPixelFormat)tmp_frame->format, tmp_frame->linesize,
tmp_frame->data, key_frame);
}
}
_exit:
av_packet_unref(pkt_);
Expand Down Expand Up @@ -335,3 +411,25 @@ extern "C" int ffmpeg_ram_decode(FFmpegRamDecoder *decoder, const uint8_t *data,
}
return HWCODEC_ERR_COMMON;
}

/* Install (or clear, with NULL) the GPU-frame callback.
*
* Contract: must be called by the owner of the decoder while it is idle
* (before the first decode, or between two decodes). It is deliberately not
* synchronized against a concurrent decode() - the owner-serialized lifetime
* keeps the callback context alive, so no locking is needed here. */
extern "C" void ffmpeg_ram_set_pixelbuffer_callback(
void *decoder, RamPixelBufferCallback callback) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
#ifdef HWCODEC_HAVE_PIXELBUFFER
if (decoder == NULL) {
return;
}
/* Object pointer to object pointer: the header hands us `void *` so the symbol
* stays callable from Rust, where the decoder handle is opaque. The callback is
* NOT cast: it is a function pointer on both sides of the boundary. */
static_cast<FFmpegRamDecoder *>(decoder)->set_pixelbuffer_callback(callback);
#else
(void)decoder;
(void)callback;
#endif
}
31 changes: 31 additions & 0 deletions cpp/ffmpeg_ram/ffmpeg_ram_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ typedef void (*RamDecodeCallback)(const void *obj, int width, int height,
typedef void (*RamEncodeCallback)(const uint8_t *data, int len, int64_t pts,
int key, const void *obj);

/* GPU-frame delivery (used by macOS / VideoToolbox today).
*
* The callback is synchronous and the descriptor is BORROWED: `pixel_buffer`
* (a CVPixelBufferRef) is valid only during the invocation and the decoder
* performs its own frame cleanup right after it returns. The callee must
* retain the buffer (CVPixelBufferRetain) before keeping it.
*
* Return PIXELBUFFER_ACCEPTED when the frame was taken over (the decoder then
* skips the software transfer for that frame), or PIXELBUFFER_REJECTED when it
* was not; a rejected frame is delivered through the normal software path
* (same decoded frame, no re-decode). */
#define PIXELBUFFER_REJECTED 0
#define PIXELBUFFER_ACCEPTED 1

typedef struct FfiPixelBufferFrame {
void *pixel_buffer; /* borrowed CVPixelBufferRef, valid only during the call */
int32_t width;
int32_t height;
uint32_t pixfmt; /* e.g. '420f' / '420v' */
} FfiPixelBufferFrame;

typedef int32_t (*RamPixelBufferCallback)(void *obj,
const FfiPixelBufferFrame *frame);

void *ffmpeg_ram_new_encoder(const char *name, const char *mc_name, int width,
int height, int pixfmt, int align, int fps,
int gop, int rc, int quality, int kbs, int q,
Expand All @@ -31,4 +55,11 @@ int ffmpeg_ram_get_linesize_offset_length(int pix_fmt, int width, int height,
int *length);
int ffmpeg_ram_set_bitrate(void *encoder, int kbs);

/* Install (or clear, with NULL) the GPU-frame callback. Must be called by the
* owner of the decoder, only while it is idle: before the first
* ffmpeg_ram_decode() call or in between two calls. Not thread safe against a
* concurrent decode. */
void ffmpeg_ram_set_pixelbuffer_callback(void *decoder,
RamPixelBufferCallback callback);

#endif // FFMPEG_RAM_FFI_H
Loading