From 3684d4e24fe1a858a55e78e2cf4d440910e792fc Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:54:06 -0400 Subject: [PATCH 1/6] libobs: Move the async frame copy plan into obs-source-frame-copy.c Behaviour-neutral extraction before the Rust port. The decisions of the async frame copy and cache in obs-source.c.h move to libobs/obs-source-frame-copy.c.h, declared in the new libobs/obs-source-frame-copy.h without EXPORT (hidden, as -fvisibility=hidden keeps them), as six functions on plain values: - obs_source_frame_copy_plan(format, full_range, height, src, dst): copy_frame_data()'s copies. The colour range is copied only when the frame is not full range; then, per format, each plane's rows ((height + 1) / 2 for 4:2:0 chroma), one copy of linesize x rows in size_t when the two line sizes are equal (copy_frame_data_plane()), else rows of the smaller line size (copy_frame_data_line()). P216, P416 and values outside the enum copy no plane. The switch moved as it was. The line sizes cross as a struct of four uint32_t, by value. - obs_source_frame_copy_cache_step(queued, cache, frame): cache_video()'s queue limit (MAX_ASYNC_FRAMES or more queued: clear and refuse), then async_texture_changed() (the size or the convert type differs: clear and take the size), else keep. - obs_source_frame_copy_cache_clean(used, unused_count): one entry of clean_cache(): an unused entry counts one more call and is dropped at MAX_UNUSED_FRAME_DURATION. unused_count stays long. - obs_source_frame_copy_full_range() and _full_range2(): the range rule of obs_source_output_video(), obs_source_preload_video() and obs_source_set_video_frame() (a frame that is not YUV is full range), and of their *2() variants (resolve_video_range() gives VIDEO_RANGE_FULL). - obs_source_frame_copy_preload_changed(): preload_frame_changed(). obs-source.c.h keeps every memcpy(): copy_frame_data() still copies the metadata and the colour matrix, then gets the plan and runs it, plane i from plane i, each row offset y * line size in uint32_t as before. cache_video() keeps async_mutex, free_async_cache(), the fields it stores, the walk that picks the first unused entry (#2009's prev_frame reset included), obs_source_frame_create(), the darrays and the refcount atomics; it passes the two convert types from get_convert_type(). clean_cache() keeps the walk, the destroy and the erase, and stores the count only into an unused entry, as before. MAX_ASYNC_FRAMES and MAX_UNUSED_FRAME_DURATION stay where they are, checked against the header's copies at compile time, as are the plan's 4 planes against MAX_AV_PLANES. What the C now reads that it did not read before. None of these reads changes a result: - copy_frame_data() reads the first four line sizes of both frames for every format; - cache_video() asks get_convert_type() for both convert types before the queue limit (a pure function; before, only a frame under the limit asked); - preload_frame_changed() reads all three fields of both frames when there is a preload frame (before, the || stopped at the first difference); - the *2() variants resolve the range where they store it, not first. Checked against the unchanged functions of origin/master (outside the tree, not committed): a scratch harness compiles master's copy_frame_data_line() through cache_video(), preload_frame_changed() and the six range lines verbatim next to the moved code, with memcpy(), obs_source_frame_create() and _destroy() hooked, against the C backend's libobs: - 2,742,507 copies with fake plane pointers (every format from -3 to 32 and the int ends against all 625 patterns of the four line sizes, equal, smaller, larger, 0 or any, at 11 heights; then random formats, heights up to UINT32_MAX and full-range line sizes) make the same 697,179,469 memcpy() calls (destination, source, size, in order) and leave the same destination fields; - 200,000 copies on real buffers write the same bytes; - 400 sequences of 2000 steps through cache_video() with a model of obs_source_output_video_internal() and the renderer (frames queued, released, the queue stalled to its limit, 1949 frames refused there, and obs_source_output_video(NULL)) leave the same cache entries (frame, used, unused count, fields), queue, cache size, format, range, transfer, last_frame_ts and frame pointers after every step, and make the same memcpy() calls; - 200,000 clean_cache() runs over entries of any used flag and count (26,755 dropped); - every preload pair over sizes and formats around the edges, and the range rule over formats -300 to 300 and the int ends, both ranges and every video_range_type from -5 to 10. Under ASan and UBSan (halt on error, leak detection), a run of 1,142,507 fake copies, 40,000 real ones and 80 sequences reports nothing. 13 of 14 deliberately broken moves fail the harness; the one that survives (storing a used entry's count back) is equivalent. git log -L on each moved function shows only upstream history, plus the fork's #2048 (#2009), whose prev_frame reset stays in cache_video(). The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs #2192 --- libobs/build/libobs.core-source+.h.c | 1 + libobs/build/libobs.pch.h | 1 + libobs/build/source-sets.json | 1 + libobs/obs-source-frame-copy.c.h | 184 +++++++++++++++++++++++++++ libobs/obs-source-frame-copy.h | 169 ++++++++++++++++++++++++ libobs/obs-source.c.h | 181 ++++++++++++-------------- 6 files changed, 434 insertions(+), 103 deletions(-) create mode 100644 libobs/obs-source-frame-copy.c.h create mode 100644 libobs/obs-source-frame-copy.h diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index c4cf0c47b3..cd51967df2 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -7,6 +7,7 @@ #include "libobs/obs-scene-item-audio-actions.c.h" #include "libobs/obs-scene.c.h" #include "libobs/obs-source-deinterlace.c.h" +#include "libobs/obs-source-frame-copy.c.h" #include "libobs/obs-source-transition.c.h" #include "libobs/obs-source.c.h" #include "libobs/obs-view.c.h" diff --git a/libobs/build/libobs.pch.h b/libobs/build/libobs.pch.h index be39ff0bcb..3840cf5b2d 100644 --- a/libobs/build/libobs.pch.h +++ b/libobs/build/libobs.pch.h @@ -148,6 +148,7 @@ #include "obs-source-color-space.h" #include "obs-source-conversion-plan.h" #include "obs-source-draw-plan.h" +#include "obs-source-frame-copy.h" #include "obs-source-ptm-gate.h" #include "obs-source-render-route.h" #include "obs-source-size.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index a3ec012096..493c150f29 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -54,6 +54,7 @@ "obs-source-deinterlace-pacing.h", "obs-source-draw-plan.h", "obs-source-draw-technique.h", + "obs-source-frame-copy.h", "obs-source-load.h", "obs-source-ptm-gate.h", "obs-source-render-route.h", diff --git a/libobs/obs-source-frame-copy.c.h b/libobs/obs-source-frame-copy.c.h new file mode 100644 index 0000000000..0dd267d550 --- /dev/null +++ b/libobs/obs-source-frame-copy.c.h @@ -0,0 +1,184 @@ +/****************************************************************************** + Copyright (C) 2023 by Lain Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include "libobs/build/libobs.pch.h" + +/* The async frame copy plan and cache decisions, moved from obs-source.c: + * the plane copies of copy_frame_data_line(), copy_frame_data_plane() and + * copy_frame_data(), async_texture_changed() and the queue limit of + * cache_video(), one entry of clean_cache(), the full-range rule of the + * obs_source_output_video(), _preload_video() and _set_video_frame() + * families, and preload_frame_changed(), on plain values. obs-source.c runs + * the copies and keeps the frames, the cache and the locks. Declared in + * obs-source-frame-copy.h without EXPORT. */ + +_Static_assert(sizeof(enum video_format) == sizeof(int), "enum video_format is passed as int"); +_Static_assert(sizeof(enum video_range_type) == sizeof(int), "enum video_range_type is passed as int"); + +/* copy_frame_data_plane(): one copy when the line sizes are equal, else + * lines rows of the smaller line size. */ +static inline void frame_copy_plane(struct obs_source_frame_copy_plan *plan, uint32_t plane, uint32_t lines, + const struct obs_source_frame_copy_lines *src, + const struct obs_source_frame_copy_lines *dst) +{ + struct obs_source_frame_plane_copy *copy = &plan->planes[plan->count++]; + const uint32_t src_linesize = src->linesize[plane]; + const uint32_t dst_linesize = dst->linesize[plane]; + + copy->rows = lines; + copy->src_stride = src_linesize; + copy->dst_stride = dst_linesize; + + if (dst_linesize != src_linesize) { + copy->whole = false; + copy->bytes = dst_linesize < src_linesize ? dst_linesize : src_linesize; + } else { + copy->whole = true; + copy->bytes = (size_t)dst_linesize * (size_t)lines; + } +} + +struct obs_source_frame_copy_plan obs_source_frame_copy_plan(int format, bool full_range, uint32_t height, + struct obs_source_frame_copy_lines src, + struct obs_source_frame_copy_lines dst) +{ + struct obs_source_frame_copy_plan plan; + memset(&plan, 0, sizeof(plan)); + + plan.color_range = !full_range; + + switch ((enum video_format)format) { + case VIDEO_FORMAT_I420: + case VIDEO_FORMAT_I010: { + const uint32_t half_height = (height + 1) / 2; + frame_copy_plane(&plan, 0, height, &src, &dst); + frame_copy_plane(&plan, 1, half_height, &src, &dst); + frame_copy_plane(&plan, 2, half_height, &src, &dst); + break; + } + + case VIDEO_FORMAT_NV12: + case VIDEO_FORMAT_P010: { + const uint32_t half_height = (height + 1) / 2; + frame_copy_plane(&plan, 0, height, &src, &dst); + frame_copy_plane(&plan, 1, half_height, &src, &dst); + break; + } + + case VIDEO_FORMAT_I444: + case VIDEO_FORMAT_I422: + case VIDEO_FORMAT_I210: + case VIDEO_FORMAT_I412: + frame_copy_plane(&plan, 0, height, &src, &dst); + frame_copy_plane(&plan, 1, height, &src, &dst); + frame_copy_plane(&plan, 2, height, &src, &dst); + break; + + case VIDEO_FORMAT_YVYU: + case VIDEO_FORMAT_YUY2: + case VIDEO_FORMAT_UYVY: + case VIDEO_FORMAT_NONE: + case VIDEO_FORMAT_RGBA: + case VIDEO_FORMAT_BGRA: + case VIDEO_FORMAT_BGRX: + case VIDEO_FORMAT_Y800: + case VIDEO_FORMAT_BGR3: + case VIDEO_FORMAT_AYUV: + case VIDEO_FORMAT_V210: + case VIDEO_FORMAT_R10L: + frame_copy_plane(&plan, 0, height, &src, &dst); + break; + + case VIDEO_FORMAT_I40A: { + const uint32_t half_height = (height + 1) / 2; + frame_copy_plane(&plan, 0, height, &src, &dst); + frame_copy_plane(&plan, 1, half_height, &src, &dst); + frame_copy_plane(&plan, 2, half_height, &src, &dst); + frame_copy_plane(&plan, 3, height, &src, &dst); + break; + } + + case VIDEO_FORMAT_I42A: + case VIDEO_FORMAT_YUVA: + case VIDEO_FORMAT_YA2L: + frame_copy_plane(&plan, 0, height, &src, &dst); + frame_copy_plane(&plan, 1, height, &src, &dst); + frame_copy_plane(&plan, 2, height, &src, &dst); + frame_copy_plane(&plan, 3, height, &src, &dst); + break; + + case VIDEO_FORMAT_P216: + case VIDEO_FORMAT_P416: + /* Unimplemented */ + break; + } + + return plan; +} + +/* async_texture_changed(): the size or the convert type differs. */ +static inline bool frame_copy_texture_changed(struct obs_source_frame_copy_key cache, + struct obs_source_frame_copy_key frame) +{ + return cache.width != frame.width || cache.height != frame.height || cache.convert_type != frame.convert_type; +} + +enum obs_source_frame_cache_step obs_source_frame_copy_cache_step(size_t queued, struct obs_source_frame_copy_key cache, + struct obs_source_frame_copy_key frame) +{ + if (queued >= OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES) + return OBS_SOURCE_FRAME_CACHE_OVERFLOW; + + if (frame_copy_texture_changed(cache, frame)) + return OBS_SOURCE_FRAME_CACHE_RESET; + + return OBS_SOURCE_FRAME_CACHE_KEEP; +} + +struct obs_source_frame_cache_age obs_source_frame_copy_cache_clean(bool used, long unused_count) +{ + struct obs_source_frame_cache_age age; + memset(&age, 0, sizeof(age)); + + age.unused_count = unused_count; + if (!used) { + age.counted = true; + if (++age.unused_count == OBS_SOURCE_FRAME_COPY_MAX_UNUSED) + age.drop = true; + } + + return age; +} + +bool obs_source_frame_copy_full_range(int format, bool full_range) +{ + return format_is_yuv((enum video_format)format) ? full_range : true; +} + +bool obs_source_frame_copy_full_range2(int format, int range) +{ + return resolve_video_range((enum video_format)format, (enum video_range_type)range) == VIDEO_RANGE_FULL; +} + +bool obs_source_frame_copy_preload_changed(bool has_preload, struct obs_source_frame_copy_shape preload, + struct obs_source_frame_copy_shape in) +{ + if (!has_preload) + return true; + + return in.width != preload.width || in.height != preload.height || in.format != preload.format; +} diff --git a/libobs/obs-source-frame-copy.h b/libobs/obs-source-frame-copy.h new file mode 100644 index 0000000000..0a6c57f427 --- /dev/null +++ b/libobs/obs-source-frame-copy.h @@ -0,0 +1,169 @@ +/****************************************************************************** + Copyright (C) 2023 by Lain Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#include +#include +#include + +/* The async frame copy plan and cache decisions of obs-source.c, on plain + * values (obs-source-frame-copy.c): + * - obs_source_frame_copy_plan(): what copy_frame_data() copies: the colour + * range only when the frame is not full range, and for each plane of the + * format the rows ((height + 1) / 2 for 4:2:0 chroma), as one copy when + * the two line sizes are equal, else row by row with the smaller line + * size. P216, P416 and values outside the enum copy no plane; + * - obs_source_frame_copy_cache_step(): cache_video()'s first two + * decisions: with MAX_ASYNC_FRAMES or more frames queued, clear the cache + * and refuse the frame; when the frame's size or convert type differs from + * the cache's (async_texture_changed()), clear the cache and take the + * frame's size; else keep the cache; + * - obs_source_frame_copy_cache_clean(): one entry of clean_cache(): an + * unused entry counts one more call, and is dropped at + * MAX_UNUSED_FRAME_DURATION; + * - obs_source_frame_copy_full_range() and _full_range2(): the range of a + * frame given to obs_source_output_video(), obs_source_preload_video() + * and obs_source_set_video_frame() (a frame that is not YUV is full + * range), and to their *2() variants (the range resolve_video_range() + * gives is VIDEO_RANGE_FULL); + * - obs_source_frame_copy_preload_changed(): preload_frame_changed(): + * there is no preload frame, or its size or format differs. + * + * obs-source.c keeps every memcpy() (it runs the plan), the metadata copy, + * obs_source_frame_create() and _destroy(), the async_cache and + * async_frames darrays and the walk that picks the first unused entry, + * async_mutex, the refcount atomics, and get_convert_type() (it passes the + * two convert types). Nothing here takes a lock, calls into libobs, touches + * a frame or keeps state. + * + * The enums cross as int, so this header needs no media-io header. + * obs-source.c checks the sizes and the constants. + * + * Hidden in libobs: not EXPORT. */ + +/* The planes a plan can copy: I40A, I42A, YUVA and YA2L have 4. */ +#define OBS_SOURCE_FRAME_COPY_PLANES 4 + +/* MAX_ASYNC_FRAMES of obs-source.c: the queued frames at which + * cache_video() clears the cache and refuses the frame. */ +#define OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES 30 + +/* MAX_UNUSED_FRAME_DURATION of obs-source.c: the clean_cache() calls after + * which an unused cache entry is dropped. */ +#define OBS_SOURCE_FRAME_COPY_MAX_UNUSED 5 + +/* The first OBS_SOURCE_FRAME_COPY_PLANES line sizes of a frame. */ +struct obs_source_frame_copy_lines { + uint32_t linesize[OBS_SOURCE_FRAME_COPY_PLANES]; +}; + +/* The copy of one plane, from the start of the source plane into the start + * of the destination plane. */ +struct obs_source_frame_plane_copy { + /* The two line sizes are equal: one memcpy() of bytes. */ + bool whole; + /* whole: the line size times rows, in size_t. Otherwise the bytes of + * each row, the smaller line size. */ + size_t bytes; + /* The rows of the plane. Not whole: row y is copied from + * y * src_stride into y * dst_stride, each product in uint32_t (it + * wraps). Set either way. */ + uint32_t rows; + /* The source and destination line sizes. */ + uint32_t src_stride; + uint32_t dst_stride; +}; + +/* The copies of copy_frame_data(): copy i is plane i, in this order, after + * the colour range when color_range is set. */ +struct obs_source_frame_copy_plan { + /* Copy color_range_min and color_range_max: the frame is not full + * range. */ + bool color_range; + /* The planes to copy, at most OBS_SOURCE_FRAME_COPY_PLANES. */ + size_t count; + struct obs_source_frame_plane_copy planes[OBS_SOURCE_FRAME_COPY_PLANES]; +}; + +/* What async_texture_changed() compares: a size and a convert type (an + * enum convert_type value, from get_convert_type()). */ +struct obs_source_frame_copy_key { + uint32_t width; + uint32_t height; + int convert_type; +}; + +/* What cache_video() does before it picks a frame. */ +enum obs_source_frame_cache_step { + /* Keep the cache: its size and convert type match the frame's. */ + OBS_SOURCE_FRAME_CACHE_KEEP, + /* free_async_cache(), then take the frame's width and height. */ + OBS_SOURCE_FRAME_CACHE_RESET, + /* free_async_cache(), last_frame_ts = 0, and no frame: the queue is + * full. */ + OBS_SOURCE_FRAME_CACHE_OVERFLOW, +}; + +/* One entry of clean_cache(). */ +struct obs_source_frame_cache_age { + /* The entry is unused: store unused_count into it. */ + bool counted; + /* The entry's count after this call. */ + long unused_count; + /* Destroy the entry's frame and erase the entry. */ + bool drop; +}; + +/* What preload_frame_changed() compares of a frame: an enum video_format + * value, the width and the height. */ +struct obs_source_frame_copy_shape { + int format; + uint32_t width; + uint32_t height; +}; + +/* copy_frame_data(): the copies from a frame of format (an enum + * video_format value) and range full_range into one height rows high, with + * the source's line sizes src and the destination's dst. */ +struct obs_source_frame_copy_plan obs_source_frame_copy_plan(int format, bool full_range, uint32_t height, + struct obs_source_frame_copy_lines src, + struct obs_source_frame_copy_lines dst); + +/* cache_video(): queued is async_frames.num, cache the cache's size + * (async_cache_width and _height) and the convert type of its format, range + * and transfer, frame the frame's. */ +enum obs_source_frame_cache_step obs_source_frame_copy_cache_step(size_t queued, struct obs_source_frame_copy_key cache, + struct obs_source_frame_copy_key frame); + +/* clean_cache(): an entry's used flag and unused_count. */ +struct obs_source_frame_cache_age obs_source_frame_copy_cache_clean(bool used, long unused_count); + +/* obs_source_output_video(), obs_source_preload_video() and + * obs_source_set_video_frame(): the full_range a frame of format is given + * (format is an enum video_format value). */ +bool obs_source_frame_copy_full_range(int format, bool full_range); + +/* obs_source_output_video2(), obs_source_preload_video2() and + * obs_source_set_video_frame2(): the full_range a frame of format with + * range (an enum video_range_type value) is given. */ +bool obs_source_frame_copy_full_range2(int format, int range); + +/* preload_frame_changed(): has_preload is async_preload_frame != NULL, + * preload its shape (not looked at without one), in the new frame's. */ +bool obs_source_frame_copy_preload_changed(bool has_preload, struct obs_source_frame_copy_shape preload, + struct obs_source_frame_copy_shape in); diff --git a/libobs/obs-source.c.h b/libobs/obs-source.c.h index af946ce250..88a6af02e4 100644 --- a/libobs/obs-source.c.h +++ b/libobs/obs-source.c.h @@ -2878,24 +2878,32 @@ struct obs_source_frame *filter_async_video(obs_source_t *source, struct obs_sou return in; } -static inline void copy_frame_data_line(struct obs_source_frame *dst, const struct obs_source_frame *src, - uint32_t plane, uint32_t y) +_Static_assert(OBS_SOURCE_FRAME_COPY_PLANES <= MAX_AV_PLANES, "a copy plan's planes are frame planes"); + +static inline struct obs_source_frame_copy_lines frame_copy_lines(const struct obs_source_frame *frame) { - uint32_t pos_src = y * src->linesize[plane]; - uint32_t pos_dst = y * dst->linesize[plane]; - uint32_t bytes = dst->linesize[plane] < src->linesize[plane] ? dst->linesize[plane] : src->linesize[plane]; + struct obs_source_frame_copy_lines lines; + + for (size_t i = 0; i < OBS_SOURCE_FRAME_COPY_PLANES; i++) + lines.linesize[i] = frame->linesize[i]; - memcpy(dst->data[plane] + pos_dst, src->data[plane] + pos_src, bytes); + return lines; } -static inline void copy_frame_data_plane(struct obs_source_frame *dst, const struct obs_source_frame *src, - uint32_t plane, uint32_t lines) +/* Runs one copy of the plan: copy_frame_data_plane() and + * copy_frame_data_line(). */ +static inline void copy_frame_data_plane(struct obs_source_frame *dst, const struct obs_source_frame *src, size_t plane, + const struct obs_source_frame_plane_copy *copy) { - if (dst->linesize[plane] != src->linesize[plane]) { - for (uint32_t y = 0; y < lines; y++) - copy_frame_data_line(dst, src, plane, y); + if (!copy->whole) { + for (uint32_t y = 0; y < copy->rows; y++) { + uint32_t pos_src = y * copy->src_stride; + uint32_t pos_dst = y * copy->dst_stride; + + memcpy(dst->data[plane] + pos_dst, src->data[plane] + pos_src, copy->bytes); + } } else { - memcpy(dst->data[plane], src->data[plane], (size_t)dst->linesize[plane] * (size_t)lines); + memcpy(dst->data[plane], src->data[plane], copy->bytes); } } @@ -2908,80 +2916,18 @@ static void copy_frame_data(struct obs_source_frame *dst, const struct obs_sourc dst->max_luminance = src->max_luminance; dst->timestamp = src->timestamp; memcpy(dst->color_matrix, src->color_matrix, sizeof(float) * 16); - if (!dst->full_range) { + + const struct obs_source_frame_copy_plan plan = obs_source_frame_copy_plan( + src->format, dst->full_range, dst->height, frame_copy_lines(src), frame_copy_lines(dst)); + + if (plan.color_range) { size_t const size = sizeof(float) * 3; memcpy(dst->color_range_min, src->color_range_min, size); memcpy(dst->color_range_max, src->color_range_max, size); } - switch (src->format) { - case VIDEO_FORMAT_I420: - case VIDEO_FORMAT_I010: { - const uint32_t height = dst->height; - const uint32_t half_height = (height + 1) / 2; - copy_frame_data_plane(dst, src, 0, height); - copy_frame_data_plane(dst, src, 1, half_height); - copy_frame_data_plane(dst, src, 2, half_height); - break; - } - - case VIDEO_FORMAT_NV12: - case VIDEO_FORMAT_P010: { - const uint32_t height = dst->height; - const uint32_t half_height = (height + 1) / 2; - copy_frame_data_plane(dst, src, 0, height); - copy_frame_data_plane(dst, src, 1, half_height); - break; - } - - case VIDEO_FORMAT_I444: - case VIDEO_FORMAT_I422: - case VIDEO_FORMAT_I210: - case VIDEO_FORMAT_I412: - copy_frame_data_plane(dst, src, 0, dst->height); - copy_frame_data_plane(dst, src, 1, dst->height); - copy_frame_data_plane(dst, src, 2, dst->height); - break; - - case VIDEO_FORMAT_YVYU: - case VIDEO_FORMAT_YUY2: - case VIDEO_FORMAT_UYVY: - case VIDEO_FORMAT_NONE: - case VIDEO_FORMAT_RGBA: - case VIDEO_FORMAT_BGRA: - case VIDEO_FORMAT_BGRX: - case VIDEO_FORMAT_Y800: - case VIDEO_FORMAT_BGR3: - case VIDEO_FORMAT_AYUV: - case VIDEO_FORMAT_V210: - case VIDEO_FORMAT_R10L: - copy_frame_data_plane(dst, src, 0, dst->height); - break; - - case VIDEO_FORMAT_I40A: { - const uint32_t height = dst->height; - const uint32_t half_height = (height + 1) / 2; - copy_frame_data_plane(dst, src, 0, height); - copy_frame_data_plane(dst, src, 1, half_height); - copy_frame_data_plane(dst, src, 2, half_height); - copy_frame_data_plane(dst, src, 3, height); - break; - } - - case VIDEO_FORMAT_I42A: - case VIDEO_FORMAT_YUVA: - case VIDEO_FORMAT_YA2L: - copy_frame_data_plane(dst, src, 0, dst->height); - copy_frame_data_plane(dst, src, 1, dst->height); - copy_frame_data_plane(dst, src, 2, dst->height); - copy_frame_data_plane(dst, src, 3, dst->height); - break; - - case VIDEO_FORMAT_P216: - case VIDEO_FORMAT_P416: - /* Unimplemented */ - break; - } + for (size_t i = 0; i < plan.count && i < OBS_SOURCE_FRAME_COPY_PLANES; i++) + copy_frame_data_plane(dst, src, i, &plan.planes[i]); } void obs_source_frame_copy(struct obs_source_frame *dst, const struct obs_source_frame *src) @@ -2989,13 +2935,27 @@ void obs_source_frame_copy(struct obs_source_frame *dst, const struct obs_source copy_frame_data(dst, src); } -static inline bool async_texture_changed(struct obs_source *source, const struct obs_source_frame *frame) +/* What async_texture_changed() compared: the cache's size and convert type, + * and the frame's. */ +static inline struct obs_source_frame_copy_key async_cache_key(const struct obs_source *source) { - enum convert_type prev, cur; - prev = get_convert_type(source->async_cache_format, source->async_cache_full_range, source->async_cache_trc); - cur = get_convert_type(frame->format, frame->full_range, frame->trc); + const struct obs_source_frame_copy_key key = { + .width = source->async_cache_width, + .height = source->async_cache_height, + .convert_type = (int)get_convert_type(source->async_cache_format, source->async_cache_full_range, + source->async_cache_trc), + }; + return key; +} - return source->async_cache_width != frame->width || source->async_cache_height != frame->height || prev != cur; +static inline struct obs_source_frame_copy_key async_frame_key(const struct obs_source_frame *frame) +{ + const struct obs_source_frame_copy_key key = { + .width = frame->width, + .height = frame->height, + .convert_type = (int)get_convert_type(frame->format, frame->full_range, frame->trc), + }; + return key; } static inline void free_async_cache(struct obs_source *source) @@ -3010,6 +2970,7 @@ static inline void free_async_cache(struct obs_source *source) } #define MAX_UNUSED_FRAME_DURATION 5 +_Static_assert(MAX_UNUSED_FRAME_DURATION == OBS_SOURCE_FRAME_COPY_MAX_UNUSED, "MAX_UNUSED_FRAME_DURATION"); /* frees frame allocations if they haven't been used for a specific period * of time */ @@ -3017,8 +2978,11 @@ static void clean_cache(obs_source_t *source) { for (size_t i = source->async_cache.num; i > 0; i--) { struct async_frame *af = &source->async_cache.array[i - 1]; - if (!af->used) { - if (++af->unused_count == MAX_UNUSED_FRAME_DURATION) { + const struct obs_source_frame_cache_age age = + obs_source_frame_copy_cache_clean(af->used, af->unused_count); + if (age.counted) { + af->unused_count = age.unused_count; + if (age.drop) { obs_source_frame_destroy(af->frame); da_erase(source->async_cache, i - 1); } @@ -3027,6 +2991,7 @@ static void clean_cache(obs_source_t *source) } #define MAX_ASYNC_FRAMES 30 +_Static_assert(MAX_ASYNC_FRAMES == OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES, "MAX_ASYNC_FRAMES"); //if return value is not null then do (os_atomic_dec_long(&output->refs) == 0) && obs_source_frame_destroy(output) static inline struct obs_source_frame *cache_video(struct obs_source *source, const struct obs_source_frame *frame) { @@ -3034,14 +2999,17 @@ static inline struct obs_source_frame *cache_video(struct obs_source *source, co pthread_mutex_lock(&source->async_mutex); - if (source->async_frames.num >= MAX_ASYNC_FRAMES) { + const enum obs_source_frame_cache_step step = obs_source_frame_copy_cache_step( + source->async_frames.num, async_cache_key(source), async_frame_key(frame)); + + if (step == OBS_SOURCE_FRAME_CACHE_OVERFLOW) { free_async_cache(source); source->last_frame_ts = 0; pthread_mutex_unlock(&source->async_mutex); return NULL; } - if (async_texture_changed(source, frame)) { + if (step == OBS_SOURCE_FRAME_CACHE_RESET) { free_async_cache(source); source->async_cache_width = frame->width; source->async_cache_height = frame->height; @@ -3135,7 +3103,7 @@ void obs_source_output_video(obs_source_t *source, const struct obs_source_frame } struct obs_source_frame new_frame = *frame; - new_frame.full_range = format_is_yuv(frame->format) ? new_frame.full_range : true; + new_frame.full_range = obs_source_frame_copy_full_range(frame->format, new_frame.full_range); obs_source_output_video_internal(source, &new_frame); } @@ -3150,7 +3118,6 @@ void obs_source_output_video2(obs_source_t *source, const struct obs_source_fram } struct obs_source_frame new_frame = {0}; - enum video_range_type range = resolve_video_range(frame->format, frame->range); for (size_t i = 0; i < MAX_AV_PLANES; i++) { new_frame.data[i] = frame->data[i]; @@ -3161,7 +3128,7 @@ void obs_source_output_video2(obs_source_t *source, const struct obs_source_fram new_frame.height = frame->height; new_frame.timestamp = frame->timestamp; new_frame.format = frame->format; - new_frame.full_range = range == VIDEO_RANGE_FULL; + new_frame.full_range = obs_source_frame_copy_full_range2(frame->format, frame->range); new_frame.max_luminance = 0; new_frame.flip = frame->flip; new_frame.flags = frame->flags; @@ -3229,13 +3196,23 @@ void obs_source_remove_caption_callback(obs_source_t *source, obs_source_caption pthread_mutex_unlock(&source->caption_cb_mutex); } +static inline struct obs_source_frame_copy_shape frame_copy_shape(const struct obs_source_frame *frame) +{ + const struct obs_source_frame_copy_shape shape = { + .format = frame->format, + .width = frame->width, + .height = frame->height, + }; + return shape; +} + static inline bool preload_frame_changed(obs_source_t *source, const struct obs_source_frame *in) { - if (!source->async_preload_frame) - return true; + const struct obs_source_frame_copy_shape none = {0}; + const bool has_preload = source->async_preload_frame != NULL; - return in->width != source->async_preload_frame->width || in->height != source->async_preload_frame->height || - in->format != source->async_preload_frame->format; + return obs_source_frame_copy_preload_changed( + has_preload, has_preload ? frame_copy_shape(source->async_preload_frame) : none, frame_copy_shape(in)); } static void obs_source_preload_video_internal(obs_source_t *source, const struct obs_source_frame *frame) @@ -3267,7 +3244,7 @@ void obs_source_preload_video(obs_source_t *source, const struct obs_source_fram } struct obs_source_frame new_frame = *frame; - new_frame.full_range = format_is_yuv(frame->format) ? new_frame.full_range : true; + new_frame.full_range = obs_source_frame_copy_full_range(frame->format, new_frame.full_range); obs_source_preload_video_internal(source, &new_frame); } @@ -3282,7 +3259,6 @@ void obs_source_preload_video2(obs_source_t *source, const struct obs_source_fra } struct obs_source_frame new_frame = {0}; - enum video_range_type range = resolve_video_range(frame->format, frame->range); for (size_t i = 0; i < MAX_AV_PLANES; i++) { new_frame.data[i] = frame->data[i]; @@ -3293,7 +3269,7 @@ void obs_source_preload_video2(obs_source_t *source, const struct obs_source_fra new_frame.height = frame->height; new_frame.timestamp = frame->timestamp; new_frame.format = frame->format; - new_frame.full_range = range == VIDEO_RANGE_FULL; + new_frame.full_range = obs_source_frame_copy_full_range2(frame->format, frame->range); new_frame.max_luminance = 0; new_frame.flip = frame->flip; new_frame.flags = frame->flags; @@ -3367,7 +3343,7 @@ void obs_source_set_video_frame(obs_source_t *source, const struct obs_source_fr } struct obs_source_frame new_frame = *frame; - new_frame.full_range = format_is_yuv(frame->format) ? new_frame.full_range : true; + new_frame.full_range = obs_source_frame_copy_full_range(frame->format, new_frame.full_range); obs_source_set_video_frame_internal(source, &new_frame); } @@ -3382,7 +3358,6 @@ void obs_source_set_video_frame2(obs_source_t *source, const struct obs_source_f } struct obs_source_frame new_frame = {0}; - enum video_range_type range = resolve_video_range(frame->format, frame->range); for (size_t i = 0; i < MAX_AV_PLANES; i++) { new_frame.data[i] = frame->data[i]; @@ -3393,7 +3368,7 @@ void obs_source_set_video_frame2(obs_source_t *source, const struct obs_source_f new_frame.height = frame->height; new_frame.timestamp = frame->timestamp; new_frame.format = frame->format; - new_frame.full_range = range == VIDEO_RANGE_FULL; + new_frame.full_range = obs_source_frame_copy_full_range2(frame->format, frame->range); new_frame.max_luminance = 0; new_frame.flip = frame->flip; new_frame.flags = frame->flags; From df2f494547f7b85fc27fdbb7c9caa7389160e4e3 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:56:07 -0400 Subject: [PATCH 2/6] test: Characterize the async frame copy plan and cache decisions test_source_frame_copy_steps compiles libobs/obs-source-frame-copy.c.h (ASan and UBSan) and calls only its six functions through their header, with media-io/video-io.h for the enum values. 19 cases: - the constants (4 planes, MAX_ASYNC_FRAMES 30, MAX_UNUSED_FRAME_DURATION 5, the step values), and a table of every enum video_format value in order with the planes the copy makes and each plane's rows (full or (height + 1) / 2); - the copy plan of every format at 12 heights (0 to 5, 9, 719, 720 and 1079 to 1081) with equal line sizes (one copy of line size x rows) and with a padded source (rows of the destination's line size); planes past the count are zero; - no plane for -1, VIDEO_FORMAT_R10L + 1, 1000 and the int ends, nor for P216 and P416 ("Unimplemented"; characterized, not endorsed: no in-tree source outputs them); the colour range is still copied; - odd heights: 4:2:0 chroma rounds up (I420, I010, NV12, P010 and I40A, whose alpha plane is full height), 4:2:2 keeps the full height; - (height + 1) / 2 wraps in uint32_t: UINT32_MAX has 0 chroma rows (characterized, not endorsed), and the one copy's size is the size_t product (0xffffffff x 0xffffffff here); - per plane of every format, the source line size equal, smaller, 1, larger, UINT32_MAX or 0 against the destination's, a destination line size of 0 (rows of 0 bytes), and both 0 (one copy of 0 bytes); each plane decides on its own two line sizes, and a fifth is never read; - the colour range for each range, from format -2 to R10L + 2; - the queue limit at 0, MAX_ASYNC_FRAMES - 1, at it, + 1 and SIZE_MAX, with the keys equal and different: the limit comes first; - async_texture_changed(): the width, the height or the convert type alone resets, swapped sizes reset, equal keys keep (any values); - clean_cache(): an unused entry counts 0 to 4 up by one and is dropped at 5; past 5 it is never dropped (unreachable, as it is erased at 5; characterized, not endorsed), a used entry keeps any count and is not counted, and five calls from 0 drop it on the fifth; - the range rule: YUV keeps its range, any other format and values outside the enum are full range; the *2() rule: VIDEO_RANGE_DEFAULT is partial for YUV and full otherwise, an explicit range is kept (an RGB frame with VIDEO_RANGE_PARTIAL stays limited there, unlike the first rule), a range outside the enum is not full; - preload_frame_changed(): no preload frame is always a change; the width, the height or the format alone is one. The test is in the native test graph in name order, with libm except on MSVC. On the C backend it passes 19 of 19, also on its own with ASan leak detection and halt on error. Refs #2192 --- test/cmocka/build/test-graph.json | 29 + test/cmocka/test_source_frame_copy_steps.c | 556 ++++++++++++++++++ .../test_source_frame_copy_steps.cmake | 15 + 3 files changed, 600 insertions(+) create mode 100644 test/cmocka/test_source_frame_copy_steps.c create mode 100644 test/cmocka/tests.d/test_source_frame_copy_steps.cmake diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 84a6ee9c88..8c055aafd2 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -2797,6 +2797,29 @@ ], "sanitizers": "address,undefined" }, + "test_source_frame_copy_steps": { + "sources": [ + "test/cmocka/test_source_frame_copy_steps.c", + "libobs/obs-source-frame-copy.c.h" + ], + "includes": [ + "libobs", + "deps/libcaption" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "m", + "requires": { + "not": "MSVC" + } + } + ], + "sanitizers": "address,undefined" + }, "test_source_load_migration_steps": { "sources": [ "test/cmocka/test_source_load_migration_steps.c", @@ -4708,6 +4731,12 @@ "${target}" ] }, + "test_source_frame_copy_steps": { + "target": "test_source_frame_copy_steps", + "command": [ + "${target}" + ] + }, "test_source_load_migration_steps": { "target": "test_source_load_migration_steps", "command": [ diff --git a/test/cmocka/test_source_frame_copy_steps.c b/test/cmocka/test_source_frame_copy_steps.c new file mode 100644 index 0000000000..773ee3fbe5 --- /dev/null +++ b/test/cmocka/test_source_frame_copy_steps.c @@ -0,0 +1,556 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "obs-source-frame-copy.h" +#include "media-io/video-io.h" + +/* + * Characterization of the async frame copy plan and cache decisions of + * libobs/obs-source.c, moved to libobs/obs-source-frame-copy.c before their + * Rust port: + * - obs_source_frame_copy_plan(): what copy_frame_data() copies: the colour + * range when the frame is not full range, then for each plane of the + * format its rows ((height + 1) / 2 for 4:2:0 chroma), as one copy of + * line size x rows when the two line sizes are equal, else rows of the + * smaller line size; + * - obs_source_frame_copy_cache_step(): cache_video()'s queue limit at + * MAX_ASYNC_FRAMES and async_texture_changed()'s reset; + * - obs_source_frame_copy_cache_clean(): one entry of clean_cache(), the + * drop at MAX_UNUSED_FRAME_DURATION; + * - obs_source_frame_copy_full_range() and _full_range2(): the range rule + * of obs_source_output_video(), _preload_video(), _set_video_frame() and + * their *2() variants; + * - obs_source_frame_copy_preload_changed(): preload_frame_changed(). + * They pin the current C behaviour; cases marked "characterized, not + * endorsed" are odd but real. + * + * The functions are hidden in libobs, so this test compiles the C source + * into itself and calls only them, through their header. obs-source.c runs + * the copies (every memcpy() stays there) and keeps the cache; + * test_video_frame, test_async_frame_pacing_steps, the media restart tests, + * the v4l2 and PipeWire camera tests, test_async_rotation_normalize and the + * deinterlace tests cover the callers through the real libobs. + * + * Frames come from capture devices, decoded media and plugins, so the + * format, height and line sizes can be anything: the plan's arithmetic is + * the C's, in uint32_t and size_t. + */ + +/* ------------------------------------------------------------------------ */ +/* helpers */ + +enum rows_rule { ROWS_H, ROWS_HALF }; + +struct format_shape { + int format; + const char *name; + size_t count; + enum rows_rule rows[OBS_SOURCE_FRAME_COPY_PLANES]; +}; + +/* Every enum video_format value in order, with the planes the copy makes. */ +static const struct format_shape shapes[] = { + {VIDEO_FORMAT_NONE, "NONE", 1, {ROWS_H}}, + {VIDEO_FORMAT_I420, "I420", 3, {ROWS_H, ROWS_HALF, ROWS_HALF}}, + {VIDEO_FORMAT_NV12, "NV12", 2, {ROWS_H, ROWS_HALF}}, + {VIDEO_FORMAT_YVYU, "YVYU", 1, {ROWS_H}}, + {VIDEO_FORMAT_YUY2, "YUY2", 1, {ROWS_H}}, + {VIDEO_FORMAT_UYVY, "UYVY", 1, {ROWS_H}}, + {VIDEO_FORMAT_RGBA, "RGBA", 1, {ROWS_H}}, + {VIDEO_FORMAT_BGRA, "BGRA", 1, {ROWS_H}}, + {VIDEO_FORMAT_BGRX, "BGRX", 1, {ROWS_H}}, + {VIDEO_FORMAT_Y800, "Y800", 1, {ROWS_H}}, + {VIDEO_FORMAT_I444, "I444", 3, {ROWS_H, ROWS_H, ROWS_H}}, + {VIDEO_FORMAT_BGR3, "BGR3", 1, {ROWS_H}}, + {VIDEO_FORMAT_I422, "I422", 3, {ROWS_H, ROWS_H, ROWS_H}}, + {VIDEO_FORMAT_I40A, "I40A", 4, {ROWS_H, ROWS_HALF, ROWS_HALF, ROWS_H}}, + {VIDEO_FORMAT_I42A, "I42A", 4, {ROWS_H, ROWS_H, ROWS_H, ROWS_H}}, + {VIDEO_FORMAT_YUVA, "YUVA", 4, {ROWS_H, ROWS_H, ROWS_H, ROWS_H}}, + {VIDEO_FORMAT_AYUV, "AYUV", 1, {ROWS_H}}, + {VIDEO_FORMAT_I010, "I010", 3, {ROWS_H, ROWS_HALF, ROWS_HALF}}, + {VIDEO_FORMAT_P010, "P010", 2, {ROWS_H, ROWS_HALF}}, + {VIDEO_FORMAT_I210, "I210", 3, {ROWS_H, ROWS_H, ROWS_H}}, + {VIDEO_FORMAT_I412, "I412", 3, {ROWS_H, ROWS_H, ROWS_H}}, + {VIDEO_FORMAT_YA2L, "YA2L", 4, {ROWS_H, ROWS_H, ROWS_H, ROWS_H}}, + /* "Unimplemented": characterized, not endorsed. No in-tree source + * outputs them. */ + {VIDEO_FORMAT_P216, "P216", 0, {ROWS_H}}, + {VIDEO_FORMAT_P416, "P416", 0, {ROWS_H}}, + {VIDEO_FORMAT_V210, "V210", 1, {ROWS_H}}, + {VIDEO_FORMAT_R10L, "R10L", 1, {ROWS_H}}, +}; + +#define SHAPE_COUNT (sizeof(shapes) / sizeof(shapes[0])) + +/* YUV per format_is_yuv(), in enum order */ +static const bool yuv[] = { + false, true, true, true, true, true, false, false, false, false, true, false, true, + true, true, true, true, true, true, true, true, true, true, true, true, false, +}; + +static const uint32_t heights[] = {0, 1, 2, 3, 4, 5, 9, 719, 720, 1079, 1080, 1081}; + +#define HEIGHT_COUNT (sizeof(heights) / sizeof(heights[0])) + +static uint32_t rows_of(enum rows_rule rule, uint32_t height) +{ + return rule == ROWS_HALF ? (height + 1) / 2 : height; +} + +static struct obs_source_frame_copy_lines lines4(uint32_t l0, uint32_t l1, uint32_t l2, uint32_t l3) +{ + struct obs_source_frame_copy_lines lines = {{l0, l1, l2, l3}}; + return lines; +} + +static struct obs_source_frame_copy_lines lines_all(uint32_t l) +{ + return lines4(l, l, l, l); +} + +static void check_copy(const char *what, size_t plane, const struct obs_source_frame_plane_copy *got, bool whole, + size_t bytes, uint32_t rows, uint32_t src_stride, uint32_t dst_stride) +{ + if (got->whole != whole || got->bytes != bytes || got->rows != rows || got->src_stride != src_stride || + got->dst_stride != dst_stride) { + fprintf(stderr, + "%s plane %zu: whole %d bytes %zu rows %u strides %u/%u, " + "expected whole %d bytes %zu rows %u strides %u/%u\n", + what, plane, got->whole, got->bytes, got->rows, got->src_stride, got->dst_stride, whole, bytes, + rows, src_stride, dst_stride); + fail(); + } +} + +static void check_count(const char *what, const struct obs_source_frame_copy_plan *plan, size_t count) +{ + if (plan->count != count) { + fprintf(stderr, "%s: %zu planes, expected %zu\n", what, plan->count, count); + fail(); + } +} + +/* The copy plane i makes for a source line size s and a destination line + * size d over rows rows, as the C decides it. */ +static void check_relation(const char *what, size_t plane, const struct obs_source_frame_plane_copy *got, uint32_t s, + uint32_t d, uint32_t rows) +{ + if (s == d) + check_copy(what, plane, got, true, (size_t)d * (size_t)rows, rows, s, d); + else + check_copy(what, plane, got, false, d < s ? d : s, rows, s, d); +} + +static const struct obs_source_frame_plane_copy zero_copy = {0}; + +static void check_unused_zero(const char *what, const struct obs_source_frame_copy_plan *plan) +{ + for (size_t i = plan->count; i < OBS_SOURCE_FRAME_COPY_PLANES; i++) + check_copy(what, i, &plan->planes[i], zero_copy.whole, zero_copy.bytes, zero_copy.rows, + zero_copy.src_stride, zero_copy.dst_stride); +} + +static struct obs_source_frame_copy_key key(uint32_t width, uint32_t height, int convert_type) +{ + struct obs_source_frame_copy_key k = {width, height, convert_type}; + return k; +} + +static struct obs_source_frame_copy_shape shape(int format, uint32_t width, uint32_t height) +{ + struct obs_source_frame_copy_shape s = {format, width, height}; + return s; +} + +static void check_age(const char *what, struct obs_source_frame_cache_age got, bool counted, long unused_count, + bool drop) +{ + if (got.counted != counted || got.unused_count != unused_count || got.drop != drop) { + fprintf(stderr, "%s: counted %d unused %ld drop %d, expected counted %d unused %ld drop %d\n", what, + got.counted, got.unused_count, got.drop, counted, unused_count, drop); + fail(); + } +} + +/* ------------------------------------------------------------------------ */ +/* constants */ + +static void constants(void **state) +{ + (void)state; + assert_int_equal(OBS_SOURCE_FRAME_COPY_PLANES, 4); + assert_int_equal(OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES, 30); + assert_int_equal(OBS_SOURCE_FRAME_COPY_MAX_UNUSED, 5); + assert_int_equal(OBS_SOURCE_FRAME_CACHE_KEEP, 0); + assert_int_equal(OBS_SOURCE_FRAME_CACHE_RESET, 1); + assert_int_equal(OBS_SOURCE_FRAME_CACHE_OVERFLOW, 2); + assert_true(OBS_SOURCE_FRAME_COPY_PLANES <= MAX_AV_PLANES); +} + +/* The shape table lists every enum video_format value once, in order. */ +static void table_covers_every_format(void **state) +{ + (void)state; + assert_int_equal(SHAPE_COUNT, VIDEO_FORMAT_R10L + 1); + assert_int_equal(sizeof(yuv) / sizeof(yuv[0]), VIDEO_FORMAT_R10L + 1); + for (size_t i = 0; i < SHAPE_COUNT; i++) + assert_int_equal(shapes[i].format, (int)i); +} + +/* ------------------------------------------------------------------------ */ +/* the copy plan */ + +/* Every format at every height, equal line sizes: one copy of line size x + * rows per plane. */ +static void plan_every_format_equal_lines(void **state) +{ + (void)state; + for (size_t f = 0; f < SHAPE_COUNT; f++) { + for (size_t h = 0; h < HEIGHT_COUNT; h++) { + const struct obs_source_frame_copy_lines l = lines4(1920, 960, 961, 1921); + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(shapes[f].format, true, heights[h], l, l); + check_count(shapes[f].name, &plan, shapes[f].count); + for (size_t i = 0; i < plan.count; i++) { + const uint32_t rows = rows_of(shapes[f].rows[i], heights[h]); + check_copy(shapes[f].name, i, &plan.planes[i], true, (size_t)l.linesize[i] * rows, rows, + l.linesize[i], l.linesize[i]); + } + check_unused_zero(shapes[f].name, &plan); + } + } +} + +/* Every format at every height, the source's line sizes padded: rows of the + * destination's line size. */ +static void plan_every_format_padded_source(void **state) +{ + (void)state; + for (size_t f = 0; f < SHAPE_COUNT; f++) { + for (size_t h = 0; h < HEIGHT_COUNT; h++) { + const struct obs_source_frame_copy_lines src = lines4(2048, 1024, 1024, 2048); + const struct obs_source_frame_copy_lines dst = lines4(1920, 960, 960, 1920); + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(shapes[f].format, true, heights[h], src, dst); + check_count(shapes[f].name, &plan, shapes[f].count); + for (size_t i = 0; i < plan.count; i++) { + const uint32_t rows = rows_of(shapes[f].rows[i], heights[h]); + check_copy(shapes[f].name, i, &plan.planes[i], false, dst.linesize[i], rows, + src.linesize[i], dst.linesize[i]); + } + } + } +} + +/* Values outside the enum copy no plane, nor do P216 and P416 + * (characterized, not endorsed). */ +static void plan_formats_without_copy(void **state) +{ + (void)state; + const int formats[] = {-1, VIDEO_FORMAT_R10L + 1, 1000, INT_MIN, INT_MAX, VIDEO_FORMAT_P216, VIDEO_FORMAT_P416}; + for (size_t i = 0; i < sizeof(formats) / sizeof(formats[0]); i++) { + for (size_t h = 0; h < HEIGHT_COUNT; h++) { + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(formats[i], false, heights[h], lines_all(64), lines_all(32)); + check_count("no copy", &plan, 0); + check_unused_zero("no copy", &plan); + /* the colour range is still copied */ + assert_true(plan.color_range); + } + } +} + +/* 4:2:0 chroma rows round up: odd heights get the last half row. */ +static void plan_odd_heights(void **state) +{ + (void)state; + const int formats[] = {VIDEO_FORMAT_I420, VIDEO_FORMAT_I010, VIDEO_FORMAT_NV12, VIDEO_FORMAT_P010, + VIDEO_FORMAT_I40A}; + const uint32_t h[] = {1, 3, 5, 719, 1081, 2161}; + const uint32_t half[] = {1, 2, 3, 360, 541, 1081}; + for (size_t f = 0; f < sizeof(formats) / sizeof(formats[0]); f++) { + for (size_t i = 0; i < sizeof(h) / sizeof(h[0]); i++) { + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(formats[f], true, h[i], lines_all(16), lines_all(16)); + assert_int_equal(plan.planes[0].rows, h[i]); + assert_int_equal(plan.planes[1].rows, half[i]); + if (plan.count > 2) + assert_int_equal(plan.planes[2].rows, half[i]); + if (plan.count > 3) + assert_int_equal(plan.planes[3].rows, h[i]); + } + } + /* 4:2:2 and 4:4:4 chroma keep the full height */ + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(VIDEO_FORMAT_I422, true, 1081, lines_all(16), lines_all(16)); + assert_int_equal(plan.planes[1].rows, 1081); + assert_int_equal(plan.planes[2].rows, 1081); +} + +/* (height + 1) / 2 wraps in uint32_t: UINT32_MAX has 0 chroma rows + * (characterized, not endorsed). */ +static void plan_height_wrap(void **state) +{ + (void)state; + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(VIDEO_FORMAT_I420, true, UINT32_MAX, lines_all(8), lines_all(8)); + check_count("I420 UINT32_MAX", &plan, 3); + check_copy("I420 UINT32_MAX", 0, &plan.planes[0], true, (size_t)8 * UINT32_MAX, UINT32_MAX, 8, 8); + check_copy("I420 UINT32_MAX", 1, &plan.planes[1], true, 0, 0, 8, 8); + check_copy("I420 UINT32_MAX", 2, &plan.planes[2], true, 0, 0, 8, 8); + + plan = obs_source_frame_copy_plan(VIDEO_FORMAT_NV12, true, UINT32_MAX - 1, lines_all(8), lines_all(9)); + check_copy("NV12 UINT32_MAX - 1", 1, &plan.planes[1], false, 8, 0x7fffffffu, 8, 9); +} + +/* The one copy's size is line size x rows in size_t (64 bits here; it + * would wrap with a 32-bit size_t). */ +static void plan_whole_size(void **state) +{ + (void)state; + struct obs_source_frame_copy_plan plan = obs_source_frame_copy_plan( + VIDEO_FORMAT_RGBA, true, UINT32_MAX, lines_all(UINT32_MAX), lines_all(UINT32_MAX)); + check_copy("RGBA max", 0, &plan.planes[0], true, (size_t)UINT32_MAX * (size_t)UINT32_MAX, UINT32_MAX, + UINT32_MAX, UINT32_MAX); +} + +/* Each plane decides on its own two line sizes: equal, a smaller, larger or + * 0 source, a 0 destination, both 0. */ +static void plan_each_line_size(void **state) +{ + (void)state; + /* source line size relative to the destination's 100 */ + const uint32_t rel[] = {100, 99, 1, 101, UINT32_MAX, 0}; + for (size_t f = 0; f < SHAPE_COUNT; f++) { + for (size_t p = 0; p < shapes[f].count; p++) { + for (size_t r = 0; r < sizeof(rel) / sizeof(rel[0]); r++) { + struct obs_source_frame_copy_lines src = lines_all(100); + const struct obs_source_frame_copy_lines dst = lines_all(100); + src.linesize[p] = rel[r]; + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(shapes[f].format, true, 7, src, dst); + check_count(shapes[f].name, &plan, shapes[f].count); + for (size_t i = 0; i < plan.count; i++) + check_relation(shapes[f].name, i, &plan.planes[i], src.linesize[i], + dst.linesize[i], rows_of(shapes[f].rows[i], 7)); + } + /* a 0 destination line size: rows of 0 bytes */ + struct obs_source_frame_copy_lines dst = lines_all(100); + dst.linesize[p] = 0; + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(shapes[f].format, true, 7, lines_all(100), dst); + check_copy(shapes[f].name, p, &plan.planes[p], false, 0, rows_of(shapes[f].rows[p], 7), 100, 0); + /* both 0: one copy of 0 bytes */ + plan = obs_source_frame_copy_plan(shapes[f].format, true, 7, lines_all(0), lines_all(0)); + check_copy(shapes[f].name, p, &plan.planes[p], true, 0, rows_of(shapes[f].rows[p], 7), 0, 0); + } + } +} + +/* Only the first four line sizes are planes; a fifth is never read. */ +static void plan_line_sizes_beyond_planes(void **state) +{ + (void)state; + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(VIDEO_FORMAT_YUVA, true, 2, lines4(1, 2, 3, 4), lines4(1, 2, 3, 5)); + check_count("YUVA", &plan, 4); + check_copy("YUVA", 3, &plan.planes[3], false, 4, 2, 4, 5); +} + +/* The colour range is copied only for a frame that is not full range, for + * every format and outside the enum. */ +static void plan_color_range(void **state) +{ + (void)state; + for (int f = -2; f <= VIDEO_FORMAT_R10L + 2; f++) { + struct obs_source_frame_copy_plan plan = + obs_source_frame_copy_plan(f, false, 4, lines_all(4), lines_all(4)); + assert_true(plan.color_range); + plan = obs_source_frame_copy_plan(f, true, 4, lines_all(4), lines_all(4)); + assert_false(plan.color_range); + } +} + +/* ------------------------------------------------------------------------ */ +/* the cache */ + +/* The queue limit comes first: at MAX_ASYNC_FRAMES or more queued frames, + * the cache is cleared and the frame refused, whatever the keys. */ +static void cache_queue_limit(void **state) +{ + (void)state; + const struct obs_source_frame_copy_key same = key(1920, 1080, 2); + const struct obs_source_frame_copy_key other = key(1280, 720, 1); + const size_t limit = OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES; + + assert_int_equal(obs_source_frame_copy_cache_step(0, same, same), OBS_SOURCE_FRAME_CACHE_KEEP); + assert_int_equal(obs_source_frame_copy_cache_step(limit - 1, same, same), OBS_SOURCE_FRAME_CACHE_KEEP); + assert_int_equal(obs_source_frame_copy_cache_step(limit - 1, same, other), OBS_SOURCE_FRAME_CACHE_RESET); + assert_int_equal(obs_source_frame_copy_cache_step(limit, same, same), OBS_SOURCE_FRAME_CACHE_OVERFLOW); + assert_int_equal(obs_source_frame_copy_cache_step(limit, same, other), OBS_SOURCE_FRAME_CACHE_OVERFLOW); + assert_int_equal(obs_source_frame_copy_cache_step(limit + 1, same, same), OBS_SOURCE_FRAME_CACHE_OVERFLOW); + assert_int_equal(obs_source_frame_copy_cache_step(limit + 1, same, other), OBS_SOURCE_FRAME_CACHE_OVERFLOW); + assert_int_equal(obs_source_frame_copy_cache_step(SIZE_MAX, same, same), OBS_SOURCE_FRAME_CACHE_OVERFLOW); +} + +/* async_texture_changed(): a change in the width, the height or the + * convert type alone resets the cache. */ +static void cache_texture_changed(void **state) +{ + (void)state; + const struct obs_source_frame_copy_key cache = key(1920, 1080, 2); + assert_int_equal(obs_source_frame_copy_cache_step(3, cache, key(1920, 1080, 2)), OBS_SOURCE_FRAME_CACHE_KEEP); + assert_int_equal(obs_source_frame_copy_cache_step(3, cache, key(1921, 1080, 2)), OBS_SOURCE_FRAME_CACHE_RESET); + assert_int_equal(obs_source_frame_copy_cache_step(3, cache, key(1920, 1079, 2)), OBS_SOURCE_FRAME_CACHE_RESET); + assert_int_equal(obs_source_frame_copy_cache_step(3, cache, key(1920, 1080, 3)), OBS_SOURCE_FRAME_CACHE_RESET); + assert_int_equal(obs_source_frame_copy_cache_step(3, cache, key(1080, 1920, 2)), OBS_SOURCE_FRAME_CACHE_RESET); + /* a new source's cache is 0 x 0 and the convert type of format 0 */ + assert_int_equal(obs_source_frame_copy_cache_step(0, key(0, 0, 0), key(0, 0, 0)), OBS_SOURCE_FRAME_CACHE_KEEP); + assert_int_equal(obs_source_frame_copy_cache_step(0, key(UINT32_MAX, UINT32_MAX, INT_MIN), + key(UINT32_MAX, UINT32_MAX, INT_MIN)), + OBS_SOURCE_FRAME_CACHE_KEEP); + assert_int_equal(obs_source_frame_copy_cache_step(0, key(1, 1, INT_MAX), key(1, 1, INT_MIN)), + OBS_SOURCE_FRAME_CACHE_RESET); +} + +/* clean_cache(): an unused entry counts one more call, and is dropped when + * the count reaches MAX_UNUSED_FRAME_DURATION. */ +static void cache_clean_counts(void **state) +{ + (void)state; + check_age("unused 0", obs_source_frame_copy_cache_clean(false, 0), true, 1, false); + check_age("unused 1", obs_source_frame_copy_cache_clean(false, 1), true, 2, false); + check_age("unused 2", obs_source_frame_copy_cache_clean(false, 2), true, 3, false); + check_age("unused 3", obs_source_frame_copy_cache_clean(false, 3), true, 4, false); + check_age("unused 4", obs_source_frame_copy_cache_clean(false, 4), true, 5, true); + /* past the limit it is never dropped: unreachable, as the entry is + * erased at the limit (characterized, not endorsed) */ + check_age("unused 5", obs_source_frame_copy_cache_clean(false, 5), true, 6, false); + check_age("unused 6", obs_source_frame_copy_cache_clean(false, 6), true, 7, false); + check_age("unused -1", obs_source_frame_copy_cache_clean(false, -1), true, 0, false); + check_age("unused LONG_MIN", obs_source_frame_copy_cache_clean(false, LONG_MIN), true, LONG_MIN + 1, false); + check_age("unused LONG_MAX - 1", obs_source_frame_copy_cache_clean(false, LONG_MAX - 1), true, LONG_MAX, false); +} + +/* A used entry is not counted and keeps its count, whatever it is. */ +static void cache_clean_used(void **state) +{ + (void)state; + const long counts[] = {0, 1, 3, 4, 5, 6, -1, LONG_MIN, LONG_MAX}; + for (size_t i = 0; i < sizeof(counts) / sizeof(counts[0]); i++) + check_age("used", obs_source_frame_copy_cache_clean(true, counts[i]), false, counts[i], false); +} + +/* The drop comes after exactly MAX_UNUSED_FRAME_DURATION calls from 0. */ +static void cache_clean_drop_at_limit(void **state) +{ + (void)state; + long count = 0; + for (int call = 1; call <= OBS_SOURCE_FRAME_COPY_MAX_UNUSED; call++) { + struct obs_source_frame_cache_age age = obs_source_frame_copy_cache_clean(false, count); + assert_true(age.counted); + assert_int_equal(age.unused_count, call); + assert_int_equal(age.drop, call == OBS_SOURCE_FRAME_COPY_MAX_UNUSED); + count = age.unused_count; + } +} + +/* ------------------------------------------------------------------------ */ +/* the range rule */ + +/* obs_source_output_video() and its kin: a YUV frame keeps its range, any + * other format (RGB, Y800, NONE, values outside the enum) is full range. */ +static void full_range_rule(void **state) +{ + (void)state; + for (int f = 0; f <= VIDEO_FORMAT_R10L; f++) { + assert_int_equal(obs_source_frame_copy_full_range(f, false), !yuv[f]); + assert_true(obs_source_frame_copy_full_range(f, true)); + } + const int outside[] = {-1, VIDEO_FORMAT_R10L + 1, 1000, INT_MIN, INT_MAX}; + for (size_t i = 0; i < sizeof(outside) / sizeof(outside[0]); i++) { + assert_true(obs_source_frame_copy_full_range(outside[i], false)); + assert_true(obs_source_frame_copy_full_range(outside[i], true)); + } +} + +/* The *2() variants: full range exactly when resolve_video_range() gives + * VIDEO_RANGE_FULL. VIDEO_RANGE_DEFAULT is partial for YUV and full for any + * other format; an explicit range is kept, so an RGB frame with + * VIDEO_RANGE_PARTIAL stays limited here, unlike the rule above. A range + * outside the enum is not full. */ +static void full_range2_rule(void **state) +{ + (void)state; + for (int f = 0; f <= VIDEO_FORMAT_R10L; f++) { + assert_int_equal(obs_source_frame_copy_full_range2(f, VIDEO_RANGE_DEFAULT), !yuv[f]); + assert_false(obs_source_frame_copy_full_range2(f, VIDEO_RANGE_PARTIAL)); + assert_true(obs_source_frame_copy_full_range2(f, VIDEO_RANGE_FULL)); + assert_false(obs_source_frame_copy_full_range2(f, VIDEO_RANGE_FULL + 1)); + assert_false(obs_source_frame_copy_full_range2(f, -1)); + assert_false(obs_source_frame_copy_full_range2(f, INT_MIN)); + assert_false(obs_source_frame_copy_full_range2(f, INT_MAX)); + } + const int outside[] = {-1, VIDEO_FORMAT_R10L + 1, INT_MIN, INT_MAX}; + for (size_t i = 0; i < sizeof(outside) / sizeof(outside[0]); i++) { + assert_true(obs_source_frame_copy_full_range2(outside[i], VIDEO_RANGE_DEFAULT)); + assert_false(obs_source_frame_copy_full_range2(outside[i], VIDEO_RANGE_PARTIAL)); + assert_true(obs_source_frame_copy_full_range2(outside[i], VIDEO_RANGE_FULL)); + } +} + +/* ------------------------------------------------------------------------ */ +/* the preload frame */ + +static void preload_changed(void **state) +{ + (void)state; + const struct obs_source_frame_copy_shape p = shape(VIDEO_FORMAT_NV12, 1920, 1080); + /* no preload frame: always a new one, whatever the shapes */ + assert_true(obs_source_frame_copy_preload_changed(false, p, p)); + assert_true(obs_source_frame_copy_preload_changed(false, shape(0, 0, 0), shape(0, 0, 0))); + assert_false(obs_source_frame_copy_preload_changed(true, p, p)); + assert_true(obs_source_frame_copy_preload_changed(true, p, shape(VIDEO_FORMAT_NV12, 1921, 1080))); + assert_true(obs_source_frame_copy_preload_changed(true, p, shape(VIDEO_FORMAT_NV12, 1920, 1081))); + assert_true(obs_source_frame_copy_preload_changed(true, p, shape(VIDEO_FORMAT_I420, 1920, 1080))); + assert_true(obs_source_frame_copy_preload_changed(true, p, shape(VIDEO_FORMAT_NV12, 1080, 1920))); + /* the format alone, not its convert type or range: P010 and I010 are + * both 4:2:0 */ + assert_true(obs_source_frame_copy_preload_changed(true, shape(VIDEO_FORMAT_P010, 64, 64), + shape(VIDEO_FORMAT_I010, 64, 64))); + assert_false(obs_source_frame_copy_preload_changed(true, shape(INT_MIN, UINT32_MAX, 0), + shape(INT_MIN, UINT32_MAX, 0))); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(constants), + cmocka_unit_test(table_covers_every_format), + cmocka_unit_test(plan_every_format_equal_lines), + cmocka_unit_test(plan_every_format_padded_source), + cmocka_unit_test(plan_formats_without_copy), + cmocka_unit_test(plan_odd_heights), + cmocka_unit_test(plan_height_wrap), + cmocka_unit_test(plan_whole_size), + cmocka_unit_test(plan_each_line_size), + cmocka_unit_test(plan_line_sizes_beyond_planes), + cmocka_unit_test(plan_color_range), + cmocka_unit_test(cache_queue_limit), + cmocka_unit_test(cache_texture_changed), + cmocka_unit_test(cache_clean_counts), + cmocka_unit_test(cache_clean_used), + cmocka_unit_test(cache_clean_drop_at_limit), + cmocka_unit_test(full_range_rule), + cmocka_unit_test(full_range2_rule), + cmocka_unit_test(preload_changed), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_source_frame_copy_steps.cmake b/test/cmocka/tests.d/test_source_frame_copy_steps.cmake new file mode 100644 index 0000000000..1a49f97fc2 --- /dev/null +++ b/test/cmocka/tests.d/test_source_frame_copy_steps.cmake @@ -0,0 +1,15 @@ +# Characterization of the async frame copy plan and cache decisions of +# libobs/obs-source.c, moved to libobs/obs-source-frame-copy.c before its +# port: the copies of copy_frame_data() for every video format and values +# outside the enum (P216 and P416 copy nothing), odd heights and the uint32_t +# wrap of (height + 1) / 2, equal, smaller, larger and 0 line sizes per plane, +# the colour range, the queue limit of cache_video() at MAX_ASYNC_FRAMES - 1, +# at it and + 1, async_texture_changed(), clean_cache()'s drop at +# MAX_UNUSED_FRAME_DURATION, the full-range rule of obs_source_output_video() +# and its *2() variant, and preload_frame_changed(). Builds the moved file on +# its own: no display and no libobs core, so it runs on every platform. The +# functions are hidden in libobs, so this characterizes that C source; the +# test calls only them. test_video_frame, the media restart tests, the v4l2 +# and PipeWire camera tests and the deinterlace tests cover the callers +# through the real libobs. +# Target and test are declared in ../build/test-graph.json. From bf9d77e12f1b86aa6f00fa91619ebe81287df529 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 14:01:12 -0400 Subject: [PATCH 3/6] obs-source: Add Tier 1 tests for the async frame copy plan port Tests for the port of libobs/obs-source-frame-copy.c.h to the frame_copy module of rust/obs-source, next to conversion_plan. The safe core is a stub (todo!()): - copy_plan(), obs_source_frame_copy_plan() on plain values: the format as the C's int, the range, the height and the four line sizes of each frame, a CopyPlan back (the colour range flag, the count and four PlaneCopy of whole, bytes as usize, rows and the two strides); - its pieces: plane_rows() (the planes of each format and whether each one's rows are the height or half()), half() ((height + 1) / 2 in u32) and plane_copy() (one plane); - cache_step() (cache_video()'s queue limit, then texture_changed(), async_texture_changed() on two Keys of size and convert type) and clean() (one entry of clean_cache(), a CacheAge of in use, counted or dropped, the count a c_long as the C's long); - full_range() and full_range2() (the range rules of obs_source_output_video() and its *2() variant), with is_yuv() and resolve_range() (format_is_yuv() and resolve_video_range()); - preload_changed() (preload_frame_changed(), the preload frame an Option). The video_format values are conversion_plan's; MAX_ASYNC_FRAMES, MAX_UNUSED and the video_range_type values are new constants. tests/source/frame_copy.rs ports test_source_frame_copy_steps 1:1: its 19 cases under the same names (steps_*), in the same order, with the same inputs and expectations. Edge cases (edge_*): half() at its ends; plane_copy() for each relation; the plan is its pieces for formats -3 to 30 at heights up to u32::MAX; the queue limit over every length up to 64 and usize::MAX; clean() over counts -10 to 10 and at c_long::MAX (the C's ++ would overflow there; no entry gets past 5, and the port wraps); the range rules against is_yuv() and resolve_range() for formats -3 to 40 and ranges -3 to 5. Proptests (4000 cases each): any format outside the enum copies nothing; each plane against a model in u64 arithmetic (height + 1 reduced modulo 2^32, then halved) for any i32 format and any u32 height and line sizes, biased to the ends; cache_step() for any keys and queue length; clean() for any count; preload_changed() for any shapes. soldr cargo test -p obs-data --test obs-tests-core source::frame_copy fails 28 of 30 with "not yet implemented". The two that pass, steps_constants and steps_table_covers_every_format, read only the constants and the test's own tables. Refs #2192 --- rust/obs-source/src/frame_copy.rs | 257 +++++++ rust/obs-source/src/lib.rs | 1 + rust/obs-source/tests/source/frame_copy.rs | 785 +++++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 4 files changed, 1044 insertions(+) create mode 100644 rust/obs-source/src/frame_copy.rs create mode 100644 rust/obs-source/tests/source/frame_copy.rs diff --git a/rust/obs-source/src/frame_copy.rs b/rust/obs-source/src/frame_copy.rs new file mode 100644 index 0000000000..7316652a5b --- /dev/null +++ b/rust/obs-source/src/frame_copy.rs @@ -0,0 +1,257 @@ +//! Safe core of `libobs/obs-source-frame-copy.c.h`: the async frame copy +//! plan and cache decisions of `obs-source.c`. +//! +//! Every frame an async source outputs (`obs_source_output_video()`, +//! `obs_source_preload_video()`, `obs_source_set_video_frame()` and their +//! `*2()` variants) is copied into a frame of the source's cache. This +//! module decides, on plain values: +//! - the frame's range ([`full_range`], [`full_range2`]): a frame that is +//! not YUV is full range; for the `*2()` variants, full range is what +//! `resolve_video_range()` gives; +//! - whether the cache is kept, reset or the frame refused ([`cache_step`]: +//! the queue limit at [`MAX_ASYNC_FRAMES`], then [`texture_changed`]); +//! - what one entry of `clean_cache()` does ([`clean`]: an unused entry +//! counts one more call and is dropped at [`MAX_UNUSED`]); +//! - what `copy_frame_data()` copies ([`copy_plan`]): the colour range when +//! the frame is not full range, then each plane of the format +//! ([`plane_rows`]), one copy of line size x rows when the two line sizes +//! are equal, else rows of the smaller line size ([`plane_copy`]); +//! - whether a preload frame must be made again ([`preload_changed`]). +//! +//! `obs-source.c.h` keeps every `memcpy()` (it runs the plan), the metadata +//! copy, `obs_source_frame_create()` and `_destroy()`, the `async_cache` and +//! `async_frames` darrays and the walk that picks the first unused entry, +//! `async_mutex`, the refcount atomics and `get_convert_type()` (it passes +//! the two convert types). +//! +//! Frames come from capture devices, decoded media and plugins, so any +//! format, height and line size can arrive. Formats and ranges are the C's +//! raw `int`s: a value outside the enum takes the C's paths (no plane, not +//! YUV). The arithmetic is the C's, wrap for wrap: `(height + 1) / 2` wraps +//! in `u32` (0 chroma rows at `u32::MAX`), and the one copy's size is a +//! `usize` product (`size_t`). The plan drives `memcpy()`, so it must be +//! exactly the C's: a row too many overruns a buffer. Nothing here panics, +//! allocates, keeps state or takes a lock. + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::unimplemented, + clippy::unreachable +)] + +use core::ffi::c_long; + +pub use crate::conversion_plan::video_format; + +/// `OBS_SOURCE_FRAME_COPY_PLANES`: the planes a plan can copy. +pub const PLANES: usize = 4; + +/// `MAX_ASYNC_FRAMES`: the queued frames at which `cache_video()` clears the +/// cache and refuses the frame. +pub const MAX_ASYNC_FRAMES: usize = 30; + +/// `MAX_UNUSED_FRAME_DURATION`: the `clean_cache()` calls after which an +/// unused cache entry is dropped. +pub const MAX_UNUSED: c_long = 5; + +/// `enum video_range_type` (`media-io/video-io.h`), as the C's `int`. +pub mod video_range { + pub const DEFAULT: i32 = 0; + pub const PARTIAL: i32 = 1; + pub const FULL: i32 = 2; +} + +/// The copy of one plane, from the start of the source plane into the start +/// of the destination plane (`struct obs_source_frame_plane_copy`). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct PlaneCopy { + /// The two line sizes are equal: one `memcpy()` of `bytes`. + pub whole: bool, + /// `whole`: the line size times `rows`, as `size_t`. Otherwise the bytes + /// of each row, the smaller line size. + pub bytes: usize, + /// The rows of the plane. Not `whole`: row `y` is copied from + /// `y * src_stride` into `y * dst_stride`, each product in `u32` (the + /// C's `uint32_t`, so it wraps). + pub rows: u32, + /// The source's line size. + pub src_stride: u32, + /// The destination's line size. + pub dst_stride: u32, +} + +/// The copies of `copy_frame_data()` (`struct obs_source_frame_copy_plan`): +/// copy `i` is plane `i`, in order, after the colour range when +/// `color_range` is set. Planes from `count` on are zero. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct CopyPlan { + /// Copy `color_range_min` and `color_range_max`: the frame is not full + /// range. + pub color_range: bool, + /// The planes to copy, at most [`PLANES`]. + pub count: usize, + pub planes: [PlaneCopy; PLANES], +} + +/// How each plane's rows follow the frame's height. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Rows { + /// The height. + Full, + /// `(height + 1) / 2`: 4:2:0 chroma. + Half, +} + +/// What `async_texture_changed()` compares (`struct +/// obs_source_frame_copy_key`): a size and a convert type (an `enum +/// convert_type` value). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Key { + pub width: u32, + pub height: u32, + pub convert_type: i32, +} + +/// What `cache_video()` does before it picks a frame (`enum +/// obs_source_frame_cache_step`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CacheStep { + /// Keep the cache: its size and convert type match the frame's. + Keep, + /// `free_async_cache()`, then take the frame's width and height. + Reset, + /// `free_async_cache()`, `last_frame_ts = 0` and no frame: the queue is + /// full. + Overflow, +} + +/// One entry of `clean_cache()` (`struct obs_source_frame_cache_age`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CacheAge { + /// A used entry: nothing is stored. + InUse, + /// An unused entry: store its count after this call. + Counted(c_long), + /// An unused entry that reached [`MAX_UNUSED`]: store the count, then + /// destroy its frame and erase it. + Drop(c_long), +} + +/// What `preload_frame_changed()` compares of a frame (`struct +/// obs_source_frame_copy_shape`): an `enum video_format` value and the size. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct Shape { + pub format: i32, + pub width: u32, + pub height: u32, +} + +/// `(height + 1) / 2` in `u32`: 4:2:0 chroma rows, wrapping to 0 at +/// `u32::MAX` as the C's `uint32_t` does. +#[must_use] +pub const fn half(height: u32) -> u32 { + let _ = height; + todo!() +} + +/// `copy_frame_data()`'s switch: the planes of `format` and how each one's +/// rows follow the height. P216, P416 and values outside the enum have none. +#[must_use] +pub const fn plane_rows(format: i32) -> &'static [Rows] { + let _ = format; + todo!() +} + +/// `copy_frame_data_plane()` and `copy_frame_data_line()`: one copy of +/// `dst * rows` (in `size_t`) when the line sizes are equal, else `rows` rows +/// of the smaller line size. +#[must_use] +pub const fn plane_copy(rows: u32, src: u32, dst: u32) -> PlaneCopy { + let _ = (rows, src, dst); + todo!() +} + +/// `obs_source_frame_copy_plan()`: the copies from a frame of `format` and +/// range `full_range` into one `height` rows high, with the source's line +/// sizes `src` and the destination's `dst`. +#[must_use] +pub fn copy_plan( + format: i32, + full_range: bool, + height: u32, + src: [u32; PLANES], + dst: [u32; PLANES], +) -> CopyPlan { + let _ = (format, full_range, height, src, dst); + todo!() +} + +/// `async_texture_changed()`: the size or the convert type differs. +#[must_use] +pub const fn texture_changed(cache: Key, frame: Key) -> bool { + let _ = (cache, frame); + todo!() +} + +/// `cache_video()`: `queued` is `async_frames.num`. The queue limit comes +/// first, then [`texture_changed`]. +#[must_use] +pub const fn cache_step(queued: usize, cache: Key, frame: Key) -> CacheStep { + let _ = (queued, cache, frame); + todo!() +} + +/// One entry of `clean_cache()`: an unused entry counts one more call +/// (`++unused_count`) and is dropped when the count reaches [`MAX_UNUSED`]. +/// The increment wraps; the C's `long` would overflow at `c_long::MAX`, +/// which no entry reaches (counts start at 0 and the entry is erased at 5). +#[must_use] +pub const fn clean(used: bool, unused_count: c_long) -> CacheAge { + let _ = (used, unused_count); + todo!() +} + +/// `format_is_yuv()` (`media-io/video-io.h`): false outside the enum. +#[must_use] +pub const fn is_yuv(format: i32) -> bool { + let _ = format; + todo!() +} + +/// `resolve_video_range()` (`media-io/video-io.h`): `DEFAULT` is partial +/// for YUV and full otherwise; any other value is kept. +#[must_use] +pub const fn resolve_range(format: i32, range: i32) -> i32 { + let _ = (format, range); + todo!() +} + +/// `obs_source_output_video()`, `obs_source_preload_video()` and +/// `obs_source_set_video_frame()`: a YUV frame keeps `full_range`, any other +/// is full range. +#[must_use] +pub const fn full_range(format: i32, full_range: bool) -> bool { + let _ = (format, full_range); + todo!() +} + +/// The `*2()` variants: full range exactly when [`resolve_range`] gives +/// `FULL`. +#[must_use] +pub const fn full_range2(format: i32, range: i32) -> bool { + let _ = (format, range); + todo!() +} + +/// `preload_frame_changed()`: there is no preload frame, or its size or +/// format differs from the new frame's. +#[must_use] +pub const fn preload_changed(preload: Option, frame: Shape) -> bool { + let _ = (preload, frame); + todo!() +} diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index 9c03fd00bd..d5de1fa402 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -50,6 +50,7 @@ pub mod deinterlace_draw; pub mod deinterlace_pacing; pub mod draw_plan; pub mod draw_technique; +pub mod frame_copy; pub mod ptm_gate; pub mod render_route; pub mod size_route; diff --git a/rust/obs-source/tests/source/frame_copy.rs b/rust/obs-source/tests/source/frame_copy.rs new file mode 100644 index 0000000000..2e309354d3 --- /dev/null +++ b/rust/obs-source/tests/source/frame_copy.rs @@ -0,0 +1,785 @@ +//! Tier 1: `obs_source::frame_copy`, the async frame copy plan and cache +//! decisions of `obs-source.c`. +//! +//! The `steps_*` cases port `test/cmocka/test_source_frame_copy_steps.c` +//! 1:1, under the same case names, in the same order, with the same inputs +//! and expectations. +//! +//! The `edge_*` cases are new: `half()` at its ends; the plan is its pieces +//! for formats -3 to 30; the queue limit over every count up to 64 and the +//! `usize` ends; `clean()` over counts around 0, the limit and the `c_long` +//! ends; the range rules against `is_yuv()`; and proptests over any `i32` +//! format, any `u32` height and line sizes (against a model in `u64`), any +//! keys and queue length, any count and any preload shapes. + +use core::ffi::c_long; + +use obs_source::frame_copy::{ + CacheAge, CacheStep, CopyPlan, Key, MAX_ASYNC_FRAMES, MAX_UNUSED, PLANES, PlaneCopy, Rows, + Shape, cache_step, clean, copy_plan, full_range, full_range2, half, is_yuv, plane_copy, + plane_rows, preload_changed, resolve_range, texture_changed, video_format as F, + video_range as R, +}; +use proptest::prelude::*; + +use Rows::{Full as H, Half as HALF}; + +/// Every `enum video_format` value in order, with the planes the copy makes +/// (the C test's `shapes`). +const SHAPES: [(i32, &str, &[Rows]); 26] = [ + (F::NONE, "NONE", &[H]), + (F::I420, "I420", &[H, HALF, HALF]), + (F::NV12, "NV12", &[H, HALF]), + (F::YVYU, "YVYU", &[H]), + (F::YUY2, "YUY2", &[H]), + (F::UYVY, "UYVY", &[H]), + (F::RGBA, "RGBA", &[H]), + (F::BGRA, "BGRA", &[H]), + (F::BGRX, "BGRX", &[H]), + (F::Y800, "Y800", &[H]), + (F::I444, "I444", &[H, H, H]), + (F::BGR3, "BGR3", &[H]), + (F::I422, "I422", &[H, H, H]), + (F::I40A, "I40A", &[H, HALF, HALF, H]), + (F::I42A, "I42A", &[H, H, H, H]), + (F::YUVA, "YUVA", &[H, H, H, H]), + (F::AYUV, "AYUV", &[H]), + (F::I010, "I010", &[H, HALF, HALF]), + (F::P010, "P010", &[H, HALF]), + (F::I210, "I210", &[H, H, H]), + (F::I412, "I412", &[H, H, H]), + (F::YA2L, "YA2L", &[H, H, H, H]), + // "Unimplemented": characterized, not endorsed + (F::P216, "P216", &[]), + (F::P416, "P416", &[]), + (F::V210, "V210", &[H]), + (F::R10L, "R10L", &[H]), +]; + +/// `format_is_yuv()` per format, in enum order. +const YUV: [bool; 26] = [ + false, true, true, true, true, true, false, false, false, false, true, false, true, true, true, + true, true, true, true, true, true, true, true, true, true, false, +]; + +const HEIGHTS: [u32; 12] = [0, 1, 2, 3, 4, 5, 9, 719, 720, 1079, 1080, 1081]; + +fn rows_of(rule: Rows, height: u32) -> u32 { + match rule { + Rows::Half => height.wrapping_add(1) / 2, + Rows::Full => height, + } +} + +const fn lines4(l0: u32, l1: u32, l2: u32, l3: u32) -> [u32; PLANES] { + [l0, l1, l2, l3] +} + +const fn lines_all(l: u32) -> [u32; PLANES] { + [l; PLANES] +} + +fn size(value: u32) -> usize { + usize::try_from(value).unwrap() +} + +#[track_caller] +#[allow(clippy::too_many_arguments)] +fn check_copy( + what: &str, + plane: usize, + got: &PlaneCopy, + whole: bool, + bytes: usize, + rows: u32, + src_stride: u32, + dst_stride: u32, +) { + assert_eq!( + *got, + PlaneCopy { + whole, + bytes, + rows, + src_stride, + dst_stride + }, + "{what} plane {plane}" + ); +} + +#[track_caller] +fn check_count(what: &str, plan: &CopyPlan, count: usize) { + assert_eq!(plan.count, count, "{what}: planes"); +} + +#[track_caller] +fn check_relation(what: &str, plane: usize, got: &PlaneCopy, s: u32, d: u32, rows: u32) { + if s == d { + check_copy(what, plane, got, true, size(d) * size(rows), rows, s, d); + } else { + check_copy(what, plane, got, false, size(d.min(s)), rows, s, d); + } +} + +#[track_caller] +fn check_unused_zero(what: &str, plan: &CopyPlan) { + for (i, copy) in plan.planes.iter().enumerate().skip(plan.count) { + assert_eq!(*copy, PlaneCopy::default(), "{what} unused plane {i}"); + } +} + +const fn key(width: u32, height: u32, convert_type: i32) -> Key { + Key { + width, + height, + convert_type, + } +} + +const fn shape(format: i32, width: u32, height: u32) -> Shape { + Shape { + format, + width, + height, + } +} + +/// `struct obs_source_frame_cache_age` as the C test checks it: counted, +/// the count, drop. +fn age_fields(age: CacheAge, unused_count: c_long) -> (bool, c_long, bool) { + match age { + CacheAge::InUse => (false, unused_count, false), + CacheAge::Counted(n) => (true, n, false), + CacheAge::Drop(n) => (true, n, true), + } +} + +#[track_caller] +fn check_age(what: &str, used: bool, unused_count: c_long, counted: bool, n: c_long, drop: bool) { + assert_eq!( + age_fields(clean(used, unused_count), unused_count), + (counted, n, drop), + "{what}" + ); +} + +// --------------------------------------------------------------------------- +// the C test, 1:1 + +#[test] +fn steps_constants() { + assert_eq!(PLANES, 4); + assert_eq!(MAX_ASYNC_FRAMES, 30); + assert_eq!(MAX_UNUSED, 5); + assert_ne!(CacheStep::Keep, CacheStep::Reset); + assert_ne!(CacheStep::Reset, CacheStep::Overflow); +} + +#[test] +fn steps_table_covers_every_format() { + assert_eq!(SHAPES.len(), usize::try_from(F::R10L + 1).unwrap()); + assert_eq!(YUV.len(), SHAPES.len()); + for (i, (format, _, _)) in SHAPES.iter().enumerate() { + assert_eq!(usize::try_from(*format).unwrap(), i); + } +} + +#[test] +fn steps_plan_every_format_equal_lines() { + for (format, name, rules) in SHAPES { + for height in HEIGHTS { + let l = lines4(1920, 960, 961, 1921); + let plan = copy_plan(format, true, height, l, l); + check_count(name, &plan, rules.len()); + for (i, rule) in rules.iter().enumerate() { + let rows = rows_of(*rule, height); + check_copy( + name, + i, + &plan.planes[i], + true, + size(l[i]) * size(rows), + rows, + l[i], + l[i], + ); + } + check_unused_zero(name, &plan); + } + } +} + +#[test] +fn steps_plan_every_format_padded_source() { + for (format, name, rules) in SHAPES { + for height in HEIGHTS { + let src = lines4(2048, 1024, 1024, 2048); + let dst = lines4(1920, 960, 960, 1920); + let plan = copy_plan(format, true, height, src, dst); + check_count(name, &plan, rules.len()); + for (i, rule) in rules.iter().enumerate() { + let rows = rows_of(*rule, height); + check_copy( + name, + i, + &plan.planes[i], + false, + size(dst[i]), + rows, + src[i], + dst[i], + ); + } + } + } +} + +#[test] +fn steps_plan_formats_without_copy() { + let formats = [-1, F::R10L + 1, 1000, i32::MIN, i32::MAX, F::P216, F::P416]; + for format in formats { + for height in HEIGHTS { + let plan = copy_plan(format, false, height, lines_all(64), lines_all(32)); + check_count("no copy", &plan, 0); + check_unused_zero("no copy", &plan); + assert!(plan.color_range); + } + } +} + +#[test] +fn steps_plan_odd_heights() { + let formats = [F::I420, F::I010, F::NV12, F::P010, F::I40A]; + let h = [1, 3, 5, 719, 1081, 2161]; + let halves = [1, 2, 3, 360, 541, 1081]; + for format in formats { + for (height, half_rows) in h.into_iter().zip(halves) { + let plan = copy_plan(format, true, height, lines_all(16), lines_all(16)); + assert_eq!(plan.planes[0].rows, height); + assert_eq!(plan.planes[1].rows, half_rows); + if plan.count > 2 { + assert_eq!(plan.planes[2].rows, half_rows); + } + if plan.count > 3 { + assert_eq!(plan.planes[3].rows, height); + } + } + } + let plan = copy_plan(F::I422, true, 1081, lines_all(16), lines_all(16)); + assert_eq!(plan.planes[1].rows, 1081); + assert_eq!(plan.planes[2].rows, 1081); +} + +#[test] +fn steps_plan_height_wrap() { + let plan = copy_plan(F::I420, true, u32::MAX, lines_all(8), lines_all(8)); + check_count("I420 UINT32_MAX", &plan, 3); + check_copy( + "I420 UINT32_MAX", + 0, + &plan.planes[0], + true, + 8 * size(u32::MAX), + u32::MAX, + 8, + 8, + ); + check_copy("I420 UINT32_MAX", 1, &plan.planes[1], true, 0, 0, 8, 8); + check_copy("I420 UINT32_MAX", 2, &plan.planes[2], true, 0, 0, 8, 8); + + let plan = copy_plan(F::NV12, true, u32::MAX - 1, lines_all(8), lines_all(9)); + check_copy( + "NV12 UINT32_MAX - 1", + 1, + &plan.planes[1], + false, + 8, + 0x7fff_ffff, + 8, + 9, + ); +} + +#[test] +fn steps_plan_whole_size() { + let plan = copy_plan( + F::RGBA, + true, + u32::MAX, + lines_all(u32::MAX), + lines_all(u32::MAX), + ); + check_copy( + "RGBA max", + 0, + &plan.planes[0], + true, + size(u32::MAX).wrapping_mul(size(u32::MAX)), + u32::MAX, + u32::MAX, + u32::MAX, + ); +} + +#[test] +fn steps_plan_each_line_size() { + let rel = [100, 99, 1, 101, u32::MAX, 0]; + for (format, name, rules) in SHAPES { + for (p, rule) in rules.iter().enumerate() { + for r in rel { + let mut src = lines_all(100); + let dst = lines_all(100); + src[p] = r; + let plan = copy_plan(format, true, 7, src, dst); + check_count(name, &plan, rules.len()); + for (i, rule_i) in rules.iter().enumerate() { + check_relation( + name, + i, + &plan.planes[i], + src[i], + dst[i], + rows_of(*rule_i, 7), + ); + } + } + let mut dst = lines_all(100); + dst[p] = 0; + let plan = copy_plan(format, true, 7, lines_all(100), dst); + check_copy( + name, + p, + &plan.planes[p], + false, + 0, + rows_of(*rule, 7), + 100, + 0, + ); + let plan = copy_plan(format, true, 7, lines_all(0), lines_all(0)); + check_copy(name, p, &plan.planes[p], true, 0, rows_of(*rule, 7), 0, 0); + } + } +} + +#[test] +fn steps_plan_line_sizes_beyond_planes() { + let plan = copy_plan(F::YUVA, true, 2, lines4(1, 2, 3, 4), lines4(1, 2, 3, 5)); + check_count("YUVA", &plan, 4); + check_copy("YUVA", 3, &plan.planes[3], false, 4, 2, 4, 5); +} + +#[test] +fn steps_plan_color_range() { + for format in -2..=F::R10L + 2 { + assert!(copy_plan(format, false, 4, lines_all(4), lines_all(4)).color_range); + assert!(!copy_plan(format, true, 4, lines_all(4), lines_all(4)).color_range); + } +} + +#[test] +fn steps_cache_queue_limit() { + let same = key(1920, 1080, 2); + let other = key(1280, 720, 1); + let limit = MAX_ASYNC_FRAMES; + assert_eq!(cache_step(0, same, same), CacheStep::Keep); + assert_eq!(cache_step(limit - 1, same, same), CacheStep::Keep); + assert_eq!(cache_step(limit - 1, same, other), CacheStep::Reset); + assert_eq!(cache_step(limit, same, same), CacheStep::Overflow); + assert_eq!(cache_step(limit, same, other), CacheStep::Overflow); + assert_eq!(cache_step(limit + 1, same, same), CacheStep::Overflow); + assert_eq!(cache_step(limit + 1, same, other), CacheStep::Overflow); + assert_eq!(cache_step(usize::MAX, same, same), CacheStep::Overflow); +} + +#[test] +fn steps_cache_texture_changed() { + let cache = key(1920, 1080, 2); + assert_eq!(cache_step(3, cache, key(1920, 1080, 2)), CacheStep::Keep); + assert_eq!(cache_step(3, cache, key(1921, 1080, 2)), CacheStep::Reset); + assert_eq!(cache_step(3, cache, key(1920, 1079, 2)), CacheStep::Reset); + assert_eq!(cache_step(3, cache, key(1920, 1080, 3)), CacheStep::Reset); + assert_eq!(cache_step(3, cache, key(1080, 1920, 2)), CacheStep::Reset); + assert_eq!(cache_step(0, key(0, 0, 0), key(0, 0, 0)), CacheStep::Keep); + assert_eq!( + cache_step( + 0, + key(u32::MAX, u32::MAX, i32::MIN), + key(u32::MAX, u32::MAX, i32::MIN) + ), + CacheStep::Keep + ); + assert_eq!( + cache_step(0, key(1, 1, i32::MAX), key(1, 1, i32::MIN)), + CacheStep::Reset + ); +} + +#[test] +fn steps_cache_clean_counts() { + check_age("unused 0", false, 0, true, 1, false); + check_age("unused 1", false, 1, true, 2, false); + check_age("unused 2", false, 2, true, 3, false); + check_age("unused 3", false, 3, true, 4, false); + check_age("unused 4", false, 4, true, 5, true); + check_age("unused 5", false, 5, true, 6, false); + check_age("unused 6", false, 6, true, 7, false); + check_age("unused -1", false, -1, true, 0, false); + check_age( + "unused LONG_MIN", + false, + c_long::MIN, + true, + c_long::MIN + 1, + false, + ); + check_age( + "unused LONG_MAX - 1", + false, + c_long::MAX - 1, + true, + c_long::MAX, + false, + ); +} + +#[test] +fn steps_cache_clean_used() { + let counts: [c_long; 9] = [0, 1, 3, 4, 5, 6, -1, c_long::MIN, c_long::MAX]; + for count in counts { + check_age("used", true, count, false, count, false); + } +} + +#[test] +fn steps_cache_clean_drop_at_limit() { + let mut count: c_long = 0; + for call in 1..=MAX_UNUSED { + let (counted, n, drop) = age_fields(clean(false, count), count); + assert!(counted); + assert_eq!(n, call); + assert_eq!(drop, call == MAX_UNUSED); + count = n; + } +} + +#[test] +fn steps_full_range_rule() { + for (format, yuv) in (0..).zip(YUV) { + assert_eq!(full_range(format, false), !yuv, "format {format}"); + assert!(full_range(format, true)); + } + for format in [-1, F::R10L + 1, 1000, i32::MIN, i32::MAX] { + assert!(full_range(format, false)); + assert!(full_range(format, true)); + } +} + +#[test] +fn steps_full_range2_rule() { + for (format, yuv) in (0..).zip(YUV) { + assert_eq!(full_range2(format, R::DEFAULT), !yuv, "format {format}"); + assert!(!full_range2(format, R::PARTIAL)); + assert!(full_range2(format, R::FULL)); + assert!(!full_range2(format, R::FULL + 1)); + assert!(!full_range2(format, -1)); + assert!(!full_range2(format, i32::MIN)); + assert!(!full_range2(format, i32::MAX)); + } + for format in [-1, F::R10L + 1, i32::MIN, i32::MAX] { + assert!(full_range2(format, R::DEFAULT)); + assert!(!full_range2(format, R::PARTIAL)); + assert!(full_range2(format, R::FULL)); + } +} + +#[test] +fn steps_preload_changed() { + let p = shape(F::NV12, 1920, 1080); + assert!(preload_changed(None, p)); + assert!(preload_changed(None, shape(0, 0, 0))); + assert!(!preload_changed(Some(p), p)); + assert!(preload_changed(Some(p), shape(F::NV12, 1921, 1080))); + assert!(preload_changed(Some(p), shape(F::NV12, 1920, 1081))); + assert!(preload_changed(Some(p), shape(F::I420, 1920, 1080))); + assert!(preload_changed(Some(p), shape(F::NV12, 1080, 1920))); + assert!(preload_changed( + Some(shape(F::P010, 64, 64)), + shape(F::I010, 64, 64) + )); + assert!(!preload_changed( + Some(shape(i32::MIN, u32::MAX, 0)), + shape(i32::MIN, u32::MAX, 0) + )); +} + +// --------------------------------------------------------------------------- +// new edge cases + +#[test] +fn edge_half() { + assert_eq!(half(0), 0); + assert_eq!(half(1), 1); + assert_eq!(half(2), 1); + assert_eq!(half(3), 2); + assert_eq!(half(u32::MAX - 1), 0x7fff_ffff); + assert_eq!(half(u32::MAX), 0); +} + +#[test] +fn edge_plane_copy() { + assert_eq!( + plane_copy(3, 7, 7), + PlaneCopy { + whole: true, + bytes: 21, + rows: 3, + src_stride: 7, + dst_stride: 7 + } + ); + assert_eq!( + plane_copy(3, 8, 7), + PlaneCopy { + whole: false, + bytes: 7, + rows: 3, + src_stride: 8, + dst_stride: 7 + } + ); + assert_eq!( + plane_copy(3, 6, 7), + PlaneCopy { + whole: false, + bytes: 6, + rows: 3, + src_stride: 6, + dst_stride: 7 + } + ); + assert_eq!( + plane_copy(0, 0, 0), + PlaneCopy { + whole: true, + ..PlaneCopy::default() + } + ); +} + +/// The plan is `plane_rows()` and `plane_copy()` for every format around +/// the enum, and the range flag. +#[test] +fn edge_plan_is_its_pieces() { + let lines = [ + (lines4(1, 2, 3, 4), lines4(1, 3, 3, 0)), + (lines_all(0), lines_all(u32::MAX)), + (lines4(7, 7, 7, 7), lines4(7, 7, 7, 7)), + ]; + for format in -3..=30 { + for height in [0, 1, 2, 3, 1081, u32::MAX - 1, u32::MAX] { + for (src, dst) in lines { + for full in [false, true] { + let plan = copy_plan(format, full, height, src, dst); + let rules = plane_rows(format); + assert_eq!(plan.color_range, !full); + assert_eq!(plan.count, rules.len()); + for (i, copy) in plan.planes.iter().enumerate() { + let want = rules.get(i).map_or(PlaneCopy::default(), |rule| { + plane_copy(rows_of(*rule, height), src[i], dst[i]) + }); + assert_eq!(*copy, want, "format {format} height {height} plane {i}"); + } + } + } + } + } +} + +#[test] +fn edge_cache_step_every_queue_length() { + let same = key(16, 9, 1); + let other = key(16, 9, 2); + for queued in 0..=64 { + let full = queued >= MAX_ASYNC_FRAMES; + assert_eq!( + cache_step(queued, same, same), + if full { + CacheStep::Overflow + } else { + CacheStep::Keep + } + ); + assert_eq!( + cache_step(queued, same, other), + if full { + CacheStep::Overflow + } else { + CacheStep::Reset + } + ); + } + assert_eq!(cache_step(usize::MAX, same, other), CacheStep::Overflow); + assert!(!texture_changed(same, same)); + assert!(texture_changed(same, other)); +} + +#[test] +fn edge_clean_counts() { + for count in -10..=10 { + let next = count + 1; + let want = if next == MAX_UNUSED { + CacheAge::Drop(next) + } else { + CacheAge::Counted(next) + }; + assert_eq!(clean(false, count), want, "count {count}"); + assert_eq!(clean(true, count), CacheAge::InUse); + } + // The C's ++ would overflow at LONG_MAX: no entry gets there (counts + // start at 0 and the entry is erased at 5); the port wraps. + assert_eq!(clean(false, c_long::MAX), CacheAge::Counted(c_long::MIN)); +} + +#[test] +fn edge_range_rules() { + for format in -3..=40 { + let yuv = is_yuv(format); + assert_eq!( + yuv, + usize::try_from(format) + .ok() + .and_then(|i| YUV.get(i)) + .copied() + .unwrap_or(false) + ); + assert_eq!(full_range(format, false), !yuv); + assert!(full_range(format, true)); + assert_eq!( + resolve_range(format, R::DEFAULT), + if yuv { R::PARTIAL } else { R::FULL } + ); + for range in -3..=5 { + if range != R::DEFAULT { + assert_eq!(resolve_range(format, range), range); + } + assert_eq!( + full_range2(format, range), + resolve_range(format, range) == R::FULL + ); + } + } +} + +fn any_u32() -> impl Strategy { + prop_oneof![ + Just(0u32), + Just(1u32), + Just(u32::MAX), + Just(u32::MAX - 1), + Just(0x8000_0000u32), + 0u32..4096, + any::(), + ] +} + +fn any_format() -> impl Strategy { + prop_oneof![-3i32..=30, any::()] +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(4000))] + + /// Any format outside the enum copies no plane, at any range, height + /// and line sizes. + #[test] + fn edge_outside_the_enum_copies_nothing( + format in prop_oneof![i32::MIN..0, 26i32..=i32::MAX], + full in any::(), + height in any_u32(), + src in proptest::array::uniform4(any_u32()), + dst in proptest::array::uniform4(any_u32()), + ) { + let plan = copy_plan(format, full, height, src, dst); + prop_assert_eq!(plan.count, 0); + prop_assert_eq!(plan.planes, [PlaneCopy::default(); PLANES]); + prop_assert_eq!(plan.color_range, !full); + } + + /// Each plane against a model in u64: the rows (height + 1 reduced + /// modulo 2^32, then halved), one copy of d * rows exactly when the line sizes are + /// equal (the product fits a 64-bit usize), else rows of the smaller. + #[test] + fn edge_plan_against_a_u64_model( + format in any_format(), + height in any_u32(), + src in proptest::array::uniform4(any_u32()), + dst in proptest::array::uniform4(any_u32()), + ) { + let plan = copy_plan(format, true, height, src, dst); + let rules = plane_rows(format); + prop_assert_eq!(plan.count, rules.len()); + for (i, rule) in rules.iter().enumerate() { + let rows = match rule { + Rows::Full => u64::from(height), + Rows::Half => ((u64::from(height) + 1) % (1 << 32)) / 2, + }; + let rows32 = u32::try_from(rows).unwrap(); + let (s, d) = (u64::from(src[i]), u64::from(dst[i])); + let copy = plan.planes[i]; + prop_assert_eq!(copy.rows, rows32); + prop_assert_eq!(copy.src_stride, src[i]); + prop_assert_eq!(copy.dst_stride, dst[i]); + prop_assert_eq!(copy.whole, s == d); + let bytes = if s == d { d * rows } else { d.min(s) }; + prop_assert_eq!(u64::try_from(copy.bytes).unwrap(), bytes); + } + } + + /// The queue limit comes first, then any difference in the keys. + #[test] + fn edge_cache_step_model( + queued in prop_oneof![0usize..=64, any::()], + cw in any_u32(), ch in any_u32(), ct in any::(), + fw in any_u32(), fh in any_u32(), ft in any::(), + same in any::(), + ) { + let cache = key(cw, ch, ct); + let frame = if same { cache } else { key(fw, fh, ft) }; + let want = if queued >= 30 { + CacheStep::Overflow + } else if cw != frame.width || ch != frame.height || ct != frame.convert_type { + CacheStep::Reset + } else { + CacheStep::Keep + }; + prop_assert_eq!(cache_step(queued, cache, frame), want); + } + + /// clean() over any count. + #[test] + fn edge_clean_model(used in any::(), count in any::()) { + let got = clean(used, count); + if used { + prop_assert_eq!(got, CacheAge::InUse); + } else { + let next = count.wrapping_add(1); + prop_assert_eq!(got, if next == 5 { CacheAge::Drop(next) } else { CacheAge::Counted(next) }); + } + } + + /// preload_changed() over any shapes. + #[test] + fn edge_preload_model( + has in any::(), + pf in any_format(), pw in any_u32(), ph in any_u32(), + nf in any_format(), nw in any_u32(), nh in any_u32(), + same in any::(), + ) { + let preload = shape(pf, pw, ph); + let frame = if same { preload } else { shape(nf, nw, nh) }; + let want = !has || preload != frame; + prop_assert_eq!(preload_changed(has.then_some(preload), frame), want); + } +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 53be9d9b46..1e016100cd 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -16,6 +16,7 @@ mod deinterlace_pacing; mod deinterlace_pacing_parity; mod draw_plan; mod draw_plan_parity; +mod frame_copy; mod ptm_gate; mod ptm_gate_parity; mod render_route; From 96cd623f9ed47c7b5f7ae4e80a9adfc4f489fd15 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 14:08:37 -0400 Subject: [PATCH 4/6] obs-source: Port the async frame copy plan Ports libobs/obs-source-frame-copy.c.h, the async frame copy plan and cache decisions of obs-source.c, to the frame_copy module of rust/obs-source. obs-source.c.h stays C: every memcpy() (it runs the plan), the metadata copy, obs_source_frame_create() and _destroy(), the async_cache and async_frames darrays and the walk that picks the first unused entry, async_mutex, the refcount atomics and get_convert_type(). The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/frame_copy.rs): - plane_rows(): copy_frame_data()'s switch, a match over the format's int giving each plane's rows rule (the height, or half() for 4:2:0 chroma), none for P216, P416 and any value outside the enum. Every format is matched by name, so a new one must be placed. - half() is (height + 1) / 2 with wrapping_add, so UINT32_MAX gives 0 as the C's uint32_t does; plane_copy() is one copy of dst x rows as a usize product (wrapping_mul, the C's size_t) when the line sizes are equal, else rows of the smaller line size; copy_plan() zips the rules with the four line sizes of each frame and sets the colour range flag when the frame is not full range. - cache_step(): the queue limit (MAX_ASYNC_FRAMES) first, then texture_changed() on the two keys. - clean(): an unused entry's ++unused_count in c_long, wrapping, and the drop when it reaches MAX_UNUSED. - full_range() and full_range2(), with is_yuv() and resolve_range() (format_is_yuv() and resolve_video_range(), matched by name). - preload_changed() on an Option. The module denies clippy's arithmetic, indexing, as (one allowed cast, u32 to usize), unwrap, expect, panic, todo, unimplemented and unreachable lints, keeps no state and takes no lock. Shims (src/ffi/source_frame_copy.rs): the six functions, with the header's structs mirrored #[repr(C)] and everything passed and returned by value, so no pointer crosses and the shims read and write no memory of the caller. The step crosses as the C's enum value. No intentional difference, except where the C is undefined: ++unused_count overflows a long at LONG_MAX, where the shim wraps; no entry gets past 5. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-frame-copy (LIBOBS_PORTED_SOURCES), whose PCH is , media-io/video-io.h (the enums, format_is_yuv() and resolve_video_range()) and the header. libobs.pch.h keeps the header for obs-source.c. The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. The exported symbol sets are unchanged and equal: 1766 on the C backend, the hybrid and libobs-c-reference; the six functions are local in all three, and the hybrid's come from the Rust archive (no C object of the group is in the hybrid libobs). ci/test_ported_sources.py checks the group on every host. test_source_frame_copy_steps still compiles the C file into itself, unchanged. The new test_source_frame_copy_steps_rust builds the same test source against libobs_rust-static (and libm except on MSVC): the test calls only the six functions and includes their header and media-io/video-io.h, neither of which needs SIMDe, so this variant runs the shims and the structs' layout. It passes 19 of 19. test_video_frame, the media restart tests, the v4l2 and PipeWire camera tests and the deinterlace tests see the swap through the hybrid. Tier 1 source::frame_copy passes 30/30. Refs #2192 --- ci/test_ported_sources.py | 1 + .../libobs-ported-source-frame-copy.pch.h | 14 ++ libobs/build/libobs.core-source+.h.c | 1 - .../libobs.ported-source-frame-copy+.h.c | 5 + libobs/build/source-sets.json | 1 + libobs/cmake/rust-exports.map | 8 + libobs/cmake/rust-hidden.txt | 11 + libobs/cmake/rust-unexports-macos.txt | 7 + libobs/obs-source-frame-copy.c.h | 2 +- rust/obs-source/src/ffi/mod.rs | 1 + rust/obs-source/src/ffi/source_frame_copy.rs | 224 ++++++++++++++++++ rust/obs-source/src/frame_copy.rs | 160 +++++++++++-- .../test_source_frame_copy_steps.cmake | 20 ++ 13 files changed, 429 insertions(+), 26 deletions(-) create mode 100644 libobs/build/libobs-ported-source-frame-copy.pch.h create mode 100644 libobs/build/libobs.ported-source-frame-copy+.h.c create mode 100644 rust/obs-source/src/ffi/source_frame_copy.rs diff --git a/ci/test_ported_sources.py b/ci/test_ported_sources.py index 305650ba7b..489d3c49da 100644 --- a/ci/test_ported_sources.py +++ b/ci/test_ported_sources.py @@ -45,6 +45,7 @@ "obs-source-deinterlace-draw.c.h.o", "obs-source-deinterlace-pacing.c.h.o", "obs-source-draw-plan.c.h.o", + "obs-source-frame-copy.c.h.o", "obs-source-load.c.h.o", "obs-source-ptm-gate.c.h.o", "obs-source-render-route.c.h.o", diff --git a/libobs/build/libobs-ported-source-frame-copy.pch.h b/libobs/build/libobs-ported-source-frame-copy.pch.h new file mode 100644 index 0000000000..1568554181 --- /dev/null +++ b/libobs/build/libobs-ported-source-frame-copy.pch.h @@ -0,0 +1,14 @@ +/* Precompiled header (BUILD.md) for the libobs unity group + * libobs.ported-source-frame-copy. obs-source-frame-copy.c needs its + * declarations (obs-source-frame-copy.h), the video_format and + * video_range_type enums and the format_is_yuv() and resolve_video_range() + * inlines (media-io/video-io.h) and memset, so the C oracle and the C test + * can compile it on their own. The implementation file includes this header + * and nothing else. */ + +#pragma once + +#include + +#include "media-io/video-io.h" +#include "obs-source-frame-copy.h" diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index cd51967df2..c4cf0c47b3 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -7,7 +7,6 @@ #include "libobs/obs-scene-item-audio-actions.c.h" #include "libobs/obs-scene.c.h" #include "libobs/obs-source-deinterlace.c.h" -#include "libobs/obs-source-frame-copy.c.h" #include "libobs/obs-source-transition.c.h" #include "libobs/obs-source.c.h" #include "libobs/obs-view.c.h" diff --git a/libobs/build/libobs.ported-source-frame-copy+.h.c b/libobs/build/libobs.ported-source-frame-copy+.h.c new file mode 100644 index 0000000000..ed9cce39e6 --- /dev/null +++ b/libobs/build/libobs.ported-source-frame-copy+.h.c @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): ported obs-source-frame-copy.c. */ + +#include "libobs/build/libobs-ported-source-frame-copy.pch.h" + +#include "libobs/obs-source-frame-copy.c.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index 493c150f29..3ffd9f4453 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -195,6 +195,7 @@ "build/libobs.ported-source-deinterlace-draw+.h.c", "build/libobs.ported-source-deinterlace-pacing+.h.c", "build/libobs.ported-source-draw-plan+.h.c", + "build/libobs.ported-source-frame-copy+.h.c", "build/libobs.ported-source-load+.h.c", "build/libobs.ported-source-profiler+.h.c", "build/libobs.ported-source-ptm-gate+.h.c", diff --git a/libobs/cmake/rust-exports.map b/libobs/cmake/rust-exports.map index 919f3c67a8..79d7edcda4 100644 --- a/libobs/cmake/rust-exports.map +++ b/libobs/cmake/rust-exports.map @@ -195,6 +195,14 @@ obs_source_draw_plan_reads_white_level; obs_source_draw_plan_multiplier; obs_source_draw_plan_convert; + /* obs-source-frame-copy.c. obs-source.c calls them inside libobs. Not + * EXPORT. */ + obs_source_frame_copy_plan; + obs_source_frame_copy_cache_step; + obs_source_frame_copy_cache_clean; + obs_source_frame_copy_full_range; + obs_source_frame_copy_full_range2; + obs_source_frame_copy_preload_changed; /* obs-source-ptm-gate.c. obs-source.c calls them inside libobs. Not * EXPORT. */ obs_source_ptm_gate_stop_time; diff --git a/libobs/cmake/rust-hidden.txt b/libobs/cmake/rust-hidden.txt index 7e56251857..ed50537c67 100644 --- a/libobs/cmake/rust-hidden.txt +++ b/libobs/cmake/rust-hidden.txt @@ -206,6 +206,17 @@ obs_source_draw_plan_technique obs_source_draw_plan_reads_white_level obs_source_draw_plan_multiplier obs_source_draw_plan_convert +# obs-source-frame-copy.c: copy_frame_data(), cache_video(), clean_cache(), +# preload_frame_changed() and the obs_source_output_video(), +# obs_source_preload_video() and obs_source_set_video_frame() families in +# obs-source.c call them inside libobs. Declared in obs-source-frame-copy.h +# without EXPORT, so the shims stay hidden the same way. +obs_source_frame_copy_plan +obs_source_frame_copy_cache_step +obs_source_frame_copy_cache_clean +obs_source_frame_copy_full_range +obs_source_frame_copy_full_range2 +obs_source_frame_copy_preload_changed # obs-source-ptm-gate.c: source_muted() and get_source_volume() in # obs-source.c call them inside libobs. Declared in obs-source-ptm-gate.h # without EXPORT, so the shims stay hidden the same way. diff --git a/libobs/cmake/rust-unexports-macos.txt b/libobs/cmake/rust-unexports-macos.txt index e361d13892..dd80a7ad9c 100644 --- a/libobs/cmake/rust-unexports-macos.txt +++ b/libobs/cmake/rust-unexports-macos.txt @@ -170,6 +170,13 @@ _obs_source_draw_plan_technique _obs_source_draw_plan_reads_white_level _obs_source_draw_plan_multiplier _obs_source_draw_plan_convert +# obs-source-frame-copy.c: obs-source.c calls them inside libobs. Not EXPORT. +_obs_source_frame_copy_plan +_obs_source_frame_copy_cache_step +_obs_source_frame_copy_cache_clean +_obs_source_frame_copy_full_range +_obs_source_frame_copy_full_range2 +_obs_source_frame_copy_preload_changed # obs-source-ptm-gate.c: obs-source.c calls them inside libobs. Not EXPORT. _obs_source_ptm_gate_stop_time _obs_source_ptm_gate_held diff --git a/libobs/obs-source-frame-copy.c.h b/libobs/obs-source-frame-copy.c.h index 0dd267d550..8f0cf27243 100644 --- a/libobs/obs-source-frame-copy.c.h +++ b/libobs/obs-source-frame-copy.c.h @@ -15,7 +15,7 @@ along with this program. If not, see . ******************************************************************************/ -#include "libobs/build/libobs.pch.h" +#include "libobs/build/libobs-ported-source-frame-copy.pch.h" /* The async frame copy plan and cache decisions, moved from obs-source.c: * the plane copies of copy_frame_data_line(), copy_frame_data_plane() and diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index f4595c2d6f..29f4e3ee79 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -9,6 +9,7 @@ pub mod source_deinterlace_draw; pub mod source_deinterlace_pacing; pub mod source_draw_plan; pub mod source_draw_technique; +pub mod source_frame_copy; pub mod source_ptm_gate; pub mod source_render_route; pub mod source_size; diff --git a/rust/obs-source/src/ffi/source_frame_copy.rs b/rust/obs-source/src/ffi/source_frame_copy.rs new file mode 100644 index 0000000000..b82244228f --- /dev/null +++ b/rust/obs-source/src/ffi/source_frame_copy.rs @@ -0,0 +1,224 @@ +//! C ABI for `libobs/obs-source-frame-copy.c.h`, the async frame copy plan +//! and cache decisions, declared in `libobs/obs-source-frame-copy.h` +//! without `EXPORT`. +//! +//! `obs-source.c.h` (still C) calls these with plain values it reads from +//! the frames and the source, and acts on the answers itself: +//! `copy_frame_data()` runs the [`obs_source_frame_copy_plan`] it gets with +//! its own `memcpy()`s; `cache_video()` (under `async_mutex`) clears the +//! cache or refuses the frame per [`obs_source_frame_copy_cache_step`], +//! with the two convert types it got from `get_convert_type()`; +//! `clean_cache()` stores the count and destroys and erases an entry per +//! [`obs_source_frame_copy_cache_clean`]; the `obs_source_output_video()`, +//! `_preload_video()` and `_set_video_frame()` families store +//! [`obs_source_frame_copy_full_range`] or +//! [`obs_source_frame_copy_full_range2`]; `preload_frame_changed()` returns +//! [`obs_source_frame_copy_preload_changed`]. Everything crosses by value: +//! no pointer crosses, `struct obs_source` and `struct obs_source_frame` are +//! never mirrored, and the shims read and write no memory of the caller, +//! take no lock and call nothing in libobs. They stay hidden +//! (`rust-hidden.txt`). +//! +//! No intentional differences from C, except where the C is undefined: +//! `clean_cache()`'s `++unused_count` would overflow a `long` at +//! `LONG_MAX`; the shim wraps. No entry gets there: counts start at 0 and +//! an entry is erased at 5. + +use std::ffi::{c_int, c_long}; + +use crate::frame_copy::{self as core, CacheAge, CacheStep, Key, PLANES, Shape}; + +/// `struct obs_source_frame_copy_lines` (`obs-source-frame-copy.h`). +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(non_camel_case_types)] +pub struct obs_source_frame_copy_lines { + pub linesize: [u32; PLANES], +} + +/// `struct obs_source_frame_plane_copy` (`obs-source-frame-copy.h`). +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(non_camel_case_types)] +pub struct obs_source_frame_plane_copy { + pub whole: bool, + /// `size_t`. + pub bytes: usize, + pub rows: u32, + pub src_stride: u32, + pub dst_stride: u32, +} + +/// `struct obs_source_frame_copy_plan` (`obs-source-frame-copy.h`). +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(non_camel_case_types)] +pub struct obs_source_frame_copy_plan { + pub color_range: bool, + /// `size_t`: at most `OBS_SOURCE_FRAME_COPY_PLANES`. + pub count: usize, + pub planes: [obs_source_frame_plane_copy; PLANES], +} + +/// `struct obs_source_frame_copy_key` (`obs-source-frame-copy.h`). +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(non_camel_case_types)] +pub struct obs_source_frame_copy_key { + pub width: u32, + pub height: u32, + /// An `enum convert_type` value. + pub convert_type: c_int, +} + +/// `struct obs_source_frame_cache_age` (`obs-source-frame-copy.h`). +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(non_camel_case_types)] +pub struct obs_source_frame_cache_age { + pub counted: bool, + /// `long`. + pub unused_count: c_long, + pub drop: bool, +} + +/// `struct obs_source_frame_copy_shape` (`obs-source-frame-copy.h`). +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[allow(non_camel_case_types)] +pub struct obs_source_frame_copy_shape { + /// An `enum video_format` value. + pub format: c_int, + pub width: u32, + pub height: u32, +} + +/// `enum obs_source_frame_cache_step`, as the C's enum is returned (an `int` +/// or `unsigned int`; every value fits both). +#[allow(non_camel_case_types)] +pub type obs_source_frame_cache_step = u32; +/// `OBS_SOURCE_FRAME_CACHE_KEEP`. +pub const OBS_SOURCE_FRAME_CACHE_KEEP: obs_source_frame_cache_step = 0; +/// `OBS_SOURCE_FRAME_CACHE_RESET`. +pub const OBS_SOURCE_FRAME_CACHE_RESET: obs_source_frame_cache_step = 1; +/// `OBS_SOURCE_FRAME_CACHE_OVERFLOW`. +pub const OBS_SOURCE_FRAME_CACHE_OVERFLOW: obs_source_frame_cache_step = 2; + +impl From for obs_source_frame_plane_copy { + fn from(copy: core::PlaneCopy) -> Self { + Self { + whole: copy.whole, + bytes: copy.bytes, + rows: copy.rows, + src_stride: copy.src_stride, + dst_stride: copy.dst_stride, + } + } +} + +impl From for obs_source_frame_copy_plan { + fn from(plan: core::CopyPlan) -> Self { + Self { + color_range: plan.color_range, + count: plan.count, + planes: plan.planes.map(obs_source_frame_plane_copy::from), + } + } +} + +const fn key(key: obs_source_frame_copy_key) -> Key { + Key { + width: key.width, + height: key.height, + convert_type: key.convert_type, + } +} + +const fn shape(shape: obs_source_frame_copy_shape) -> Shape { + Shape { + format: shape.format, + width: shape.width, + height: shape.height, + } +} + +/// `struct obs_source_frame_copy_plan obs_source_frame_copy_plan(int +/// format, bool full_range, uint32_t height, struct +/// obs_source_frame_copy_lines src, struct obs_source_frame_copy_lines +/// dst)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_frame_copy_plan( + format: c_int, + full_range: bool, + height: u32, + src: obs_source_frame_copy_lines, + dst: obs_source_frame_copy_lines, +) -> obs_source_frame_copy_plan { + core::copy_plan(format, full_range, height, src.linesize, dst.linesize).into() +} + +/// `enum obs_source_frame_cache_step obs_source_frame_copy_cache_step(size_t +/// queued, struct obs_source_frame_copy_key cache, struct +/// obs_source_frame_copy_key frame)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_frame_copy_cache_step( + queued: usize, + cache: obs_source_frame_copy_key, + frame: obs_source_frame_copy_key, +) -> obs_source_frame_cache_step { + match core::cache_step(queued, key(cache), key(frame)) { + CacheStep::Keep => OBS_SOURCE_FRAME_CACHE_KEEP, + CacheStep::Reset => OBS_SOURCE_FRAME_CACHE_RESET, + CacheStep::Overflow => OBS_SOURCE_FRAME_CACHE_OVERFLOW, + } +} + +/// `struct obs_source_frame_cache_age obs_source_frame_copy_cache_clean(bool +/// used, long unused_count)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_frame_copy_cache_clean( + used: bool, + unused_count: c_long, +) -> obs_source_frame_cache_age { + match core::clean(used, unused_count) { + CacheAge::InUse => obs_source_frame_cache_age { + counted: false, + unused_count, + drop: false, + }, + CacheAge::Counted(count) => obs_source_frame_cache_age { + counted: true, + unused_count: count, + drop: false, + }, + CacheAge::Drop(count) => obs_source_frame_cache_age { + counted: true, + unused_count: count, + drop: true, + }, + } +} + +/// `bool obs_source_frame_copy_full_range(int format, bool full_range)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_frame_copy_full_range(format: c_int, full_range: bool) -> bool { + core::full_range(format, full_range) +} + +/// `bool obs_source_frame_copy_full_range2(int format, int range)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_frame_copy_full_range2(format: c_int, range: c_int) -> bool { + core::full_range2(format, range) +} + +/// `bool obs_source_frame_copy_preload_changed(bool has_preload, struct +/// obs_source_frame_copy_shape preload, struct obs_source_frame_copy_shape +/// in)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_frame_copy_preload_changed( + has_preload: bool, + preload: obs_source_frame_copy_shape, + frame: obs_source_frame_copy_shape, +) -> bool { + core::preload_changed(has_preload.then_some(shape(preload)), shape(frame)) +} diff --git a/rust/obs-source/src/frame_copy.rs b/rust/obs-source/src/frame_copy.rs index 7316652a5b..0b6f811e63 100644 --- a/rust/obs-source/src/frame_copy.rs +++ b/rust/obs-source/src/frame_copy.rs @@ -40,6 +40,7 @@ clippy::unwrap_used, clippy::expect_used, clippy::panic, + clippy::todo, clippy::unimplemented, clippy::unreachable )] @@ -155,16 +156,44 @@ pub struct Shape { /// `u32::MAX` as the C's `uint32_t` does. #[must_use] pub const fn half(height: u32) -> u32 { - let _ = height; - todo!() + height.wrapping_add(1) / 2 } /// `copy_frame_data()`'s switch: the planes of `format` and how each one's /// rows follow the height. P216, P416 and values outside the enum have none. #[must_use] pub const fn plane_rows(format: i32) -> &'static [Rows] { - let _ = format; - todo!() + use Rows::{Full, Half}; + match format { + video_format::I420 | video_format::I010 => &[Full, Half, Half], + video_format::NV12 | video_format::P010 => &[Full, Half], + video_format::I444 | video_format::I422 | video_format::I210 | video_format::I412 => { + &[Full, Full, Full] + } + video_format::YVYU + | video_format::YUY2 + | video_format::UYVY + | video_format::NONE + | video_format::RGBA + | video_format::BGRA + | video_format::BGRX + | video_format::Y800 + | video_format::BGR3 + | video_format::AYUV + | video_format::V210 + | video_format::R10L => &[Full], + video_format::I40A => &[Full, Half, Half, Full], + video_format::I42A | video_format::YUVA | video_format::YA2L => &[Full, Full, Full, Full], + // "Unimplemented" + video_format::P216 | video_format::P416 => &[], + _ => &[], + } +} + +/// `size_t` of a `u32`. +#[allow(clippy::as_conversions)] +const fn size(value: u32) -> usize { + value as usize } /// `copy_frame_data_plane()` and `copy_frame_data_line()`: one copy of @@ -172,8 +201,23 @@ pub const fn plane_rows(format: i32) -> &'static [Rows] { /// of the smaller line size. #[must_use] pub const fn plane_copy(rows: u32, src: u32, dst: u32) -> PlaneCopy { - let _ = (rows, src, dst); - todo!() + if dst == src { + PlaneCopy { + whole: true, + bytes: size(dst).wrapping_mul(size(rows)), + rows, + src_stride: src, + dst_stride: dst, + } + } else { + PlaneCopy { + whole: false, + bytes: size(if dst < src { dst } else { src }), + rows, + src_stride: src, + dst_stride: dst, + } + } } /// `obs_source_frame_copy_plan()`: the copies from a frame of `format` and @@ -187,23 +231,46 @@ pub fn copy_plan( src: [u32; PLANES], dst: [u32; PLANES], ) -> CopyPlan { - let _ = (format, full_range, height, src, dst); - todo!() + let mut plan = CopyPlan { + color_range: !full_range, + ..CopyPlan::default() + }; + for (((slot, rule), s), d) in plan + .planes + .iter_mut() + .zip(plane_rows(format)) + .zip(src) + .zip(dst) + { + let rows = match rule { + Rows::Full => height, + Rows::Half => half(height), + }; + *slot = plane_copy(rows, s, d); + plan.count = plan.count.saturating_add(1); + } + plan } /// `async_texture_changed()`: the size or the convert type differs. #[must_use] pub const fn texture_changed(cache: Key, frame: Key) -> bool { - let _ = (cache, frame); - todo!() + cache.width != frame.width + || cache.height != frame.height + || cache.convert_type != frame.convert_type } /// `cache_video()`: `queued` is `async_frames.num`. The queue limit comes /// first, then [`texture_changed`]. #[must_use] pub const fn cache_step(queued: usize, cache: Key, frame: Key) -> CacheStep { - let _ = (queued, cache, frame); - todo!() + if queued >= MAX_ASYNC_FRAMES { + CacheStep::Overflow + } else if texture_changed(cache, frame) { + CacheStep::Reset + } else { + CacheStep::Keep + } } /// One entry of `clean_cache()`: an unused entry counts one more call @@ -212,23 +279,64 @@ pub const fn cache_step(queued: usize, cache: Key, frame: Key) -> CacheStep { /// which no entry reaches (counts start at 0 and the entry is erased at 5). #[must_use] pub const fn clean(used: bool, unused_count: c_long) -> CacheAge { - let _ = (used, unused_count); - todo!() + if used { + return CacheAge::InUse; + } + let count = unused_count.wrapping_add(1); + if count == MAX_UNUSED { + CacheAge::Drop(count) + } else { + CacheAge::Counted(count) + } } /// `format_is_yuv()` (`media-io/video-io.h`): false outside the enum. #[must_use] pub const fn is_yuv(format: i32) -> bool { - let _ = format; - todo!() + match format { + video_format::I420 + | video_format::NV12 + | video_format::I422 + | video_format::I210 + | video_format::YVYU + | video_format::YUY2 + | video_format::UYVY + | video_format::I444 + | video_format::I412 + | video_format::I40A + | video_format::I42A + | video_format::YUVA + | video_format::YA2L + | video_format::AYUV + | video_format::I010 + | video_format::P010 + | video_format::P216 + | video_format::P416 + | video_format::V210 => true, + video_format::NONE + | video_format::RGBA + | video_format::BGRA + | video_format::BGRX + | video_format::Y800 + | video_format::BGR3 + | video_format::R10L => false, + _ => false, + } } /// `resolve_video_range()` (`media-io/video-io.h`): `DEFAULT` is partial /// for YUV and full otherwise; any other value is kept. #[must_use] pub const fn resolve_range(format: i32, range: i32) -> i32 { - let _ = (format, range); - todo!() + if range == video_range::DEFAULT { + if is_yuv(format) { + video_range::PARTIAL + } else { + video_range::FULL + } + } else { + range + } } /// `obs_source_output_video()`, `obs_source_preload_video()` and @@ -236,22 +344,26 @@ pub const fn resolve_range(format: i32, range: i32) -> i32 { /// is full range. #[must_use] pub const fn full_range(format: i32, full_range: bool) -> bool { - let _ = (format, full_range); - todo!() + if is_yuv(format) { full_range } else { true } } /// The `*2()` variants: full range exactly when [`resolve_range`] gives /// `FULL`. #[must_use] pub const fn full_range2(format: i32, range: i32) -> bool { - let _ = (format, range); - todo!() + resolve_range(format, range) == video_range::FULL } /// `preload_frame_changed()`: there is no preload frame, or its size or /// format differs from the new frame's. #[must_use] pub const fn preload_changed(preload: Option, frame: Shape) -> bool { - let _ = (preload, frame); - todo!() + match preload { + None => true, + Some(preload) => { + frame.width != preload.width + || frame.height != preload.height + || frame.format != preload.format + } + } } diff --git a/test/cmocka/tests.d/test_source_frame_copy_steps.cmake b/test/cmocka/tests.d/test_source_frame_copy_steps.cmake index 1a49f97fc2..5c3e0a80a4 100644 --- a/test/cmocka/tests.d/test_source_frame_copy_steps.cmake +++ b/test/cmocka/tests.d/test_source_frame_copy_steps.cmake @@ -13,3 +13,23 @@ # and PipeWire camera tests and the deinterlace tests cover the callers # through the real libobs. # Target and test are declared in ../build/test-graph.json. + +# Exercise the unchanged characterization against the Rust shims. The test +# calls only the six hidden functions and includes only their header and +# media-io/video-io.h (for the enum values; neither needs SIMDe or +# obsconfig.h), so it links the archive alone: its obs-source member needs +# nothing from libobs or the other crates. Linking the archive directly avoids +# exporting private symbols just for a test. +if(TARGET libobs_rust-static) + add_executable(test_source_frame_copy_steps_rust test_source_frame_copy_steps.c) + target_include_directories( + test_source_frame_copy_steps_rust + PRIVATE "${CMAKE_SOURCE_DIR}/libobs" ${CMOCKA_INCLUDE_DIR} + ) + target_link_libraries(test_source_frame_copy_steps_rust PRIVATE libobs_rust-static ${CMOCKA_LIBRARIES}) + if(NOT MSVC) + target_link_libraries(test_source_frame_copy_steps_rust PRIVATE m) + endif() + add_dependencies(cmocka-tests test_source_frame_copy_steps_rust) + add_test(test_source_frame_copy_steps_rust ${CMAKE_CURRENT_BINARY_DIR}/test_source_frame_copy_steps_rust) +endif() From 4f9f756c51ffa02132479daf01de6e27e7f0dd1a Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 14:29:20 -0400 Subject: [PATCH 5/6] obs-source: Check the async frame copy plan port against the C Oracle (oracle/source_frame_copy.c): the original obs-source-frame-copy.c.h, unmodified, with its six functions renamed to oracle_* (the rename also renames the plan struct's tag, consistently), plus the layout of the six structs (size, alignment, every offset), the constants, the step and range values, the long, size_t, int, bool and enum sizes, and every enum video_format value. The structs are mirrored on the Rust side and passed by value, as the C does. Tier 3 (tests/source/frame_copy_parity.rs, 14 tests): every call runs the shim, the oracle and the core, and compares every field: - the layout and the values against the C headers; - exhaustive over format x line-size relation: 38 format values (-3 to 30 and the int ends) against all 625 patterns of the four planes (the source's line size equal, smaller, larger or 0, or the destination's 0), at 16 heights (0 to 5, 1079 to 1081 and the u32 wrap edges), 3 destination line sizes (1920, 7, UINT32_MAX) and both ranges: 2,280,000 plans, with one copies, row copies and no copy reached; - the line sizes real frames have, padded and not, at real sizes; - the cache step over every queue length up to 64 and the usize ends, against keys at the field ends that differ in one field alone or with the size swapped; - the clean step over every count from -1000 to 1000 and the c_long ends; - the range rules over formats -300 to 300 and the int ends, both ranges and every range from -10 to 10 and the int ends; - the preload rule over every combination of shapes around the edges; - proptests (8192 cases each) over full-range u32 heights and line sizes with any format, any queue length and keys, any count, any format and range, and any preload shapes. Intentional difference, unreachable, kept out of the oracle on every platform: an unused entry's ++unused_count at LONG_MAX overflows a long, which is undefined (MSVC can trap overflows, as STATUS_INTEGER_OVERFLOW did in #2184's Windows run). The oracle is never called with that input; there the shim and the core both wrap to LONG_MIN and are compared with each other. Counts start at 0 and an entry is erased at 5. Every other input of every function is defined C on every platform: the plan's arithmetic is unsigned (no division but (height + 1) / 2 in uint32_t), and the formats and ranges are only converted and compared. Tier 1 and the parity module passed 10 runs in a row with fresh seeds. The regression files hold the seeds proptest saved while mutants failed; all of them pass on the port. Mutation check (once, not committed; Tier 1 with 30 tests and Tier 3 with 14 in one run, failures per mutant): 40 mutants, 31 in the core and 9 in the shims. 37 fail; the 3 that survive are equivalent. | Mutant | Tier 1 | Tier 3 | |---|---|---| | half saturating / rounds down | 4 / 8 | 2 / 3 | | I420 plane 2 full height / NV12 chroma full height | 5 / 5 | 3 / 3 | | I40A alpha half height / I422 chroma half height | 4 / 4 | 3 / 3 | | P216 copies plane 0 / Y800 copies nothing | 3 / 3 | 3 / 3 | | one copy when dst >= src | 5 | 3 | | row bytes the larger / the source's | 6 / 4 | 3 / 3 | | one copy of a single row | 6 | 3 | | colour range inverted / strides swapped | 4 / 6 | 3 / 3 | | queue limit strict / at 31 | 3 / 2 | 2 / 3 | | texture change ignores the convert type / the width | 3 / 1 | 2 / 1 | | drop at or past the limit / used entries counted | 3 / 3 | 2 / 2 | | count saturating / not incremented on drop | 1 / 3 | 1 / 1 | | V210 not YUV / Y800 YUV | 3 / 3 | 1 / 1 | | default range always partial / *2() "not partial" is full | 2 / 2 | 1 / 2 | | RGB keeps its range / preload ignores the format / no preload is no change | 2 / 2 / 2 | 2 / 2 / 2 | | shim: lines swapped / reset and overflow swapped / plan count capped at 3 | 0 | 3 / 3 / 3 | | shim: in use counted / drop not set / preload always present / range2 arguments swapped | 0 | 2 / 1 / 2 / 1 | | equivalent: one copy's size saturating (a u32 x u32 product fits a 64-bit usize) | 0 | 0 | | equivalent: one copy's size from the source's line size (equal to the destination's) | 0 | 0 | | equivalent: shim swaps width and height in both keys (the comparison is symmetric) | 0 | 0 | Tier 1 does not reach the shims; Tier 3 kills every shim mutant but the equivalent one. "count saturating" differs only at LONG_MAX, where the oracle is not called: Tier 1's edge case and Tier 3's shim-to-core check there kill it. Refs #2192 --- rust/obs-c-oracle/oracle/source_frame_copy.c | 102 +++ rust/obs-c-oracle/src/lib.rs | 1 + rust/obs-c-oracle/src/source_frame_copy.rs | 155 +++++ .../tests/proptest-regressions/frame_copy.txt | 12 + .../frame_copy_parity.txt | 14 + .../tests/source/frame_copy_parity.rs | 627 ++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 7 files changed, 912 insertions(+) create mode 100644 rust/obs-c-oracle/oracle/source_frame_copy.c create mode 100644 rust/obs-c-oracle/src/source_frame_copy.rs create mode 100644 rust/obs-source/tests/proptest-regressions/frame_copy.txt create mode 100644 rust/obs-source/tests/proptest-regressions/frame_copy_parity.txt create mode 100644 rust/obs-source/tests/source/frame_copy_parity.rs diff --git a/rust/obs-c-oracle/oracle/source_frame_copy.c b/rust/obs-c-oracle/oracle/source_frame_copy.c new file mode 100644 index 0000000000..d337037fa2 --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_frame_copy.c @@ -0,0 +1,102 @@ +/* Test-only oracle: the original libobs/obs-source-frame-copy.c.h, + * unmodified, with its six functions renamed to oracle_* so they can link + * next to the Rust shims. The rename also renames the struct of the same + * name as the plan function (struct obs_source_frame_copy_plan), in the + * header and here alike. */ +#include +#include +#include + +#define obs_source_frame_copy_plan oracle_obs_source_frame_copy_plan +#define obs_source_frame_copy_cache_step oracle_obs_source_frame_copy_cache_step +#define obs_source_frame_copy_cache_clean oracle_obs_source_frame_copy_cache_clean +#define obs_source_frame_copy_full_range oracle_obs_source_frame_copy_full_range +#define obs_source_frame_copy_full_range2 oracle_obs_source_frame_copy_full_range2 +#define obs_source_frame_copy_preload_changed oracle_obs_source_frame_copy_preload_changed + +#include "obs-source-frame-copy.c.h" + +/* The layouts and values the C uses, in the order of + * rust/obs-c-oracle/src/source_frame_copy.rs; SIZE_MAX past the end. */ +size_t oracle_source_frame_copy_layout(size_t which); + +#define OFF(type, member) offsetof(struct type, member) + +size_t oracle_source_frame_copy_layout(size_t which) +{ + const size_t values[] = { + sizeof(struct obs_source_frame_copy_lines), + _Alignof(struct obs_source_frame_copy_lines), + OFF(obs_source_frame_copy_lines, linesize), + sizeof(struct obs_source_frame_plane_copy), + _Alignof(struct obs_source_frame_plane_copy), + OFF(obs_source_frame_plane_copy, whole), + OFF(obs_source_frame_plane_copy, bytes), + OFF(obs_source_frame_plane_copy, rows), + OFF(obs_source_frame_plane_copy, src_stride), + OFF(obs_source_frame_plane_copy, dst_stride), + /* the macro above renamed this struct's tag */ + sizeof(struct obs_source_frame_copy_plan), + _Alignof(struct obs_source_frame_copy_plan), + OFF(obs_source_frame_copy_plan, color_range), + OFF(obs_source_frame_copy_plan, count), + OFF(obs_source_frame_copy_plan, planes), + sizeof(struct obs_source_frame_copy_key), + _Alignof(struct obs_source_frame_copy_key), + OFF(obs_source_frame_copy_key, width), + OFF(obs_source_frame_copy_key, height), + OFF(obs_source_frame_copy_key, convert_type), + sizeof(struct obs_source_frame_cache_age), + _Alignof(struct obs_source_frame_cache_age), + OFF(obs_source_frame_cache_age, counted), + OFF(obs_source_frame_cache_age, unused_count), + OFF(obs_source_frame_cache_age, drop), + sizeof(struct obs_source_frame_copy_shape), + _Alignof(struct obs_source_frame_copy_shape), + OFF(obs_source_frame_copy_shape, format), + OFF(obs_source_frame_copy_shape, width), + OFF(obs_source_frame_copy_shape, height), + sizeof(enum obs_source_frame_cache_step), + OBS_SOURCE_FRAME_CACHE_KEEP, + OBS_SOURCE_FRAME_CACHE_RESET, + OBS_SOURCE_FRAME_CACHE_OVERFLOW, + OBS_SOURCE_FRAME_COPY_PLANES, + OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES, + OBS_SOURCE_FRAME_COPY_MAX_UNUSED, + sizeof(long), + sizeof(size_t), + sizeof(int), + sizeof(bool), + sizeof(enum video_format), + sizeof(enum video_range_type), + VIDEO_RANGE_DEFAULT, + VIDEO_RANGE_PARTIAL, + VIDEO_RANGE_FULL, + }; + return which < sizeof(values) / sizeof(values[0]) ? values[which] : SIZE_MAX; +} + +/* Every enum video_format value, in enum order (VIDEO_FORMAT_NONE to + * VIDEO_FORMAT_R10L); INT_MIN past the end. */ +int oracle_source_frame_copy_format(size_t which); + +int oracle_source_frame_copy_format(size_t which) +{ + const int values[] = { + VIDEO_FORMAT_NONE, VIDEO_FORMAT_I420, VIDEO_FORMAT_NV12, VIDEO_FORMAT_YVYU, VIDEO_FORMAT_YUY2, + VIDEO_FORMAT_UYVY, VIDEO_FORMAT_RGBA, VIDEO_FORMAT_BGRA, VIDEO_FORMAT_BGRX, VIDEO_FORMAT_Y800, + VIDEO_FORMAT_I444, VIDEO_FORMAT_BGR3, VIDEO_FORMAT_I422, VIDEO_FORMAT_I40A, VIDEO_FORMAT_I42A, + VIDEO_FORMAT_YUVA, VIDEO_FORMAT_AYUV, VIDEO_FORMAT_I010, VIDEO_FORMAT_P010, VIDEO_FORMAT_I210, + VIDEO_FORMAT_I412, VIDEO_FORMAT_YA2L, VIDEO_FORMAT_P216, VIDEO_FORMAT_P416, VIDEO_FORMAT_V210, + VIDEO_FORMAT_R10L, + }; + return which < sizeof(values) / sizeof(values[0]) ? values[which] : INT_MIN; +} + +#undef OFF +#undef obs_source_frame_copy_preload_changed +#undef obs_source_frame_copy_full_range2 +#undef obs_source_frame_copy_full_range +#undef obs_source_frame_copy_cache_clean +#undef obs_source_frame_copy_cache_step +#undef obs_source_frame_copy_plan diff --git a/rust/obs-c-oracle/src/lib.rs b/rust/obs-c-oracle/src/lib.rs index fdba683e90..b22abd2b91 100644 --- a/rust/obs-c-oracle/src/lib.rs +++ b/rust/obs-c-oracle/src/lib.rs @@ -135,6 +135,7 @@ pub mod source_conversion_plan; pub mod source_deinterlace_draw; pub mod source_deinterlace_pacing; pub mod source_draw_plan; +pub mod source_frame_copy; pub mod source_load; pub mod source_profiler; pub mod source_ptm_gate; diff --git a/rust/obs-c-oracle/src/source_frame_copy.rs b/rust/obs-c-oracle/src/source_frame_copy.rs new file mode 100644 index 0000000000..d4b1c3c46a --- /dev/null +++ b/rust/obs-c-oracle/src/source_frame_copy.rs @@ -0,0 +1,155 @@ +//! `oracle_*` build of `libobs/obs-source-frame-copy.c.h` +//! (oracle/source_frame_copy.c): the async frame copy plan and cache +//! decisions, renamed. +//! +//! The header's structs are mirrored here, passed and returned by value as +//! the C does; `oracle_source_frame_copy_layout` reports their layout and +//! the constants. The enums cross as `c_int`s, the step as the C's enum +//! value. + +use core::ffi::{c_int, c_long}; + +/// `OBS_SOURCE_FRAME_COPY_PLANES`. +pub const PLANES: usize = 4; + +/// `struct obs_source_frame_copy_lines`. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct OracleLines { + pub linesize: [u32; PLANES], +} + +/// `struct obs_source_frame_plane_copy`. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct OraclePlaneCopy { + pub whole: bool, + pub bytes: usize, + pub rows: u32, + pub src_stride: u32, + pub dst_stride: u32, +} + +/// `struct obs_source_frame_copy_plan`. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct OracleCopyPlan { + pub color_range: bool, + pub count: usize, + pub planes: [OraclePlaneCopy; PLANES], +} + +/// `struct obs_source_frame_copy_key`. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct OracleKey { + pub width: u32, + pub height: u32, + pub convert_type: c_int, +} + +/// `struct obs_source_frame_cache_age`. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct OracleCacheAge { + pub counted: bool, + pub unused_count: c_long, + pub drop: bool, +} + +/// `struct obs_source_frame_copy_shape`. +#[repr(C)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct OracleShape { + pub format: c_int, + pub width: u32, + pub height: u32, +} + +/// The entries of `oracle_source_frame_copy_layout`, in order. +pub const LAYOUT: [&str; 46] = [ + "sizeof(struct obs_source_frame_copy_lines)", + "_Alignof(struct obs_source_frame_copy_lines)", + "offsetof(lines, linesize)", + "sizeof(struct obs_source_frame_plane_copy)", + "_Alignof(struct obs_source_frame_plane_copy)", + "offsetof(plane_copy, whole)", + "offsetof(plane_copy, bytes)", + "offsetof(plane_copy, rows)", + "offsetof(plane_copy, src_stride)", + "offsetof(plane_copy, dst_stride)", + "sizeof(struct obs_source_frame_copy_plan)", + "_Alignof(struct obs_source_frame_copy_plan)", + "offsetof(plan, color_range)", + "offsetof(plan, count)", + "offsetof(plan, planes)", + "sizeof(struct obs_source_frame_copy_key)", + "_Alignof(struct obs_source_frame_copy_key)", + "offsetof(key, width)", + "offsetof(key, height)", + "offsetof(key, convert_type)", + "sizeof(struct obs_source_frame_cache_age)", + "_Alignof(struct obs_source_frame_cache_age)", + "offsetof(age, counted)", + "offsetof(age, unused_count)", + "offsetof(age, drop)", + "sizeof(struct obs_source_frame_copy_shape)", + "_Alignof(struct obs_source_frame_copy_shape)", + "offsetof(shape, format)", + "offsetof(shape, width)", + "offsetof(shape, height)", + "sizeof(enum obs_source_frame_cache_step)", + "OBS_SOURCE_FRAME_CACHE_KEEP", + "OBS_SOURCE_FRAME_CACHE_RESET", + "OBS_SOURCE_FRAME_CACHE_OVERFLOW", + "OBS_SOURCE_FRAME_COPY_PLANES", + "OBS_SOURCE_FRAME_COPY_MAX_ASYNC_FRAMES", + "OBS_SOURCE_FRAME_COPY_MAX_UNUSED", + "sizeof(long)", + "sizeof(size_t)", + "sizeof(int)", + "sizeof(bool)", + "sizeof(enum video_format)", + "sizeof(enum video_range_type)", + "VIDEO_RANGE_DEFAULT", + "VIDEO_RANGE_PARTIAL", + "VIDEO_RANGE_FULL", +]; + +unsafe extern "C" { + /// `obs_source_frame_copy_plan`. + pub fn oracle_obs_source_frame_copy_plan( + format: c_int, + full_range: bool, + height: u32, + src: OracleLines, + dst: OracleLines, + ) -> OracleCopyPlan; + /// `obs_source_frame_copy_cache_step`: the step as the C's enum value. + pub fn oracle_obs_source_frame_copy_cache_step( + queued: usize, + cache: OracleKey, + frame: OracleKey, + ) -> u32; + /// `obs_source_frame_copy_cache_clean`. Undefined in C for + /// `unused_count == LONG_MAX` with `used` false (`++` overflows). + pub fn oracle_obs_source_frame_copy_cache_clean( + used: bool, + unused_count: c_long, + ) -> OracleCacheAge; + /// `obs_source_frame_copy_full_range`. + pub fn oracle_obs_source_frame_copy_full_range(format: c_int, full_range: bool) -> bool; + /// `obs_source_frame_copy_full_range2`. + pub fn oracle_obs_source_frame_copy_full_range2(format: c_int, range: c_int) -> bool; + /// `obs_source_frame_copy_preload_changed`. + pub fn oracle_obs_source_frame_copy_preload_changed( + has_preload: bool, + preload: OracleShape, + frame: OracleShape, + ) -> bool; + /// The entry named `LAYOUT[which]`; `usize::MAX` past the end. + pub fn oracle_source_frame_copy_layout(which: usize) -> usize; + /// `enum video_format` value `which` in enum order; `i32::MIN` past the + /// end. + pub fn oracle_source_frame_copy_format(which: usize) -> c_int; +} diff --git a/rust/obs-source/tests/proptest-regressions/frame_copy.txt b/rust/obs-source/tests/proptest-regressions/frame_copy.txt new file mode 100644 index 0000000000..3fca921da6 --- /dev/null +++ b/rust/obs-source/tests/proptest-regressions/frame_copy.txt @@ -0,0 +1,12 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 16308e3820af2fec50d09fe0e117f736ba8db88f724c953b1e4694f3feadfc17 # shrinks to format = 1, height = 4294967295, src = [0, 0, 0, 0], dst = [0, 0, 0, 0] +cc adc216a154a3b742b5cdcb55de0540ec75e8f790bf448d95def9f6284918335b # shrinks to format = 0, height = 0, src = [1, 0, 0, 0], dst = [0, 0, 0, 0] +cc 150cbdaf24574bd955ce378a885008cbbadbcca3278ac55741b46a2f423c0d84 # shrinks to queued = 30, cw = 0, ch = 0, ct = 0, fw = 0, fh = 0, ft = 0, same = false +cc 055d0df56821b4b9f37ad7a5ff62d12e7c111b43bbbe8b04d94192be81fd15ad # shrinks to queued = 0, cw = 4294967295, ch = 4294967295, ct = 0, fw = 4294967295, fh = 4294967295, ft = -1, same = false +cc 195ba24b0df767c36858da51de06756c379433b2735ae8c323368ebbd708e053 # shrinks to used = true, count = 0 +cc afb756ee6827734b0ba5b38d57772178f7113b6f7a70c705a1f2cb527a5438c4 # shrinks to has = true, pf = 0, pw = 1, ph = 4294967295, nf = 1, nw = 1, nh = 4294967295, same = false diff --git a/rust/obs-source/tests/proptest-regressions/frame_copy_parity.txt b/rust/obs-source/tests/proptest-regressions/frame_copy_parity.txt new file mode 100644 index 0000000000..6e489626da --- /dev/null +++ b/rust/obs-source/tests/proptest-regressions/frame_copy_parity.txt @@ -0,0 +1,14 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 2cffe3ca7beb60eac02c82d81cf5ee3f3b88e92337f3fec62ac5f76b39535924 # shrinks to format = 13, full = false, height = 4294967295, src = [0, 0, 0, 0], dst = [0, 0, 0, 0], equal = [false, false, false, false] +cc b4f9104542753dab62bdf505cf618e27e1f742c2431ee7ded392b931a801cbaa # shrinks to format = 17, full = false, height = 4294967295, src = [0, 0, 0, 0], dst = [0, 0, 0, 0], equal = [false, false, false, false] +cc 588260675500a16f03fd430ffb02d91fee3e709a453cfbea0579af424f0deab5 # shrinks to format = 18, full = false, height = 4294967295, src = [0, 0, 0, 0], dst = [0, 0, 0, 0], equal = [false, false, false, false] +cc 40fc726869684a03eb5d5d1e7c65da0a3176783490a9a38c3d3c09ab6ec3b1b0 # shrinks to format = 12, full = false, height = 4294967295, src = [0, 0, 0, 0], dst = [0, 0, 0, 0], equal = [false, false, false, false] +cc 332e4e78dd08043d64e0d9cf39033eff061b0bdf1abc4848b87b5d8a12c444ec # shrinks to format = 22, full = false, height = 0, src = [0, 0, 0, 0], dst = [0, 0, 0, 0], equal = [false, false, false, false] +cc e5ae738129558fcca8941f87a12eb27469dbbaa8cd949657ae38dfd70a25b78f # shrinks to format = 9, full = false, height = 0, src = [0, 0, 0, 0], dst = [0, 0, 0, 0], equal = [false, false, false, false] +cc 93f4f13a0bcbe4d445ad26bc9e2910f0938c88d51007a888d895fd5c53242630 # shrinks to queued = 30, cache = (0, 0, 0), frame = (0, 0, 0), same = false +cc 902d9d7ee29dd374b05cffd3464433de2ffe3737dc3ff49abeadae9f6425acce # shrinks to queued = 0, cache = (2147483648, 4294967294, 0), frame = (2147483648, 4294967294, 1), same = false diff --git a/rust/obs-source/tests/source/frame_copy_parity.rs b/rust/obs-source/tests/source/frame_copy_parity.rs new file mode 100644 index 0000000000..9600eff7d4 --- /dev/null +++ b/rust/obs-source/tests/source/frame_copy_parity.rs @@ -0,0 +1,627 @@ +//! Tier 3: the six `obs_source_frame_copy_*` shims and the safe core +//! against the original C (`obs-c-oracle`, `oracle/source_frame_copy.c`: the +//! unmodified `libobs/obs-source-frame-copy.c.h` with its functions renamed). +//! +//! Every call runs the shim, the oracle and the core, and compares each +//! result field by field: every plane's whole flag, bytes, rows and strides, +//! the count and the colour range flag; the step as the C's enum value; the +//! age's three fields; the range and preload answers as bools. +//! +//! Runs: +//! - the layout of the six structs, the constants, the step and range values, +//! and every `enum video_format` value, against the C headers; +//! - exhaustive over format x line-size relation: every C format value (-3 +//! to 30 and the ends of `i32`, 38 values) against all 625 patterns of the +//! four planes (the source's line size equal to the destination's, +//! smaller, larger or 0, or the destination's 0), at 16 heights (0 to 5, +//! 1079 to 1081 and the `u32` wrap edges) and 3 destination line sizes +//! (1920, 7 and `u32::MAX`, each plane's 3 less than the one before), for +//! both ranges: 2,280,000 plans, every kind of copy reached; and the line +//! sizes real frames have, padded and not, at real sizes; +//! - a proptest (8192 cases) over full-range `u32` heights and line sizes, +//! any `i32` format and both ranges; +//! - the cache step over every queue length up to 64 and the `usize` ends +//! against keys that differ in each field alone, at the field ends; the +//! clean step over every count from -1000 to 1000 and the `c_long` ends; +//! the range rules over formats -300 to 300 and the `i32` ends, both ranges +//! and every `video_range_type` from -10 to 10 and the ends; the preload +//! rule over every combination of shapes around the edges; and proptests +//! over each. +//! +//! Intentional difference, unreachable, kept out of the oracle: for an +//! unused entry with a count of `LONG_MAX`, the C's `++unused_count` +//! overflows a `long`, which is undefined (and MSVC can trap it, as it traps +//! other overflows). The oracle is never called with that input, on any +//! platform; the shim and the core wrap to `LONG_MIN` and are compared with +//! each other there. No entry gets past 5: counts start at 0 and an entry is +//! erased at 5. Every other input of every function is defined in C on every +//! platform: the plan's arithmetic is unsigned, and the formats and ranges +//! only convert and compare. + +use std::ffi::{c_int, c_long}; +use std::mem::{align_of, offset_of, size_of}; + +use obs_c_oracle::source_frame_copy::{ + self as oracle, OracleCacheAge, OracleCopyPlan, OracleKey, OracleLines, OracleShape, +}; +use obs_source::ffi::source_frame_copy::{ + self as shim, obs_source_frame_cache_age, obs_source_frame_copy_key, + obs_source_frame_copy_lines, obs_source_frame_copy_plan, obs_source_frame_copy_shape, + obs_source_frame_plane_copy, +}; +use obs_source::frame_copy::{ + self as core, CacheAge, CacheStep, Key, MAX_ASYNC_FRAMES, MAX_UNUSED, PLANES, Shape, + video_format as F, video_range as R, +}; +use proptest::prelude::*; + +/// One plan as plain fields: the colour range, the count, and each plane's +/// whole, bytes, rows and strides. +type PlanFields = (bool, usize, [(bool, usize, u32, u32, u32); PLANES]); + +fn shim_fields(plan: obs_source_frame_copy_plan) -> PlanFields { + ( + plan.color_range, + plan.count, + plan.planes + .map(|p| (p.whole, p.bytes, p.rows, p.src_stride, p.dst_stride)), + ) +} + +fn oracle_fields(plan: OracleCopyPlan) -> PlanFields { + ( + plan.color_range, + plan.count, + plan.planes + .map(|p| (p.whole, p.bytes, p.rows, p.src_stride, p.dst_stride)), + ) +} + +fn core_fields(plan: core::CopyPlan) -> PlanFields { + ( + plan.color_range, + plan.count, + plan.planes + .map(|p| (p.whole, p.bytes, p.rows, p.src_stride, p.dst_stride)), + ) +} + +#[track_caller] +fn check_plan( + format: c_int, + full_range: bool, + height: u32, + src: [u32; PLANES], + dst: [u32; PLANES], +) { + let s = shim_fields(shim::obs_source_frame_copy_plan( + format, + full_range, + height, + obs_source_frame_copy_lines { linesize: src }, + obs_source_frame_copy_lines { linesize: dst }, + )); + // SAFETY: plain values in and out; every input is defined in C. + let c = oracle_fields(unsafe { + oracle::oracle_obs_source_frame_copy_plan( + format, + full_range, + height, + OracleLines { linesize: src }, + OracleLines { linesize: dst }, + ) + }); + let r = core_fields(core::copy_plan(format, full_range, height, src, dst)); + assert_eq!( + s, c, + "plan: shim vs C, format {format} full {full_range} height {height} src {src:?} dst {dst:?}" + ); + assert_eq!(r, c, "plan: core vs C, format {format}"); +} + +fn step_value(step: CacheStep) -> u32 { + match step { + CacheStep::Keep => shim::OBS_SOURCE_FRAME_CACHE_KEEP, + CacheStep::Reset => shim::OBS_SOURCE_FRAME_CACHE_RESET, + CacheStep::Overflow => shim::OBS_SOURCE_FRAME_CACHE_OVERFLOW, + } +} + +#[track_caller] +fn check_step(queued: usize, cache: (u32, u32, c_int), frame: (u32, u32, c_int)) { + let s = shim::obs_source_frame_copy_cache_step( + queued, + obs_source_frame_copy_key { + width: cache.0, + height: cache.1, + convert_type: cache.2, + }, + obs_source_frame_copy_key { + width: frame.0, + height: frame.1, + convert_type: frame.2, + }, + ); + // SAFETY: plain values. + let c = unsafe { + oracle::oracle_obs_source_frame_copy_cache_step( + queued, + OracleKey { + width: cache.0, + height: cache.1, + convert_type: cache.2, + }, + OracleKey { + width: frame.0, + height: frame.1, + convert_type: frame.2, + }, + ) + }; + let r = step_value(core::cache_step( + queued, + Key { + width: cache.0, + height: cache.1, + convert_type: cache.2, + }, + Key { + width: frame.0, + height: frame.1, + convert_type: frame.2, + }, + )); + assert_eq!(s, c, "step: shim vs C, {queued} {cache:?} {frame:?}"); + assert_eq!(r, c, "step: core vs C, {queued} {cache:?} {frame:?}"); +} + +fn age_core(used: bool, count: c_long) -> (bool, c_long, bool) { + match core::clean(used, count) { + CacheAge::InUse => (false, count, false), + CacheAge::Counted(n) => (true, n, false), + CacheAge::Drop(n) => (true, n, true), + } +} + +const fn age_shim(age: obs_source_frame_cache_age) -> (bool, c_long, bool) { + (age.counted, age.unused_count, age.drop) +} + +const fn age_oracle(age: OracleCacheAge) -> (bool, c_long, bool) { + (age.counted, age.unused_count, age.drop) +} + +/// Whether the C is defined for this input: `++unused_count` overflows at +/// `LONG_MAX`. +const fn clean_defined(used: bool, count: c_long) -> bool { + used || count != c_long::MAX +} + +#[track_caller] +fn check_clean(used: bool, count: c_long) { + let s = age_shim(shim::obs_source_frame_copy_cache_clean(used, count)); + let r = age_core(used, count); + assert_eq!(s, r, "clean: shim vs core, {used} {count}"); + if clean_defined(used, count) { + // SAFETY: plain values; defined in C (not LONG_MAX unused). + let c = + age_oracle(unsafe { oracle::oracle_obs_source_frame_copy_cache_clean(used, count) }); + assert_eq!(s, c, "clean: shim vs C, {used} {count}"); + } +} + +#[track_caller] +fn check_full_range(format: c_int, full: bool) { + let s = shim::obs_source_frame_copy_full_range(format, full); + // SAFETY: plain values. + let c = unsafe { oracle::oracle_obs_source_frame_copy_full_range(format, full) }; + assert_eq!(s, c, "full_range({format}, {full})"); + assert_eq!(core::full_range(format, full), c); +} + +#[track_caller] +fn check_full_range2(format: c_int, range: c_int) { + let s = shim::obs_source_frame_copy_full_range2(format, range); + // SAFETY: plain values. + let c = unsafe { oracle::oracle_obs_source_frame_copy_full_range2(format, range) }; + assert_eq!(s, c, "full_range2({format}, {range})"); + assert_eq!(core::full_range2(format, range), c); +} + +#[track_caller] +fn check_preload(has: bool, preload: (c_int, u32, u32), frame: (c_int, u32, u32)) { + let shape = |(format, width, height): (c_int, u32, u32)| obs_source_frame_copy_shape { + format, + width, + height, + }; + let oshape = |(format, width, height): (c_int, u32, u32)| OracleShape { + format, + width, + height, + }; + let cshape = |(format, width, height): (c_int, u32, u32)| Shape { + format, + width, + height, + }; + let s = shim::obs_source_frame_copy_preload_changed(has, shape(preload), shape(frame)); + // SAFETY: plain values. + let c = unsafe { + oracle::oracle_obs_source_frame_copy_preload_changed(has, oshape(preload), oshape(frame)) + }; + let r = core::preload_changed(has.then_some(cshape(preload)), cshape(frame)); + assert_eq!(s, c, "preload {has} {preload:?} {frame:?}"); + assert_eq!(r, c, "preload core {has} {preload:?} {frame:?}"); +} + +// --------------------------------------------------------------------------- + +#[test] +fn layout_matches_the_c_header() { + type L = obs_source_frame_copy_lines; + type PC = obs_source_frame_plane_copy; + type P = obs_source_frame_copy_plan; + type K = obs_source_frame_copy_key; + type A = obs_source_frame_cache_age; + type S = obs_source_frame_copy_shape; + let rust: [usize; 46] = [ + size_of::(), + align_of::(), + offset_of!(L, linesize), + size_of::(), + align_of::(), + offset_of!(PC, whole), + offset_of!(PC, bytes), + offset_of!(PC, rows), + offset_of!(PC, src_stride), + offset_of!(PC, dst_stride), + size_of::

(), + align_of::

(), + offset_of!(P, color_range), + offset_of!(P, count), + offset_of!(P, planes), + size_of::(), + align_of::(), + offset_of!(K, width), + offset_of!(K, height), + offset_of!(K, convert_type), + size_of::(), + align_of::(), + offset_of!(A, counted), + offset_of!(A, unused_count), + offset_of!(A, drop), + size_of::(), + align_of::(), + offset_of!(S, format), + offset_of!(S, width), + offset_of!(S, height), + size_of::(), + usize::try_from(shim::OBS_SOURCE_FRAME_CACHE_KEEP).unwrap(), + usize::try_from(shim::OBS_SOURCE_FRAME_CACHE_RESET).unwrap(), + usize::try_from(shim::OBS_SOURCE_FRAME_CACHE_OVERFLOW).unwrap(), + PLANES, + MAX_ASYNC_FRAMES, + usize::try_from(MAX_UNUSED).unwrap(), + size_of::(), + size_of::(), + size_of::(), + size_of::(), + size_of::(), + size_of::(), + usize::try_from(R::DEFAULT).unwrap(), + usize::try_from(R::PARTIAL).unwrap(), + usize::try_from(R::FULL).unwrap(), + ]; + for (which, (name, value)) in oracle::LAYOUT.iter().zip(rust).enumerate() { + // SAFETY: a pure lookup. + let c = unsafe { oracle::oracle_source_frame_copy_layout(which) }; + assert_eq!(value, c, "{name}"); + } + // SAFETY: a pure lookup. + assert_eq!( + unsafe { oracle::oracle_source_frame_copy_layout(oracle::LAYOUT.len()) }, + usize::MAX + ); + assert_eq!(size_of::(), size_of::

()); + assert_eq!(size_of::(), size_of::()); +} + +#[test] +fn format_values_match_the_c_header() { + let formats = [ + F::NONE, + F::I420, + F::NV12, + F::YVYU, + F::YUY2, + F::UYVY, + F::RGBA, + F::BGRA, + F::BGRX, + F::Y800, + F::I444, + F::BGR3, + F::I422, + F::I40A, + F::I42A, + F::YUVA, + F::AYUV, + F::I010, + F::P010, + F::I210, + F::I412, + F::YA2L, + F::P216, + F::P416, + F::V210, + F::R10L, + ]; + for (which, format) in formats.into_iter().enumerate() { + // SAFETY: a pure lookup. + assert_eq!( + unsafe { oracle::oracle_source_frame_copy_format(which) }, + format + ); + } + // SAFETY: a pure lookup. + assert_eq!( + unsafe { oracle::oracle_source_frame_copy_format(formats.len()) }, + i32::MIN + ); +} + +/// Every C format value. +fn formats() -> Vec { + (-3..=30) + .chain([i32::MIN, i32::MIN + 1, i32::MAX - 1, i32::MAX]) + .collect() +} + +const HEIGHTS: [u32; 16] = [ + 0, + 1, + 2, + 3, + 4, + 5, + 1079, + 1080, + 1081, + 0x7fff_ffff, + 0x8000_0000, + 0x8000_0001, + u32::MAX - 2, + u32::MAX - 1, + u32::MAX, + 0x5555_5556, +]; + +const DST_LINES: [u32; 3] = [1920, 7, u32::MAX]; + +/// The source line size for relation `rel` against the destination's `d`: +/// equal, smaller, larger, 0, or (with the destination's set to 0 by the +/// caller) any. +fn related(d: u32, rel: u32) -> (u32, u32) { + match rel { + 0 => (d, d), + 1 => (d.saturating_sub(1), d), + 2 => (d.checked_add(1).unwrap_or(0), d), + 3 => (0, d), + _ => (d, 0), + } +} + +#[test] +fn plan_exhaustive_over_format_and_line_size_relation() { + let mut whole = 0u64; + let mut rows = 0u64; + let mut none = 0u64; + for format in formats() { + for pattern in 0..5u32.pow(4) { + for height in HEIGHTS { + for base in DST_LINES { + let mut src = [0; PLANES]; + let mut dst = [0; PLANES]; + let mut p = pattern; + for i in 0..PLANES { + // each plane's base differs, so the planes cannot be + // read from one another unnoticed + let d = base.wrapping_sub(u32::try_from(i).unwrap() * 3); + (src[i], dst[i]) = related(d, p % 5); + p /= 5; + } + for full in [false, true] { + check_plan(format, full, height, src, dst); + } + let plan = core::copy_plan(format, true, height, src, dst); + if plan.count == 0 { + none += 1; + } + for copy in &plan.planes[..plan.count] { + if copy.whole { + whole += 1; + } else { + rows += 1; + } + } + } + } + } + } + assert!(whole > 0 && rows > 0 && none > 0, "{whole} {rows} {none}"); +} + +/// The line sizes real frames have (video_frame_init()'s widths x 1, 2, 3, +/// 4 and the v210 rule), padded and not, at real heights. +#[test] +fn plan_real_frames() { + for format in formats() { + for width in [1u32, 2, 3, 640, 1280, 1281, 1920, 3840] { + for height in [1u32, 2, 360, 480, 719, 720, 1080, 2160] { + for pad in [0u32, 1, 32, 64] { + let l = [width * 2, width, width.div_ceil(2), width * 4]; + let padded = l.map(|x| x + pad); + check_plan(format, false, height, padded, l); + check_plan(format, true, height, l, padded); + check_plan(format, false, height, l, l); + } + } + } + } +} + +fn any_u32() -> impl Strategy { + prop_oneof![ + Just(0u32), + Just(1u32), + Just(u32::MAX), + Just(u32::MAX - 1), + Just(0x8000_0000u32), + 0u32..4096, + any::(), + ] +} + +fn any_format() -> impl Strategy { + prop_oneof![-3i32..=30, any::()] +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(8192))] + + #[test] + fn plan_full_range( + format in any_format(), + full in any::(), + height in any_u32(), + src in proptest::array::uniform4(any_u32()), + dst in proptest::array::uniform4(any_u32()), + equal in proptest::array::uniform4(any::()), + ) { + let mut src = src; + for i in 0..PLANES { + if equal[i] { + src[i] = dst[i]; + } + } + check_plan(format, full, height, src, dst); + } + + #[test] + fn step_full_range( + queued in prop_oneof![0usize..=64, any::()], + cache in (any_u32(), any_u32(), any::()), + frame in (any_u32(), any_u32(), any::()), + same in any::(), + ) { + check_step(queued, cache, if same { cache } else { frame }); + } + + #[test] + fn clean_full_range(used in any::(), count in any::()) { + check_clean(used, count); + } + + #[test] + fn range_full_range(format in any::(), full in any::(), range in any::()) { + check_full_range(format, full); + check_full_range2(format, range); + } + + #[test] + fn preload_full_range( + has in any::(), + preload in (any_format(), any_u32(), any_u32()), + frame in (any_format(), any_u32(), any_u32()), + same in any::(), + ) { + check_preload(has, preload, if same { preload } else { frame }); + } +} + +#[test] +fn step_every_queue_length_and_key_field() { + let ends = [0u32, 1, 0x7fff_ffff, 0x8000_0000, u32::MAX]; + let types = [0, 1, 20, -1, i32::MIN, i32::MAX]; + let queues = (0..=64).chain([usize::MAX - 1, usize::MAX]); + for queued in queues { + for &w in &ends { + for &h in &ends { + for &t in &types { + let cache = (w, h, t); + check_step(queued, cache, cache); + check_step(queued, cache, (w ^ 1, h, t)); + check_step(queued, cache, (w, h ^ 1, t)); + check_step(queued, cache, (w, h, t ^ 1)); + check_step(queued, cache, (h, w, t)); + } + } + } + } +} + +#[test] +fn clean_every_count() { + for count in -1000..=1000 { + check_clean(false, count); + check_clean(true, count); + } + for count in [ + c_long::MIN, + c_long::MIN + 1, + c_long::MAX - 1, + c_long::MAX, + MAX_UNUSED - 1, + MAX_UNUSED, + ] { + check_clean(false, count); + check_clean(true, count); + } +} + +/// The intentional difference: the shim and the core wrap where the C is +/// undefined; the oracle is not called there. +#[test] +fn clean_long_max_wraps() { + assert!(!clean_defined(false, c_long::MAX)); + assert_eq!( + age_shim(shim::obs_source_frame_copy_cache_clean(false, c_long::MAX)), + (true, c_long::MIN, false) + ); + assert_eq!(age_core(false, c_long::MAX), (true, c_long::MIN, false)); +} + +#[test] +fn range_rules_every_format_and_range() { + let ends = [i32::MIN, i32::MIN + 1, i32::MAX - 1, i32::MAX]; + for format in (-300..=300).chain(ends) { + for full in [false, true] { + check_full_range(format, full); + } + for range in (-10..=10).chain(ends) { + check_full_range2(format, range); + } + } +} + +#[test] +fn preload_every_combination() { + let formats = [-1, 0, 1, 2, 17, 18, 25, 26, i32::MIN, i32::MAX]; + let sizes = [0u32, 1, 2, 1080, 1920, u32::MAX]; + for has in [false, true] { + for &pf in &formats { + for &pw in &sizes { + for &ph in &sizes { + for &nf in &[pf, formats[0], i32::MAX] { + for &nw in &[pw, 0, u32::MAX] { + for &nh in &[ph, 1, u32::MAX] { + check_preload(has, (pf, pw, ph), (nf, nw, nh)); + } + } + } + } + } + } + } +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 1e016100cd..0742349596 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -17,6 +17,7 @@ mod deinterlace_pacing_parity; mod draw_plan; mod draw_plan_parity; mod frame_copy; +mod frame_copy_parity; mod ptm_gate; mod ptm_gate_parity; mod render_route; From fb4e0fb8aabd1bf9ac6d9e67aaf4711b54e6d43d Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 14:34:02 -0400 Subject: [PATCH 6/6] docs: Record the async frame copy plan port The rust/README.md crate table now lists libobs/obs-source-frame-copy.c.h among the rust/obs-source units, and its port table has a row for it: the six functions and the rules each keeps (with the upstream commits that made them), what stays C (every memcpy(), the metadata copy, frame create and destroy, the darrays and the first-unused walk with #2048's prev_frame reset, async_mutex, the refcount atomics and get_convert_type()), everything crossing by value, the wraps it keeps, what is characterized and not endorsed (P216 and P416 copy nothing, UINT32_MAX rows have no chroma rows, a count past 5 is never dropped, the *2() rule keeps an explicit partial range on RGB), the intentional difference (the long overflow at LONG_MAX, unreachable), the _rust variant of the characterization, the real-libobs tests and what Tier 3 compares. The crate's description and lib.rs say what it holds. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source. port_comparison gets its own bench file, benches/port_comparison/source_frame_copy.rs, in the sorted modules! list: 1024 copy plans (every format and two values outside the enum at 1920 x 1080, 1281 x 721 and 3840 x 2160, equal and padded line sizes) and 1024 frames' cache, clean, range and preload decisions, each folded into a checksum both sides must match. Neither side allocates, so the module's fixture check passes. On x86_64 Linux (GCC, not pinned, load average 45; median per 1024 calls): plan 62.24 us Rust and 39.48 us C (1.58x), cache 39.00 us Rust and 48.34 us C (0.81x), so about 22 ns more a plan and well under 0.1 us a frame. Fixes #2192 --- rust/README.md | 3 +- rust/obs-c-oracle/benches/port_comparison.rs | 3 + .../port_comparison/source_frame_copy.rs | 281 ++++++++++++++++++ rust/obs-source/Cargo.toml | 2 +- rust/obs-source/src/lib.rs | 5 +- 5 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 rust/obs-c-oracle/benches/port_comparison/source_frame_copy.rs diff --git a/rust/README.md b/rust/README.md index c90b41448e..8a408f0429 100644 --- a/rust/README.md +++ b/rust/README.md @@ -22,7 +22,7 @@ are in [docs/rust-port/naming.md](../docs/rust-port/naming.md#repository-layout- | `obs-missing-files/` | Port of `libobs/obs-missing-files.c.h` | | `obs-output/` | Ports of `libobs/obs-output-delay.c.h` (the stream delay), `libobs/obs-output-interleave.c.h` (the encoded packet interleaver), `libobs/obs-output-caption-text.c.h` (the caption text wrap) , `libobs/obs-output-protocols.c.h` (the output protocol-list match), `libobs/obs-encoder-roi.c.h` (the encoder region of interest checks and scaling), `libobs/obs-output-video-checks.c.h` (the output video size, conversion and capture checks), `libobs/obs-encoder-size.c.h` (the encoder size and info choices) and `libobs/obs-service-getters.c.h` (the service capability, connect-info, settings, properties and identity functions, and its lifecycle, references and activation) | | `obs-scene/` | Ports of the units extracted from `libobs/obs-scene.c`: `libobs/obs-scene-bounds.c.h`, `libobs/obs-scene-blend.c.h`, `libobs/obs-scene-coords.c.h`, `libobs/obs-scene-group-transform.c.h`, `libobs/obs-scene-audio-actions.c.h` (the core of the scene item visibility audio ramp), `libobs/obs-scene-extent.c.h` (the content extent of a scene or group), `libobs/obs-scene-crop.c.h` (the scene item crop arithmetic), `libobs/obs-scene-render-plan.c.h` (the per-item render decisions of `render_item` and `render_item_texture`), and `libobs/obs-scene-item-names.c.h` (the scene item save/load names and coordinate keys), and `libobs/obs-scene-audio-window.c.h` (the audio window decisions of `scene_audio_render`) | -| `obs-source/` | Ports of the units extracted from `libobs/obs-source.c`, `libobs/obs-source-deinterlace.c` and `libobs/obs-source-transition.c`: so far `libobs/obs-source-async-pacing.c.h` (the async video frame pacing of `get_closest_frame()`), `libobs/obs-source-audio-input.c.h` (the audio input steps of `process_audio()` and `reset_resampler()`), `libobs/obs-source-audio-timing.c.h` (the audio timestamp placement of `source_output_audio_data()`), `libobs/obs-source-color-space.c.h` (the colour space query routing of `obs_source_get_color_space()`), `libobs/obs-source-conversion-plan.c.h` (the async video GPU conversion plan), `libobs/obs-source-deinterlace-draw.c.h` (the deinterlace draw decisions), `libobs/obs-source-deinterlace-pacing.c.h` (the 2x deinterlace frame pacing), `libobs/obs-source-draw-plan.c.h` (the source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`), `libobs/obs-source-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `libobs/obs-source-render-route.c.h` (the source and filter render routing), `libobs/obs-source-size.c.h` (the size query routing of `obs_source_get_width()` and `obs_source_get_height()`), `libobs/obs-source-transition-clock.c.h` (the transition clock and fit of `libobs/obs-source-transition.c.h`), `libobs/obs-source-transition-render.c.h` (the transition render and audio steps of `libobs/obs-source-transition.c.h`) and `libobs/obs-source-volume.c.h` (the volume snap of `get_source_volume()` and the apply decisions of `apply_audio_volume()`) | +| `obs-source/` | Ports of the units extracted from `libobs/obs-source.c`, `libobs/obs-source-deinterlace.c` and `libobs/obs-source-transition.c`: so far `libobs/obs-source-async-pacing.c.h` (the async video frame pacing of `get_closest_frame()`), `libobs/obs-source-audio-input.c.h` (the audio input steps of `process_audio()` and `reset_resampler()`), `libobs/obs-source-audio-timing.c.h` (the audio timestamp placement of `source_output_audio_data()`), `libobs/obs-source-color-space.c.h` (the colour space query routing of `obs_source_get_color_space()`), `libobs/obs-source-conversion-plan.c.h` (the async video GPU conversion plan), `libobs/obs-source-deinterlace-draw.c.h` (the deinterlace draw decisions), `libobs/obs-source-deinterlace-pacing.c.h` (the 2x deinterlace frame pacing), `libobs/obs-source-draw-plan.c.h` (the source draw decisions of `rotate_async_video(), `libobs/obs-source-frame-copy.c.h` (the async frame copy plan and cache decisions)`, `obs_source_render_async_video()` and `source_render()`), `libobs/obs-source-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `libobs/obs-source-render-route.c.h` (the source and filter render routing), `libobs/obs-source-size.c.h` (the size query routing of `obs_source_get_width()` and `obs_source_get_height()`), `libobs/obs-source-transition-clock.c.h` (the transition clock and fit of `libobs/obs-source-transition.c.h`), `libobs/obs-source-transition-render.c.h` (the transition render and audio steps of `libobs/obs-source-transition.c.h`) and `libobs/obs-source-volume.c.h` (the volume snap of `get_source_volume()` and the apply decisions of `apply_audio_volume()`) | | `obs-audio-monitoring/` | Ports of `libobs/audio-monitoring/*` (so far `pulse/pulseaudio-stream-flags.c.h` and `pulse/pulseaudio-monitor-format.c.h`) | | `obs-audio-controls/` | Port of `libobs/obs-audio-controls.c.h` | | `obs-core/` | Ports of the units extracted from `libobs/obs.c.h`, `libobs/obs-module.c.h` and `libobs/obs-audio.c.h`: so far `libobs/obs-video-plan.c.h` (the video output plan), `libobs/obs-context-names.c.h` (the context names), `libobs/obs-source-load.c.h` (the source load migration and value rules), `libobs/obs-module-paths.c.h` (the module search paths), `libobs/obs-module-names.c.h` (module names and files), `libobs/obs-module-locale.c.h` (the module locale files), `libobs/obs-module-source-rules.c.h` (the source registration rules), `libobs/obs-module-type-rules.c.h` (the output, encoder and service registration rules), `libobs/obs-audio-mix.c.h` (the audio tick arithmetic); of `libobs/util/source-profiler.c.h` (per-source tick and render timing), which reads core state; of `libobs/obs-view-api.c.h` (views); and of `libobs/obs-video-copy-plan.c.h` (the output frame copy plan and scale effect choice, from `libobs/obs-video.c.h`); and of `libobs/obs-video-output-step.c.h` (the per-frame video output step decisions, from `libobs/obs-video.c.h`) | @@ -167,6 +167,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/obs-source-deinterlace-draw.c.h` | `obs-source::deinterlace_draw` | Swapped in hybrid `libobs` (group `libobs.ported-source-deinterlace-draw`). The draw decisions of deinterlacing, from `get_effect()`, `deinterlace_linear_required()`, `deinterlace_render()` and `obs_source_set_deinterlace_mode()`, as ten functions on plain values: `obs_source_deinterlace_draw_effect()` (the effect a mode draws with, an enum C maps to the effect slot and file; none for DISABLE and modes outside the enum), `obs_source_deinterlace_draw_linear_required()` (BLEND to YADIF_2X), `obs_source_deinterlace_draw_skip()` (the early return), `obs_source_deinterlace_draw_reads_linear_srgb()` and `_linear_srgb()` (the linear sRGB flag, `gs_get_linear_srgb()` read for a `GS_CS_SRGB` source only), `obs_source_deinterlace_draw_technique()`, `_reads_white_level()` and `_multiplier()` (the technique, the shared `enum obs_source_draw_technique` of `obs-source-draw-technique.h` and `obs-source::draw_technique`, which C maps to `Draw`, `DrawMultiply`, `DrawTonemap` or `DrawMultiplyTonemap`, and the multiplier for each pair of source and current colour spaces, `obs_get_video_sdr_white_level()` read only for a pair that multiplies), `obs_source_deinterlace_draw_frame2()` (the second field is due at `frame_ts + offset + half_duration - TWOX_TOLERANCE`, #395, #1995, #2048) and `obs_source_deinterlace_draw_mode_change()` (same, enable, disable or switch; a mode outside the enum is DISABLE, #2120). They are declared in `obs-source-deinterlace-draw.h` without `EXPORT`, so the shims stay hidden. `obs-source-deinterlace.c.h` keeps every `gs_*` call in its order and under its conditions (the decisions are separate calls, not one plan, because the early return comes before `convert_video_space()` and `gs_get_color_space()`), `obs_load_effect()` and the effect slots, the technique names, `convert_video_space()`, `obs_get_video_sdr_white_level()`, `enable_deinterlacing()`, `disable_deinterlacing()` and `obs_enter_graphics()`. Modes and colour spaces cross as `int`, so values outside the enums take the C's paths. The multiplier is one `f32` division, the C's, bit for bit (NaN payloads and infinities included); the frame2 sum wraps as the C's `uint64_t` does. Kept, characterized, not endorsed: a white level of 0 gives a multiplier of +inf from scRGB (-0 gives -inf, a NaN stays a NaN); the frame2 threshold wraps past `UINT64_MAX` (due at once) and below 0 when `half_duration < TWOX_TOLERANCE` (never due). No intentional differences. `test_deinterlace_draw_plan_steps` compiles the C into itself, and `test_deinterlace_draw_plan_steps_rust` runs the same test source against `libobs_rust-static`; `test_deinterlace_2x_field_order`, `test_deinterlace_mode_load` and `test_media_restart_first_frame` see the swap through the real libobs. Tier 3 compares the result bytes of every function: modes around the enum and a stride over all of `i32`, every pair of 649 spaces at 26 special white levels, every flag, frame2 over special `u64` values and at its threshold, `deinterlace_render()`'s decisions in its order, and proptests over full-range `u64` times and `f32` white levels as bit patterns. | | `libobs/obs-source-deinterlace-pacing.c.h` | `obs-source::deinterlace_pacing` | Swapped in hybrid `libobs` (group `libobs.ported-source-deinterlace-pacing`). The frame pacing of double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x), from `deinterlace_get_closest_frames()` and `ready_deinterlace_frames()`, as six functions on plain integers: `obs_source_deinterlace_pacing_hold()` (an unbuffered source keeps its frame while the frame's end is ahead of the clock by less than `TS_JUMP_THRESHOLD`), `obs_source_deinterlace_pacing_wait_for_next()` and `_waiting_frame_ts()` (the "don't skip ahead" guard of an unbuffered source with two frames queued), `obs_source_deinterlace_pacing_half_duration()` (half the time from the frame before; with no usable frame before, that is none, a timestamp going backwards, or a gap past twice `TS_JUMP_THRESHOLD`, the half duration before when it is sane, else half the canvas interval, #1965), `obs_source_deinterlace_pacing_buffered_offset()` (a buffered source's offset from the source clock, #395) and `obs_source_deinterlace_pacing_reanchor()` (an unbuffered source's offset, kept unless the frame comes early or misses its tick and its first field, #395). They are declared in `obs-source-deinterlace-pacing.h` without `EXPORT`, so the shims stay hidden. `obs-source-deinterlace.c.h` keeps the `async_unbuffered` and `deinterlace_offset` tests before the hold and the guard, the field and `obs->video` reads and the stores in their order, the `async_frames` queue, the buffered walk, `first_frame()`, the textures and the render. Every sum and difference wraps as the C's `uint64_t` does, and the re-anchor's lateness is `cast_signed()` of the wrapped difference, the C's `(int64_t)`. The crate still depends on no other crate. Kept, characterized, not endorsed: a timestamp going backwards wraps the guard's end and the waiting timestamp; the offsets wrap when the frame's clock is ahead of the canvas, and a lateness of 2^63 or more counts as early; at 29.97 and 59.94 frames a second into a slightly faster canvas the offset re-anchors on every frame. No intentional differences. `test_deinterlace_pacing_steps` compiles the C into itself, and `test_deinterlace_pacing_steps_rust` runs the same test source against `libobs_rust-static`; `test_deinterlace_2x_field_order` sees the swap through the real libobs. Tier 3 compares the result bytes of every function over the special values and full-range proptests, and sequences of 25, 29.97, 50 and 59.94 frames a second into 30 and 60 fps, buffered and unbuffered, through a model of the caller (the queue, the buffered walk and the stores), every call and the state after every tick; `first_frame_cadences` runs the first frame of a new source and the first after a pause at those rates and checks its fields and that every later state is the one of before #1965. | | `libobs/obs-source-draw-plan.c.h` | `obs-source::draw_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-draw-plan`). The source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`, as eight functions on plain values: `obs_source_draw_plan_rotation_x()` and `_y()` (the translate that moves a rotated async frame back into its box: height or width for 270 and 90, both for 180, nothing for any other angle; the rotation is in 0-359 since #2136), `obs_source_draw_plan_async_linear_srgb()` (the graphics flag, or an SDR frame into anything but `GS_CS_SRGB`, or an HDR frame tonemapped into SDR), `obs_source_draw_plan_async_nonlinear_alpha()` (an SDR frame drawn linear whose alpha is not linear), `obs_source_draw_plan_technique()` (the technique for each pair of source and current colour spaces, with or without non-linear alpha; `source_render()`'s conversion uses the same table without it), `obs_source_draw_plan_reads_white_level()` and `_multiplier()` (into or out of scRGB, the level over 80 or 80 over the level, `obs_get_video_sdr_white_level()` read only then, the same for both callers) and `obs_source_draw_plan_convert()` (whether `source_render()` draws through `color_space_texrender`: the two spaces in different classes, SDR, 709_EXTENDED and scRGB). The techniques are `enum obs_source_draw_technique` (`obs-source-draw-technique.h`, `obs-source::draw_technique`), shared with the deinterlace draw decisions; C maps them to names. They are declared in `obs-source-draw-plan.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `gs_*` call in its order and under its conditions (the decisions are separate calls, not one plan, so neither `gs_get_linear_srgb()` nor the white level read is hoisted), `convert_video_space()`, `obs_get_video_sdr_white_level()`, `gs_get_format_from_space()`, the texrenders and the technique names. Colour spaces cross as `int` and the rotation as `long` (`c_long`), so values outside the enum or 0-359 take the C's paths. The multiplier is one `f32` division, the C's, bit for bit (NaN payloads and infinities included), and the size converts to `f32` as `(float)uint32_t` does. Kept, characterized, not endorsed: a white level of 0 gives a multiplier of 0 into scRGB and +inf out of it (-0 gives -inf, a NaN stays a NaN); a rotation outside 0-359 is not moved; an SDR frame into a current space outside the enum is drawn linear with non-linear alpha, with `Draw`. No intentional differences. `test_source_draw_plan_steps` compiles the C into itself, and `test_source_draw_plan_steps_rust` runs the same test source against `libobs_rust-static`; `test_async_rotation_normalize`, `test_filter_color_space`, `test_source_color_space_steps`, `test_color_correction_16f`, `test_pipewire_cursor_hdr` and `test_texture_render` see the swap through the real libobs. Tier 3 compares the result bytes of every function: rotations around 0-359, a stride over all of `c_long` and past 32 bits, sizes around 2^24 and `u32::MAX`, every pair of 649 spaces with every flag, every pair of 12 spaces at 26 special white levels, both callers' decisions in their order, and proptests over any rotation, size, space and `f32` white level bit pattern. | +| `libobs/obs-source-frame-copy.c.h` | `obs-source::frame_copy` | Swapped in hybrid `libobs` (group `libobs.ported-source-frame-copy`). The async frame copy plan and cache decisions of `obs-source.c`, as six functions on plain values: `obs_source_frame_copy_plan()` (`copy_frame_data()`, `copy_frame_data_plane()` and `copy_frame_data_line()`: the colour range only when the frame is not full range, 065379b; then per format each plane's rows, `(height + 1) / 2` for 4:2:0 chroma, 8440a53; one copy of line size x rows in `size_t` when the two line sizes are equal, else rows of the smaller line size; no plane for P216, P416 and values outside the enum), `obs_source_frame_copy_cache_step()` (`cache_video()`'s queue limit at `MAX_ASYNC_FRAMES`, 7f7901b, which clears the cache and refuses the frame, then `async_texture_changed()`'s reset on a new size or convert type), `obs_source_frame_copy_cache_clean()` (one entry of `clean_cache()`: an unused entry counts one more call and is dropped at `MAX_UNUSED_FRAME_DURATION`, 3a90be3, 0815d5c), `obs_source_frame_copy_full_range()` and `_full_range2()` (a frame given to `obs_source_output_video()`, `obs_source_preload_video()` or `obs_source_set_video_frame()` that is not YUV is full range, 8d125dc; their `*2()` variants are full range when `resolve_video_range()` gives `VIDEO_RANGE_FULL`) and `obs_source_frame_copy_preload_changed()` (`preload_frame_changed()`). They are declared in `obs-source-frame-copy.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `memcpy()` (it runs the plan, each row at `y * line size` in `uint32_t`), the metadata copy, `obs_source_frame_create()` and `_destroy()`, the `async_cache` and `async_frames` darrays and the walk that picks the first unused entry (with #2048's `prev_frame` reset, #2009), `async_mutex`, the refcount atomics, and `get_convert_type()` (it passes the two convert types). Everything crosses by value, as `#[repr(C)]` mirrors of the header's structs: no pointer crosses. The formats and ranges cross as `int`, so values outside the enums take the C's paths. The arithmetic is the C's, wrap for wrap: `(height + 1) / 2` in `u32` and the one copy's size as a `usize` product. Kept, characterized, not endorsed: P216 and P416 copy no plane ("Unimplemented"); `UINT32_MAX` rows have 0 chroma rows; an unused count past 5 is never dropped (unreachable); the `*2()` rule keeps an explicit `VIDEO_RANGE_PARTIAL` on an RGB frame, which the first rule would make full range. Intentional difference, unreachable: `clean_cache()`'s `++unused_count` overflows a `long` at `LONG_MAX`, which is undefined in C; the port wraps. No entry gets past 5. `test_source_frame_copy_steps` compiles the C into itself, and `test_source_frame_copy_steps_rust` runs the same test source against `libobs_rust-static`; `test_video_frame`, the media restart tests, the v4l2 and PipeWire camera tests, `test_async_rotation_normalize` and the deinterlace tests see the swap through the real libobs. Tier 3 compares every field of every result over every C format value against every pattern of the four planes' line sizes (equal, smaller, larger, 0, or a destination of 0) at the `u32` height edges (2,280,000 plans), real frame line sizes, the queue limit over every length up to 64 with each key field changed alone, every count from -1000 to 1000 and the `c_long` ends but `LONG_MAX`, the range rules over formats -300 to 300 and every range from -10 to 10 with the `i32` ends, every preload combination around the edges, and proptests over each. | | `libobs/obs-source-ptm-gate.c.h` | `obs-source::ptm_gate` | Swapped in hybrid `libobs` (group `libobs.ported-source-ptm-gate`). The push-to-mute and push-to-talk gate of `source_muted()`, which gives every audio capture callback its muted flag, as four functions on plain values: `obs_source_ptm_gate_stop_time()` (#2058's `push_to_stop_time()`: while a gate's key is held, the moment it stops holding once let go, `os_time` plus the delay in milliseconds, saturated at `UINT64_MAX` where the product or the sum would wrap, #2051; `get_source_volume()` stores its stop times through it too), `obs_source_ptm_gate_held()` (a key let go holds strictly before its stop time), and `obs_source_ptm_gate_mutes()` and `obs_source_ptm_gate_talk_mutes()` (push-to-mute mutes while enabled and active, push-to-talk while enabled and not active). They are declared in `obs-source-ptm-gate.h` without `EXPORT`, so the shims stay hidden. `source_muted()` and `get_source_volume()` stay C with every read and write of the source's fields in the same order: a stop time is stored only while its gate is enabled and its key pressed (the mix thread stores the same two fields), each pressed flag is read again for its active flag (the hotkey thread writes `user_push_to_*_pressed` without atomics), a stop time is read only when its key is not pressed, and `enabled`, `user_muted` and the enabled flags only where the C's short-circuits reached them. `get_source_volume()` goes through the same four for its stop times, holds and gates (#2158), with the mix's pressed flags and `muted`. No pointer crosses. The core's stop time is `os_time.saturating_add(delay_ms.saturating_mul(NS_PER_MS))`, the same as the C's two checks. Kept, characterized, not endorsed: a saturated stop time does not hold at `os_time` `UINT64_MAX` itself; a stop time stored before a gate was turned off still holds when it is turned back on; while held, an earlier packet moves the stop time back; the mix and the capture callbacks share the stop times, so either one's store holds the other open. No intentional differences. `test_source_ptm_gate_steps` compiles the C into itself, and `test_source_ptm_gate_steps_rust` runs the same test source against `libobs_rust-static`; `test_source_audio_ranges` (the keys pressed through the hotkey router, the stop time read back after a packet, the saturation edges) sees the swap through the real libobs. Tier 3 compares the oracle, the shims and the core: the constant, the gates over their four inputs, the stop time and the hold over every pair of 46 special values and full-range proptests, and `source_muted()` copied onto a stand-in source over every combination of its six flags with stop times around the clock and saturated, and a full-range proptest, the result and both stored stop times. | | `libobs/obs-source-render-route.c.h` | `obs-source::render_route` | Swapped in hybrid `libobs` (group `libobs.ported-source-render-route`). The source and filter render routing of `source_render_video()`, `obs_source_main_render()`, `can_bypass()`, `obs_source_process_filter_begin_with_color_space()`, `obs_source_process_filter_tech_end()` and `obs_source_skip_video_filter()`, as 23 functions on plain values: `obs_source_render_route_filter()` (the target and parent check, no target first, #2177), the "no video", async update and inactive checks of `source_render_video()` with a `_reads_` function for each value the old short circuits read on one side only, the draw as a route over `enum obs_source_render_route_draw` (`_draw()`, `_skip()` and `_filter_render()` start it, `_draw_next()` takes the one value a `READ_` step asks for and returns the next step or the draw: default render, filters, main render, filter target, deinterlace or async video), `obs_source_main_render()`'s custom draw, sRGB-aware and default-effect rules, `can_bypass()` (`_bypass_reads_space()`, then `_bypass()`; the SRGB and space rules of #1493's filter colour space class), the zero size skip, the texrender format check, and the tech end's linear sRGB flag, technique name (`"Draw"` for NULL) and bypass or texture. They are declared in `obs-source-render-route.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `gs_*` call and texrender, the refs, the render calls, `get_base_width()` and `get_base_height()` (it passes the sizes), the debug markers and every field read in its order and under its conditions; that is why the decisions are many small calls and not one plan per function. Types, the direct render choice, colour spaces and formats cross as `int`; `obs-source.c.h` checks the four enums are `int`-sized. A draw step outside the enum comes back unchanged. Kept, characterized, not endorsed: `ASYNC` without `VIDEO` is no video; a direct render choice outside the enum is not direct; a size of 2^31 or more is negative in the C's `int` and does not skip. One pointer crosses: the technique name, returned as given or as a static `"Draw"`, never read. No other intentional differences. `test_source_render_route_steps` compiles the C into itself, and `test_source_render_route_steps_rust` runs the same test source against `libobs_rust-static`; `test_filter_color_space`, `test_filter_skip_removed`, `test_scroll_filter_wide_source`, `test_color_correction_16f`, `test_darken_transparent`, `test_scene_rotated_scale_filter`, `test_slideshow_size` and `test_texture_render` see the swap through the real libobs. Tier 3 compares the result bytes of every function over every boolean, sweeps of types, choices, spaces, formats and sizes over all of `i32`, every 16-bit flag pattern, 17 million bypass inputs, every draw step value, and each caller's decisions in its order (19.9 million filters, 166,400 sources), plus full-range proptests. | | `libobs/obs-source-size.c.h` | `obs-source::size_route` | Swapped in hybrid `libobs` (group `libobs.ported-source-size`). The size query routing of `obs_source_get_width()` and `obs_source_get_height()`, as two functions: `obs_source_size_route()` (where `get_base_width()` and `get_base_height()` take a source's size from: a transition reports `transition_actual_cx`/`cy` while enabled and 0 otherwise, 6839ff7; a source with data and a size callback asks it, f1aa71b, a filter only while enabled, f04ef23; a filter without one asks its target; an active async source reports its frame, `async_width` and `async_height` swapped unless `async_rotation % 180 == 0`, b9d6675, and 0 before its first frame, 506e30d; anything else reports 0) and `obs_source_size_entry()` (where `obs_source_get_width()` starts: a filter at itself, ed8998e; any other source at its first filter if it has filters, 3510e69, else at itself). A filter is a source with a filter parent, 8a1486d, not one of the filter type. They are declared in `obs-source-size.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps `data_valid()`, `filter_mutex` (a filter takes no lock; any other source holds its `filter_mutex` while it reads `filters.num`, asks the start and reads the first filter), the recursion into the target and the first filter, the `get_width` and `get_height` callbacks and the field each route reads. It reads `info.type`, `context.data != NULL`, the axis's callback `!= NULL`, `filter_parent != NULL`, `enabled`, `async_active` and the `long` `async_rotation` into `struct obs_source_size_node` (passed by value, mirrored); one route serves both axes. No source pointer crosses. The rotation is `c_long` (64 bits on Linux and macOS, 32 on Windows) and the swap is `wrapping_rem(180) != 0`, the C's `%` for every value. `obs-source.c.h` checks that the header's filter and transition types match `obs-source.h` and that `enum obs_source_type` is `int`-sized. Kept, characterized, not endorsed: a filter with no parent (detached) asks its callback even while disabled, and without one reports its async frame or 0 instead of asking a target; a source with a callback but no data reports its async frame or 0. No intentional differences. `test_source_size_route_steps` compiles the C into itself, and `test_source_size_route_steps_rust` runs the same test source against `libobs_rust-static`; `test_async_rotation_normalize` (#2136), `test_slideshow_size`, `test_filter_color_space`, `test_scene_item_crop`, `test_scene_rotated_scale_filter` and the transition and scene size tests see the swap through the real libobs. Tier 3 compares the node's layout and values, the route over every combination of the five flags with eight types and 46 rotations, every rotation from -100,000 to 100,000, the entry, and proptests over any node with any `c_long` rotation; a second oracle builds the unchanged C with a 32-bit `long`, as MSVC does, and is compared over the same rows and any `i32` rotation. | diff --git a/rust/obs-c-oracle/benches/port_comparison.rs b/rust/obs-c-oracle/benches/port_comparison.rs index 02d9bc40e4..17982c1bcb 100644 --- a/rust/obs-c-oracle/benches/port_comparison.rs +++ b/rust/obs-c-oracle/benches/port_comparison.rs @@ -58,6 +58,8 @@ mod source_deinterlace_draw; mod source_deinterlace_pacing; #[path = "port_comparison/source_draw_plan.rs"] mod source_draw_plan; +#[path = "port_comparison/source_frame_copy.rs"] +mod source_frame_copy; #[path = "port_comparison/source_load.rs"] mod source_load; #[path = "port_comparison/source_profiler.rs"] @@ -164,6 +166,7 @@ fn comparisons(criterion: &mut Criterion) { source_deinterlace_draw, source_deinterlace_pacing, source_draw_plan, + source_frame_copy, source_load, source_profiler, source_ptm_gate, diff --git a/rust/obs-c-oracle/benches/port_comparison/source_frame_copy.rs b/rust/obs-c-oracle/benches/port_comparison/source_frame_copy.rs new file mode 100644 index 0000000000..7a25f53038 --- /dev/null +++ b/rust/obs-c-oracle/benches/port_comparison/source_frame_copy.rs @@ -0,0 +1,281 @@ +//! obs-source: the async frame copy plan and cache decisions +//! (`libobs/obs-source-frame-copy.c.h`), which every frame an async source +//! outputs goes through: `obs_source_output_video()` (the range rule, the +//! cache step, the clean step of each cache entry and the copy plan), +//! `obs_source_preload_video()` and `obs_source_set_video_frame()` (the +//! preload rule). Each measurement folds its results into a checksum that +//! both sides must match: +//! - `plan`: 1024 copy plans, every video format and two values outside the +//! enum, at 1920 x 1080, 1281 x 721 and 3840 x 2160, with the line sizes +//! equal and with the source's padded; +//! - `cache`: 1024 frames' decisions: the range rule (`*2()` too), the cache +//! step at queue lengths around the limit, the clean step of 4 entries and +//! the preload rule. +//! +//! Neither side allocates. +//! +//! Measured when it was added (x86_64 Linux, GCC, not pinned to a core, a +//! heavily loaded machine with load average 45; median per 1024 calls): +//! plan 62.24 us Rust, 39.48 us C (1.58x); cache 39.00 us Rust, 48.34 us C +//! (0.81x). About 22 ns more a plan, as the core builds the plan and the +//! shim copies it out; a frame asks for one plan and one set of decisions, +//! so well under 0.1 us a frame. + +use std::ffi::{c_int, c_long}; +use std::hint::black_box; + +use criterion::{Criterion, Throughput}; +use obs_c_oracle::source_frame_copy::{ + self as c, OracleCopyPlan, OracleKey, OracleLines, OracleShape, +}; +use obs_source::ffi::source_frame_copy::{ + self as rs, obs_source_frame_copy_key, obs_source_frame_copy_lines, obs_source_frame_copy_plan, + obs_source_frame_copy_shape, +}; + +const COUNT: usize = 1024; + +#[derive(Clone, Copy)] +struct PlanInput { + format: c_int, + full_range: bool, + height: u32, + src: [u32; 4], + dst: [u32; 4], +} + +#[derive(Clone, Copy)] +struct CacheInput { + format: c_int, + full_range: bool, + range: c_int, + queued: usize, + cache: (u32, u32, c_int), + frame: (u32, u32, c_int), + used: [bool; 4], + unused: [c_long; 4], + has_preload: bool, + preload: (c_int, u32, u32), +} + +fn index(i: usize, n: usize) -> u32 { + u32::try_from(i % n).unwrap() +} + +fn plans() -> Vec { + let sizes = [(1920u32, 1080u32), (1281, 721), (3840, 2160)]; + (0..COUNT) + .map(|i| { + // formats 0 to 25 and two outside the enum + let format = i32::try_from(i % 28).unwrap() - 1; + let (width, height) = sizes[(i / 28) % 3]; + let dst = [width * 2, width, width.div_ceil(2), width]; + let pad = if (i / 84) % 2 == 1 { 64 } else { 0 }; + PlanInput { + format, + full_range: i % 2 == 0, + height, + src: dst.map(|l| l + pad), + dst, + } + }) + .collect() +} + +fn caches() -> Vec { + (0..COUNT) + .map(|i| { + let format = i32::try_from(i % 26).unwrap(); + let size = 1920 + index(i, 3); + CacheInput { + format, + full_range: i % 3 == 0, + range: i32::try_from(i % 4).unwrap(), + queued: 27 + (i % 6), + cache: (1920, 1080, 1), + frame: (size, 1080, i32::try_from(i % 2).unwrap()), + used: [i % 2 == 0, i % 3 == 0, false, true], + unused: [0, 4, c_long::from(u8::try_from(i % 5).unwrap()), 2], + has_preload: i % 5 != 0, + preload: (format, 1920, 1080), + } + }) + .collect() +} + +type Fields = (bool, usize, [(bool, usize, u32, u32, u32); 4]); + +struct Side { + plan: fn(PlanInput) -> Fields, + cache: fn(CacheInput) -> u64, +} + +const RUST: Side = Side { + plan: |p| { + let plan: obs_source_frame_copy_plan = rs::obs_source_frame_copy_plan( + p.format, + p.full_range, + p.height, + obs_source_frame_copy_lines { linesize: p.src }, + obs_source_frame_copy_lines { linesize: p.dst }, + ); + ( + plan.color_range, + plan.count, + plan.planes + .map(|c| (c.whole, c.bytes, c.rows, c.src_stride, c.dst_stride)), + ) + }, + cache: |x| { + let key = |(width, height, convert_type)| obs_source_frame_copy_key { + width, + height, + convert_type, + }; + let shape = |(format, width, height)| obs_source_frame_copy_shape { + format, + width, + height, + }; + let mut sum = u64::from(rs::obs_source_frame_copy_full_range(x.format, x.full_range)); + sum = sum * 2 + u64::from(rs::obs_source_frame_copy_full_range2(x.format, x.range)); + sum = sum * 4 + + u64::from(rs::obs_source_frame_copy_cache_step( + x.queued, + key(x.cache), + key(x.frame), + )); + for (used, unused) in x.used.into_iter().zip(x.unused) { + let age = rs::obs_source_frame_copy_cache_clean(used, unused); + sum = sum * 31 + + u64::from(age.counted) + + 2 * u64::from(age.drop) + + u64::try_from(age.unused_count).unwrap_or(0); + } + sum * 2 + + u64::from(rs::obs_source_frame_copy_preload_changed( + x.has_preload, + shape(x.preload), + shape((x.format, x.frame.0, 1080)), + )) + }, +}; + +const C: Side = Side { + plan: |p| { + // SAFETY: plain values. + let plan: OracleCopyPlan = unsafe { + c::oracle_obs_source_frame_copy_plan( + p.format, + p.full_range, + p.height, + OracleLines { linesize: p.src }, + OracleLines { linesize: p.dst }, + ) + }; + ( + plan.color_range, + plan.count, + plan.planes + .map(|c| (c.whole, c.bytes, c.rows, c.src_stride, c.dst_stride)), + ) + }, + // SAFETY: plain values; no count is LONG_MAX. + cache: |x| unsafe { + let key = |(width, height, convert_type)| OracleKey { + width, + height, + convert_type, + }; + let shape = |(format, width, height)| OracleShape { + format, + width, + height, + }; + let mut sum = u64::from(c::oracle_obs_source_frame_copy_full_range( + x.format, + x.full_range, + )); + sum = sum * 2 + + u64::from(c::oracle_obs_source_frame_copy_full_range2( + x.format, x.range, + )); + sum = sum * 4 + + u64::from(c::oracle_obs_source_frame_copy_cache_step( + x.queued, + key(x.cache), + key(x.frame), + )); + for (used, unused) in x.used.into_iter().zip(x.unused) { + let age = c::oracle_obs_source_frame_copy_cache_clean(used, unused); + sum = sum * 31 + + u64::from(age.counted) + + 2 * u64::from(age.drop) + + u64::try_from(age.unused_count).unwrap_or(0); + } + sum * 2 + + u64::from(c::oracle_obs_source_frame_copy_preload_changed( + x.has_preload, + shape(x.preload), + shape((x.format, x.frame.0, 1080)), + )) + }, +}; + +fn plan(side: &Side, inputs: &[PlanInput]) -> u64 { + let mut sum = 0u64; + for input in inputs { + let (color_range, count, planes) = (side.plan)(black_box(*input)); + sum = sum + .wrapping_mul(31) + .wrapping_add(u64::from(color_range)) + .wrapping_add(u64::try_from(count).unwrap()); + for (whole, bytes, rows, src, dst) in planes { + sum = sum + .wrapping_mul(31) + .wrapping_add(u64::from(whole)) + .wrapping_add(u64::try_from(bytes).unwrap()) + .wrapping_add(u64::from(rows)) + .wrapping_add(u64::from(src) << 1) + .wrapping_add(u64::from(dst) << 2); + } + } + sum +} + +fn cache(side: &Side, inputs: &[CacheInput]) -> u64 { + let mut sum = 0u64; + for input in inputs { + sum = sum + .wrapping_mul(31) + .wrapping_add((side.cache)(black_box(*input))); + } + sum +} + +pub(crate) fn benchmarks(criterion: &mut Criterion) { + let plans = plans(); + let caches = caches(); + assert_eq!( + plan(&RUST, &plans), + plan(&C, &plans), + "benchmark fixture differs: plan" + ); + assert_eq!( + cache(&RUST, &caches), + cache(&C, &caches), + "benchmark fixture differs: cache" + ); + + let mut group = criterion.benchmark_group("source_frame_copy/plan"); + group.throughput(Throughput::Elements(COUNT as u64)); + group.bench_function("rust", |b| b.iter(|| black_box(plan(&RUST, &plans)))); + group.bench_function("c", |b| b.iter(|| black_box(plan(&C, &plans)))); + group.finish(); + + let mut group = criterion.benchmark_group("source_frame_copy/cache"); + group.throughput(Throughput::Elements(COUNT as u64)); + group.bench_function("rust", |b| b.iter(|| black_box(cache(&RUST, &caches)))); + group.bench_function("c", |b| b.iter(|| black_box(cache(&C, &caches)))); + group.finish(); +} diff --git a/rust/obs-source/Cargo.toml b/rust/obs-source/Cargo.toml index d2c4b30fc3..5bf1fac9d0 100644 --- a/rust/obs-source/Cargo.toml +++ b/rust/obs-source/Cargo.toml @@ -2,7 +2,7 @@ autotests = false name = "obs-source" version = "0.1.0" -description = "Rust ports of libobs/obs-source-async-pacing.c.h (the async video frame pacing), libobs/obs-source-audio-input.c.h (the source audio input steps), libobs/obs-source-audio-timing.c.h (the source audio timestamp placement), libobs/obs-source-color-space.c.h (the colour space query routing), libobs/obs-source-conversion-plan.c.h (the async video GPU conversion plan), libobs/obs-source-deinterlace-draw.c.h (the deinterlace draw decisions), libobs/obs-source-deinterlace-pacing.c.h (the 2x deinterlace frame pacing), libobs/obs-source-draw-plan.c.h (the source draw decisions), libobs/obs-source-ptm-gate.c.h (the push-to-mute and push-to-talk gate), libobs/obs-source-render-route.c.h (the source and filter render routing), libobs/obs-source-size.c.h (the size query routing), libobs/obs-source-transition-clock.c.h (the transition clock and fit), libobs/obs-source-transition-render.c.h (the transition render and audio steps) and libobs/obs-source-volume.c.h (the source volume snap and apply decisions)" +description = "Rust ports of libobs/obs-source-async-pacing.c.h (the async video frame pacing), libobs/obs-source-audio-input.c.h (the source audio input steps), libobs/obs-source-audio-timing.c.h (the source audio timestamp placement), libobs/obs-source-color-space.c.h (the colour space query routing), libobs/obs-source-conversion-plan.c.h (the async video GPU conversion plan), libobs/obs-source-deinterlace-draw.c.h (the deinterlace draw decisions), libobs/obs-source-deinterlace-pacing.c.h (the 2x deinterlace frame pacing), libobs/obs-source-draw-plan.c.h (the source draw decisions), libobs/obs-source-frame-copy.c.h (the async frame copy plan and cache decisions), libobs/obs-source-ptm-gate.c.h (the push-to-mute and push-to-talk gate), libobs/obs-source-render-route.c.h (the source and filter render routing), libobs/obs-source-size.c.h (the size query routing), libobs/obs-source-transition-clock.c.h (the transition clock and fit), libobs/obs-source-transition-render.c.h (the transition render and audio steps) and libobs/obs-source-volume.c.h (the source volume snap and apply decisions)" edition.workspace = true rust-version.workspace = true license.workspace = true diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index d5de1fa402..c0f6e5f5bf 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -18,6 +18,9 @@ //! `obs_source_render_async_video()` and `source_render()` (the rotation //! translate, the linear sRGB flag and non-linear alpha, the technique, the //! multiplier and the colour space conversion); +//! `libobs/obs-source-frame-copy.c.h`, the async frame copy plan and cache +//! decisions of `copy_frame_data()`, `cache_video()`, `clean_cache()` and the +//! `obs_source_output_video()` families; //! `libobs/obs-source-ptm-gate.c.h`, the //! push-to-mute and push-to-talk gate of `source_muted()`; //! `libobs/obs-source-render-route.c.h`, the source and filter render @@ -33,7 +36,7 @@ //! //! The safe cores are [`async_pacing`], [`audio_input`], [`audio_timing`], //! [`color_space`], [`conversion_plan`], [`deinterlace_draw`], -//! [`deinterlace_pacing`], [`draw_plan`], [`ptm_gate`], [`render_route`], [`size_route`], +//! [`deinterlace_pacing`], [`draw_plan`], [`frame_copy`], [`ptm_gate`], [`render_route`], [`size_route`], //! [`transition_clock`], [`transition_render`] and [`volume_apply`]; [`draw_technique`] holds the //! technique and colour space values [`deinterlace_draw`] and [`draw_plan`] //! share. The C