From 2d4f74b21d9e2815892707cbbb1ae21baa32b190 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:15:56 -0400 Subject: [PATCH 1/6] libobs: Move the source draw decisions into obs-source-draw-plan.c Behaviour-neutral extraction before the Rust port. The decisions of rotate_async_video(), obs_source_render_async_video() and source_render() in obs-source.c.h move to libobs/obs-source-draw-plan.c.h as eight functions on plain values, declared in the new libobs/obs-source-draw-plan.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - 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 linear sRGB flag of an async frame, 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(): the non-linear alpha blend, 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, so both callers share it; - obs_source_draw_plan_reads_white_level() and obs_source_draw_plan_multiplier(): when the SDR white level is read and the multiplier, the level over 80 into scRGB and 80 over the level out of it, the same for both callers; - obs_source_draw_plan_convert(): whether source_render() draws through color_space_texrender, when the source and current spaces are in different classes (SDR, 709_EXTENDED, scRGB). The techniques are a new shared enum obs_source_draw_technique in libobs/obs-source-draw-technique.h, with obs_source_draw_technique_name() mapping it to the effect's technique name. The deinterlace draw decisions (#2153) move to it from their own technique enum: the four values keep their numbers (0 to 3) and two are added for the async draw (DrawNonlinearAlpha 4, DrawNonlinearAlphaMultiply 5), so the deinterlace shims, their oracle and test_deinterlace_draw_plan_steps change names only. deinterlace_render() maps the technique with the shared function instead of its own copy, and obs_source_deinterlace_draw_reads_white_level() lists the two new values (it never sees them). obs-source.c.h keeps every gs_* call in its order and under the same conditions, convert_video_space(), obs_get_video_sdr_white_level(), gs_get_format_from_space() and the texrenders. gs_get_linear_srgb() is still read for every async frame, after gs_get_color_space(), and obs_get_video_sdr_white_level() only for a pair that multiplies, after the colour spaces and the linear flag (async) or after gs_get_format_from_space() (source_render()); the decisions are separate calls so neither is hoisted. The multiplier is the same single float division, sdr_white_level / 80.0f or 80.0f / sdr_white_level, and the size converts to float as (float)uint32_t did. The rotation stays a long, so a value outside 0-359 takes the path it took before. Colour spaces cross as int, so a value outside the enum takes the paths it took before; obs-source.c.h already checks that the enum is int-sized. Checked against the unchanged functions of origin/master (outside the tree, not committed): both versions compiled against stubs that log every gs_*, obs_* and texrender call with its arguments (floats as their bits), and the traces compared byte for byte, under GCC -O0 and -O2 and clang -O0 and -O2, one trace hash for all four. 1,112,600 async renders (the eight texture, active and stale gates; 9 source x 9 current spaces, in the enum and out, x both linear flags x both alpha flags x 22 white levels, NaNs, infinities, signed zeros and subnormals included, x 19 rotations, LONG_MIN, LONG_MAX and values past 32 bits included, x 6 sizes, past 2^24 and UINT32_MAX included; 300,000 random) and 328,304 source renders (every pair x 22 white levels x no, a matching or a mismatched texrender x begin failing or not x 0 to 3 passes x 3 base sizes; 200,000 random). The traces are identical (24,697,614 calls); each of 12 planted changes (the 792f8473e and 080095786 linear rules, the non-linear alpha rule, the 90 and 270 translates, the multiplier, the white level read and its hoisting, the gs_get_linear_srgb() read, the conversion rule, the non-linear alpha technique, the conversion passing non-linear alpha) is caught. The new file joins libobs.core-source with libobs.pch.h, which includes the new header. The exported symbol sets are unchanged (1766). Refs #2187 --- libobs/build/libobs.core-source+.h.c | 1 + libobs/build/libobs.pch.h | 1 + libobs/build/source-sets.json | 2 + libobs/obs-source-deinterlace-draw.c.h | 24 +- libobs/obs-source-deinterlace-draw.h | 17 +- libobs/obs-source-deinterlace.c.h | 19 +- libobs/obs-source-draw-plan.c.h | 222 ++++++++++++++++++ libobs/obs-source-draw-plan.h | 107 +++++++++ libobs/obs-source-draw-technique.h | 61 +++++ libobs/obs-source.c.h | 143 ++--------- .../oracle/source_deinterlace_draw.c | 14 +- .../src/source_deinterlace_draw.rs | 10 +- .../cmocka/test_deinterlace_draw_plan_steps.c | 39 ++- 13 files changed, 463 insertions(+), 197 deletions(-) create mode 100644 libobs/obs-source-draw-plan.c.h create mode 100644 libobs/obs-source-draw-plan.h create mode 100644 libobs/obs-source-draw-technique.h diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index c4cf0c47b3..0bebf53bdc 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-draw-plan.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 0a84a36f25..bf83582380 100644 --- a/libobs/build/libobs.pch.h +++ b/libobs/build/libobs.pch.h @@ -147,6 +147,7 @@ #include "obs-source-audio-timing.h" #include "obs-source-color-space.h" #include "obs-source-conversion-plan.h" +#include "obs-source-draw-plan.h" #include "obs-source-ptm-gate.h" #include "obs-source-size.h" #include "obs-source-volume.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index 56b868d38e..6b593246f9 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -52,6 +52,8 @@ "obs-source-conversion-plan.h", "obs-source-deinterlace-draw.h", "obs-source-deinterlace-pacing.h", + "obs-source-draw-plan.h", + "obs-source-draw-technique.h", "obs-source-load.h", "obs-source-ptm-gate.h", "obs-source-size.h", diff --git a/libobs/obs-source-deinterlace-draw.c.h b/libobs/obs-source-deinterlace-draw.c.h index 964dd26793..7b9613fb17 100644 --- a/libobs/obs-source-deinterlace-draw.c.h +++ b/libobs/obs-source-deinterlace-draw.c.h @@ -109,23 +109,23 @@ bool obs_source_deinterlace_draw_linear_srgb(int source_space, bool gs_linear_sr obs_source_deinterlace_draw_linear_required(mode); } -enum obs_source_deinterlace_draw_technique obs_source_deinterlace_draw_technique(int source_space, int current_space) +enum obs_source_draw_technique obs_source_deinterlace_draw_technique(int source_space, int current_space) { - enum obs_source_deinterlace_draw_technique technique = OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW; + enum obs_source_draw_technique technique = OBS_SOURCE_DRAW_TECH_DRAW; switch ((enum gs_color_space)source_space) { case GS_CS_SRGB: case GS_CS_SRGB_16F: if ((enum gs_color_space)current_space == GS_CS_709_SCRGB) - technique = OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY; + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; break; case GS_CS_709_EXTENDED: switch ((enum gs_color_space)current_space) { case GS_CS_SRGB: case GS_CS_SRGB_16F: - technique = OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP; + technique = OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP; break; case GS_CS_709_SCRGB: - technique = OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY; + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; break; case GS_CS_709_EXTENDED: break; @@ -135,10 +135,10 @@ enum obs_source_deinterlace_draw_technique obs_source_deinterlace_draw_technique switch ((enum gs_color_space)current_space) { case GS_CS_SRGB: case GS_CS_SRGB_16F: - technique = OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP; + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP; break; case GS_CS_709_EXTENDED: - technique = OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY; + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; break; case GS_CS_709_SCRGB: break; @@ -151,11 +151,13 @@ enum obs_source_deinterlace_draw_technique obs_source_deinterlace_draw_technique bool obs_source_deinterlace_draw_reads_white_level(int source_space, int current_space) { switch (obs_source_deinterlace_draw_technique(source_space, current_space)) { - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY: - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP: + case OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY: + case OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP: + case OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY: return true; - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW: - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP: + case OBS_SOURCE_DRAW_TECH_DRAW: + case OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP: + case OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA: break; } diff --git a/libobs/obs-source-deinterlace-draw.h b/libobs/obs-source-deinterlace-draw.h index c860498810..971528c44d 100644 --- a/libobs/obs-source-deinterlace-draw.h +++ b/libobs/obs-source-deinterlace-draw.h @@ -20,6 +20,8 @@ #include #include +#include "obs-source-draw-technique.h" + /* The deinterlace draw decisions of obs-source-deinterlace.c, on plain values * (obs-source-deinterlace-draw.c): * - obs_source_deinterlace_draw_effect(): which effect a mode draws with @@ -63,19 +65,6 @@ enum obs_source_deinterlace_draw_effect { OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF_2X, }; -/* The technique deinterlace_render() draws with; obs-source-deinterlace.c - * maps it to the name. */ -enum obs_source_deinterlace_draw_technique { - /* "Draw" */ - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, - /* "DrawMultiply" */ - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY, - /* "DrawTonemap" */ - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP, - /* "DrawMultiplyTonemap" */ - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, -}; - /* What obs_source_set_deinterlace_mode() does. */ enum obs_source_deinterlace_draw_mode_change { /* nothing: the mode is the current one */ @@ -114,7 +103,7 @@ bool obs_source_deinterlace_draw_linear_srgb(int source_space, bool gs_linear_sr * GS_CS_709_SCRGB, DrawTonemap for GS_CS_709_EXTENDED into SDR, * DrawMultiplyTonemap for GS_CS_709_SCRGB into SDR, DrawMultiply for * GS_CS_709_SCRGB into GS_CS_709_EXTENDED, Draw for any other pair. */ -enum obs_source_deinterlace_draw_technique obs_source_deinterlace_draw_technique(int source_space, int current_space); +enum obs_source_draw_technique obs_source_deinterlace_draw_technique(int source_space, int current_space); /* Whether deinterlace_render() reads obs_get_video_sdr_white_level() for the * pair: when the technique multiplies. */ diff --git a/libobs/obs-source-deinterlace.c.h b/libobs/obs-source-deinterlace.c.h index a9a0c8c175..2696b0ed84 100644 --- a/libobs/obs-source-deinterlace.c.h +++ b/libobs/obs-source-deinterlace.c.h @@ -281,23 +281,6 @@ static inline gs_effect_t *get_effect(enum obs_deinterlace_mode mode) return NULL; } -/* The names of enum obs_source_deinterlace_draw_technique. */ -static const char *deinterlace_technique_name(enum obs_source_deinterlace_draw_technique technique) -{ - switch (technique) { - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW: - return "Draw"; - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY: - return "DrawMultiply"; - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP: - return "DrawTonemap"; - case OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP: - return "DrawMultiplyTonemap"; - } - - return "Draw"; -} - void deinterlace_render(obs_source_t *s) { gs_effect_t *effect = s->deinterlace_effect; @@ -326,7 +309,7 @@ void deinterlace_render(obs_source_t *s) const enum gs_color_space current_space = gs_get_color_space(); const char *tech_name = - deinterlace_technique_name(obs_source_deinterlace_draw_technique(source_space, current_space)); + obs_source_draw_technique_name(obs_source_deinterlace_draw_technique(source_space, current_space)); const float multiplier = obs_source_deinterlace_draw_reads_white_level(source_space, current_space) ? obs_source_deinterlace_draw_multiplier(source_space, current_space, obs_get_video_sdr_white_level()) diff --git a/libobs/obs-source-draw-plan.c.h b/libobs/obs-source-draw-plan.c.h new file mode 100644 index 0000000000..24ce8ec2e5 --- /dev/null +++ b/libobs/obs-source-draw-plan.c.h @@ -0,0 +1,222 @@ +/****************************************************************************** + 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 source draw decisions of obs-source.c, moved from rotate_async_video(), + * obs_source_render_async_video() and source_render(): the decisions, on + * plain values. obs-source.c keeps every gs_* call in its order, reads the + * colour spaces, the graphics linear sRGB flag, the white level and the + * source's fields, passes them in and acts on what comes back. Declared in + * obs-source-draw-plan.h without EXPORT. In hybrid libobs the Rust port + * (rust/obs-source) replaces this file. */ + +float obs_source_draw_plan_rotation_x(long rotation, uint32_t width, uint32_t height) +{ + float x = 0; + + /* obs_source_set_async_rotation() keeps the rotation in 0-359 */ + switch (rotation) { + case 270: + x = (float)height; + break; + case 180: + x = (float)width; + } + + return x; +} + +float obs_source_draw_plan_rotation_y(long rotation, uint32_t width, uint32_t height) +{ + float y = 0; + + switch (rotation) { + case 90: + y = (float)width; + break; + case 180: + y = (float)height; + } + + return y; +} + +bool obs_source_draw_plan_async_linear_srgb(int source_space, int current_space, bool gs_linear_srgb) +{ + bool linear_srgb = gs_linear_srgb; + switch ((enum gs_color_space)source_space) { + case GS_CS_SRGB: + linear_srgb = linear_srgb || ((enum gs_color_space)current_space != GS_CS_SRGB); + break; + case GS_CS_SRGB_16F: + break; + case GS_CS_709_EXTENDED: + case GS_CS_709_SCRGB: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + linear_srgb = true; + break; + case GS_CS_709_EXTENDED: + case GS_CS_709_SCRGB: + break; + } + } + + return linear_srgb; +} + +bool obs_source_draw_plan_async_nonlinear_alpha(int source_space, bool linear_srgb, bool linear_alpha) +{ + return ((enum gs_color_space)source_space == GS_CS_SRGB) && linear_srgb && !linear_alpha; +} + +enum obs_source_draw_technique obs_source_draw_plan_technique(int source_space, int current_space, bool nonlinear_alpha) +{ + enum obs_source_draw_technique technique = OBS_SOURCE_DRAW_TECH_DRAW; + switch ((enum gs_color_space)source_space) { + case GS_CS_SRGB: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_EXTENDED: + if (nonlinear_alpha) + technique = OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA; + break; + case GS_CS_709_SCRGB: + technique = nonlinear_alpha ? OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY + : OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; + } + break; + case GS_CS_SRGB_16F: + if ((enum gs_color_space)current_space == GS_CS_709_SCRGB) + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; + break; + case GS_CS_709_EXTENDED: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + technique = OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP; + break; + case GS_CS_709_SCRGB: + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; + break; + case GS_CS_709_EXTENDED: + break; + } + break; + case GS_CS_709_SCRGB: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP; + break; + case GS_CS_709_EXTENDED: + technique = OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY; + break; + case GS_CS_709_SCRGB: + break; + } + } + + return technique; +} + +bool obs_source_draw_plan_reads_white_level(int source_space, int current_space) +{ + switch ((enum gs_color_space)source_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_EXTENDED: + return (enum gs_color_space)current_space == GS_CS_709_SCRGB; + case GS_CS_709_SCRGB: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_EXTENDED: + return true; + case GS_CS_709_SCRGB: + break; + } + } + + return false; +} + +float obs_source_draw_plan_multiplier(int source_space, int current_space, float sdr_white_level) +{ + float multiplier = 1.0; + switch ((enum gs_color_space)source_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_EXTENDED: + if ((enum gs_color_space)current_space == GS_CS_709_SCRGB) + multiplier = sdr_white_level / 80.0f; + break; + case GS_CS_709_SCRGB: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_EXTENDED: + multiplier = 80.0f / sdr_white_level; + break; + case GS_CS_709_SCRGB: + break; + } + } + + return multiplier; +} + +bool obs_source_draw_plan_convert(int source_space, int current_space) +{ + switch ((enum gs_color_space)source_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + switch ((enum gs_color_space)current_space) { + case GS_CS_709_EXTENDED: + case GS_CS_709_SCRGB: + return true; + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + break; + } + break; + case GS_CS_709_EXTENDED: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_SCRGB: + return true; + case GS_CS_709_EXTENDED: + break; + } + break; + case GS_CS_709_SCRGB: + switch ((enum gs_color_space)current_space) { + case GS_CS_SRGB: + case GS_CS_SRGB_16F: + case GS_CS_709_EXTENDED: + return true; + case GS_CS_709_SCRGB: + break; + } + } + + return false; +} diff --git a/libobs/obs-source-draw-plan.h b/libobs/obs-source-draw-plan.h new file mode 100644 index 0000000000..b9fec21a13 --- /dev/null +++ b/libobs/obs-source-draw-plan.h @@ -0,0 +1,107 @@ +/****************************************************************************** + 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 "obs-source-draw-technique.h" + +/* The source draw decisions of obs-source.c, on plain values + * (obs-source-draw-plan.c): + * - rotate_async_video(): the translate that moves a rotated async frame + * back into its box (obs_source_draw_plan_rotation_x() and _y()); + * - obs_source_render_async_video(): the linear sRGB flag, the non-linear + * alpha blend, the technique and the multiplier for each pair of source and + * current colour spaces; + * - source_render(): whether a source is drawn through color_space_texrender + * to convert its colour space, and the technique and multiplier of that + * draw. + * + * obs-source.c keeps every gs_* call in its order and under the same + * conditions, convert_video_space(), obs_get_video_sdr_white_level(), + * gs_get_format_from_space() and the texrenders: it passes their results in + * and acts on what comes back. obs_get_video_sdr_white_level() is called only + * when obs_source_draw_plan_reads_white_level() says so, as before. None of + * these functions takes a lock, calls into libobs or keeps state. + * + * Colour spaces are enum gs_color_space values, passed as int; obs-source.c + * checks the size. A value outside the enum takes the paths it took before. + * The techniques are enum obs_source_draw_technique + * (obs-source-draw-technique.h), which the deinterlace draw decisions share. + * + * Hidden in libobs: not EXPORT. */ + +/* The x of the translate before an async frame of width x height is rotated + * by rotation degrees: height for 270, width for 180, 0 for any other + * rotation (obs_source_set_async_rotation() keeps it in 0-359). The size + * converts to float as (float)uint32_t does. */ +float obs_source_draw_plan_rotation_x(long rotation, uint32_t width, uint32_t height); + +/* The y of that translate: width for 90, height for 180, 0 for any other + * rotation. */ +float obs_source_draw_plan_rotation_y(long rotation, uint32_t width, uint32_t height); + +/* Whether obs_source_render_async_video() draws with linear sRGB: + * gs_linear_srgb (gs_get_linear_srgb(), always read), or also a GS_CS_SRGB + * source into any current space but GS_CS_SRGB, or a GS_CS_709_EXTENDED or + * GS_CS_709_SCRGB source into SDR (GS_CS_SRGB, GS_CS_SRGB_16F), which is + * tonemapped. */ +bool obs_source_draw_plan_async_linear_srgb(int source_space, int current_space, bool gs_linear_srgb); + +/* Whether obs_source_render_async_video() blends with non-linear alpha + * (GS_BLEND_ONE, GS_BLEND_INVSRCALPHA): a GS_CS_SRGB source drawn with linear + * sRGB (obs_source_draw_plan_async_linear_srgb()) whose alpha is not linear + * (async_linear_alpha). */ +bool obs_source_draw_plan_async_nonlinear_alpha(int source_space, bool linear_srgb, bool linear_alpha); + +/* The technique obs_source_render_async_video() draws with, and the one + * source_render() converts with (nonlinear_alpha false): + * - SDR (GS_CS_SRGB, GS_CS_SRGB_16F) into GS_CS_709_SCRGB: DrawMultiply, or + * DrawNonlinearAlphaMultiply with non-linear alpha; + * - GS_CS_SRGB into GS_CS_SRGB, GS_CS_SRGB_16F or GS_CS_709_EXTENDED with + * non-linear alpha: DrawNonlinearAlpha; + * - GS_CS_709_EXTENDED: DrawTonemap into SDR, DrawMultiply into + * GS_CS_709_SCRGB; + * - GS_CS_709_SCRGB: DrawMultiplyTonemap into SDR, DrawMultiply into + * GS_CS_709_EXTENDED; + * - Draw for any other pair, a space outside the enum included. + * Only a GS_CS_SRGB source has non-linear alpha + * (obs_source_draw_plan_async_nonlinear_alpha()); for any other source the + * flag changes nothing. */ +enum obs_source_draw_technique obs_source_draw_plan_technique(int source_space, int current_space, + bool nonlinear_alpha); + +/* Whether obs_source_render_async_video() or source_render() reads + * obs_get_video_sdr_white_level() for the pair: SDR or GS_CS_709_EXTENDED + * into GS_CS_709_SCRGB, GS_CS_709_SCRGB into SDR or GS_CS_709_EXTENDED. */ +bool obs_source_draw_plan_reads_white_level(int source_space, int current_space); + +/* The multiplier for the pair: sdr_white_level / 80.0f into GS_CS_709_SCRGB + * from SDR or GS_CS_709_EXTENDED, 80.0f / sdr_white_level from + * GS_CS_709_SCRGB into SDR or GS_CS_709_EXTENDED, 1.0f for any other pair. + * One float division, so a white level of 0 gives +inf from GS_CS_709_SCRGB + * and a NaN stays a NaN. */ +float obs_source_draw_plan_multiplier(int source_space, int current_space, float sdr_white_level); + +/* Whether source_render() draws the source into color_space_texrender and + * converts it: SDR into GS_CS_709_EXTENDED or GS_CS_709_SCRGB, + * GS_CS_709_EXTENDED into SDR or GS_CS_709_SCRGB, GS_CS_709_SCRGB into SDR or + * GS_CS_709_EXTENDED. Not for a pair in the same space class, nor a space + * outside the enum. */ +bool obs_source_draw_plan_convert(int source_space, int current_space); diff --git a/libobs/obs-source-draw-technique.h b/libobs/obs-source-draw-technique.h new file mode 100644 index 0000000000..13412fd12f --- /dev/null +++ b/libobs/obs-source-draw-technique.h @@ -0,0 +1,61 @@ +/****************************************************************************** + 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 + +/* The techniques of the default and deinterlace effects that a source draws + * with, shared by the source draw plan (obs-source-draw-plan.h) and the + * deinterlace draw decisions (obs-source-deinterlace-draw.h). The decisions + * return the enum value; obs-source.c and obs-source-deinterlace.c map it to + * the name with obs_source_draw_technique_name(). The values cross into the + * Rust port (rust/obs-source draw_technique) as the C's numbers. */ +enum obs_source_draw_technique { + /* "Draw" */ + OBS_SOURCE_DRAW_TECH_DRAW, + /* "DrawMultiply" */ + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY, + /* "DrawTonemap" */ + OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, + /* "DrawMultiplyTonemap" */ + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, + /* "DrawNonlinearAlpha" */ + OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA, + /* "DrawNonlinearAlphaMultiply" */ + OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY, +}; + +/* The technique's name in default.effect and the deinterlace effects; "Draw" + * for a value outside the enum. */ +static inline const char *obs_source_draw_technique_name(enum obs_source_draw_technique technique) +{ + switch (technique) { + case OBS_SOURCE_DRAW_TECH_DRAW: + return "Draw"; + case OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY: + return "DrawMultiply"; + case OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP: + return "DrawTonemap"; + case OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP: + return "DrawMultiplyTonemap"; + case OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA: + return "DrawNonlinearAlpha"; + case OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY: + return "DrawNonlinearAlphaMultiply"; + } + + return "Draw"; +} diff --git a/libobs/obs-source.c.h b/libobs/obs-source.c.h index 1fcc09add4..cd28607019 100644 --- a/libobs/obs-source.c.h +++ b/libobs/obs-source.c.h @@ -2079,21 +2079,8 @@ static void obs_source_update_async_video(obs_source_t *source) static void rotate_async_video(obs_source_t *source, long rotation) { - float x = 0; - float y = 0; - - /* obs_source_set_async_rotation() keeps the rotation in 0-359 */ - switch (rotation) { - case 90: - y = (float)source->async_width; - break; - case 270: - x = (float)source->async_height; - break; - case 180: - x = (float)source->async_width; - y = (float)source->async_height; - } + const float x = obs_source_draw_plan_rotation_x(rotation, source->async_width, source->async_height); + const float y = obs_source_draw_plan_rotation_y(rotation, source->async_width, source->async_height); gs_matrix_translate3f(x, y, 0); gs_matrix_rotaa4f(0.0f, 0.0f, -1.0f, RAD((float)rotation)); @@ -2108,64 +2095,17 @@ static inline void obs_source_render_async_video(obs_source_t *source) const enum gs_color_space source_space = convert_video_space(source->async_format, source->async_trc); gs_effect_t *const effect = obs_get_base_effect(OBS_EFFECT_DEFAULT); - const char *tech_name = "Draw"; - float multiplier = 1.0; const enum gs_color_space current_space = gs_get_color_space(); - bool linear_srgb = gs_get_linear_srgb(); - bool nonlinear_alpha = false; - switch (source_space) { - case GS_CS_SRGB: - linear_srgb = linear_srgb || (current_space != GS_CS_SRGB); - nonlinear_alpha = linear_srgb && !source->async_linear_alpha; - switch (current_space) { - case GS_CS_SRGB: - case GS_CS_SRGB_16F: - case GS_CS_709_EXTENDED: - if (nonlinear_alpha) - tech_name = "DrawNonlinearAlpha"; - break; - case GS_CS_709_SCRGB: - tech_name = nonlinear_alpha ? "DrawNonlinearAlphaMultiply" : "DrawMultiply"; - multiplier = obs_get_video_sdr_white_level() / 80.0f; - } - break; - case GS_CS_SRGB_16F: - if (current_space == GS_CS_709_SCRGB) { - tech_name = "DrawMultiply"; - multiplier = obs_get_video_sdr_white_level() / 80.0f; - } - break; - case GS_CS_709_EXTENDED: - switch (current_space) { - case GS_CS_SRGB: - case GS_CS_SRGB_16F: - tech_name = "DrawTonemap"; - linear_srgb = true; - break; - case GS_CS_709_SCRGB: - tech_name = "DrawMultiply"; - multiplier = obs_get_video_sdr_white_level() / 80.0f; - break; - case GS_CS_709_EXTENDED: - break; - } - break; - case GS_CS_709_SCRGB: - switch (current_space) { - case GS_CS_SRGB: - case GS_CS_SRGB_16F: - tech_name = "DrawMultiplyTonemap"; - multiplier = 80.0f / obs_get_video_sdr_white_level(); - linear_srgb = true; - break; - case GS_CS_709_EXTENDED: - tech_name = "DrawMultiply"; - multiplier = 80.0f / obs_get_video_sdr_white_level(); - break; - case GS_CS_709_SCRGB: - break; - } - } + const bool linear_srgb = + obs_source_draw_plan_async_linear_srgb(source_space, current_space, gs_get_linear_srgb()); + const bool nonlinear_alpha = obs_source_draw_plan_async_nonlinear_alpha(source_space, linear_srgb, + source->async_linear_alpha); + const char *tech_name = obs_source_draw_technique_name( + obs_source_draw_plan_technique(source_space, current_space, nonlinear_alpha)); + const float multiplier = obs_source_draw_plan_reads_white_level(source_space, current_space) + ? obs_source_draw_plan_multiplier(source_space, current_space, + obs_get_video_sdr_white_level()) + : 1.0f; const bool previous = gs_set_linear_srgb(linear_srgb); @@ -2292,57 +2232,16 @@ static void source_render(obs_source_t *source, gs_effect_t *effect) const enum gs_color_space current_space = gs_get_color_space(); const enum gs_color_space source_space = obs_source_get_color_space(source, 1, ¤t_space); - const char *convert_tech = NULL; - float multiplier = 1.0; enum gs_color_format format = gs_get_format_from_space(source_space); - switch (source_space) { - case GS_CS_SRGB: - case GS_CS_SRGB_16F: - switch (current_space) { - case GS_CS_709_EXTENDED: - convert_tech = "Draw"; - break; - case GS_CS_709_SCRGB: - convert_tech = "DrawMultiply"; - multiplier = obs_get_video_sdr_white_level() / 80.0f; - break; - case GS_CS_SRGB: - break; - case GS_CS_SRGB_16F: - break; - } - break; - case GS_CS_709_EXTENDED: - switch (current_space) { - case GS_CS_SRGB: - case GS_CS_SRGB_16F: - convert_tech = "DrawTonemap"; - break; - case GS_CS_709_SCRGB: - convert_tech = "DrawMultiply"; - multiplier = obs_get_video_sdr_white_level() / 80.0f; - break; - case GS_CS_709_EXTENDED: - break; - } - break; - case GS_CS_709_SCRGB: - switch (current_space) { - case GS_CS_SRGB: - case GS_CS_SRGB_16F: - convert_tech = "DrawMultiplyTonemap"; - multiplier = 80.0f / obs_get_video_sdr_white_level(); - break; - case GS_CS_709_EXTENDED: - convert_tech = "DrawMultiply"; - multiplier = 80.0f / obs_get_video_sdr_white_level(); - break; - case GS_CS_709_SCRGB: - break; - } - } - - if (convert_tech) { + const bool convert = obs_source_draw_plan_convert(source_space, current_space); + const char *convert_tech = + obs_source_draw_technique_name(obs_source_draw_plan_technique(source_space, current_space, false)); + const float multiplier = + obs_source_draw_plan_reads_white_level(source_space, current_space) + ? obs_source_draw_plan_multiplier(source_space, current_space, obs_get_video_sdr_white_level()) + : 1.0f; + + if (convert) { if (source->color_space_texrender) { if (gs_texrender_get_format(source->color_space_texrender) != format) { gs_texrender_destroy(source->color_space_texrender); diff --git a/rust/obs-c-oracle/oracle/source_deinterlace_draw.c b/rust/obs-c-oracle/oracle/source_deinterlace_draw.c index 795826ec3a..b319f0a6f7 100644 --- a/rust/obs-c-oracle/oracle/source_deinterlace_draw.c +++ b/rust/obs-c-oracle/oracle/source_deinterlace_draw.c @@ -60,11 +60,11 @@ int64_t oracle_source_deinterlace_draw_value(size_t which) OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_LINEAR_2X, OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF, OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF_2X, - /* enum obs_source_deinterlace_draw_technique */ - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, + /* enum obs_source_draw_technique */ + OBS_SOURCE_DRAW_TECH_DRAW, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY, + OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, /* enum obs_source_deinterlace_draw_mode_change */ OBS_SOURCE_DEINTERLACE_DRAW_MODE_SAME, OBS_SOURCE_DEINTERLACE_DRAW_MODE_ENABLE, @@ -72,12 +72,12 @@ int64_t oracle_source_deinterlace_draw_value(size_t which) OBS_SOURCE_DEINTERLACE_DRAW_MODE_SWITCH, /* TWOX_TOLERANCE */ (int64_t)OBS_SOURCE_DEINTERLACE_PACING_TWOX_TOLERANCE, - /* sizes; the renames above also renamed the three enum tags that + /* sizes; the renames above also renamed the two enum tags that * share their function's name */ sizeof(enum obs_deinterlace_mode), sizeof(enum gs_color_space), sizeof(enum oracle_obs_source_deinterlace_draw_effect), - sizeof(enum oracle_obs_source_deinterlace_draw_technique), + sizeof(enum obs_source_draw_technique), sizeof(enum oracle_obs_source_deinterlace_draw_mode_change), sizeof(int), sizeof(bool), diff --git a/rust/obs-c-oracle/src/source_deinterlace_draw.rs b/rust/obs-c-oracle/src/source_deinterlace_draw.rs index f3967ca58a..a3a5bdf070 100644 --- a/rust/obs-c-oracle/src/source_deinterlace_draw.rs +++ b/rust/obs-c-oracle/src/source_deinterlace_draw.rs @@ -31,10 +31,10 @@ pub const VALUE_NAMES: [&str; 39] = [ "OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_LINEAR_2X", "OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF", "OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF_2X", - "OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW", - "OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY", - "OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP", - "OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP", + "OBS_SOURCE_DRAW_TECH_DRAW", + "OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY", + "OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP", + "OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP", "OBS_SOURCE_DEINTERLACE_DRAW_MODE_SAME", "OBS_SOURCE_DEINTERLACE_DRAW_MODE_ENABLE", "OBS_SOURCE_DEINTERLACE_DRAW_MODE_DISABLE", @@ -43,7 +43,7 @@ pub const VALUE_NAMES: [&str; 39] = [ "sizeof(enum obs_deinterlace_mode)", "sizeof(enum gs_color_space)", "sizeof(enum obs_source_deinterlace_draw_effect)", - "sizeof(enum obs_source_deinterlace_draw_technique)", + "sizeof(enum obs_source_draw_technique)", "sizeof(enum obs_source_deinterlace_draw_mode_change)", "sizeof(int)", "sizeof(bool)", diff --git a/test/cmocka/test_deinterlace_draw_plan_steps.c b/test/cmocka/test_deinterlace_draw_plan_steps.c index cd98ee8f4d..75f3ca8722 100644 --- a/test/cmocka/test_deinterlace_draw_plan_steps.c +++ b/test/cmocka/test_deinterlace_draw_plan_steps.c @@ -119,16 +119,16 @@ static void enum_values(void **state) CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_LINEAR_2X, 5); CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF, 6); CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_YADIF_2X, 7); - CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, 0); - CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY, 1); - CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP, 2); - CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, 3); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW, 0); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY, 1); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, 2); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, 3); CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_MODE_SAME, 0); CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_MODE_ENABLE, 1); CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_MODE_DISABLE, 2); CHECK_INT(OBS_SOURCE_DEINTERLACE_DRAW_MODE_SWITCH, 3); CHECK_INT(sizeof(enum obs_source_deinterlace_draw_effect), sizeof(int)); - CHECK_INT(sizeof(enum obs_source_deinterlace_draw_technique), sizeof(int)); + CHECK_INT(sizeof(enum obs_source_draw_technique), sizeof(int)); CHECK_INT(sizeof(enum obs_source_deinterlace_draw_mode_change), sizeof(int)); } @@ -249,19 +249,19 @@ static void linear_srgb_every_input(void **state) /* --- technique and multiplier per space pair ---------------------------- */ /* [source][current], in SPACES order */ -static const enum obs_source_deinterlace_draw_technique TECHNIQUES[4][4] = { +static const enum obs_source_draw_technique TECHNIQUES[4][4] = { /* GS_CS_SRGB into SRGB, SRGB_16F, 709_EXTENDED, 709_SCRGB */ - {OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY}, + {OBS_SOURCE_DRAW_TECH_DRAW, OBS_SOURCE_DRAW_TECH_DRAW, OBS_SOURCE_DRAW_TECH_DRAW, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY}, /* GS_CS_SRGB_16F */ - {OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY}, + {OBS_SOURCE_DRAW_TECH_DRAW, OBS_SOURCE_DRAW_TECH_DRAW, OBS_SOURCE_DRAW_TECH_DRAW, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY}, /* GS_CS_709_EXTENDED */ - {OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_TONEMAP, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY}, + {OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, OBS_SOURCE_DRAW_TECH_DRAW, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY}, /* GS_CS_709_SCRGB */ - {OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY, OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW}, + {OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY, OBS_SOURCE_DRAW_TECH_DRAW}, }; /* The multiplier at a white level of 80, 300 and 0, as float bits: @@ -299,16 +299,15 @@ static void technique_space_pairs(void **state) (void)state; for (size_t s = 0; s < SPACE_COUNT; s++) for (size_t c = 0; c < SPACE_COUNT; c++) { - const enum obs_source_deinterlace_draw_technique t = + const enum obs_source_draw_technique t = obs_source_deinterlace_draw_technique(SPACES[s], SPACES[c]); if (t != TECHNIQUES[s][c]) { fprintf(stderr, "source %d into %d: technique %d, expected %d\n", SPACES[s], SPACES[c], (int)t, (int)TECHNIQUES[s][c]); fail(); } - const bool multiplies = TECHNIQUES[s][c] == OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY || - TECHNIQUES[s][c] == - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP; + const bool multiplies = TECHNIQUES[s][c] == OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY || + TECHNIQUES[s][c] == OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP; CHECK_BOOL(obs_source_deinterlace_draw_reads_white_level(SPACES[s], SPACES[c]), multiplies); } } @@ -321,9 +320,9 @@ static void technique_outside_enum(void **state) for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) { for (size_t s = 0; s < SPACE_COUNT; s++) { CHECK_INT(obs_source_deinterlace_draw_technique(OUTSIDE_SPACES[o], SPACES[s]), - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW); + OBS_SOURCE_DRAW_TECH_DRAW); CHECK_INT(obs_source_deinterlace_draw_technique(SPACES[s], OUTSIDE_SPACES[o]), - OBS_SOURCE_DEINTERLACE_DRAW_TECH_DRAW); + OBS_SOURCE_DRAW_TECH_DRAW); CHECK_BOOL(obs_source_deinterlace_draw_reads_white_level(OUTSIDE_SPACES[o], SPACES[s]), false); CHECK_BOOL(obs_source_deinterlace_draw_reads_white_level(SPACES[s], OUTSIDE_SPACES[o]), false); CHECK_BITS(obs_source_deinterlace_draw_multiplier(OUTSIDE_SPACES[o], SPACES[s], 300.0f), From 2d7924814effe82e162ff15c50ac50ff333b971e Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:17:46 -0400 Subject: [PATCH 2/6] test: Characterize the source draw decisions test_source_draw_plan_steps compiles libobs/obs-source-draw-plan.c.h (ASan and UBSan) and calls only its eight hidden functions. 17 cases: - enum_values: the technique numbers the C maps (and the port will return), their names, and that the enum and enum gs_color_space are int-sized; - rotation_multiples_of_90: the translate of a 1920 x 1080 frame and of a 1080 x 1920 one at 0, 90, 180 and 270, as float bits; - rotation_other_angles: 1, 45, 89, 91, 135, 179, 181, 269, 271 and 359 are not moved, nor are 360, 450, -90, -180, -270 and the ends of long; - rotation_size_to_float: sizes of 0, past 2^24 (rounded to nearest even) and UINT32_MAX (2^32); - async_linear_srgb_space_pairs, async_linear_srgb_outside_enum: the 4 x 4 pairs with the graphics flag off and on (an SDR frame into anything but GS_CS_SRGB, 792f8473e; an HDR frame into SDR, 080095786); a space outside the enum keeps the graphics flag; - async_nonlinear_alpha_every_input: only an SDR frame drawn linear whose alpha is not linear; - technique_space_pairs, technique_outside_enum: the 4 x 4 pairs with and without non-linear alpha; Draw for a space outside the enum; - reads_white_level_space_pairs, multiplier_space_pairs, multiplier_outside_enum: the pairs that read the level, and the multiplier as float bits at SDR white levels of 80, 300 and 0 (300 / 80 = 3.75, 80 / 300 = 0x3e888889, 80 / 0 = +inf); - multiplier_any_level: -0, infinity, -80, 480, 203, the smallest subnormal and NaN, each one IEEE division; - convert_space_pairs, convert_outside_enum: source_render() converts between classes (SDR, 709_EXTENDED, scRGB) only; - async_plan_every_input, convert_plan_every_input: the decisions in their callers' order for every pair, both linear flags, both alpha flags and the three white levels. Characterized, not endorsed: - obs_get_video_sdr_white_level() is not checked: a level of 0 gives a multiplier of 0 into scRGB and +inf out of it, -0 gives -inf, a negative level a negative multiplier and a NaN a NaN. OBS sets levels from 80 to 480 nits; - a rotation outside 0-359, which obs_source_set_async_rotation() no longer stores since #2136, is not moved, even a multiple of 90; - an SDR frame into a current space outside the enum is drawn linear with non-linear alpha, but with Draw (the space is not GS_CS_SRGB, and the technique switch has no case for it). The test is in the native test graph in name order, with libm except on MSVC, in the core suite (no suite size is pinned in rust/obs-build/tests/native.rs). On the C backend it passes 17 of 17. Refs #2187 --- test/cmocka/build/test-graph.json | 29 + test/cmocka/test_source_draw_plan_steps.c | 521 ++++++++++++++++++ .../tests.d/test_source_draw_plan_steps.cmake | 13 + 3 files changed, 563 insertions(+) create mode 100644 test/cmocka/test_source_draw_plan_steps.c create mode 100644 test/cmocka/tests.d/test_source_draw_plan_steps.cmake diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 45733d96cb..bf18d2614a 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -2765,6 +2765,29 @@ ], "sanitizers": "address,undefined" }, + "test_source_draw_plan_steps": { + "sources": [ + "test/cmocka/test_source_draw_plan_steps.c", + "libobs/obs-source-draw-plan.c.h" + ], + "includes": [ + "libobs", + "deps/libcaption" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + { + "name": "m", + "requires": { + "not": "MSVC" + } + }, + "OBS::native-test-cmocka" + ], + "sanitizers": "address,undefined" + }, "test_source_load_migration_steps": { "sources": [ "test/cmocka/test_source_load_migration_steps.c", @@ -4633,6 +4656,12 @@ "${target}" ] }, + "test_source_draw_plan_steps": { + "target": "test_source_draw_plan_steps", + "command": [ + "${target}" + ] + }, "test_source_load_migration_steps": { "target": "test_source_load_migration_steps", "command": [ diff --git a/test/cmocka/test_source_draw_plan_steps.c b/test/cmocka/test_source_draw_plan_steps.c new file mode 100644 index 0000000000..75dd68cab3 --- /dev/null +++ b/test/cmocka/test_source_draw_plan_steps.c @@ -0,0 +1,521 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "obs.h" +#include "obs-source-draw-plan.h" + +/* + * Characterization of the source draw decisions of libobs/obs-source.c + * (rotate_async_video(), obs_source_render_async_video() and + * source_render()), moved to libobs/obs-source-draw-plan.c before its Rust + * port: + * - obs_source_draw_plan_rotation_x() and _y(): the translate that moves a + * rotated async frame back into its box; + * - obs_source_draw_plan_async_linear_srgb() and + * obs_source_draw_plan_async_nonlinear_alpha(): the linear sRGB flag and + * the non-linear alpha blend of an async frame (792f8473e, 080095786); + * - obs_source_draw_plan_technique(): the technique of an async frame, and + * of source_render()'s colour space conversion; + * - obs_source_draw_plan_reads_white_level() and + * obs_source_draw_plan_multiplier(): when the SDR white level is read and + * the multiplier it gives, for both; + * - obs_source_draw_plan_convert(): whether source_render() draws through + * color_space_texrender (#1493 is the colour space that picks it). + * 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. test_async_rotation_normalize, + * test_filter_color_space, test_source_color_space_steps, + * test_color_correction_16f and test_pipewire_cursor_hdr cover the callers + * through the real libobs. + */ + +/* colour spaces outside enum gs_color_space */ +static const int OUTSIDE_SPACES[] = {-1, 4, 99, INT_MIN, INT_MAX}; +#define OUTSIDE_SPACE_COUNT (sizeof(OUTSIDE_SPACES) / sizeof(OUTSIDE_SPACES[0])) + +static const int SPACES[] = {GS_CS_SRGB, GS_CS_SRGB_16F, GS_CS_709_EXTENDED, GS_CS_709_SCRGB}; +#define SPACE_COUNT (sizeof(SPACES) / sizeof(SPACES[0])) + +/* SDR white levels: OBS's default, a common one, and 0 */ +static const float WHITES[] = {80.0f, 300.0f, 0.0f}; +#define WHITE_COUNT (sizeof(WHITES) / sizeof(WHITES[0])) + +static uint32_t float_bits(float f) +{ + uint32_t u; + memcpy(&u, &f, sizeof(u)); + return u; +} + +static float bits_float(uint32_t u) +{ + float f; + memcpy(&f, &u, sizeof(f)); + return f; +} + +static void check_int(long long actual, long long expected, const char *what, int line) +{ + if (actual != expected) { + fprintf(stderr, "line %d: %s is %lld, expected %lld\n", line, what, actual, expected); + fail(); + } +} + +static void check_bool(bool actual, bool expected, const char *what, int line) +{ + if (actual != expected) { + fprintf(stderr, "line %d: %s is %s, expected %s\n", line, what, actual ? "true" : "false", + expected ? "true" : "false"); + fail(); + } +} + +static void check_bits(float actual, uint32_t expected, const char *what, int line) +{ + if (float_bits(actual) != expected) { + fprintf(stderr, "line %d: %s is %a (0x%08" PRIx32 "), expected %a (0x%08" PRIx32 ")\n", line, what, + (double)actual, float_bits(actual), (double)bits_float(expected), expected); + fail(); + } +} + +#define CHECK_INT(actual, expected) check_int((long long)(actual), (long long)(expected), #actual, __LINE__) +#define CHECK_BOOL(actual, expected) check_bool((actual), (expected), #actual, __LINE__) +#define CHECK_BITS(actual, expected) check_bits((actual), (expected), #actual, __LINE__) + +#define D OBS_SOURCE_DRAW_TECH_DRAW +#define M OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY +#define T OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP +#define MT OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP +#define NA OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA +#define NAM OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY + +/* --- enum values ------------------------------------------------------- */ + +/* The Rust port returns these numbers; the C maps them to names. The first + * four are the deinterlace draw decisions' too. */ +static void enum_values(void **state) +{ + (void)state; + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW, 0); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY, 1); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, 2); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, 3); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA, 4); + CHECK_INT(OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY, 5); + CHECK_INT(sizeof(enum obs_source_draw_technique), sizeof(int)); + CHECK_INT(sizeof(enum gs_color_space), sizeof(int)); + assert_string_equal(obs_source_draw_technique_name(D), "Draw"); + assert_string_equal(obs_source_draw_technique_name(M), "DrawMultiply"); + assert_string_equal(obs_source_draw_technique_name(T), "DrawTonemap"); + assert_string_equal(obs_source_draw_technique_name(MT), "DrawMultiplyTonemap"); + assert_string_equal(obs_source_draw_technique_name(NA), "DrawNonlinearAlpha"); + assert_string_equal(obs_source_draw_technique_name(NAM), "DrawNonlinearAlphaMultiply"); + assert_string_equal(obs_source_draw_technique_name((enum obs_source_draw_technique)6), "Draw"); + assert_string_equal(obs_source_draw_technique_name((enum obs_source_draw_technique) - 1), "Draw"); +} + +/* --- rotation ---------------------------------------------------------- */ + +/* The frame is rotated about its top left corner, clockwise on screen + * (gs_matrix_rotaa4f() about -z), then moved back into its box: a 1920 x 1080 + * frame turned by 90 is moved down by its width, by 180 right by its width + * and down by its height, by 270 right by its height. */ +static void rotation_multiples_of_90(void **state) +{ + (void)state; + CHECK_BITS(obs_source_draw_plan_rotation_x(0, 1920, 1080), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_y(0, 1920, 1080), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_x(90, 1920, 1080), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_y(90, 1920, 1080), 0x44f00000); /* 1920 */ + CHECK_BITS(obs_source_draw_plan_rotation_x(180, 1920, 1080), 0x44f00000); + CHECK_BITS(obs_source_draw_plan_rotation_y(180, 1920, 1080), 0x44870000); /* 1080 */ + CHECK_BITS(obs_source_draw_plan_rotation_x(270, 1920, 1080), 0x44870000); + CHECK_BITS(obs_source_draw_plan_rotation_y(270, 1920, 1080), 0x00000000); + /* a portrait frame: the width and height trade places */ + CHECK_BITS(obs_source_draw_plan_rotation_y(90, 1080, 1920), 0x44870000); + CHECK_BITS(obs_source_draw_plan_rotation_x(180, 1080, 1920), 0x44870000); + CHECK_BITS(obs_source_draw_plan_rotation_y(180, 1080, 1920), 0x44f00000); + CHECK_BITS(obs_source_draw_plan_rotation_x(270, 1080, 1920), 0x44f00000); +} + +/* Any other angle is not moved: the frame turns about its corner. In 0-359 + * only 90, 180 and 270 move (obs_source_set_async_rotation(), #2136). + * Characterized, not endorsed: a rotation outside 0-359, which the setter + * no longer stores, is not moved either, even a multiple of 90. */ +static void rotation_other_angles(void **state) +{ + (void)state; + const long angles[] = {1, 45, 89, 91, 135, 179, 181, 269, 271, + 359, 360, 450, -90, -180, -270, LONG_MIN, LONG_MAX}; + for (size_t i = 0; i < sizeof(angles) / sizeof(angles[0]); i++) { + CHECK_BITS(obs_source_draw_plan_rotation_x(angles[i], 1920, 1080), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_y(angles[i], 1920, 1080), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_x(angles[i], 640, 480), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_y(angles[i], 640, 480), 0x00000000); + } +} + +/* The size converts as (float)uint32_t: 0 is +0, sizes past 2^24 round to + * nearest even, UINT32_MAX is 2^32. */ +static void rotation_size_to_float(void **state) +{ + (void)state; + CHECK_BITS(obs_source_draw_plan_rotation_y(90, 0, 7), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_x(270, 7, 0), 0x00000000); + CHECK_BITS(obs_source_draw_plan_rotation_y(90, 16777217, 3), 0x4b800000); /* 2^24 */ + CHECK_BITS(obs_source_draw_plan_rotation_x(180, 16777219, 3), 0x4b800002); /* 2^24 + 4 */ + CHECK_BITS(obs_source_draw_plan_rotation_y(180, 3, UINT32_MAX), 0x4f800000); /* 2^32 */ + CHECK_BITS(obs_source_draw_plan_rotation_x(270, 3, UINT32_MAX), 0x4f800000); + CHECK_BITS(obs_source_draw_plan_rotation_x(180, 1, 1), 0x3f800000); +} + +/* --- async video: linear sRGB and non-linear alpha ------------------------- */ + +/* With the graphics linear flag off: an SDR (GS_CS_SRGB) frame into anything + * but GS_CS_SRGB (792f8473e), and a 709_EXTENDED or scRGB frame into SDR, + * which is tonemapped (080095786). GS_CS_SRGB_16F never forces it. With the + * flag on, always. */ +static const bool ASYNC_LINEAR[4][4] = { + {false, true, true, true}, + {false, false, false, false}, + {true, true, false, false}, + {true, true, false, false}, +}; + +static void async_linear_srgb_space_pairs(void **state) +{ + (void)state; + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) { + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(SPACES[s], SPACES[c], false), + ASYNC_LINEAR[s][c]); + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(SPACES[s], SPACES[c], true), true); + } +} + +/* A space outside the enum keeps the graphics flag, except that an SDR + * frame goes linear into a current space outside the enum, as it is not + * GS_CS_SRGB (characterized, not endorsed). */ +static void async_linear_srgb_outside_enum(void **state) +{ + (void)state; + for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) { + for (int gs = 0; gs < 2; gs++) { + for (size_t s = 0; s < SPACE_COUNT; s++) + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(OUTSIDE_SPACES[o], SPACES[s], gs), + gs); + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(GS_CS_SRGB, OUTSIDE_SPACES[o], gs), true); + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(GS_CS_SRGB_16F, OUTSIDE_SPACES[o], gs), gs); + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(GS_CS_709_EXTENDED, OUTSIDE_SPACES[o], gs), + gs); + CHECK_BOOL(obs_source_draw_plan_async_linear_srgb(GS_CS_709_SCRGB, OUTSIDE_SPACES[o], gs), gs); + } + } +} + +/* Only an SDR (GS_CS_SRGB) frame drawn with linear sRGB whose alpha is not + * linear blends with non-linear alpha. */ +static void async_nonlinear_alpha_every_input(void **state) +{ + (void)state; + for (int linear = 0; linear < 2; linear++) + for (int alpha = 0; alpha < 2; alpha++) { + CHECK_BOOL(obs_source_draw_plan_async_nonlinear_alpha(GS_CS_SRGB, linear, alpha), + linear && !alpha); + for (size_t s = 1; s < SPACE_COUNT; s++) + CHECK_BOOL(obs_source_draw_plan_async_nonlinear_alpha(SPACES[s], linear, alpha), false); + for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) + CHECK_BOOL(obs_source_draw_plan_async_nonlinear_alpha(OUTSIDE_SPACES[o], linear, alpha), + false); + } +} + +/* --- technique ---------------------------------------------------------- */ + +/* [source][current], without and with non-linear alpha */ +static const enum obs_source_draw_technique TECHNIQUES[2][4][4] = { + { + {D, D, D, M}, + {D, D, D, M}, + {T, T, D, M}, + {MT, MT, M, D}, + }, + { + {NA, NA, NA, NAM}, + {D, D, D, M}, + {T, T, D, M}, + {MT, MT, M, D}, + }, +}; + +static void technique_space_pairs(void **state) +{ + (void)state; + for (int na = 0; na < 2; na++) + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) { + const enum obs_source_draw_technique t = + obs_source_draw_plan_technique(SPACES[s], SPACES[c], na); + if (t != TECHNIQUES[na][s][c]) { + fprintf(stderr, + "source %d into %d, non-linear alpha %d: technique %d, expected %d\n", + SPACES[s], SPACES[c], na, (int)t, (int)TECHNIQUES[na][s][c]); + fail(); + } + } +} + +/* A space outside the enum, as source or current, draws with Draw, with + * non-linear alpha too (characterized, not endorsed: an SDR frame into a + * current space outside the enum still blends with non-linear alpha, but + * with Draw). */ +static void technique_outside_enum(void **state) +{ + (void)state; + for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) + for (int na = 0; na < 2; na++) + for (size_t s = 0; s < SPACE_COUNT; s++) { + CHECK_INT(obs_source_draw_plan_technique(OUTSIDE_SPACES[o], SPACES[s], na), D); + CHECK_INT(obs_source_draw_plan_technique(SPACES[s], OUTSIDE_SPACES[o], na), D); + } + CHECK_BOOL(obs_source_draw_plan_async_nonlinear_alpha( + GS_CS_SRGB, obs_source_draw_plan_async_linear_srgb(GS_CS_SRGB, 4, false), false), + true); +} + +/* --- white level and multiplier ------------------------------------------ */ + +static const bool READS_WHITE[4][4] = { + {false, false, false, true}, + {false, false, false, true}, + {false, false, false, true}, + {true, true, true, false}, +}; + +/* [source][current][white level 80, 300, 0]: the level over 80 into scRGB, + * 80 over the level out of it (300 / 80 = 3.75, 80 / 300 = 0x3e888889). + * Characterized, not endorsed: a level of 0 gives 0 into scRGB and +inf out + * of it (OBS sets levels from 80 to 480 nits). */ +static const uint32_t MULTIPLIERS[4][4][3] = { + {{0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x40700000, 0x00000000}}, + {{0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x40700000, 0x00000000}}, + {{0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}, + {0x3f800000, 0x40700000, 0x00000000}}, + {{0x3f800000, 0x3e888889, 0x7f800000}, + {0x3f800000, 0x3e888889, 0x7f800000}, + {0x3f800000, 0x3e888889, 0x7f800000}, + {0x3f800000, 0x3f800000, 0x3f800000}}, +}; + +static void reads_white_level_space_pairs(void **state) +{ + (void)state; + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) + CHECK_BOOL(obs_source_draw_plan_reads_white_level(SPACES[s], SPACES[c]), READS_WHITE[s][c]); + for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) + for (size_t s = 0; s < SPACE_COUNT; s++) { + CHECK_BOOL(obs_source_draw_plan_reads_white_level(OUTSIDE_SPACES[o], SPACES[s]), false); + CHECK_BOOL(obs_source_draw_plan_reads_white_level(SPACES[s], OUTSIDE_SPACES[o]), false); + } +} + +static void multiplier_space_pairs(void **state) +{ + (void)state; + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) + for (size_t w = 0; w < WHITE_COUNT; w++) { + const float m = obs_source_draw_plan_multiplier(SPACES[s], SPACES[c], WHITES[w]); + if (float_bits(m) != MULTIPLIERS[s][c][w]) { + fprintf(stderr, + "source %d into %d at %g: 0x%08" PRIx32 ", expected 0x%08" PRIx32 "\n", + SPACES[s], SPACES[c], (double)WHITES[w], float_bits(m), + MULTIPLIERS[s][c][w]); + fail(); + } + } +} + +/* A space outside the enum gives 1 at any level. */ +static void multiplier_outside_enum(void **state) +{ + (void)state; + for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t w = 0; w < WHITE_COUNT; w++) { + CHECK_BITS(obs_source_draw_plan_multiplier(OUTSIDE_SPACES[o], SPACES[s], WHITES[w]), + 0x3f800000); + CHECK_BITS(obs_source_draw_plan_multiplier(SPACES[s], OUTSIDE_SPACES[o], WHITES[w]), + 0x3f800000); + } +} + +/* One float division each way, so the multiplier is the IEEE quotient of + * whatever level comes in. Characterized, not endorsed: + * obs_get_video_sdr_white_level() is passed through unchecked: -0 gives -inf + * from scRGB, a negative level a negative multiplier, and a NaN stays a + * NaN. */ +static void multiplier_any_level(void **state) +{ + (void)state; + /* 80 / -0, -0 / 80 */ + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, -0.0f), 0xff800000); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, -0.0f), 0x80000000); + /* 80 / inf, inf / 80 */ + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_SCRGB, GS_CS_709_EXTENDED, INFINITY), 0x00000000); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_EXTENDED, GS_CS_709_SCRGB, INFINITY), 0x7f800000); + /* -80 */ + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_SRGB_16F, GS_CS_709_SCRGB, -80.0f), 0xbf800000); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_SCRGB, GS_CS_SRGB_16F, -80.0f), 0xbf800000); + /* 480 nits, the largest OBS offers: 6 and 1/6 */ + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, 480.0f), 0x40c00000); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, 480.0f), 0x3e2aaaab); + /* 203 nits (BT.2408 reference white) */ + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_EXTENDED, GS_CS_709_SCRGB, 203.0f), 0x40226666); + /* the smallest subnormal: 80 / it overflows to +inf, it / 80 is 0 */ + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, bits_float(1)), 0x7f800000); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, bits_float(1)), 0x00000000); + /* a NaN stays a NaN either way; pairs that do not multiply give 1 */ + assert_true(isnan(obs_source_draw_plan_multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, NAN))); + assert_true(isnan(obs_source_draw_plan_multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, NAN))); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_SRGB, GS_CS_SRGB, NAN), 0x3f800000); + CHECK_BITS(obs_source_draw_plan_multiplier(GS_CS_709_EXTENDED, GS_CS_SRGB, 0.0f), 0x3f800000); +} + +/* --- source_render() colour space conversion ------------------------------ */ + +/* Converted when the classes differ (SDR, 709_EXTENDED, scRGB); SDR and + * SDR 16F are one class. */ +static const bool CONVERTS[4][4] = { + {false, false, true, true}, + {false, false, true, true}, + {true, true, false, true}, + {true, true, true, false}, +}; + +static void convert_space_pairs(void **state) +{ + (void)state; + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) + CHECK_BOOL(obs_source_draw_plan_convert(SPACES[s], SPACES[c]), CONVERTS[s][c]); +} + +/* A space outside the enum, as source or current, is drawn directly. */ +static void convert_outside_enum(void **state) +{ + (void)state; + for (size_t o = 0; o < OUTSIDE_SPACE_COUNT; o++) { + for (size_t s = 0; s < SPACE_COUNT; s++) { + CHECK_BOOL(obs_source_draw_plan_convert(OUTSIDE_SPACES[o], SPACES[s]), false); + CHECK_BOOL(obs_source_draw_plan_convert(SPACES[s], OUTSIDE_SPACES[o]), false); + } + CHECK_BOOL(obs_source_draw_plan_convert(OUTSIDE_SPACES[o], OUTSIDE_SPACES[o]), false); + } +} + +/* --- the decisions in their callers' order ------------------------------- */ + +/* What obs_source_render_async_video() draws with for every pair, both + * graphics linear flags, both alpha flags and white levels of 80, 300 and + * 0: the linear flag, the blend, the technique, and the multiplier (1 when + * the level is not read). */ +static void async_plan_every_input(void **state) +{ + (void)state; + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) + for (int gs = 0; gs < 2; gs++) + for (int alpha = 0; alpha < 2; alpha++) + for (size_t w = 0; w < WHITE_COUNT; w++) { + const bool linear = obs_source_draw_plan_async_linear_srgb( + SPACES[s], SPACES[c], gs); + const bool na = obs_source_draw_plan_async_nonlinear_alpha( + SPACES[s], linear, alpha); + const enum obs_source_draw_technique t = + obs_source_draw_plan_technique(SPACES[s], SPACES[c], na); + const float m = + obs_source_draw_plan_reads_white_level(SPACES[s], SPACES[c]) + ? obs_source_draw_plan_multiplier(SPACES[s], SPACES[c], + WHITES[w]) + : 1.0f; + const bool want_linear = gs || ASYNC_LINEAR[s][c]; + const bool want_na = s == 0 && want_linear && !alpha; + CHECK_BOOL(linear, want_linear); + CHECK_BOOL(na, want_na); + CHECK_INT(t, TECHNIQUES[want_na][s][c]); + CHECK_BITS(m, MULTIPLIERS[s][c][w]); + } +} + +/* What source_render() does for every pair at white levels of 80, 300 and + * 0: draw directly, or through color_space_texrender with the technique + * (never with non-linear alpha) and the multiplier. */ +static void convert_plan_every_input(void **state) +{ + (void)state; + for (size_t s = 0; s < SPACE_COUNT; s++) + for (size_t c = 0; c < SPACE_COUNT; c++) + for (size_t w = 0; w < WHITE_COUNT; w++) { + CHECK_BOOL(obs_source_draw_plan_convert(SPACES[s], SPACES[c]), CONVERTS[s][c]); + if (!CONVERTS[s][c]) + continue; + CHECK_INT(obs_source_draw_plan_technique(SPACES[s], SPACES[c], false), + TECHNIQUES[0][s][c]); + const float m = + obs_source_draw_plan_reads_white_level(SPACES[s], SPACES[c]) + ? obs_source_draw_plan_multiplier(SPACES[s], SPACES[c], WHITES[w]) + : 1.0f; + CHECK_BITS(m, MULTIPLIERS[s][c][w]); + } + /* SDR into 709_EXTENDED converts with plain Draw at 1 */ + CHECK_INT(obs_source_draw_plan_technique(GS_CS_SRGB, GS_CS_709_EXTENDED, false), D); + CHECK_BOOL(obs_source_draw_plan_reads_white_level(GS_CS_SRGB_16F, GS_CS_709_EXTENDED), false); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(enum_values), + cmocka_unit_test(rotation_multiples_of_90), + cmocka_unit_test(rotation_other_angles), + cmocka_unit_test(rotation_size_to_float), + cmocka_unit_test(async_linear_srgb_space_pairs), + cmocka_unit_test(async_linear_srgb_outside_enum), + cmocka_unit_test(async_nonlinear_alpha_every_input), + cmocka_unit_test(technique_space_pairs), + cmocka_unit_test(technique_outside_enum), + cmocka_unit_test(reads_white_level_space_pairs), + cmocka_unit_test(multiplier_space_pairs), + cmocka_unit_test(multiplier_outside_enum), + cmocka_unit_test(multiplier_any_level), + cmocka_unit_test(convert_space_pairs), + cmocka_unit_test(convert_outside_enum), + cmocka_unit_test(async_plan_every_input), + cmocka_unit_test(convert_plan_every_input), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_source_draw_plan_steps.cmake b/test/cmocka/tests.d/test_source_draw_plan_steps.cmake new file mode 100644 index 0000000000..0cf0f14d11 --- /dev/null +++ b/test/cmocka/tests.d/test_source_draw_plan_steps.cmake @@ -0,0 +1,13 @@ +# Characterization of the source draw decisions of libobs/obs-source.c +# (rotate_async_video(), obs_source_render_async_video() and source_render()), +# moved to libobs/obs-source-draw-plan.c and ported to rust/obs-source +# (draw_plan): the rotation translate for 0, 90, 180, 270 and other angles on +# non-square sizes, the linear sRGB flag and the non-linear alpha blend of an +# async frame, the technique of every pair of colour spaces with and without +# non-linear alpha, the white level read and the multiplier at SDR white +# levels of 80, 300 and 0, and whether source_render() converts through +# color_space_texrender. Builds the moved file into itself. The functions are +# hidden in libobs, so this characterizes that C source; the test calls only +# them. test_async_rotation_normalize, test_filter_color_space and +# test_source_color_space_steps cover the callers through the real libobs. +# Target and test are declared in ../build/test-graph.json. From 09f3d52d8a6006fee12173707a6f9e509bcf4277 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:23:01 -0400 Subject: [PATCH 3/6] obs-source: Add Tier 1 tests for the source draw plan port Tests for the port of libobs/obs-source-draw-plan.c.h to the draw_plan module of rust/obs-source. The safe core is a stub (todo!()): - rotation_x() and rotation_y() (the rotation as the C's long, c_long, the size as u32, the translate as f32), async_linear_srgb(), async_nonlinear_alpha(), technique() (a Technique), reads_white_level(), multiplier() (f32) and convert(), on plain values: colour spaces as the C's int; - Technique, enum obs_source_draw_technique with the C's numbers. The technique enum and the enum gs_color_space values move from deinterlace_draw to a new shared module, draw_technique, with Technique::value() (the C's number) and Technique::name() (the name C maps it to); Technique gains DrawNonlinearAlpha (4) and DrawNonlinearAlphaMultiply (5). deinterlace_draw re-exports them, so its users and tests are unchanged, and its shim returns Technique::value() through the shared ffi type obs_source_draw_technique (src/ffi/source_draw_technique.rs) instead of its own copy. tests/source/draw_plan.rs ports test_source_draw_plan_steps 1:1: its 17 cases under the same names (steps_*), in the same order, with the same inputs and expectations, the floats compared as bits. Edge cases (edge_*): the rotation translate for every angle from -1000 to 1000, the ends of c_long and, where long is 64 bits, 90, 180 and 270 plus 2^32, against a model that converts the size through f64; every size from 0 to 1024 and around 2^24, 2^25, 2^31 and u32::MAX; every decision over every pair of 260 spaces around the enum and the ends of i32 with every flag, against a model written from the space classes; the multiplier at 26 special f32 bit patterns (zeros, subnormals, the normal ends, OBS's levels, infinities, quiet and signalling NaNs of both signs with payloads) for every pair of 9 spaces, against the one division written out; the white level is read exactly when the multiplier at 300 nits is not 1. Proptests (4000 cases each): the rotation over any c_long and u32 sizes, a third of them at 90, 180 or 270; every decision over any space, flag and white level bit pattern. soldr cargo test -p obs-data --test obs-tests-core source::draw_plan fails 23 of 24 with "not yet implemented"; steps_enum_values, which tests only the enum, passes. source::deinterlace_draw still passes 39 of 39. Refs #2187 --- rust/obs-source/src/deinterlace_draw.rs | 37 +- rust/obs-source/src/draw_plan.rs | 85 +++ rust/obs-source/src/draw_technique.rs | 84 +++ rust/obs-source/src/ffi/mod.rs | 1 + .../src/ffi/source_deinterlace_draw.rs | 23 +- .../src/ffi/source_draw_technique.rs | 9 + rust/obs-source/src/lib.rs | 2 + rust/obs-source/tests/source/draw_plan.rs | 713 ++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 9 files changed, 907 insertions(+), 48 deletions(-) create mode 100644 rust/obs-source/src/draw_plan.rs create mode 100644 rust/obs-source/src/draw_technique.rs create mode 100644 rust/obs-source/src/ffi/source_draw_technique.rs create mode 100644 rust/obs-source/tests/source/draw_plan.rs diff --git a/rust/obs-source/src/deinterlace_draw.rs b/rust/obs-source/src/deinterlace_draw.rs index 3c0dbbdd20..bf4b13f515 100644 --- a/rust/obs-source/src/deinterlace_draw.rs +++ b/rust/obs-source/src/deinterlace_draw.rs @@ -42,6 +42,11 @@ )] use crate::deinterlace_pacing::TWOX_TOLERANCE; +/// The colour spaces and the technique, shared with [`crate::draw_plan`]. +pub use crate::draw_technique::{ + GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, GS_CS_SRGB_16F, Technique, +}; +use crate::draw_technique::{SCRGB_WHITE, sdr}; /// `OBS_DEINTERLACE_MODE_DISABLE` (`enum obs_deinterlace_mode`, `obs.h`). pub const MODE_DISABLE: i32 = 0; @@ -62,18 +67,6 @@ pub const MODE_YADIF: i32 = 7; /// `OBS_DEINTERLACE_MODE_YADIF_2X`. pub const MODE_YADIF_2X: i32 = 8; -/// `GS_CS_SRGB` (`enum gs_color_space`, `graphics/graphics.h`): SDR. -pub const GS_CS_SRGB: i32 = 0; -/// `GS_CS_SRGB_16F`: high-precision SDR. -pub const GS_CS_SRGB_16F: i32 = 1; -/// `GS_CS_709_EXTENDED`: the canvas, Mac EDR (HDR). -pub const GS_CS_709_EXTENDED: i32 = 2; -/// `GS_CS_709_SCRGB`: 1.0 = 80 nits, Windows and Linux HDR. -pub const GS_CS_709_SCRGB: i32 = 3; - -/// The nits of 1.0 in scRGB: the multiplier is the white level over it. -const SCRGB_WHITE: f32 = 80.0; - /// `enum obs_source_deinterlace_draw_effect`: the effect a mode draws with, /// `obs->video.deinterlace__effect`. The discriminants are the C's. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -96,21 +89,6 @@ pub enum Effect { Yadif2x = 7, } -/// `enum obs_source_deinterlace_draw_technique`: the technique -/// `deinterlace_render()` draws with; C maps it to the name. The -/// discriminants are the C's. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum Technique { - /// `"Draw"`. - Draw = 0, - /// `"DrawMultiply"`. - DrawMultiply = 1, - /// `"DrawTonemap"`. - DrawTonemap = 2, - /// `"DrawMultiplyTonemap"`. - DrawMultiplyTonemap = 3, -} - /// `enum obs_source_deinterlace_draw_mode_change`: what /// `obs_source_set_deinterlace_mode()` does. The discriminants are the /// C's. @@ -198,11 +176,6 @@ pub fn linear_srgb(source_space: i32, gs_linear_srgb: bool, mode: i32) -> bool { source_space != GS_CS_SRGB || gs_linear_srgb || linear_required(mode) } -/// Whether a space is SDR, [`GS_CS_SRGB`] or [`GS_CS_SRGB_16F`]. -fn sdr(space: i32) -> bool { - space == GS_CS_SRGB || space == GS_CS_SRGB_16F -} - /// `obs_source_deinterlace_draw_technique()`: the technique for a source in /// `source_space` drawn into `current_space`. DrawMultiply for SDR or /// [`GS_CS_709_EXTENDED`] into [`GS_CS_709_SCRGB`], DrawTonemap for diff --git a/rust/obs-source/src/draw_plan.rs b/rust/obs-source/src/draw_plan.rs new file mode 100644 index 0000000000..d03866a055 --- /dev/null +++ b/rust/obs-source/src/draw_plan.rs @@ -0,0 +1,85 @@ +//! Safe core of `libobs/obs-source-draw-plan.c.h`: the source draw +//! decisions of `obs-source.c`. +//! +//! - [`rotation_x`] and [`rotation_y`] (`rotate_async_video()`): the +//! translate that moves a rotated async frame back into its box; +//! - [`async_linear_srgb`] and [`async_nonlinear_alpha`] +//! (`obs_source_render_async_video()`): the linear sRGB flag and the +//! non-linear alpha blend of an async frame; +//! - [`technique`]: the technique for each pair of source and current colour +//! spaces, of an async frame and of `source_render()`'s conversion; +//! - [`reads_white_level`] and [`multiplier`]: when the SDR white level is +//! read and the multiplier, for both; +//! - [`convert`] (`source_render()`): whether the source is drawn through +//! `color_space_texrender`. +//! +//! `obs-source.c.h` keeps every `gs_*` call in its order and under the same +//! conditions, `convert_video_space()`, `obs_get_video_sdr_white_level()`, +//! `gs_get_format_from_space()` and the texrenders. It passes their results +//! in and acts on what comes back. Nothing here keeps state, takes a lock or +//! calls libobs. + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::unimplemented, + clippy::unreachable +)] + +use std::os::raw::c_long; + +pub use crate::draw_technique::{ + GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, GS_CS_SRGB_16F, Technique, +}; + +/// `obs_source_draw_plan_rotation_x()`. +pub fn rotation_x(rotation: c_long, width: u32, height: u32) -> f32 { + let _ = (rotation, width, height); + todo!() +} + +/// `obs_source_draw_plan_rotation_y()`. +pub fn rotation_y(rotation: c_long, width: u32, height: u32) -> f32 { + let _ = (rotation, width, height); + todo!() +} + +/// `obs_source_draw_plan_async_linear_srgb()`. +pub fn async_linear_srgb(source_space: i32, current_space: i32, gs_linear_srgb: bool) -> bool { + let _ = (source_space, current_space, gs_linear_srgb); + todo!() +} + +/// `obs_source_draw_plan_async_nonlinear_alpha()`. +pub fn async_nonlinear_alpha(source_space: i32, linear_srgb: bool, linear_alpha: bool) -> bool { + let _ = (source_space, linear_srgb, linear_alpha); + todo!() +} + +/// `obs_source_draw_plan_technique()`. +pub fn technique(source_space: i32, current_space: i32, nonlinear_alpha: bool) -> Technique { + let _ = (source_space, current_space, nonlinear_alpha); + todo!() +} + +/// `obs_source_draw_plan_reads_white_level()`. +pub fn reads_white_level(source_space: i32, current_space: i32) -> bool { + let _ = (source_space, current_space); + todo!() +} + +/// `obs_source_draw_plan_multiplier()`. +pub fn multiplier(source_space: i32, current_space: i32, sdr_white_level: f32) -> f32 { + let _ = (source_space, current_space, sdr_white_level); + todo!() +} + +/// `obs_source_draw_plan_convert()`. +pub fn convert(source_space: i32, current_space: i32) -> bool { + let _ = (source_space, current_space); + todo!() +} diff --git a/rust/obs-source/src/draw_technique.rs b/rust/obs-source/src/draw_technique.rs new file mode 100644 index 0000000000..5bb35837c3 --- /dev/null +++ b/rust/obs-source/src/draw_technique.rs @@ -0,0 +1,84 @@ +//! The draw technique and colour space values shared by the source draw +//! decisions: [`crate::draw_plan`] (`libobs/obs-source-draw-plan.c.h`) and +//! [`crate::deinterlace_draw`] (`libobs/obs-source-deinterlace-draw.c.h`). +//! +//! [`Technique`] is `enum obs_source_draw_technique` +//! (`libobs/obs-source-draw-technique.h`), the technique of the default or +//! deinterlace effect a source draws with; C maps it to the name +//! (`obs_source_draw_technique_name()`). The colour spaces are +//! `enum gs_color_space`, which the decisions take as the C's `int`. + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::todo, + clippy::unimplemented, + clippy::unreachable +)] + +/// `GS_CS_SRGB` (`enum gs_color_space`, `graphics/graphics.h`): SDR. +pub const GS_CS_SRGB: i32 = 0; +/// `GS_CS_SRGB_16F`: high-precision SDR. +pub const GS_CS_SRGB_16F: i32 = 1; +/// `GS_CS_709_EXTENDED`: the canvas, Mac EDR (HDR). +pub const GS_CS_709_EXTENDED: i32 = 2; +/// `GS_CS_709_SCRGB`: 1.0 = 80 nits, Windows and Linux HDR. +pub const GS_CS_709_SCRGB: i32 = 3; + +/// The nits of 1.0 in scRGB: the multiplier is the white level over it. +pub(crate) const SCRGB_WHITE: f32 = 80.0; + +/// Whether a space is SDR, [`GS_CS_SRGB`] or [`GS_CS_SRGB_16F`]. +pub(crate) fn sdr(space: i32) -> bool { + space == GS_CS_SRGB || space == GS_CS_SRGB_16F +} + +/// `enum obs_source_draw_technique`: the technique a source draws with; C +/// maps it to the name. The discriminants are the C's. The deinterlace +/// draw decisions return the first four only. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Technique { + /// `"Draw"`. + Draw = 0, + /// `"DrawMultiply"`. + DrawMultiply = 1, + /// `"DrawTonemap"`. + DrawTonemap = 2, + /// `"DrawMultiplyTonemap"`. + DrawMultiplyTonemap = 3, + /// `"DrawNonlinearAlpha"`: an SDR async frame with non-linear alpha. + DrawNonlinearAlpha = 4, + /// `"DrawNonlinearAlphaMultiply"`: the same into scRGB. + DrawNonlinearAlphaMultiply = 5, +} + +impl Technique { + /// The C's enum value. + pub const fn value(self) -> u32 { + match self { + Technique::Draw => 0, + Technique::DrawMultiply => 1, + Technique::DrawTonemap => 2, + Technique::DrawMultiplyTonemap => 3, + Technique::DrawNonlinearAlpha => 4, + Technique::DrawNonlinearAlphaMultiply => 5, + } + } + + /// The technique's name in `default.effect` and the deinterlace + /// effects, as `obs_source_draw_technique_name()` maps it. + pub const fn name(self) -> &'static str { + match self { + Technique::Draw => "Draw", + Technique::DrawMultiply => "DrawMultiply", + Technique::DrawTonemap => "DrawTonemap", + Technique::DrawMultiplyTonemap => "DrawMultiplyTonemap", + Technique::DrawNonlinearAlpha => "DrawNonlinearAlpha", + Technique::DrawNonlinearAlphaMultiply => "DrawNonlinearAlphaMultiply", + } + } +} diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index 992743851d..ee050e213c 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -7,6 +7,7 @@ pub mod source_color_space; pub mod source_conversion_plan; pub mod source_deinterlace_draw; pub mod source_deinterlace_pacing; +pub mod source_draw_technique; pub mod source_ptm_gate; pub mod source_size; pub mod source_transition_clock; diff --git a/rust/obs-source/src/ffi/source_deinterlace_draw.rs b/rust/obs-source/src/ffi/source_deinterlace_draw.rs index df9cb83fde..0df6d3da6d 100644 --- a/rust/obs-source/src/ffi/source_deinterlace_draw.rs +++ b/rust/obs-source/src/ffi/source_deinterlace_draw.rs @@ -21,7 +21,7 @@ use std::os::raw::c_int; -use crate::deinterlace_draw::{self as core, ModeChange, Technique}; +use crate::deinterlace_draw::{self as core, ModeChange}; /// `enum obs_source_deinterlace_draw_effect`. #[allow(non_camel_case_types)] @@ -29,23 +29,14 @@ pub type obs_source_deinterlace_draw_effect = c_int; /// `OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_NONE`. pub const OBS_SOURCE_DEINTERLACE_DRAW_EFFECT_NONE: obs_source_deinterlace_draw_effect = -1; -/// `enum obs_source_deinterlace_draw_technique`. -#[allow(non_camel_case_types)] -pub type obs_source_deinterlace_draw_technique = u32; +/// `enum obs_source_draw_technique` (`obs-source-draw-technique.h`), +/// shared with the source draw plan. +pub use super::source_draw_technique::obs_source_draw_technique; /// `enum obs_source_deinterlace_draw_mode_change`. #[allow(non_camel_case_types)] pub type obs_source_deinterlace_draw_mode_change = u32; -const fn technique_value(technique: Technique) -> obs_source_deinterlace_draw_technique { - match technique { - Technique::Draw => 0, - Technique::DrawMultiply => 1, - Technique::DrawTonemap => 2, - Technique::DrawMultiplyTonemap => 3, - } -} - const fn mode_change_value(change: ModeChange) -> obs_source_deinterlace_draw_mode_change { match change { ModeChange::Same => 0, @@ -99,15 +90,15 @@ pub extern "C" fn obs_source_deinterlace_draw_linear_srgb( core::linear_srgb(source_space, gs_linear_srgb, mode) } -/// `enum obs_source_deinterlace_draw_technique +/// `enum obs_source_draw_technique /// obs_source_deinterlace_draw_technique(int source_space, int /// current_space)`. #[unsafe(no_mangle)] pub extern "C" fn obs_source_deinterlace_draw_technique( source_space: c_int, current_space: c_int, -) -> obs_source_deinterlace_draw_technique { - technique_value(core::technique(source_space, current_space)) +) -> obs_source_draw_technique { + core::technique(source_space, current_space).value() } /// `bool obs_source_deinterlace_draw_reads_white_level(int source_space, diff --git a/rust/obs-source/src/ffi/source_draw_technique.rs b/rust/obs-source/src/ffi/source_draw_technique.rs new file mode 100644 index 0000000000..59d4520772 --- /dev/null +++ b/rust/obs-source/src/ffi/source_draw_technique.rs @@ -0,0 +1,9 @@ +//! `enum obs_source_draw_technique` (`libobs/obs-source-draw-technique.h`), +//! which the deinterlace draw shims (`source_deinterlace_draw`) and the +//! source draw plan shims (`source_draw_plan`) return. Its values run from 0 +//! to 5, so the C's enum type is `unsigned int` or `int`, and every value +//! fits both; [`crate::draw_technique::Technique::value`] gives it. + +/// `enum obs_source_draw_technique`. +#[allow(non_camel_case_types)] +pub type obs_source_draw_technique = u32; diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index 008aba65e8..5069b9c1e1 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -38,6 +38,8 @@ pub mod color_space; pub mod conversion_plan; pub mod deinterlace_draw; pub mod deinterlace_pacing; +pub mod draw_plan; +pub mod draw_technique; pub mod ptm_gate; pub mod size_route; pub mod transition_clock; diff --git a/rust/obs-source/tests/source/draw_plan.rs b/rust/obs-source/tests/source/draw_plan.rs new file mode 100644 index 0000000000..c58cb10ce5 --- /dev/null +++ b/rust/obs-source/tests/source/draw_plan.rs @@ -0,0 +1,713 @@ +//! Tier 1: `obs_source::draw_plan`, the source draw decisions. +//! +//! The `steps_*` cases port `test/cmocka/test_source_draw_plan_steps.c` 1:1, +//! under the same case names, in the same order, with the same inputs and +//! expectations, the floats compared as bits. +//! +//! The `edge_*` cases are new: the rotation translate for every angle from +//! -1000 to 1000, the ends of `c_long` and (where `long` is 64 bits) angles +//! past 32 bits, against a model that converts the size through `f64`; the +//! sizes around 2^24 and `u32::MAX`; every decision over every pair of 260 +//! spaces around the enum and the ends of `i32` with every flag, against a +//! model written from the space classes; the multiplier at every special +//! `f32` bit pattern against the one division; and proptests of each over +//! full-range values (white levels as any bit pattern). + +use std::os::raw::c_long; + +use obs_source::draw_plan::{ + GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, GS_CS_SRGB_16F, Technique, async_linear_srgb, + async_nonlinear_alpha, convert, multiplier, reads_white_level, rotation_x, rotation_y, + technique, +}; +use proptest::prelude::*; + +/// Colour spaces outside `enum gs_color_space`, as the C test's +/// `OUTSIDE_SPACES`. +const OUTSIDE_SPACES: [i32; 5] = [-1, 4, 99, i32::MIN, i32::MAX]; + +const SPACES: [i32; 4] = [ + GS_CS_SRGB, + GS_CS_SRGB_16F, + GS_CS_709_EXTENDED, + GS_CS_709_SCRGB, +]; + +/// SDR white levels: OBS's default, a common one, and 0. +const WHITES: [f32; 3] = [80.0, 300.0, 0.0]; + +const ONE: u32 = 0x3f80_0000; + +use Technique::{ + Draw as D, DrawMultiply as M, DrawMultiplyTonemap as MT, DrawNonlinearAlpha as NA, + DrawNonlinearAlphaMultiply as NAM, DrawTonemap as T, +}; + +fn bits(f: f32) -> u32 { + f.to_bits() +} + +// --- enum values --------------------------------------------------------- + +#[test] +fn steps_enum_values() { + // The C maps these numbers to names; the first four are the + // deinterlace draw decisions' too. + let all = [ + (D, 0, "Draw"), + (M, 1, "DrawMultiply"), + (T, 2, "DrawTonemap"), + (MT, 3, "DrawMultiplyTonemap"), + (NA, 4, "DrawNonlinearAlpha"), + (NAM, 5, "DrawNonlinearAlphaMultiply"), + ]; + for (technique, value, name) in all { + assert_eq!(technique as i32, value); + assert_eq!(technique.value(), u32::try_from(value).unwrap()); + assert_eq!(technique.name(), name); + } + assert_eq!( + obs_source::deinterlace_draw::Technique::DrawMultiplyTonemap, + MT + ); +} + +// --- rotation ------------------------------------------------------------ + +#[test] +fn steps_rotation_multiples_of_90() { + let cases: [(c_long, u32, u32, u32, u32); 12] = [ + (0, 1920, 1080, 0, 0), + (90, 1920, 1080, 0, 0x44f0_0000), + (180, 1920, 1080, 0x44f0_0000, 0x4487_0000), + (270, 1920, 1080, 0x4487_0000, 0), + (90, 1080, 1920, 0, 0x4487_0000), + (180, 1080, 1920, 0x4487_0000, 0x44f0_0000), + (270, 1080, 1920, 0x44f0_0000, 0), + (0, 1080, 1920, 0, 0), + (90, 640, 480, 0, 0x4420_0000), + (180, 640, 480, 0x4420_0000, 0x43f0_0000), + (270, 640, 480, 0x43f0_0000, 0), + (0, 640, 480, 0, 0), + ]; + for (rotation, w, h, x, y) in cases { + assert_eq!( + bits(rotation_x(rotation, w, h)), + x, + "x at {rotation} for {w}x{h}" + ); + assert_eq!( + bits(rotation_y(rotation, w, h)), + y, + "y at {rotation} for {w}x{h}" + ); + } +} + +#[test] +fn steps_rotation_other_angles() { + let angles: [c_long; 17] = [ + 1, + 45, + 89, + 91, + 135, + 179, + 181, + 269, + 271, + 359, + 360, + 450, + -90, + -180, + -270, + c_long::MIN, + c_long::MAX, + ]; + for angle in angles { + for (w, h) in [(1920, 1080), (640, 480)] { + assert_eq!(bits(rotation_x(angle, w, h)), 0, "x at {angle}"); + assert_eq!(bits(rotation_y(angle, w, h)), 0, "y at {angle}"); + } + } +} + +#[test] +fn steps_rotation_size_to_float() { + assert_eq!(bits(rotation_y(90, 0, 7)), 0); + assert_eq!(bits(rotation_x(270, 7, 0)), 0); + assert_eq!(bits(rotation_y(90, 16_777_217, 3)), 0x4b80_0000); + assert_eq!(bits(rotation_x(180, 16_777_219, 3)), 0x4b80_0002); + assert_eq!(bits(rotation_y(180, 3, u32::MAX)), 0x4f80_0000); + assert_eq!(bits(rotation_x(270, 3, u32::MAX)), 0x4f80_0000); + assert_eq!(bits(rotation_x(180, 1, 1)), ONE); +} + +// --- async video: linear sRGB and non-linear alpha ------------------------ + +const ASYNC_LINEAR: [[bool; 4]; 4] = [ + [false, true, true, true], + [false, false, false, false], + [true, true, false, false], + [true, true, false, false], +]; + +#[test] +fn steps_async_linear_srgb_space_pairs() { + for (s, row) in SPACES.iter().zip(ASYNC_LINEAR) { + for (c, want) in SPACES.iter().zip(row) { + assert_eq!(async_linear_srgb(*s, *c, false), want, "{s} into {c}"); + assert!(async_linear_srgb(*s, *c, true), "{s} into {c}, flag on"); + } + } +} + +#[test] +fn steps_async_linear_srgb_outside_enum() { + for o in OUTSIDE_SPACES { + for gs in [false, true] { + for s in SPACES { + assert_eq!(async_linear_srgb(o, s, gs), gs); + } + assert!(async_linear_srgb(GS_CS_SRGB, o, gs)); + assert_eq!(async_linear_srgb(GS_CS_SRGB_16F, o, gs), gs); + assert_eq!(async_linear_srgb(GS_CS_709_EXTENDED, o, gs), gs); + assert_eq!(async_linear_srgb(GS_CS_709_SCRGB, o, gs), gs); + } + } +} + +#[test] +fn steps_async_nonlinear_alpha_every_input() { + for linear in [false, true] { + for alpha in [false, true] { + assert_eq!( + async_nonlinear_alpha(GS_CS_SRGB, linear, alpha), + linear && !alpha + ); + for s in &SPACES[1..] { + assert!(!async_nonlinear_alpha(*s, linear, alpha)); + } + for o in OUTSIDE_SPACES { + assert!(!async_nonlinear_alpha(o, linear, alpha)); + } + } + } +} + +// --- technique ----------------------------------------------------------- + +const TECHNIQUES: [[[Technique; 4]; 4]; 2] = [ + [[D, D, D, M], [D, D, D, M], [T, T, D, M], [MT, MT, M, D]], + [ + [NA, NA, NA, NAM], + [D, D, D, M], + [T, T, D, M], + [MT, MT, M, D], + ], +]; + +#[test] +fn steps_technique_space_pairs() { + for (na, table) in [false, true].into_iter().zip(TECHNIQUES) { + for (s, row) in SPACES.iter().zip(table) { + for (c, want) in SPACES.iter().zip(row) { + assert_eq!(technique(*s, *c, na), want, "{s} into {c}, non-linear {na}"); + } + } + } +} + +#[test] +fn steps_technique_outside_enum() { + for o in OUTSIDE_SPACES { + for na in [false, true] { + for s in SPACES { + assert_eq!(technique(o, s, na), D); + assert_eq!(technique(s, o, na), D); + } + } + } + assert!(async_nonlinear_alpha( + GS_CS_SRGB, + async_linear_srgb(GS_CS_SRGB, 4, false), + false + )); +} + +// --- white level and multiplier -------------------------------------------- + +const READS_WHITE: [[bool; 4]; 4] = [ + [false, false, false, true], + [false, false, false, true], + [false, false, false, true], + [true, true, true, false], +]; + +const MULTIPLIERS: [[[u32; 3]; 4]; 4] = [ + [ + [ONE, ONE, ONE], + [ONE, ONE, ONE], + [ONE, ONE, ONE], + [ONE, 0x4070_0000, 0], + ], + [ + [ONE, ONE, ONE], + [ONE, ONE, ONE], + [ONE, ONE, ONE], + [ONE, 0x4070_0000, 0], + ], + [ + [ONE, ONE, ONE], + [ONE, ONE, ONE], + [ONE, ONE, ONE], + [ONE, 0x4070_0000, 0], + ], + [ + [ONE, 0x3e88_8889, 0x7f80_0000], + [ONE, 0x3e88_8889, 0x7f80_0000], + [ONE, 0x3e88_8889, 0x7f80_0000], + [ONE, ONE, ONE], + ], +]; + +#[test] +fn steps_reads_white_level_space_pairs() { + for (s, row) in SPACES.iter().zip(READS_WHITE) { + for (c, want) in SPACES.iter().zip(row) { + assert_eq!(reads_white_level(*s, *c), want, "{s} into {c}"); + } + } + for o in OUTSIDE_SPACES { + for s in SPACES { + assert!(!reads_white_level(o, s)); + assert!(!reads_white_level(s, o)); + } + } +} + +#[test] +fn steps_multiplier_space_pairs() { + for (s, plane) in SPACES.iter().zip(MULTIPLIERS) { + for (c, row) in SPACES.iter().zip(plane) { + for (w, want) in WHITES.iter().zip(row) { + assert_eq!(bits(multiplier(*s, *c, *w)), want, "{s} into {c} at {w}"); + } + } + } +} + +#[test] +fn steps_multiplier_outside_enum() { + for o in OUTSIDE_SPACES { + for s in SPACES { + for w in WHITES { + assert_eq!(bits(multiplier(o, s, w)), ONE); + assert_eq!(bits(multiplier(s, o, w)), ONE); + } + } + } +} + +#[test] +fn steps_multiplier_any_level() { + assert_eq!( + bits(multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, -0.0)), + 0xff80_0000 + ); + assert_eq!( + bits(multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, -0.0)), + 0x8000_0000 + ); + assert_eq!( + bits(multiplier( + GS_CS_709_SCRGB, + GS_CS_709_EXTENDED, + f32::INFINITY + )), + 0 + ); + assert_eq!( + bits(multiplier( + GS_CS_709_EXTENDED, + GS_CS_709_SCRGB, + f32::INFINITY + )), + 0x7f80_0000 + ); + assert_eq!( + bits(multiplier(GS_CS_SRGB_16F, GS_CS_709_SCRGB, -80.0)), + 0xbf80_0000 + ); + assert_eq!( + bits(multiplier(GS_CS_709_SCRGB, GS_CS_SRGB_16F, -80.0)), + 0xbf80_0000 + ); + assert_eq!( + bits(multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, 480.0)), + 0x40c0_0000 + ); + assert_eq!( + bits(multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, 480.0)), + 0x3e2a_aaab + ); + assert_eq!( + bits(multiplier(GS_CS_709_EXTENDED, GS_CS_709_SCRGB, 203.0)), + 0x4022_6666 + ); + assert_eq!( + bits(multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, f32::from_bits(1))), + 0x7f80_0000 + ); + assert_eq!( + bits(multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, f32::from_bits(1))), + 0 + ); + assert!(multiplier(GS_CS_709_SCRGB, GS_CS_SRGB, f32::NAN).is_nan()); + assert!(multiplier(GS_CS_SRGB, GS_CS_709_SCRGB, f32::NAN).is_nan()); + assert_eq!(bits(multiplier(GS_CS_SRGB, GS_CS_SRGB, f32::NAN)), ONE); + assert_eq!(bits(multiplier(GS_CS_709_EXTENDED, GS_CS_SRGB, 0.0)), ONE); +} + +// --- source_render() colour space conversion -------------------------------- + +const CONVERTS: [[bool; 4]; 4] = [ + [false, false, true, true], + [false, false, true, true], + [true, true, false, true], + [true, true, true, false], +]; + +#[test] +fn steps_convert_space_pairs() { + for (s, row) in SPACES.iter().zip(CONVERTS) { + for (c, want) in SPACES.iter().zip(row) { + assert_eq!(convert(*s, *c), want, "{s} into {c}"); + } + } +} + +#[test] +fn steps_convert_outside_enum() { + for o in OUTSIDE_SPACES { + for s in SPACES { + assert!(!convert(o, s)); + assert!(!convert(s, o)); + } + assert!(!convert(o, o)); + } +} + +// --- the decisions in their callers' order --------------------------------- + +#[test] +fn steps_async_plan_every_input() { + for (si, s) in SPACES.iter().enumerate() { + for (ci, c) in SPACES.iter().enumerate() { + for gs in [false, true] { + for alpha in [false, true] { + for (wi, w) in WHITES.iter().enumerate() { + let linear = async_linear_srgb(*s, *c, gs); + let na = async_nonlinear_alpha(*s, linear, alpha); + let t = technique(*s, *c, na); + let m = if reads_white_level(*s, *c) { + multiplier(*s, *c, *w) + } else { + 1.0 + }; + let want_linear = gs || ASYNC_LINEAR[si][ci]; + let want_na = si == 0 && want_linear && !alpha; + assert_eq!(linear, want_linear); + assert_eq!(na, want_na); + assert_eq!(t, TECHNIQUES[usize::from(want_na)][si][ci]); + assert_eq!(bits(m), MULTIPLIERS[si][ci][wi]); + } + } + } + } + } +} + +#[test] +fn steps_convert_plan_every_input() { + for (si, s) in SPACES.iter().enumerate() { + for (ci, c) in SPACES.iter().enumerate() { + for (wi, w) in WHITES.iter().enumerate() { + assert_eq!(convert(*s, *c), CONVERTS[si][ci]); + if !CONVERTS[si][ci] { + continue; + } + assert_eq!(technique(*s, *c, false), TECHNIQUES[0][si][ci]); + let m = if reads_white_level(*s, *c) { + multiplier(*s, *c, *w) + } else { + 1.0 + }; + assert_eq!(bits(m), MULTIPLIERS[si][ci][wi]); + } + } + } + assert_eq!(technique(GS_CS_SRGB, GS_CS_709_EXTENDED, false), D); + assert!(!reads_white_level(GS_CS_SRGB_16F, GS_CS_709_EXTENDED)); +} + +// --- edge cases: models ---------------------------------------------------- + +/// The C's `(float)uint32_t`, through `f64` (exact for any `u32`) and one +/// rounding to nearest even. +fn model_size(size: u32) -> f32 { + #[allow(clippy::cast_possible_truncation)] + let f = f64::from(size) as f32; + f +} + +fn model_rotation(rotation: c_long, w: u32, h: u32) -> (f32, f32) { + match rotation { + 90 => (0.0, model_size(w)), + 180 => (model_size(w), model_size(h)), + 270 => (model_size(h), 0.0), + _ => (0.0, 0.0), + } +} + +/// The space class: SDR, 709_EXTENDED, scRGB, or none outside the enum. +fn class(space: i32) -> Option { + match space { + GS_CS_SRGB | GS_CS_SRGB_16F => Some(0), + GS_CS_709_EXTENDED => Some(1), + GS_CS_709_SCRGB => Some(2), + _ => None, + } +} + +fn model_linear(s: i32, c: i32, gs: bool) -> bool { + gs || (s == GS_CS_SRGB && c != GS_CS_SRGB) + || (matches!(class(s), Some(1 | 2)) && class(c) == Some(0)) +} + +fn model_nonlinear(s: i32, linear: bool, alpha: bool) -> bool { + s == GS_CS_SRGB && linear && !alpha +} + +fn model_technique(s: i32, c: i32, na: bool) -> Technique { + match (class(s), class(c)) { + (Some(0), Some(0 | 1)) if s == GS_CS_SRGB && na => NA, + (Some(0), Some(2)) if s == GS_CS_SRGB && na => NAM, + (Some(0), Some(2)) => M, + (Some(1), Some(0)) => T, + (Some(1), Some(2)) => M, + (Some(2), Some(0)) => MT, + (Some(2), Some(1)) => M, + _ => D, + } +} + +fn model_reads_white(s: i32, c: i32) -> bool { + matches!( + (class(s), class(c)), + (Some(0 | 1), Some(2)) | (Some(2), Some(0 | 1)) + ) +} + +fn model_multiplier(s: i32, c: i32, w: f32) -> f32 { + match (class(s), class(c)) { + (Some(0 | 1), Some(2)) => w / 80.0, + (Some(2), Some(0 | 1)) => 80.0 / w, + _ => 1.0, + } +} + +fn model_convert(s: i32, c: i32) -> bool { + matches!((class(s), class(c)), (Some(a), Some(b)) if a != b) +} + +fn edge_spaces() -> Vec { + let mut v: Vec = (-120..=120).collect(); + v.extend([ + i32::MIN, + i32::MIN + 1, + i32::MIN + 2, + i32::MAX - 2, + i32::MAX - 1, + i32::MAX, + ]); + v.extend([1000, -1000, 65_536, -65_536, 1 << 24, 1 << 30, -(1 << 30)]); + v.extend([0x100, 0x101, 0x102, 0x103, -0x100, 0x7fff_ff00]); + v +} + +/// Special `f32` levels: zeros, subnormals, the normal ends, OBS's levels, +/// infinities, quiet and signalling NaNs of both signs with payloads. +const SPECIAL_LEVELS: [u32; 26] = [ + 0x0000_0000, + 0x8000_0000, + 0x0000_0001, + 0x8000_0001, + 0x007f_ffff, + 0x0080_0000, + 0x7f7f_ffff, + 0xff7f_ffff, + 0x3f80_0000, + 0x42a0_0000, + 0x4296_0000, + 0x434b_0000, + 0x4396_0000, + 0x43f0_0000, + 0x42a0_0001, + 0x429f_ffff, + 0xc2a0_0000, + 0x7f80_0000, + 0xff80_0000, + 0x7fc0_0000, + 0xffc0_0000, + 0x7fc1_2345, + 0xffc5_4321, + 0x7fa0_0001, + 0xff80_0001, + 0x7f80_0001, +]; + +// --- edge cases ---------------------------------------------------------- + +#[test] +fn edge_rotation_every_angle() { + let mut angles: Vec = (-1000..=1000).collect(); + angles.extend([c_long::MIN, c_long::MIN + 1, c_long::MAX - 1, c_long::MAX]); + // Angles past 32 bits, where long has them: 90, 180 and 270 plus 2^32 + // are not 90, 180 or 270. + if let Some(big) = c_long::from(1_u8).checked_shl(32) { + angles.extend([big + 90, big + 180, big + 270, -big + 90, big]); + } + for (w, h) in [(1920, 1080), (1080, 1920), (0, 0), (1, u32::MAX)] { + for &angle in &angles { + let (x, y) = model_rotation(angle, w, h); + assert_eq!(bits(rotation_x(angle, w, h)), bits(x), "x at {angle}"); + assert_eq!(bits(rotation_y(angle, w, h)), bits(y), "y at {angle}"); + } + } +} + +#[test] +fn edge_rotation_sizes() { + let mut sizes: Vec = (0..=1024).collect(); + for base in [1_u32 << 24, 1 << 25, 1 << 31, u32::MAX - 300] { + sizes.extend(base.saturating_sub(300)..=base.saturating_add(300)); + } + sizes.extend([u32::MAX - 1, u32::MAX]); + for &size in &sizes { + let want = bits(model_size(size)); + assert_eq!(bits(rotation_y(90, size, 7)), want, "{size}"); + assert_eq!(bits(rotation_x(180, size, 7)), want, "{size}"); + assert_eq!(bits(rotation_y(180, 7, size)), want, "{size}"); + assert_eq!(bits(rotation_x(270, 7, size)), want, "{size}"); + } +} + +#[test] +fn edge_space_pairs() { + let spaces = edge_spaces(); + for &s in &spaces { + for &c in &spaces { + for gs in [false, true] { + assert_eq!( + async_linear_srgb(s, c, gs), + model_linear(s, c, gs), + "{s} {c} {gs}" + ); + } + for na in [false, true] { + assert_eq!( + technique(s, c, na), + model_technique(s, c, na), + "{s} {c} {na}" + ); + } + assert_eq!(reads_white_level(s, c), model_reads_white(s, c), "{s} {c}"); + assert_eq!(convert(s, c), model_convert(s, c), "{s} {c}"); + assert_eq!( + bits(multiplier(s, c, 300.0)), + bits(model_multiplier(s, c, 300.0)), + "{s} {c}" + ); + } + for linear in [false, true] { + for alpha in [false, true] { + assert_eq!( + async_nonlinear_alpha(s, linear, alpha), + model_nonlinear(s, linear, alpha) + ); + } + } + } +} + +#[test] +fn edge_multiplier_special_levels() { + let spaces = [-1, 0, 1, 2, 3, 4, 99, i32::MIN, i32::MAX]; + for level in SPECIAL_LEVELS { + let w = f32::from_bits(level); + for s in spaces { + for c in spaces { + assert_eq!( + bits(multiplier(s, c, w)), + bits(model_multiplier(s, c, w)), + "{s} into {c} at {level:#010x}" + ); + } + } + } +} + +#[test] +fn edge_white_level_read_iff_multiplier_moves() { + // At 300 nits every pair that reads the level gives something other + // than 1, and every pair that does not gives exactly 1. + let spaces = edge_spaces(); + for &s in &spaces { + for &c in &spaces { + let reads = reads_white_level(s, c); + assert_eq!(bits(multiplier(s, c, 300.0)) != ONE, reads, "{s} {c}"); + } + } +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(4000))] + + #[test] + fn edge_prop_rotation( + rotation in prop_oneof![ + Just(90 as c_long), + Just(180 as c_long), + Just(270 as c_long), + 0 as c_long..360, + any::() + ], + w in any::(), + h in any::() + ) { + let (x, y) = model_rotation(rotation, w, h); + prop_assert_eq!(bits(rotation_x(rotation, w, h)), bits(x)); + prop_assert_eq!(bits(rotation_y(rotation, w, h)), bits(y)); + } + + #[test] + fn edge_prop_space_pairs( + s in prop_oneof![-2_i32..6, any::()], + c in prop_oneof![-2_i32..6, any::()], + gs in any::(), + alpha in any::(), + level in any::() + ) { + let linear = async_linear_srgb(s, c, gs); + prop_assert_eq!(linear, model_linear(s, c, gs)); + let na = async_nonlinear_alpha(s, linear, alpha); + prop_assert_eq!(na, model_nonlinear(s, linear, alpha)); + prop_assert_eq!(technique(s, c, na), model_technique(s, c, na)); + prop_assert_eq!(reads_white_level(s, c), model_reads_white(s, c)); + prop_assert_eq!(convert(s, c), model_convert(s, c)); + let w = f32::from_bits(level); + prop_assert_eq!(bits(multiplier(s, c, w)), bits(model_multiplier(s, c, w))); + } +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 674bc09d1d..4e65257b0a 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -14,6 +14,7 @@ mod deinterlace_draw; mod deinterlace_draw_parity; mod deinterlace_pacing; mod deinterlace_pacing_parity; +mod draw_plan; mod ptm_gate; mod ptm_gate_parity; mod size_route; From 50b90b7d7a31c596186ab31c49f9e3a0fc0b3193 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:30:44 -0400 Subject: [PATCH 4/6] obs-source: Port the source draw decisions Ports libobs/obs-source-draw-plan.c.h, the source draw decisions of obs-source.c, to the draw_plan module of rust/obs-source. obs-source.c.h stays C: every gs_* call in its order and under its conditions, convert_video_space(), obs_get_video_sdr_white_level(), gs_get_format_from_space(), the texrenders and the technique names. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/draw_plan.rs): - rotation_x() and rotation_y(): a match over the C's long (c_long, never converted, so a value past 32 bits is matched as the C's switch matches it on every target): height or width for 270 and 90, both for 180, 0 otherwise; the size converts to f32 to nearest even, as (float)uint32_t does; - async_linear_srgb(): the graphics flag, or GS_CS_SRGB into anything but GS_CS_SRGB (792f8473e), or 709_EXTENDED or scRGB into SDR (080095786); async_nonlinear_alpha(): GS_CS_SRGB, linear, alpha not linear; - technique(), Technique with the C's numbers: DrawMultiply (or DrawNonlinearAlphaMultiply) for SDR into scRGB, DrawNonlinearAlpha for GS_CS_SRGB with non-linear alpha into SDR or 709_EXTENDED, DrawTonemap for 709_EXTENDED into SDR, DrawMultiply for it into scRGB, DrawMultiplyTonemap for scRGB into SDR, DrawMultiply for it into 709_EXTENDED, Draw otherwise (spaces outside the enum included); - reads_white_level() and multiplier(): into scRGB from SDR or 709_EXTENDED, sdr_white_level / 80.0; out of scRGB into them, 80.0 / sdr_white_level; 1.0 otherwise: one f32 division, the same IEEE operation as the C's, so the bits match, NaN payloads and infinities included; - convert(): both spaces in the enum, in different classes (SDR, 709_EXTENDED, scRGB). The module denies clippy's arithmetic, indexing, as, unwrap, expect, panic, todo, unimplemented and unreachable lints (one as, the size to f32, is allowed on its own helper: there is no From for f32), keeps no state and takes no lock. Shim (src/ffi/source_draw_plan.rs): the eight functions keep the C signatures, plain values in and out; the technique comes back as Technique::value() through the shared obs_source_draw_technique type. No intentional differences: there is no pointer to check. C side: the file leaves libobs.core-source for its own group, libobs.ported-source-draw-plan (LIBOBS_PORTED_SOURCES), whose PCH is obs.h (the colour space enum) and obs-source-draw-plan.h (which brings obs-source-draw-technique.h). 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 eight are local in all three, and the hybrid's come from the Rust archive (it has no object for the C file). ci/test_ported_sources.py checks the group on every host. test_source_draw_plan_steps still compiles the C file into itself, unchanged. The new test_source_draw_plan_steps_rust builds the same test source against libobs_rust-static (and libm except on MSVC): the test calls only the eight functions and the header's static inline obs_source_draw_technique_name(), and includes their header and obs.h for the enum values, so it takes libobs's include directories (SIMDe, obsconfig.h) and links the archive alone. It passes 17 of 17 against the shims. 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 hybrid. Tier 1 source::draw_plan passes 24/24; source::deinterlace_draw still passes 39/39. Refs #2187 --- ci/test_ported_sources.py | 1 + .../libobs-ported-source-draw-plan.pch.h | 11 ++ libobs/build/libobs.core-source+.h.c | 1 - .../build/libobs.ported-source-draw-plan+.h.c | 5 + libobs/build/source-sets.json | 1 + libobs/cmake/rust-exports.map | 10 ++ libobs/cmake/rust-hidden.txt | 12 ++ libobs/cmake/rust-unexports-macos.txt | 9 + libobs/obs-source-draw-plan.c.h | 2 +- rust/obs-source/src/draw_plan.rs | 161 +++++++++++++++--- rust/obs-source/src/ffi/mod.rs | 1 + rust/obs-source/src/ffi/source_draw_plan.rs | 105 ++++++++++++ .../tests.d/test_source_draw_plan_steps.cmake | 21 +++ 13 files changed, 314 insertions(+), 26 deletions(-) create mode 100644 libobs/build/libobs-ported-source-draw-plan.pch.h create mode 100644 libobs/build/libobs.ported-source-draw-plan+.h.c create mode 100644 rust/obs-source/src/ffi/source_draw_plan.rs diff --git a/ci/test_ported_sources.py b/ci/test_ported_sources.py index 918158513c..f8853904e2 100644 --- a/ci/test_ported_sources.py +++ b/ci/test_ported_sources.py @@ -44,6 +44,7 @@ "obs-source-conversion-plan.c.h.o", "obs-source-deinterlace-draw.c.h.o", "obs-source-deinterlace-pacing.c.h.o", + "obs-source-draw-plan.c.h.o", "obs-source-load.c.h.o", "obs-source-ptm-gate.c.h.o", "obs-source-size.c.h.o", diff --git a/libobs/build/libobs-ported-source-draw-plan.pch.h b/libobs/build/libobs-ported-source-draw-plan.pch.h new file mode 100644 index 0000000000..d9a3d70a77 --- /dev/null +++ b/libobs/build/libobs-ported-source-draw-plan.pch.h @@ -0,0 +1,11 @@ +/* Precompiled header (BUILD.md) for the libobs unity group + * libobs.ported-source-draw-plan. obs-source-draw-plan.c needs its + * declarations (obs-source-draw-plan.h, which brings + * obs-source-draw-technique.h) and the gs_color_space enum (obs.h), 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 "obs.h" +#include "obs-source-draw-plan.h" diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index 0bebf53bdc..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-draw-plan.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-draw-plan+.h.c b/libobs/build/libobs.ported-source-draw-plan+.h.c new file mode 100644 index 0000000000..7ad7c12f48 --- /dev/null +++ b/libobs/build/libobs.ported-source-draw-plan+.h.c @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): ported obs-source-draw-plan.c. */ + +#include "libobs/build/libobs-ported-source-draw-plan.pch.h" + +#include "libobs/obs-source-draw-plan.c.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index 6b593246f9..fd804ae2e8 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -192,6 +192,7 @@ "build/libobs.ported-source-conversion-plan+.h.c", "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-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 bd3093af72..8be12efa39 100644 --- a/libobs/cmake/rust-exports.map +++ b/libobs/cmake/rust-exports.map @@ -185,6 +185,16 @@ obs_source_deinterlace_pacing_half_duration; obs_source_deinterlace_pacing_buffered_offset; obs_source_deinterlace_pacing_reanchor; + /* obs-source-draw-plan.c. obs-source.c calls them inside libobs. Not + * EXPORT. */ + obs_source_draw_plan_rotation_x; + obs_source_draw_plan_rotation_y; + obs_source_draw_plan_async_linear_srgb; + obs_source_draw_plan_async_nonlinear_alpha; + obs_source_draw_plan_technique; + obs_source_draw_plan_reads_white_level; + obs_source_draw_plan_multiplier; + obs_source_draw_plan_convert; /* 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 07e11f8505..d3dbcda4d8 100644 --- a/libobs/cmake/rust-hidden.txt +++ b/libobs/cmake/rust-hidden.txt @@ -194,6 +194,18 @@ obs_source_deinterlace_pacing_waiting_frame_ts obs_source_deinterlace_pacing_half_duration obs_source_deinterlace_pacing_buffered_offset obs_source_deinterlace_pacing_reanchor +# obs-source-draw-plan.c: rotate_async_video(), +# obs_source_render_async_video() and source_render() in obs-source.c call +# them inside libobs. Declared in obs-source-draw-plan.h without EXPORT, so the +# shims stay hidden the same way. +obs_source_draw_plan_rotation_x +obs_source_draw_plan_rotation_y +obs_source_draw_plan_async_linear_srgb +obs_source_draw_plan_async_nonlinear_alpha +obs_source_draw_plan_technique +obs_source_draw_plan_reads_white_level +obs_source_draw_plan_multiplier +obs_source_draw_plan_convert # 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 5fe596163f..e270d0279e 100644 --- a/libobs/cmake/rust-unexports-macos.txt +++ b/libobs/cmake/rust-unexports-macos.txt @@ -161,6 +161,15 @@ _obs_source_deinterlace_pacing_waiting_frame_ts _obs_source_deinterlace_pacing_half_duration _obs_source_deinterlace_pacing_buffered_offset _obs_source_deinterlace_pacing_reanchor +# obs-source-draw-plan.c: obs-source.c calls them inside libobs. Not EXPORT. +_obs_source_draw_plan_rotation_x +_obs_source_draw_plan_rotation_y +_obs_source_draw_plan_async_linear_srgb +_obs_source_draw_plan_async_nonlinear_alpha +_obs_source_draw_plan_technique +_obs_source_draw_plan_reads_white_level +_obs_source_draw_plan_multiplier +_obs_source_draw_plan_convert # 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-draw-plan.c.h b/libobs/obs-source-draw-plan.c.h index 24ce8ec2e5..7b13b52c59 100644 --- a/libobs/obs-source-draw-plan.c.h +++ b/libobs/obs-source-draw-plan.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-draw-plan.pch.h" /* The source draw decisions of obs-source.c, moved from rotate_async_video(), * obs_source_render_async_video() and source_render(): the decisions, on diff --git a/rust/obs-source/src/draw_plan.rs b/rust/obs-source/src/draw_plan.rs index d03866a055..f3db3638bf 100644 --- a/rust/obs-source/src/draw_plan.rs +++ b/rust/obs-source/src/draw_plan.rs @@ -18,6 +18,15 @@ //! `gs_get_format_from_space()` and the texrenders. It passes their results //! in and acts on what comes back. Nothing here keeps state, takes a lock or //! calls libobs. +//! +//! Colour spaces are the C's `int`: a value outside the enum takes the paths +//! it takes in the C. The rotation is the C's `long` ([`c_long`]), so a value +//! outside 0-359 is matched as the C's `switch` matches it on every target. +//! The multiplier is one `f32` division, the same IEEE operation as the C's, +//! so it matches bit for bit, NaNs and infinities included, and the size +//! converts to `f32` rounding to nearest even, as `(float)uint32_t` does. +//! Characterized, not endorsed (`test_source_draw_plan_steps`), and kept: a +//! white level of 0 gives a multiplier of 0 into scRGB and +inf out of it. #![deny( clippy::arithmetic_side_effects, @@ -26,6 +35,7 @@ clippy::unwrap_used, clippy::expect_used, clippy::panic, + clippy::todo, clippy::unimplemented, clippy::unreachable )] @@ -35,51 +45,154 @@ use std::os::raw::c_long; pub use crate::draw_technique::{ GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, GS_CS_SRGB_16F, Technique, }; +use crate::draw_technique::{SCRGB_WHITE, sdr}; + +/// `(float)size`: the nearest `f32`, ties to even, as the C's conversion. +/// No `From` for `f32` exists because sizes past 2^24 round; the C +/// rounds them the same way. +#[allow(clippy::as_conversions)] +fn size_f32(size: u32) -> f32 { + size as f32 +} -/// `obs_source_draw_plan_rotation_x()`. +/// `obs_source_draw_plan_rotation_x()`: the x of the translate before an +/// async frame of `width` x `height` is rotated by `rotation` degrees: +/// `height` for 270, `width` for 180, 0 for any other rotation +/// (`obs_source_set_async_rotation()` keeps it in 0-359, #2136). pub fn rotation_x(rotation: c_long, width: u32, height: u32) -> f32 { - let _ = (rotation, width, height); - todo!() + match rotation { + 270 => size_f32(height), + 180 => size_f32(width), + _ => 0.0, + } } -/// `obs_source_draw_plan_rotation_y()`. +/// `obs_source_draw_plan_rotation_y()`: the y of that translate: `width` +/// for 90, `height` for 180, 0 for any other rotation. pub fn rotation_y(rotation: c_long, width: u32, height: u32) -> f32 { - let _ = (rotation, width, height); - todo!() + match rotation { + 90 => size_f32(width), + 180 => size_f32(height), + _ => 0.0, + } } -/// `obs_source_draw_plan_async_linear_srgb()`. +/// Whether a space is [`GS_CS_709_EXTENDED`] or [`GS_CS_709_SCRGB`]. +fn hdr(space: i32) -> bool { + space == GS_CS_709_EXTENDED || space == GS_CS_709_SCRGB +} + +/// `obs_source_draw_plan_async_linear_srgb()`: whether +/// `obs_source_render_async_video()` draws with linear sRGB: +/// `gs_linear_srgb` (`gs_get_linear_srgb()`, always read), or a +/// [`GS_CS_SRGB`] source into any current space but [`GS_CS_SRGB`] +/// (792f8473e), or an HDR source into SDR, which is tonemapped (080095786). pub fn async_linear_srgb(source_space: i32, current_space: i32, gs_linear_srgb: bool) -> bool { - let _ = (source_space, current_space, gs_linear_srgb); - todo!() + gs_linear_srgb + || (source_space == GS_CS_SRGB && current_space != GS_CS_SRGB) + || (hdr(source_space) && sdr(current_space)) } -/// `obs_source_draw_plan_async_nonlinear_alpha()`. +/// `obs_source_draw_plan_async_nonlinear_alpha()`: whether the async +/// frame blends with non-linear alpha: a [`GS_CS_SRGB`] source drawn with +/// linear sRGB whose alpha is not linear (`async_linear_alpha`). pub fn async_nonlinear_alpha(source_space: i32, linear_srgb: bool, linear_alpha: bool) -> bool { - let _ = (source_space, linear_srgb, linear_alpha); - todo!() + source_space == GS_CS_SRGB && linear_srgb && !linear_alpha } -/// `obs_source_draw_plan_technique()`. +/// `obs_source_draw_plan_technique()`: the technique of an async frame, and +/// of `source_render()`'s conversion (`nonlinear_alpha` false). SDR into +/// [`GS_CS_709_SCRGB`] multiplies; a [`GS_CS_SRGB`] source with non-linear +/// alpha draws with DrawNonlinearAlpha into SDR or [`GS_CS_709_EXTENDED`] +/// and DrawNonlinearAlphaMultiply into [`GS_CS_709_SCRGB`]; +/// [`GS_CS_709_EXTENDED`] tonemaps into SDR and multiplies into +/// [`GS_CS_709_SCRGB`]; [`GS_CS_709_SCRGB`] multiplies and tonemaps into SDR +/// and multiplies into [`GS_CS_709_EXTENDED`]; Draw for any other pair, a +/// space outside the enum included. pub fn technique(source_space: i32, current_space: i32, nonlinear_alpha: bool) -> Technique { - let _ = (source_space, current_space, nonlinear_alpha); - todo!() + let nonlinear_alpha = nonlinear_alpha && source_space == GS_CS_SRGB; + if sdr(source_space) { + if current_space == GS_CS_709_SCRGB { + return if nonlinear_alpha { + Technique::DrawNonlinearAlphaMultiply + } else { + Technique::DrawMultiply + }; + } + if nonlinear_alpha && (sdr(current_space) || current_space == GS_CS_709_EXTENDED) { + return Technique::DrawNonlinearAlpha; + } + } else if source_space == GS_CS_709_EXTENDED { + if sdr(current_space) { + return Technique::DrawTonemap; + } + if current_space == GS_CS_709_SCRGB { + return Technique::DrawMultiply; + } + } else if source_space == GS_CS_709_SCRGB { + if sdr(current_space) { + return Technique::DrawMultiplyTonemap; + } + if current_space == GS_CS_709_EXTENDED { + return Technique::DrawMultiply; + } + } + Technique::Draw } -/// `obs_source_draw_plan_reads_white_level()`. +/// Into scRGB from SDR or [`GS_CS_709_EXTENDED`]. +fn into_scrgb(source_space: i32, current_space: i32) -> bool { + current_space == GS_CS_709_SCRGB && (sdr(source_space) || source_space == GS_CS_709_EXTENDED) +} + +/// From scRGB into SDR or [`GS_CS_709_EXTENDED`]. +fn from_scrgb(source_space: i32, current_space: i32) -> bool { + source_space == GS_CS_709_SCRGB && (sdr(current_space) || current_space == GS_CS_709_EXTENDED) +} + +/// `obs_source_draw_plan_reads_white_level()`: whether the async draw or +/// `source_render()` reads `obs_get_video_sdr_white_level()` for the pair: +/// into or out of scRGB. pub fn reads_white_level(source_space: i32, current_space: i32) -> bool { - let _ = (source_space, current_space); - todo!() + into_scrgb(source_space, current_space) || from_scrgb(source_space, current_space) } -/// `obs_source_draw_plan_multiplier()`. +/// `obs_source_draw_plan_multiplier()`: `sdr_white_level / 80.0` into +/// [`GS_CS_709_SCRGB`] from SDR or [`GS_CS_709_EXTENDED`], +/// `80.0 / sdr_white_level` from [`GS_CS_709_SCRGB`] into SDR or +/// [`GS_CS_709_EXTENDED`], 1.0 for any other pair. One `f32` division, as +/// in the C: 0 gives +inf from scRGB, -0 gives -inf, a NaN stays a NaN. pub fn multiplier(source_space: i32, current_space: i32, sdr_white_level: f32) -> f32 { - let _ = (source_space, current_space, sdr_white_level); - todo!() + if into_scrgb(source_space, current_space) { + sdr_white_level / SCRGB_WHITE + } else if from_scrgb(source_space, current_space) { + SCRGB_WHITE / sdr_white_level + } else { + 1.0 + } +} + +/// The class of a space in the enum: SDR, [`GS_CS_709_EXTENDED`] or +/// [`GS_CS_709_SCRGB`]; `None` outside the enum. +fn class(space: i32) -> Option { + if sdr(space) { + Some(0) + } else if space == GS_CS_709_EXTENDED { + Some(1) + } else if space == GS_CS_709_SCRGB { + Some(2) + } else { + None + } } -/// `obs_source_draw_plan_convert()`. +/// `obs_source_draw_plan_convert()`: whether `source_render()` draws the +/// source into `color_space_texrender` and converts it: the two spaces are +/// in the enum and in different classes (SDR, [`GS_CS_709_EXTENDED`], +/// [`GS_CS_709_SCRGB`]). pub fn convert(source_space: i32, current_space: i32) -> bool { - let _ = (source_space, current_space); - todo!() + match (class(source_space), class(current_space)) { + (Some(source), Some(current)) => source != current, + _ => false, + } } diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index ee050e213c..463ca288d4 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -7,6 +7,7 @@ pub mod source_color_space; pub mod source_conversion_plan; pub mod source_deinterlace_draw; pub mod source_deinterlace_pacing; +pub mod source_draw_plan; pub mod source_draw_technique; pub mod source_ptm_gate; pub mod source_size; diff --git a/rust/obs-source/src/ffi/source_draw_plan.rs b/rust/obs-source/src/ffi/source_draw_plan.rs new file mode 100644 index 0000000000..081c49b5ea --- /dev/null +++ b/rust/obs-source/src/ffi/source_draw_plan.rs @@ -0,0 +1,105 @@ +//! C ABI for `libobs/obs-source-draw-plan.c.h`, the source draw decisions, +//! declared in `libobs/obs-source-draw-plan.h` without `EXPORT`. +//! +//! `rotate_async_video()`, `obs_source_render_async_video()` and +//! `source_render()` in `obs-source.c.h` (still C) make every `gs_*` call in +//! its order, read the colour spaces, the graphics linear sRGB flag, the +//! white level and the source's fields, pass the values in and act on what +//! comes back. So `struct obs_source` is never mirrored, and these shims take +//! plain values, touch no memory, take no lock and call nothing in libobs. +//! All eight stay hidden (`rust-hidden.txt`). +//! +//! The rotation is the C's `long` ([`c_long`]: 64 bits on Linux and macOS, +//! 32 on Windows), passed through unconverted. The technique comes back as +//! `enum obs_source_draw_technique`, shared with the deinterlace draw shims. +//! +//! No intentional differences from C: every input is a value, so there is +//! no pointer to check. + +use std::os::raw::{c_int, c_long}; + +use super::source_draw_technique::obs_source_draw_technique; +use crate::draw_plan as core; + +/// `float obs_source_draw_plan_rotation_x(long rotation, uint32_t width, +/// uint32_t height)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_rotation_x( + rotation: c_long, + width: u32, + height: u32, +) -> f32 { + core::rotation_x(rotation, width, height) +} + +/// `float obs_source_draw_plan_rotation_y(long rotation, uint32_t width, +/// uint32_t height)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_rotation_y( + rotation: c_long, + width: u32, + height: u32, +) -> f32 { + core::rotation_y(rotation, width, height) +} + +/// `bool obs_source_draw_plan_async_linear_srgb(int source_space, int +/// current_space, bool gs_linear_srgb)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_async_linear_srgb( + source_space: c_int, + current_space: c_int, + gs_linear_srgb: bool, +) -> bool { + core::async_linear_srgb(source_space, current_space, gs_linear_srgb) +} + +/// `bool obs_source_draw_plan_async_nonlinear_alpha(int source_space, bool +/// linear_srgb, bool linear_alpha)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_async_nonlinear_alpha( + source_space: c_int, + linear_srgb: bool, + linear_alpha: bool, +) -> bool { + core::async_nonlinear_alpha(source_space, linear_srgb, linear_alpha) +} + +/// `enum obs_source_draw_technique obs_source_draw_plan_technique(int +/// source_space, int current_space, bool nonlinear_alpha)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_technique( + source_space: c_int, + current_space: c_int, + nonlinear_alpha: bool, +) -> obs_source_draw_technique { + core::technique(source_space, current_space, nonlinear_alpha).value() +} + +/// `bool obs_source_draw_plan_reads_white_level(int source_space, int +/// current_space)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_reads_white_level( + source_space: c_int, + current_space: c_int, +) -> bool { + core::reads_white_level(source_space, current_space) +} + +/// `float obs_source_draw_plan_multiplier(int source_space, int +/// current_space, float sdr_white_level)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_multiplier( + source_space: c_int, + current_space: c_int, + sdr_white_level: f32, +) -> f32 { + core::multiplier(source_space, current_space, sdr_white_level) +} + +/// `bool obs_source_draw_plan_convert(int source_space, int +/// current_space)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_draw_plan_convert(source_space: c_int, current_space: c_int) -> bool { + core::convert(source_space, current_space) +} diff --git a/test/cmocka/tests.d/test_source_draw_plan_steps.cmake b/test/cmocka/tests.d/test_source_draw_plan_steps.cmake index 0cf0f14d11..a569c77c15 100644 --- a/test/cmocka/tests.d/test_source_draw_plan_steps.cmake +++ b/test/cmocka/tests.d/test_source_draw_plan_steps.cmake @@ -11,3 +11,24 @@ # them. test_async_rotation_normalize, test_filter_color_space and # test_source_color_space_steps 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 eight hidden functions and the header's +# obs_source_draw_technique_name(), and includes their header and obs.h (for +# the enum gs_color_space values; obs.h pulls SIMDe and obsconfig.h, hence +# libobs's include directories), 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_draw_plan_steps_rust test_source_draw_plan_steps.c) + target_include_directories( + test_source_draw_plan_steps_rust + PRIVATE "${CMAKE_SOURCE_DIR}/libobs" $ ${CMOCKA_INCLUDE_DIR} + ) + target_link_libraries(test_source_draw_plan_steps_rust PRIVATE libobs_rust-static ${CMOCKA_LIBRARIES}) + if(NOT MSVC) + target_link_libraries(test_source_draw_plan_steps_rust PRIVATE m) + endif() + add_dependencies(cmocka-tests test_source_draw_plan_steps_rust) + add_test(test_source_draw_plan_steps_rust ${CMAKE_CURRENT_BINARY_DIR}/test_source_draw_plan_steps_rust) +endif() From df31be29241df3744807e3fac72469dc20d1675f Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:46:36 -0400 Subject: [PATCH 5/6] obs-source: Check the source draw plan port against the C Oracle (oracle/source_draw_plan.c): the original obs-source-draw-plan.c.h, unmodified, with its eight functions renamed to oracle_*, plus every enum value the port uses (the four colour spaces and the six techniques), the enum, int, long, bool and float sizes, and obs_source_draw_technique_name() for a technique value. No function shares its name with an enum tag, so the size table names the tags as they are. The bool results are declared as u8 on the Rust side, the byte the C returns. Tier 3 (tests/source/draw_plan_parity.rs, 10 tests). Every function takes and returns plain values, so each run compares the bytes of the two results: the bools as bytes, the technique as its value, the floats as their f32 bits. - the values and sizes against the C headers (long is c_long on every target), and the name C maps each technique the port returns to; - the rotation translate for every angle from -70000 to 70000, a stride over the whole c_long range, its ends and, where long is 64 bits, 90, 180 and 270 plus multiples of 2^32, at 6 sizes (non-square, 0, past 2^24, u32::MAX); every size from 0 to 4096 and +-2000 around 2^24, 2^25, 2^31 and u32::MAX at 0, 90, 180, 270 and 45; - space pairs: both linear flags, both non-linear alpha flags, the white level read, the conversion and the multiplier at 300 nits for every pair of 649 spaces (-320 to 320 and four near each end of i32): 421,201 pairs; every pair of -3 to 6 and the ends of i32 at 26 special white levels (zeros, subnormals, the normal ends, 80 to 480 nits, infinities, quiet and signalling NaNs of both signs with payloads); the non-linear alpha rule for every space and flag; - the decisions of obs_source_render_async_video() (with rotate_async_video()) and source_render() in their order, one copy on each side: the linear flag, the blend, the technique, the white level read, the multiplier only when it is read, and the translate only for a rotation other than 0; 9 x 9 spaces x both flags x both alpha flags x 26 levels x 7 rotations x 2 sizes (235,872 draws); - proptests (20,000 cases each): the rotation over any c_long and any sizes (a third at 90, 180 or 270); every decision over any space and any white level bit pattern (never a float range); both callers' decisions over all of it at once. No input reaches an integer division or a 128-bit multiply on either side (the only division is the f32 multiplier, which cannot trap), so the MSVC x64 _udiv128 and INT_MIN / -1 faults cannot come up and no input is skipped there. Tier 1 and the parity module passed 10 runs in a row with fresh seeds (34/34 each). No regression file came up; the ones the todo!() stub and the mutants left were deleted. soldr cargo clippy --target x86_64-pc-windows-msvc -p obs-source -p obs-data -p obs-c-oracle --all-targets -- -D warnings is clean. Mutation check (once, not committed): 36 mutants, Tier 1 (24 tests) and Tier 3 (10), failures per mutant. | Mutant | Tier 1 | Tier 3 | |---|---|---| | rotation: 270 x is width / 180 x is height / 90 y is height / 180 y not moved / -90 as 270 | 5 / 5 / 5 / 5 / 2 | 5 / 5 / 5 / 5 / 2 | | size truncated to even | 4 | 5 | | linear: SDR into non-SRGB dropped (pre-792f8473e) / tonemap dropped (pre-080095786) / SRGB into SRGB_16F not linear / HDR is 709_EXTENDED only | 6 / 4 / 4 / 4 | 5 / 5 / 5 / 5 | | non-linear alpha: linear ignored / SRGB_16F too | 4 / 4 | 3 / 3 | | technique: flag not masked to SRGB / no NA into 709_EXTENDED / 709_EXTENDED into SDR drawn / scRGB into 709_EXTENDED drawn / NAM is DrawMultiply | 2 / 4 / 5 / 5 / 4 | 3 / 5 / 5 / 5 / 5 | | into scRGB not from 709_EXTENDED / out of scRGB not into 709_EXTENDED | 8 / 8 | 5 / 5 | | multiplier: into scRGB inverted / 80.00001 / times 1/80 | 7 / 7 / 3 | 5 / 5 / 4 | | white level read into scRGB only | 6 | 5 | | convert: SRGB_16F its own class / outside the enum converts / same class converts | 4 / 3 / 4 | 5 / 5 / 5 | | Technique::value: NA and NAM swapped / tonemap numbers swapped | 1 / 1 | 6 / 6 | | shim: rotation width and height swapped / technique ignores the flag / multiplier spaces swapped / graphics flag negated | 0 / 0 / 0 / 0 | 5 / 5 / 5 / 5 | | equivalent: multiplier in f64 and rounded / size through f64 / shim convert spaces swapped / white level read as "multiplier at 300 is not 1" | 0 / 0 / 0 / 0 | 0 / 0 / 0 / 0 | 32 are killed by Tier 3. The other four are equivalent: one f32 division done in f64 and rounded is correctly rounded the same; a u32 is exact in f64, so one rounding to f32 is the same; convert() is symmetric; and every pair that multiplies gives a multiplier other than 1 at 300 nits. Tier 1 does not reach the shims; test_source_draw_plan_steps_rust and Tier 3 do. No intentional differences: there is no pointer to check. Refs #2187 --- rust/obs-c-oracle/oracle/source_draw_plan.c | 64 ++ rust/obs-c-oracle/src/lib.rs | 1 + rust/obs-c-oracle/src/source_draw_plan.rs | 73 +++ .../tests/source/draw_plan_parity.rs | 564 ++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 5 files changed, 703 insertions(+) create mode 100644 rust/obs-c-oracle/oracle/source_draw_plan.c create mode 100644 rust/obs-c-oracle/src/source_draw_plan.rs create mode 100644 rust/obs-source/tests/source/draw_plan_parity.rs diff --git a/rust/obs-c-oracle/oracle/source_draw_plan.c b/rust/obs-c-oracle/oracle/source_draw_plan.c new file mode 100644 index 0000000000..9433bb2e53 --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_draw_plan.c @@ -0,0 +1,64 @@ +/* Test-only oracle: the original libobs/obs-source-draw-plan.c.h, + * unmodified, with its eight functions renamed to oracle_* so they can link + * next to the Rust shims. */ +#include +#include + +#define obs_source_draw_plan_rotation_x oracle_obs_source_draw_plan_rotation_x +#define obs_source_draw_plan_rotation_y oracle_obs_source_draw_plan_rotation_y +#define obs_source_draw_plan_async_linear_srgb oracle_obs_source_draw_plan_async_linear_srgb +#define obs_source_draw_plan_async_nonlinear_alpha oracle_obs_source_draw_plan_async_nonlinear_alpha +#define obs_source_draw_plan_technique oracle_obs_source_draw_plan_technique +#define obs_source_draw_plan_reads_white_level oracle_obs_source_draw_plan_reads_white_level +#define obs_source_draw_plan_multiplier oracle_obs_source_draw_plan_multiplier +#define obs_source_draw_plan_convert oracle_obs_source_draw_plan_convert + +#include "obs-source-draw-plan.c.h" + +#undef obs_source_draw_plan_convert +#undef obs_source_draw_plan_multiplier +#undef obs_source_draw_plan_reads_white_level +#undef obs_source_draw_plan_technique +#undef obs_source_draw_plan_async_nonlinear_alpha +#undef obs_source_draw_plan_async_linear_srgb +#undef obs_source_draw_plan_rotation_y +#undef obs_source_draw_plan_rotation_x + +/* The technique's name as obs-source.c maps it, for a value the port + * returns. */ +const char *oracle_source_draw_plan_technique_name(int technique); + +const char *oracle_source_draw_plan_technique_name(int technique) +{ + return obs_source_draw_technique_name((enum obs_source_draw_technique)technique); +} + +/* The enum values and sizes the port relies on, in the order of + * rust/obs-c-oracle/src/source_draw_plan.rs; INT64_MIN past the end. */ +int64_t oracle_source_draw_plan_value(size_t which); + +int64_t oracle_source_draw_plan_value(size_t which) +{ + const int64_t values[] = { + /* enum gs_color_space */ + GS_CS_SRGB, + GS_CS_SRGB_16F, + GS_CS_709_EXTENDED, + GS_CS_709_SCRGB, + /* enum obs_source_draw_technique */ + OBS_SOURCE_DRAW_TECH_DRAW, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY, + OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP, + OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP, + OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA, + OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY, + /* sizes */ + sizeof(enum gs_color_space), + sizeof(enum obs_source_draw_technique), + sizeof(int), + sizeof(long), + sizeof(bool), + sizeof(float), + }; + return which < sizeof(values) / sizeof(values[0]) ? values[which] : INT64_MIN; +} diff --git a/rust/obs-c-oracle/src/lib.rs b/rust/obs-c-oracle/src/lib.rs index faf9f481a8..c26fbe1e0a 100644 --- a/rust/obs-c-oracle/src/lib.rs +++ b/rust/obs-c-oracle/src/lib.rs @@ -134,6 +134,7 @@ pub mod source_color_space; pub mod source_conversion_plan; pub mod source_deinterlace_draw; pub mod source_deinterlace_pacing; +pub mod source_draw_plan; pub mod source_load; pub mod source_profiler; pub mod source_ptm_gate; diff --git a/rust/obs-c-oracle/src/source_draw_plan.rs b/rust/obs-c-oracle/src/source_draw_plan.rs new file mode 100644 index 0000000000..3c514d4439 --- /dev/null +++ b/rust/obs-c-oracle/src/source_draw_plan.rs @@ -0,0 +1,73 @@ +//! `oracle_*` build of `libobs/obs-source-draw-plan.c.h` +//! (oracle/source_draw_plan.c): the source draw decisions, renamed. The +//! `bool` results are declared as `u8`, the byte the C returns, so a parity +//! test compares it without assuming it is 0 or 1; the technique as `u32`, +//! its value. + +use std::os::raw::{c_char, c_int, c_long}; + +/// Index of each entry in `oracle_source_draw_plan_value`, in the C's +/// order. +pub const VALUE_NAMES: [&str; 16] = [ + "GS_CS_SRGB", + "GS_CS_SRGB_16F", + "GS_CS_709_EXTENDED", + "GS_CS_709_SCRGB", + "OBS_SOURCE_DRAW_TECH_DRAW", + "OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY", + "OBS_SOURCE_DRAW_TECH_DRAW_TONEMAP", + "OBS_SOURCE_DRAW_TECH_DRAW_MULTIPLY_TONEMAP", + "OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA", + "OBS_SOURCE_DRAW_TECH_DRAW_NONLINEAR_ALPHA_MULTIPLY", + "sizeof(enum gs_color_space)", + "sizeof(enum obs_source_draw_technique)", + "sizeof(int)", + "sizeof(long)", + "sizeof(bool)", + "sizeof(float)", +]; + +unsafe extern "C" { + /// `float obs_source_draw_plan_rotation_x(long rotation, uint32_t width, + /// uint32_t height)`. + pub fn oracle_obs_source_draw_plan_rotation_x(rotation: c_long, width: u32, height: u32) + -> f32; + /// `float obs_source_draw_plan_rotation_y(...)`. + pub fn oracle_obs_source_draw_plan_rotation_y(rotation: c_long, width: u32, height: u32) + -> f32; + /// `bool obs_source_draw_plan_async_linear_srgb(...)`. + pub fn oracle_obs_source_draw_plan_async_linear_srgb( + source_space: c_int, + current_space: c_int, + gs_linear_srgb: bool, + ) -> u8; + /// `bool obs_source_draw_plan_async_nonlinear_alpha(...)`. + pub fn oracle_obs_source_draw_plan_async_nonlinear_alpha( + source_space: c_int, + linear_srgb: bool, + linear_alpha: bool, + ) -> u8; + /// `enum obs_source_draw_technique obs_source_draw_plan_technique(...)`. + pub fn oracle_obs_source_draw_plan_technique( + source_space: c_int, + current_space: c_int, + nonlinear_alpha: bool, + ) -> u32; + /// `bool obs_source_draw_plan_reads_white_level(...)`. + pub fn oracle_obs_source_draw_plan_reads_white_level( + source_space: c_int, + current_space: c_int, + ) -> u8; + /// `float obs_source_draw_plan_multiplier(...)`. + pub fn oracle_obs_source_draw_plan_multiplier( + source_space: c_int, + current_space: c_int, + sdr_white_level: f32, + ) -> f32; + /// `bool obs_source_draw_plan_convert(...)`. + pub fn oracle_obs_source_draw_plan_convert(source_space: c_int, current_space: c_int) -> u8; + /// `obs_source_draw_technique_name()` for a technique value. + pub fn oracle_source_draw_plan_technique_name(technique: c_int) -> *const c_char; + /// The entry named `VALUE_NAMES[which]`; `i64::MIN` out of range. + pub fn oracle_source_draw_plan_value(which: usize) -> i64; +} diff --git a/rust/obs-source/tests/source/draw_plan_parity.rs b/rust/obs-source/tests/source/draw_plan_parity.rs new file mode 100644 index 0000000000..b8cdeb9931 --- /dev/null +++ b/rust/obs-source/tests/source/draw_plan_parity.rs @@ -0,0 +1,564 @@ +//! Tier 3: the `obs_source_draw_plan_*` shims against the original C +//! (`obs-c-oracle`, `oracle/source_draw_plan.c`: the unmodified +//! `libobs/obs-source-draw-plan.c.h` with its functions renamed). +//! +//! Every function takes and returns plain values, so a run compares the +//! bytes of the two results: the C's `bool` as the byte it returns, the +//! technique as its value, the `float` results as their bits. +//! +//! Runs: +//! - the enum values and the type sizes against the C headers (`long` is +//! [`c_long`] on every target), and the technique names; +//! - the rotation translate for every angle from -70000 to 70000, every +//! 65521st `c_long` over the whole range, the ends and (where `long` is 64 +//! bits) 90, 180 and 270 plus multiples of 2^32, at 6 sizes; and every +//! size from 0 to 4096 and around 2^24, 2^25, 2^31 and `u32::MAX` at 90, +//! 180 and 270; +//! - space pairs: every decision (both graphics linear flags, both +//! non-linear alpha flags, the white level read, the conversion and the +//! multiplier at 300 nits) for every pair of 649 spaces (-320 to 320 and +//! four near each end of `i32`); every pair of -3 to 6 and the ends at 26 +//! special white levels; the non-linear alpha rule for every space and +//! flag; +//! - the decisions of `obs_source_render_async_video()` and +//! `source_render()` in their order (the linear flag, the blend, the +//! technique, the white level read and the multiplier only when it is +//! read), one copy on each side; +//! - proptests (20,000 cases each): the rotation over any `c_long` and any +//! sizes; every decision over any space and any white level bit pattern +//! (never a float range); both callers' decisions over all of it at once. +//! +//! No input reaches an integer division or a 128-bit multiply on either +//! side (the only division is the `f32` multiplier, which cannot trap), so +//! no input is skipped on MSVC x64. No intentional differences: there is no +//! pointer to check. + +use std::ffi::CStr; +use std::os::raw::{c_int, c_long}; + +use obs_c_oracle::source_draw_plan as oracle; +use obs_source::draw_plan::{ + GS_CS_709_EXTENDED, GS_CS_709_SCRGB, GS_CS_SRGB, GS_CS_SRGB_16F, Technique, +}; +use obs_source::ffi::source_draw_plan as shim; +use proptest::prelude::*; + +const TECHNIQUES: [Technique; 6] = [ + Technique::Draw, + Technique::DrawMultiply, + Technique::DrawTonemap, + Technique::DrawMultiplyTonemap, + Technique::DrawNonlinearAlpha, + Technique::DrawNonlinearAlphaMultiply, +]; + +#[test] +fn values_match_the_c_headers() { + let rust: [i64; 16] = [ + GS_CS_SRGB.into(), + GS_CS_SRGB_16F.into(), + GS_CS_709_EXTENDED.into(), + GS_CS_709_SCRGB.into(), + TECHNIQUES[0].value().into(), + TECHNIQUES[1].value().into(), + TECHNIQUES[2].value().into(), + TECHNIQUES[3].value().into(), + TECHNIQUES[4].value().into(), + TECHNIQUES[5].value().into(), + 4, + 4, + 4, + i64::try_from(std::mem::size_of::()).unwrap(), + 1, + 4, + ]; + for (which, (name, value)) in oracle::VALUE_NAMES.iter().zip(rust).enumerate() { + // SAFETY: a pure lookup. + let c = unsafe { oracle::oracle_source_draw_plan_value(which) }; + assert_eq!(value, c, "{name}"); + } + // SAFETY: a pure lookup, one past the end. + let past = unsafe { oracle::oracle_source_draw_plan_value(oracle::VALUE_NAMES.len()) }; + assert_eq!(past, i64::MIN, "the list has as many values as names"); + // The names C maps each value the port returns to are the port's. + for technique in TECHNIQUES { + let value = c_int::try_from(technique.value()).unwrap(); + // SAFETY: returns a static string for any value. + let name = unsafe { CStr::from_ptr(oracle::oracle_source_draw_plan_technique_name(value)) }; + assert_eq!(name.to_str().unwrap(), technique.name()); + } +} + +// -------------------------------------------------------------------------- +// One call on each side + +fn c_rotation(rotation: c_long, w: u32, h: u32) -> (u32, u32) { + // SAFETY: plain values. + unsafe { + ( + oracle::oracle_obs_source_draw_plan_rotation_x(rotation, w, h).to_bits(), + oracle::oracle_obs_source_draw_plan_rotation_y(rotation, w, h).to_bits(), + ) + } +} + +fn rs_rotation(rotation: c_long, w: u32, h: u32) -> (u32, u32) { + ( + shim::obs_source_draw_plan_rotation_x(rotation, w, h).to_bits(), + shim::obs_source_draw_plan_rotation_y(rotation, w, h).to_bits(), + ) +} + +fn check_rotation(rotation: c_long, w: u32, h: u32) { + assert_eq!( + rs_rotation(rotation, w, h), + c_rotation(rotation, w, h), + "rotation {rotation} at {w}x{h}" + ); +} + +fn c_linear(s: c_int, c: c_int, gs: bool) -> u8 { + // SAFETY: plain values. + unsafe { oracle::oracle_obs_source_draw_plan_async_linear_srgb(s, c, gs) } +} + +fn c_nonlinear(s: c_int, linear: bool, alpha: bool) -> u8 { + // SAFETY: plain values. + unsafe { oracle::oracle_obs_source_draw_plan_async_nonlinear_alpha(s, linear, alpha) } +} + +fn c_technique(s: c_int, c: c_int, na: bool) -> u32 { + // SAFETY: plain values. + unsafe { oracle::oracle_obs_source_draw_plan_technique(s, c, na) } +} + +fn c_reads_white(s: c_int, c: c_int) -> u8 { + // SAFETY: plain values. + unsafe { oracle::oracle_obs_source_draw_plan_reads_white_level(s, c) } +} + +fn c_multiplier(s: c_int, c: c_int, w: f32) -> u32 { + // SAFETY: plain values. + unsafe { oracle::oracle_obs_source_draw_plan_multiplier(s, c, w) }.to_bits() +} + +fn c_convert(s: c_int, c: c_int) -> u8 { + // SAFETY: plain values. + unsafe { oracle::oracle_obs_source_draw_plan_convert(s, c) } +} + +fn check_pair(s: c_int, c: c_int, w: f32) { + for gs in [false, true] { + assert_eq!( + u8::from(shim::obs_source_draw_plan_async_linear_srgb(s, c, gs)), + c_linear(s, c, gs), + "linear {s} into {c}, flag {gs}" + ); + } + for na in [false, true] { + assert_eq!( + shim::obs_source_draw_plan_technique(s, c, na), + c_technique(s, c, na), + "technique {s} into {c}, non-linear {na}" + ); + } + assert_eq!( + u8::from(shim::obs_source_draw_plan_reads_white_level(s, c)), + c_reads_white(s, c), + "reads white {s} into {c}" + ); + assert_eq!( + u8::from(shim::obs_source_draw_plan_convert(s, c)), + c_convert(s, c), + "convert {s} into {c}" + ); + assert_eq!( + shim::obs_source_draw_plan_multiplier(s, c, w).to_bits(), + c_multiplier(s, c, w), + "multiplier {s} into {c} at {:#010x}", + w.to_bits() + ); +} + +// -------------------------------------------------------------------------- +// Inputs + +fn spaces() -> Vec { + let mut v: Vec = (-320..=320).collect(); + v.extend([ + c_int::MIN, + c_int::MIN + 1, + c_int::MIN + 2, + c_int::MIN + 3, + c_int::MAX - 3, + c_int::MAX - 2, + c_int::MAX - 1, + c_int::MAX, + ]); + v +} + +fn small_spaces() -> Vec { + let mut v: Vec = (-3..=6).collect(); + v.extend([c_int::MIN, c_int::MAX]); + v +} + +/// Special `f32` white levels: zeros, subnormals, the normal ends, 80 to 480 +/// nits, infinities, quiet and signalling NaNs of both signs with payloads. +const WHITES: [u32; 26] = [ + 0x0000_0000, + 0x8000_0000, + 0x0000_0001, + 0x8000_0001, + 0x007f_ffff, + 0x0080_0000, + 0x7f7f_ffff, + 0xff7f_ffff, + 0x3f80_0000, + 0x42a0_0000, + 0x4296_0000, + 0x434b_0000, + 0x4396_0000, + 0x43f0_0000, + 0x42a0_0001, + 0x429f_ffff, + 0xc2a0_0000, + 0x7f80_0000, + 0xff80_0000, + 0x7fc0_0000, + 0xffc0_0000, + 0x7fc1_2345, + 0xffc5_4321, + 0x7fa0_0001, + 0xff80_0001, + 0x7f80_0001, +]; + +const SIZES: [(u32, u32); 6] = [ + (1920, 1080), + (1080, 1920), + (0, 0), + (16_777_217, 3), + (u32::MAX, 1), + (7, u32::MAX), +]; + +fn sweep_rotations() -> Vec { + let mut v: Vec = (-70_000..=70_000).collect(); + let mut r = c_long::MIN; + // A stride over the whole range, as far as it goes before overflowing. + let stride: c_long = if std::mem::size_of::() == 8 { + c_long::MAX / 65_521 + } else { + 65_521 + }; + while let Some(next) = r.checked_add(stride) { + v.push(r); + r = next; + } + v.extend([c_long::MIN, c_long::MIN + 1, c_long::MAX - 1, c_long::MAX]); + if let Some(big) = c_long::from(1_u8).checked_shl(32) { + for k in [-3, -1, 1, 2, 7] { + for base in [0, 90, 180, 270] { + v.push(big * k + base); + } + } + } + v +} + +// -------------------------------------------------------------------------- +// Sweeps + +#[test] +fn rotation_sweep() { + let rotations = sweep_rotations(); + for (w, h) in SIZES { + for &r in &rotations { + check_rotation(r, w, h); + } + } +} + +#[test] +fn rotation_every_size() { + let mut sizes: Vec = (0..=4096).collect(); + for base in [1_u32 << 24, 1 << 25, 1 << 31, u32::MAX - 2000] { + sizes.extend(base.saturating_sub(2000)..=base.saturating_add(2000)); + } + sizes.push(u32::MAX); + for r in [0, 90, 180, 270, 45] { + for &size in &sizes { + check_rotation(r, size, 7); + check_rotation(r, 7, size); + check_rotation(r, size, size); + } + } +} + +#[test] +fn space_pairs_sweep() { + let spaces = spaces(); + for &s in &spaces { + for &c in &spaces { + check_pair(s, c, 300.0); + } + } +} + +#[test] +fn space_pairs_at_special_levels() { + let spaces = small_spaces(); + for bits in WHITES { + let w = f32::from_bits(bits); + for &s in &spaces { + for &c in &spaces { + check_pair(s, c, w); + } + } + } +} + +#[test] +fn nonlinear_alpha_every_space_and_flag() { + for s in spaces() { + for linear in [false, true] { + for alpha in [false, true] { + assert_eq!( + u8::from(shim::obs_source_draw_plan_async_nonlinear_alpha( + s, linear, alpha + )), + c_nonlinear(s, linear, alpha), + "{s} {linear} {alpha}" + ); + } + } + } +} + +// -------------------------------------------------------------------------- +// The callers' decisions in their order + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct AsyncDraw { + linear: u8, + nonlinear: u8, + technique: u32, + reads_white: u8, + multiplier: u32, + x: u32, + y: u32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct ConvertDraw { + convert: u8, + technique: u32, + reads_white: u8, + multiplier: u32, +} + +#[derive(Debug, Clone, Copy)] +struct DrawIn { + s: c_int, + c: c_int, + gs: bool, + alpha: bool, + white: u32, + rotation: c_long, + w: u32, + h: u32, +} + +/// `obs_source_render_async_video()` and `rotate_async_video()`: the +/// linear flag, the blend, the technique, the white level only when read, +/// and the translate only for a rotation other than 0. +fn async_c(i: DrawIn) -> AsyncDraw { + let linear = c_linear(i.s, i.c, i.gs); + let nonlinear = c_nonlinear(i.s, linear != 0, i.alpha); + let technique = c_technique(i.s, i.c, nonlinear != 0); + let reads_white = c_reads_white(i.s, i.c); + let multiplier = if reads_white != 0 { + c_multiplier(i.s, i.c, f32::from_bits(i.white)) + } else { + 1.0_f32.to_bits() + }; + let (x, y) = if i.rotation != 0 { + c_rotation(i.rotation, i.w, i.h) + } else { + (u32::MAX, u32::MAX) + }; + AsyncDraw { + linear, + nonlinear, + technique, + reads_white, + multiplier, + x, + y, + } +} + +fn async_rust(i: DrawIn) -> AsyncDraw { + let linear = shim::obs_source_draw_plan_async_linear_srgb(i.s, i.c, i.gs); + let nonlinear = shim::obs_source_draw_plan_async_nonlinear_alpha(i.s, linear, i.alpha); + let technique = shim::obs_source_draw_plan_technique(i.s, i.c, nonlinear); + let reads_white = shim::obs_source_draw_plan_reads_white_level(i.s, i.c); + let multiplier = if reads_white { + shim::obs_source_draw_plan_multiplier(i.s, i.c, f32::from_bits(i.white)).to_bits() + } else { + 1.0_f32.to_bits() + }; + let (x, y) = if i.rotation != 0 { + rs_rotation(i.rotation, i.w, i.h) + } else { + (u32::MAX, u32::MAX) + }; + AsyncDraw { + linear: u8::from(linear), + nonlinear: u8::from(nonlinear), + technique, + reads_white: u8::from(reads_white), + multiplier, + x, + y, + } +} + +/// `source_render()`: whether it converts, and when it does, the +/// technique (never with non-linear alpha) and the multiplier. +fn convert_c(i: DrawIn) -> ConvertDraw { + let convert = c_convert(i.s, i.c); + let technique = c_technique(i.s, i.c, false); + let reads_white = c_reads_white(i.s, i.c); + let multiplier = if reads_white != 0 { + c_multiplier(i.s, i.c, f32::from_bits(i.white)) + } else { + 1.0_f32.to_bits() + }; + ConvertDraw { + convert, + technique, + reads_white, + multiplier, + } +} + +fn convert_rust(i: DrawIn) -> ConvertDraw { + let convert = shim::obs_source_draw_plan_convert(i.s, i.c); + let technique = shim::obs_source_draw_plan_technique(i.s, i.c, false); + let reads_white = shim::obs_source_draw_plan_reads_white_level(i.s, i.c); + let multiplier = if reads_white { + shim::obs_source_draw_plan_multiplier(i.s, i.c, f32::from_bits(i.white)).to_bits() + } else { + 1.0_f32.to_bits() + }; + ConvertDraw { + convert: u8::from(convert), + technique, + reads_white: u8::from(reads_white), + multiplier, + } +} + +#[test] +fn callers_every_space_flag_level_and_rotation() { + let spaces = [-1, 0, 1, 2, 3, 4, 99, c_int::MIN, c_int::MAX]; + let rotations: [c_long; 7] = [0, 90, 180, 270, 45, -90, c_long::MAX]; + let mut runs = 0_u32; + for s in spaces { + for c in spaces { + for gs in [false, true] { + for alpha in [false, true] { + for white in WHITES { + for rotation in rotations { + for (w, h) in [(1920, 1080), (u32::MAX, 3)] { + let i = DrawIn { + s, + c, + gs, + alpha, + white, + rotation, + w, + h, + }; + assert_eq!(async_rust(i), async_c(i), "{i:?}"); + assert_eq!(convert_rust(i), convert_c(i), "{i:?}"); + runs += 1; + } + } + } + } + } + } + } + assert_eq!(runs, 9 * 9 * 2 * 2 * 26 * 7 * 2); +} + +// -------------------------------------------------------------------------- +// Proptests + +fn any_space() -> impl Strategy { + prop_oneof![ + -3_i32..7, + any::(), + proptest::sample::select(vec![c_int::MIN, c_int::MAX]) + ] +} + +fn any_white_bits() -> impl Strategy { + prop_oneof![any::(), proptest::sample::select(WHITES.to_vec())] +} + +fn any_rotation() -> impl Strategy { + prop_oneof![ + proptest::sample::select(vec![0 as c_long, 90, 180, 270]), + 0 as c_long..360, + any::() + ] +} + +fn any_draw() -> impl Strategy { + ( + any_space(), + any_space(), + any::(), + any::(), + any_white_bits(), + any_rotation(), + any::(), + any::(), + ) + .prop_map(|(s, c, gs, alpha, white, rotation, w, h)| DrawIn { + s, + c, + gs, + alpha, + white, + rotation, + w, + h, + }) +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(20_000))] + + #[test] + fn prop_rotation(rotation in any_rotation(), w in any::(), h in any::()) { + prop_assert_eq!(rs_rotation(rotation, w, h), c_rotation(rotation, w, h)); + } + + #[test] + fn prop_space_pair(s in any_space(), c in any_space(), white in any_white_bits()) { + check_pair(s, c, f32::from_bits(white)); + } + + #[test] + fn prop_callers(i in any_draw()) { + prop_assert_eq!(async_rust(i), async_c(i)); + prop_assert_eq!(convert_rust(i), convert_c(i)); + } +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 4e65257b0a..8484274baf 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -15,6 +15,7 @@ mod deinterlace_draw_parity; mod deinterlace_pacing; mod deinterlace_pacing_parity; mod draw_plan; +mod draw_plan_parity; mod ptm_gate; mod ptm_gate_parity; mod size_route; From dd81f72fc2a9b2becdfea09e7169e4cba0657f0d Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 13:49:11 -0400 Subject: [PATCH 6/6] docs: Record the source draw plan port The rust/README.md crate table now lists libobs/obs-source-draw-plan.c.h among the rust/obs-source units, and its port table has a row for it: the eight functions and what they decide, the shared technique enum (obs-source-draw-technique.h, obs-source::draw_technique), what stays C (every gs_* call in its order and under its conditions, convert_video_space(), obs_get_video_sdr_white_level(), gs_get_format_from_space(), the texrenders and the technique names), why the decisions are separate calls rather than one plan (neither gs_get_linear_srgb() nor the white level read is hoisted), the spaces as int and the rotation as long, the multiplier as one f32 division matching bit for bit, what is kept and characterized, no intentional differences, the _rust variant of the characterization, the real-libobs tests and what Tier 3 compares. The deinterlace draw row names the shared enum. The crate's description and lib.rs say what it holds, draw_technique included. 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_draw_plan.rs, in the sorted modules! list: 1024 async frames (the decisions of obs_source_render_async_video() in its order and the rotation translate, over every pair of colour spaces, both linear and alpha flags, white levels of 80 to 480 nits and rotations of 0, 90, 180 and 270) and 1024 source_render() conversions, folded into checksums both sides must match. Neither side allocates, and the module passes the bench's per-module allocation check. On x86_64 Linux (GCC, not pinned, load average 6 to 8; median, two runs): async_frame 16.3 and 14.6 us Rust, 13.9 and 13.3 us C per 1024 frames (1.1x to 1.2x), about 2 ns more per async frame drawn; source_render 8.9 and 10.0 us Rust, 7.2 and 7.7 us C (1.2x to 1.3x), about 2 ns more per source drawn. soldr cargo test -p obs-c-oracle --locked --bench port_comparison passes. Fixes #2187 --- rust/README.md | 5 +- rust/obs-c-oracle/benches/port_comparison.rs | 3 + .../port_comparison/source_draw_plan.rs | 183 ++++++++++++++++++ rust/obs-source/Cargo.toml | 2 +- rust/obs-source/src/lib.rs | 13 +- 5 files changed, 200 insertions(+), 6 deletions(-) create mode 100644 rust/obs-c-oracle/benches/port_comparison/source_draw_plan.rs diff --git a/rust/README.md b/rust/README.md index d095f6796f..926bf044b3 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-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `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()`, `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-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`) | @@ -164,8 +164,9 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/obs-source-audio-timing.c.h` | `obs-source::audio_timing` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-timing`). The audio timestamp placement of `source_output_audio_data()`, in two pure steps around `audio_buf_mutex`: `obs_source_audio_timing_pre_lock()` (the direct timestamp check, then reset, jump, smooth with the drift error and the async decoupled re-anchor, or the log, and `next_audio_ts_min`) and `obs_source_audio_timing_under_lock()` (append or place, the reset when the system time jumped and the audio was not continuous, #522, the sync and resampler offsets, `next_audio_sys_ts_min`, and "the sync offset changed, so place", #333), plus `obs_source_audio_timing_buffer_start()` (where `reset_audio_data_at_sys_time()` starts an emptied buffer, #286). They are declared in `obs-source-audio-timing.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps the field reads and the stores, in their order (the volatile `timing_set` and `timing_adjust` included), the lock, `handle_ts_jump()`, `audio_resampler_set_compensation_error()`, `conv_frames_to_time()` (it passes the packet's duration, so nothing in the port divides), the deque, the signal, the muting and the log lines. The shims read the struct's input fields and write its output fields one at a time, so the inputs and the padding keep their bytes. Every sum and difference wraps as the C's `uint64_t` does; the drift error is `cast_signed()` and the sync offset `cast_unsigned()`, the C's conversions. The crate depends on no other crate. Kept, characterized, not endorsed: a timestamp ahead of the clock gives a `timing_adjust` near `u64::MAX`; `timestamp + timing_adjust`, the packet's end, a negative sync offset larger than the packet's system time and a resampler offset larger than it wrap; the distance is not modular; a direct timestamp more than 2 s from the expected one is logged, not a jump. Intentional difference: a null struct pointer does nothing (the C dereferences it; libobs passes one on its stack). `test_source_audio_timing_steps` compiles the C into itself, and `test_source_audio_timing_steps_rust` runs the same test source against `libobs_rust-static` (the first `_rust` variant since `test_scene_bounds_place_rust`: the `obs-source` member links on its own); `test_async_delay_audio`, `test_audio_sync_offset_change`, `test_media_sync_offset_restart`, `test_audio_restart_resampler_tail` and `test_audio_clock_drift` see the swap through the real libobs. Tier 3 compares every byte of the step structs (padding included, from a random fill) over the special values with every flag, proptests of each step, and 2-50-packet sequences through a model of the caller, with the written-back state. | | `libobs/obs-source-color-space.c.h` | `obs-source::color_space` | Swapped in hybrid `libobs` (group `libobs.ported-source-color-space`). The colour space query routing of `obs_source_get_color_space()`, as two functions: `obs_source_color_space_route()` (which source answers: a non-filter without video asks its filter parent; a filter without video asks its target, #1493; a source without data, or disabled, asks its target; an async source picks; anything else asks its callback, in that order) and `obs_source_color_space_pick()` (the async pick: the first preferred space equal to the frame's space, else the last one, else the frame's space). They are declared in `obs-source-color-space.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps `data_valid()`, reads `info.type`, `info.output_flags`, `context.data != NULL`, `enabled`, `filter_parent != NULL` and `filter_target != NULL` into `struct obs_source_color_space_node` (passed by value, mirrored), and keeps the recursion into the parent or target, `convert_video_space()` of the async frame (it passes the frame's space) and the `video_get_color_space` callback. No source pointer crosses. Colour spaces cross as `int`; `obs-source.c.h` checks that `enum gs_color_space` and `enum obs_source_type` are `int`-sized and that the header's filter type and flags match `obs-source.h`. Only `OBS_SOURCE_TYPE_FILTER` is a filter and only the VIDEO and ASYNC bits count. Kept, characterized, not endorsed: a filter flagged async picks unless the earlier rules send it to its target; with no data and nowhere to go the route is the callback or the pick (`data_valid()` keeps libobs from getting there); a value outside the enum in the preferred list is compared and returned as it is. Intentional difference: a null preferred list with a count above 0 is read as empty (the C reads through the null pointer; libobs passes `NULL` only with a count of 0). `test_source_color_space_steps` compiles the C into itself, and `test_source_color_space_steps_rust` runs the same test source against `libobs_rust-static`; `test_filter_color_space` (the HDR Tone Mapping chains of #1493) sees the swap through the real libobs. Tier 3 compares the route over every row of the characterization with eight types and sixteen sets of other flags, a proptest over any node, the pick over every list of 0 to 5 values from -1..=4 and a proptest over lists of 0 to 8 arbitrary `i32` values. | | `libobs/obs-source-conversion-plan.c.h` | `obs-source::conversion_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-conversion-plan`). The async video GPU conversion plan of `obs-source.c`, as one function: `obs_source_conversion_plan_fill()` takes the frame's format, range, transfer and size and fills `struct obs_source_conversion_plan` (mirrored): the convert type (`get_convert_type()`), the channel count and each plane's width, height and texture format (the nineteen `set_*_sizes()` of `init_gpu_conversion()`; the RGB limited plane takes `convert_video_format()`'s format, RGBA16F for PQ and HLG), the technique of `format_conversion.effect` (`select_conversion_technique()`) and the linear flag (`need_linear_output()`). It is declared in `obs-source-conversion-plan.h` without `EXPORT`, so the shim stays hidden. `obs-source.c.h` keeps `set_async_texture_size()` (the change check, the textures and texrenders, the graphics context), `upload_raw_frame()`, `update_async_texrender()` and its shader parameters, the `convert_video_format()` inline of `obs-internal.h` for its other callers, and the two asserts for a frame that needs no conversion; `get_convert_type()`, `select_conversion_technique()` and `need_linear_output()` keep their signatures and read the plan, and `init_gpu_conversion()` copies it into `async_convert_width`, `async_convert_height` and `async_texture_formats` for the plan's planes, then `async_channel_count`. The format and transfer cross as `int`, so values outside the enums take the C's `default:` paths (no conversion, no technique); the transfer is the frame's `uint8_t`, so it counts modulo 256. The technique crosses as a pointer to a `c"..."` literal of the core (a `&'static CStr`), or null: nothing is allocated. Kept, characterized, not endorsed: the sizes wrap in `u32` as the C's do near `UINT32_MAX` (`(width + 1) / 2` is 0 at `UINT32_MAX`, `width * 3` wraps from 0x55555556, `(width + 5) / 6 * 4` is 0 from `UINT32_MAX - 4`); the port uses `wrapping_*` for each. Intentional difference: a null plan is left alone (the C writes through it; libobs passes a plan on its stack). `test_async_conversion_plan` compiles the C into itself and pins the whole table (every format and two values outside the enum, both ranges, every transfer, 4 and 258, at 14 sizes up to `UINT32_MAX`, from tables generated from the moved C), and `test_async_conversion_plan_rust` runs the same test source against `libobs_rust-static`; `test_media_restart_first_frame`, `test_deinterlace_2x_field_order` and `test_pipewire_camera_nv12` see the swap through the real libobs. Tier 3 compares every field, the technique as a string, over format x range x transfer exhaustively (47 formats, 270 transfers, 22 widths and heights: 1.1 million plans), every pair of 22 sizes for the in-enum formats, and a proptest over any `u32` size and any `i32` format and transfer. | -| `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, an enum 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-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-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-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. | | `libobs/obs-source-transition-clock.c.h` | `obs-source::transition_clock` | Swapped in hybrid `libobs` (group `libobs.ported-source-transition-clock`). The clock and fit decisions of the transitions in `obs-source-transition.c.h`, as seven functions on plain values: `obs_source_transition_clock_time()` (`calc_time()`: the manual value in manual mode, 0 up to and at the start, 1 from the end and for a duration of 0, the ratio in between), `obs_source_transition_clock_manual()` (the manual step of `obs_transition_tick()`: hold outside manual mode, snap for a torque of ±0, step otherwise; the C then calls `calc_torquef()`), `obs_source_transition_clock_size()` and `_fold()` (`get_cx()`/`get_cy()` and the size fold of `recalculate_transition_size()`), `obs_source_transition_clock_fit()` (the scale of `recalculate_transition_matrix()`: none for a child side of ±0, max-only, aspect, stretch), `obs_source_transition_clock_mix_spaces()` and `obs_source_transition_clock_min_ts()` (the earliest audio timestamp that is not 0, #130's snapshot read stays C). They are declared in `obs-source-transition-clock.h` without `EXPORT`, so the shims stay hidden. `obs-source-transition.c.h` keeps the locks, the field reads and writes, the child size and timestamp reads, `calc_torquef()`, the offsets passed to `add_alignment()` and their `(int)` casts, `add_alignment()`, the matrix build, `get_sample_time()`, the texrenders and the swap handover (#556). The modes, scale types and colour spaces cross as `int`; `obs-source-transition.c.h` checks the values and that the enums are `int`-sized. The fit writes its scale through two `float *` only where the C does, so a scale type outside the enum writes nothing. The C divides the clock in `long double`, whose width differs by platform (80-bit x87 on x86_64, 64 bits on MSVC and Apple silicon, 128 on aarch64 Linux), and the widths round a quotient within about 2^-54 of an `f32` tie differently, below 2^53 too (a 33.5 s transition: `33554426999 / 33554431999` is `0x3f7ffffd` in x87, `0x3f7ffffe` as a `double`). The port gives the x87 result for every `u64`: an `f64` division unless its quotient lies exactly on an `f32` tie (all but about 1 in 2^29 ratios), else and from 2^53 on an integer division rounded to 64 bits, then 24. The crate depends on no other crate. Kept, characterized, not endorsed: the manual value is not clamped and NaN passes through; any ratio of 1 - 2^-25 or more rounds to 1.0f, so a transition reaches t >= 1 that much early; a NaN target or torque steps to NaN; a transition of size 0 fits every child to a scale of 0 on that side; a scale type outside the enum gives a matrix but no scale (`recalculate_transition_matrix()` then reads a scale it never set; reported, not changed). Intentional difference: a null `scale_x` or `scale_y` is not written (the C writes through it; libobs passes its own `struct vec2`). `test_transition_clock_steps` compiles the C into itself, and `test_transition_clock_steps_rust` runs the same test source against `libobs_rust-static` (libobs linked first, for `calc_torquef()`); `test_transition_swap`, `test_studio_mode_swap`, `test_transition_recursive_destination`, `test_fade_to_color_mix`, `test_transition_audio_rescale` and `test_stinger_media_duration` see the swap through the real libobs. Tier 3 compares every result bit for bit (the fit's two floats from a guard fill, written or not) over every mode and scale type with special values, sweeps of near-tie ratios in 40 binades, every ratio up to a duration of 2^11 ns (2^15 in an ignored release run) and full-range proptests; where the oracle's `long double` is not the x87's it checks the C against a model of its width instead. In the manual tick only, two NaNs from `calc_torquef()` (not part of this port) count as equal. | diff --git a/rust/obs-c-oracle/benches/port_comparison.rs b/rust/obs-c-oracle/benches/port_comparison.rs index cf518af247..3fac834eb9 100644 --- a/rust/obs-c-oracle/benches/port_comparison.rs +++ b/rust/obs-c-oracle/benches/port_comparison.rs @@ -56,6 +56,8 @@ mod source_conversion_plan; mod source_deinterlace_draw; #[path = "port_comparison/source_deinterlace_pacing.rs"] mod source_deinterlace_pacing; +#[path = "port_comparison/source_draw_plan.rs"] +mod source_draw_plan; #[path = "port_comparison/source_load.rs"] mod source_load; #[path = "port_comparison/source_profiler.rs"] @@ -159,6 +161,7 @@ fn comparisons(criterion: &mut Criterion) { source_conversion_plan, source_deinterlace_draw, source_deinterlace_pacing, + source_draw_plan, source_load, source_profiler, source_ptm_gate, diff --git a/rust/obs-c-oracle/benches/port_comparison/source_draw_plan.rs b/rust/obs-c-oracle/benches/port_comparison/source_draw_plan.rs new file mode 100644 index 0000000000..a47c1f458c --- /dev/null +++ b/rust/obs-c-oracle/benches/port_comparison/source_draw_plan.rs @@ -0,0 +1,183 @@ +//! obs-source: the source draw decisions (`libobs/obs-source-draw-plan.c.h`), +//! which `obs_source_render_async_video()` runs once a frame for each drawn +//! async source (with `rotate_async_video()` when it is rotated), and +//! `source_render()` once a frame for each drawn source with a video render +//! callback. Each measurement runs 1024 calls that reach every branch, and +//! folds the results into a checksum that both sides must match: +//! - `async_frame`: the decisions of one async draw in its order (the +//! linear sRGB flag, the non-linear alpha blend, the technique, the white +//! level read and the multiplier, then the translate for a rotation other +//! than 0), over every pair of colour spaces, both linear and alpha flags, +//! white levels of 80 to 480 nits and rotations of 0, 90, 180 and 270; +//! - `source_render`: the conversion, its technique and multiplier, over +//! every pair of colour spaces and the same white levels. +//! +//! Neither side allocates, so the module leaves no allocation live. +//! +//! Measured when it was added (x86_64 Linux, GCC, not pinned to a core, a +//! machine at a load average of 6 to 8; median per 1024 calls, two runs): +//! async_frame 16.3 and 14.6 us Rust, 13.9 and 13.3 us C (1.1x to 1.2x), +//! about 2 ns more per async frame; source_render 8.9 and 10.0 us Rust, 7.2 +//! and 7.7 us C (1.2x to 1.3x), about 2 ns more per source drawn. + +use std::hint::black_box; +use std::os::raw::{c_int, c_long}; + +use criterion::{Criterion, Throughput}; +use obs_c_oracle::source_draw_plan as c; +use obs_source::ffi::source_draw_plan as rs; + +const CALLS: usize = 1024; + +/// A small deterministic generator, so both sides see the same calls. +struct Lcg(u64); + +impl Lcg { + fn next(&mut self) -> u64 { + self.0 = self + .0 + .wrapping_mul(6_364_136_223_846_793_005) + .wrapping_add(1_442_695_040_888_963_407); + self.0 >> 11 + } +} + +fn small(r: u64, n: u64) -> c_int { + c_int::try_from(r % n).unwrap() +} + +#[derive(Clone, Copy)] +struct Draw { + source_space: c_int, + current_space: c_int, + gs_linear_srgb: bool, + linear_alpha: bool, + white: f32, + rotation: c_long, +} + +fn draws() -> Vec { + let mut rng = Lcg(2187); + (0..CALLS) + .map(|_| { + let r = rng.next(); + Draw { + source_space: small(r, 4), + current_space: small(r >> 4, 4), + gs_linear_srgb: (r >> 8).is_multiple_of(2), + linear_alpha: (r >> 9).is_multiple_of(2), + white: [80.0, 203.0, 300.0, 480.0][usize::try_from((r >> 12) % 4).unwrap()], + rotation: [0, 90, 180, 270][usize::try_from((r >> 16) % 4).unwrap()], + } + }) + .collect() +} + +struct Side { + rotation_x: fn(c_long, u32, u32) -> f32, + rotation_y: fn(c_long, u32, u32) -> f32, + linear_srgb: fn(c_int, c_int, bool) -> u8, + nonlinear_alpha: fn(c_int, bool, bool) -> u8, + technique: fn(c_int, c_int, bool) -> u32, + reads_white_level: fn(c_int, c_int) -> u8, + multiplier: fn(c_int, c_int, f32) -> f32, + convert: fn(c_int, c_int) -> u8, +} + +const RUST: Side = Side { + rotation_x: |r, w, h| rs::obs_source_draw_plan_rotation_x(r, w, h), + rotation_y: |r, w, h| rs::obs_source_draw_plan_rotation_y(r, w, h), + linear_srgb: |s, c, g| u8::from(rs::obs_source_draw_plan_async_linear_srgb(s, c, g)), + nonlinear_alpha: |s, l, a| u8::from(rs::obs_source_draw_plan_async_nonlinear_alpha(s, l, a)), + technique: |s, c, n| rs::obs_source_draw_plan_technique(s, c, n), + reads_white_level: |s, c| u8::from(rs::obs_source_draw_plan_reads_white_level(s, c)), + multiplier: |s, c, w| rs::obs_source_draw_plan_multiplier(s, c, w), + convert: |s, c| u8::from(rs::obs_source_draw_plan_convert(s, c)), +}; + +// SAFETY (each): the oracle functions take and return plain values. +const C: Side = Side { + rotation_x: |r, w, h| unsafe { c::oracle_obs_source_draw_plan_rotation_x(r, w, h) }, + rotation_y: |r, w, h| unsafe { c::oracle_obs_source_draw_plan_rotation_y(r, w, h) }, + linear_srgb: |s, cs, g| unsafe { c::oracle_obs_source_draw_plan_async_linear_srgb(s, cs, g) }, + nonlinear_alpha: |s, l, a| unsafe { + c::oracle_obs_source_draw_plan_async_nonlinear_alpha(s, l, a) + }, + technique: |s, cs, n| unsafe { c::oracle_obs_source_draw_plan_technique(s, cs, n) }, + reads_white_level: |s, cs| unsafe { c::oracle_obs_source_draw_plan_reads_white_level(s, cs) }, + multiplier: |s, cs, w| unsafe { c::oracle_obs_source_draw_plan_multiplier(s, cs, w) }, + convert: |s, cs| unsafe { c::oracle_obs_source_draw_plan_convert(s, cs) }, +}; + +fn fold(sum: u64, value: u64) -> u64 { + sum.wrapping_mul(31).wrapping_add(value) +} + +fn multiplier(side: &Side, d: Draw) -> f32 { + if (side.reads_white_level)(d.source_space, d.current_space) != 0 { + (side.multiplier)(d.source_space, d.current_space, d.white) + } else { + 1.0 + } +} + +fn async_frame(side: &Side, draws: &[Draw]) -> u64 { + let mut sum = 0u64; + for d in draws { + let d = black_box(*d); + let linear = (side.linear_srgb)(d.source_space, d.current_space, d.gs_linear_srgb); + let nonlinear = (side.nonlinear_alpha)(d.source_space, linear != 0, d.linear_alpha); + let technique = (side.technique)(d.source_space, d.current_space, nonlinear != 0); + let multiplier = multiplier(side, d); + sum = fold(sum, u64::from(linear)); + sum = fold(sum, u64::from(nonlinear)); + sum = fold(sum, u64::from(technique)); + sum = fold(sum, u64::from(multiplier.to_bits())); + if d.rotation != 0 { + sum = fold( + sum, + u64::from((side.rotation_x)(d.rotation, 1920, 1080).to_bits()), + ); + sum = fold( + sum, + u64::from((side.rotation_y)(d.rotation, 1920, 1080).to_bits()), + ); + } + } + sum +} + +fn source_render(side: &Side, draws: &[Draw]) -> u64 { + let mut sum = 0u64; + for d in draws { + let d = black_box(*d); + let convert = (side.convert)(d.source_space, d.current_space); + let technique = (side.technique)(d.source_space, d.current_space, false); + let multiplier = multiplier(side, d); + sum = fold(sum, u64::from(convert)); + if convert != 0 { + sum = fold(sum, u64::from(technique)); + sum = fold(sum, u64::from(multiplier.to_bits())); + } + } + sum +} + +pub(crate) fn benchmarks(criterion: &mut Criterion) { + let draws = draws(); + for (name, run) in [ + ("async_frame", async_frame as fn(&Side, &[Draw]) -> u64), + ("source_render", source_render), + ] { + assert_eq!( + run(&RUST, &draws), + run(&C, &draws), + "benchmark fixture differs: {name}" + ); + let mut group = criterion.benchmark_group(format!("source_draw_plan/{name}")); + group.throughput(Throughput::Elements(CALLS as u64)); + group.bench_function("rust", |b| b.iter(|| black_box(run(&RUST, &draws)))); + group.bench_function("c", |b| b.iter(|| black_box(run(&C, &draws)))); + group.finish(); + } +} diff --git a/rust/obs-source/Cargo.toml b/rust/obs-source/Cargo.toml index 977d807290..92bf83fdc1 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-ptm-gate.c.h (the push-to-mute and push-to-talk gate), 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-ptm-gate.c.h (the push-to-mute and push-to-talk gate), 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 5069b9c1e1..d798250983 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -13,7 +13,12 @@ //! deinterlacing (the effect, the early return, the linear sRGB flag, the //! technique and multiplier, the second field and the mode change); //! `libobs/obs-source-deinterlace-pacing.c.h`, the frame pacing of -//! double-rate deinterlacing; `libobs/obs-source-ptm-gate.c.h`, the +//! double-rate deinterlacing; `libobs/obs-source-draw-plan.c.h`, the source +//! draw decisions of `rotate_async_video()`, +//! `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-ptm-gate.c.h`, the //! push-to-mute and push-to-talk gate of `source_muted()`; //! `libobs/obs-source-size.c.h`, the size query routing of //! `obs_source_get_width()` and `obs_source_get_height()`; @@ -25,8 +30,10 @@ //! //! The safe cores are [`async_pacing`], [`audio_input`], [`audio_timing`], //! [`color_space`], [`conversion_plan`], [`deinterlace_draw`], -//! [`deinterlace_pacing`], [`ptm_gate`], [`size_route`], -//! [`transition_clock`], [`transition_render`] and [`volume_apply`]. The C +//! [`deinterlace_pacing`], [`draw_plan`], [`ptm_gate`], [`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 //! ABI shims live in [`ffi`]. #![deny(unsafe_code)]