Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 57 additions & 37 deletions src/filter/motiondiff0r/motiondiff0r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*
* The delayed frame is chosen from a small history using the timestamps
* given by the host, so the time offset is independent of the frame rate.
* History is preallocated at construction: at most 128 frames and 1 GiB
* of pixels per instance, including the current frame. The effective offset
* History is preallocated at construction: at most 1 GiB of pixels and
* history metadata per instance, including the current frame. The effective offset
* is shortened when the requested history does not fit. Rendering allocates
* no buffers and reuses released slots before touching unused buffers;
* dimensions that cannot fit two frames fail construction.
Expand All @@ -44,8 +44,11 @@ namespace {

const double MAX_OFFSET_SECONDS = 2.0;
const double MAX_GAIN = 16.0;
const size_t MAX_HISTORY_FRAMES = 128;
#ifdef MOTIONDIFF_TEST_HISTORY_BYTES
const size_t MAX_HISTORY_BYTES = MOTIONDIFF_TEST_HISTORY_BYTES;
#else
const size_t MAX_HISTORY_BYTES = 1024u * 1024u * 1024u;
#endif

enum Mode { MODE_OVERLAY = 0, MODE_ABSDIFF = 1, MODE_MASK = 2 };

Expand All @@ -64,9 +67,16 @@ inline int luma(const uint8_t* p)

class motiondiff0r : public frei0r::filter
{
struct FrameInfo {
double timestamp;
size_t previous;
size_t next;
};

public:
motiondiff0r(unsigned int frame_width, unsigned int frame_height)
: frame_pixels(0), capacity(0), head(0), count(0), free_count(0)
: frame_pixels(0), capacity(0), head(0), tail(0), count(0),
free_head(0), next_unused(0)
{
offset = 0.1; // 200 ms
mode = 0.0; // negative overlay
Expand All @@ -76,7 +86,7 @@ class motiondiff0r : public frei0r::filter
monochrome = false;

register_param(offset, "Time Offset",
"0..1 = 0..2 seconds, limited to 128 frames / 1 GiB of history; 0 = previous frame");
"0..1 = 0..2 seconds, limited to 1 GiB of history; 0 = previous frame");
register_param(mode, "Mode",
"0: negative overlay, 0.5: absolute difference, 1: motion mask");
register_param(blend, "Blend",
Expand All @@ -90,19 +100,19 @@ class motiondiff0r : public frei0r::filter

// The registration helper creates a zero-sized metadata-only instance.
if (!frame_width || !frame_height) return;
const size_t max_pixels = MAX_HISTORY_BYTES / (2 * sizeof(uint32_t));
const size_t max_pixels = (MAX_HISTORY_BYTES / 2 - sizeof(FrameInfo)) / sizeof(uint32_t);
if ((size_t)frame_width > max_pixels / frame_height) return;
frame_pixels = (size_t)frame_width * frame_height;
capacity = std::min(MAX_HISTORY_FRAMES,
MAX_HISTORY_BYTES / (frame_pixels * sizeof(uint32_t)));
capacity = MAX_HISTORY_BYTES / (frame_pixels * sizeof(uint32_t) + sizeof(FrameInfo));
storage.reset(new (std::nothrow) uint32_t[capacity * frame_pixels]);
// Pop the lowest unused slot first; released slots go on top for reuse.
for (size_t i = 0; i < capacity; ++i)
free_slots[i] = capacity - i - 1;
free_count = capacity;
if (!storage) return;
frames.reset(new (std::nothrow) FrameInfo[capacity]);
// capacity is the sentinel for empty links. Initialize entries on use so
// short offsets touch only the pixel buffers and metadata they need.
head = tail = free_head = capacity;
}

virtual bool is_valid() const { return storage.get() != 0; }
virtual bool is_valid() const { return storage.get() != 0 && frames.get() != 0; }

virtual void update(double time, uint32_t* out, const uint32_t* in)
{
Expand All @@ -111,28 +121,36 @@ class motiondiff0r : public frei0r::filter

// Forget frames at or after the current time: the host seeked backwards
// or is rendering the same time again.
while (count && timestamps[slot(count - 1)] >= time) {
free_slots[free_count++] = frame_slots[slot(count - 1)];
--count;
}
while (count && frames[tail].timestamp >= time)
release(tail);

// Keep the newest frame not later than the target as the oldest one.
while (count >= 2 && timestamps[slot(1)] <= target)
drop_front();
while (count >= 2 && frames[frames[head].next].timestamp <= target)
release(head);
if (count == capacity)
drop_front();
release(head);

// Store a copy of the current frame before rendering, so that the
// history stays intact when the host renders in place (in == out).
const size_t current = slot(count);
frame_slots[current] = free_slots[--free_count];
uint32_t* pixels = storage.get() + frame_slots[current] * n;
size_t current;
if (free_head != capacity) {
current = free_head;
free_head = frames[current].next;
} else {
current = next_unused++;
}
uint32_t* pixels = storage.get() + current * n;
std::copy(in, in + n, pixels);
timestamps[current] = time;
frames[current].timestamp = time;
frames[current].previous = tail;
frames[current].next = capacity;
if (count) frames[tail].next = current;
else head = current;
tail = current;
++count;

// With no older frame yet, the current frame is compared with itself.
process(pixels, storage.get() + frame_slots[head] * n, out, n);
process(pixels, storage.get() + head * n, out, n);
}

private:
Expand All @@ -146,22 +164,24 @@ class motiondiff0r : public frei0r::filter
size_t frame_pixels;
size_t capacity;
size_t head;
size_t tail;
size_t count;
size_t free_count;
double timestamps[MAX_HISTORY_FRAMES];
size_t frame_slots[MAX_HISTORY_FRAMES];
size_t free_slots[MAX_HISTORY_FRAMES];
size_t free_head;
size_t next_unused;
std::unique_ptr<FrameInfo[]> frames;
std::unique_ptr<uint32_t[]> storage;

size_t slot(size_t index) const
{
return (head + index) % capacity;
}

void drop_front()
void release(size_t index)
{
free_slots[free_count++] = frame_slots[head];
head = slot(1);
const size_t previous = frames[index].previous;
const size_t next = frames[index].next;
if (previous != capacity) frames[previous].next = next;
else head = next;
if (next != capacity) frames[next].previous = previous;
else tail = previous;
// The next link doubles as a free list while this slot is inactive.
frames[index].next = free_head;
free_head = index;
--count;
}

Expand Down
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ add_test(NAME motiondiff0r-alloc COMMAND motiondiff0r-alloc)
add_executable(motiondiff0r-memory-contract motiondiff0r-memory-contract.cpp
${CMAKE_SOURCE_DIR}/src/filter/motiondiff0r/motiondiff0r.cpp)
add_test(NAME motiondiff0r-memory-contract COMMAND motiondiff0r-memory-contract)
add_executable(motiondiff0r-small-budget motiondiff0r-memory-contract.cpp
${CMAKE_SOURCE_DIR}/src/filter/motiondiff0r/motiondiff0r.cpp)
target_compile_definitions(motiondiff0r-small-budget PRIVATE MOTIONDIFF_TEST_HISTORY_BYTES=65536)
add_test(NAME motiondiff0r-small-budget COMMAND motiondiff0r-small-budget)
add_test(NAME bundle-classification-coverage
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/test-bundle-classifications.cmake)
add_executable(aech0r-contract aech0r-contract.c
Expand Down Expand Up @@ -334,9 +338,14 @@ elseif(EMSCRIPTEN)
set_property(TARGET bundle-registry-emscripten APPEND_STRING PROPERTY LINK_FLAGS
" --no-entry -sINITIAL_MEMORY=67108864 -sMODULARIZE=1 -sEXPORT_NAME=createBundleRegistry -sENVIRONMENT=node -sINVOKE_RUN=0 -sEXPORTED_FUNCTIONS=['_f0r_bundle_emscripten_run','_f0r_bundle_emscripten_output_digest','_f0r_bundle_emscripten_application_frame_count','_f0r_bundle_emscripten_parameter_change_count','_f0r_bundle_emscripten_output_count','_f0r_bundle_emscripten_output_digest_at','_f0r_bundle_emscripten_output_id_at','_f0r_bundle_emscripten_failure_id','_f0r_bundle_emscripten_failure_stage']")
if(FREI0R_BUNDLE_EMSCRIPTEN_GC)
# Section collection is independent of the runner's heap settings below.
set_property(TARGET bundle-registry-emscripten APPEND_STRING PROPERTY LINK_FLAGS
" -Wl,--gc-sections")
endif()
# Large constructor-owned histories must be able to grow beyond the initial
# heap. If growth fails, let nothrow allocations return NULL instead of aborting.
set_property(TARGET bundle-registry-emscripten APPEND_STRING PROPERTY LINK_FLAGS
" -sALLOW_MEMORY_GROWTH=1 -sABORTING_MALLOC=0")
if(FREI0R_BUNDLE_WASM_PTHREADS)
set_property(TARGET bundle-registry-emscripten APPEND_STRING PROPERTY LINK_FLAGS
" -pthread -sUSE_PTHREADS=1 -sPTHREAD_POOL_SIZE=2")
Expand Down
96 changes: 71 additions & 25 deletions test/motiondiff0r-memory-contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,40 @@ extern "C" {
}

namespace {
#ifdef MOTIONDIFF_TEST_HISTORY_BYTES
const size_t budget = MOTIONDIFF_TEST_HISTORY_BYTES;
#else
const size_t budget = 1024u * 1024u * 1024u;
#endif
size_t allocation_calls = 0;
size_t largest_request = 0;
size_t fail_size = 0;
bool deny_allocations = false;
bool capture_storage = false;
unsigned char *captured_storage = 0;
const size_t small_frame_bytes = 8 * 8 * sizeof(uint32_t);
size_t captured_bytes = 0;
size_t array_calls = 0;
size_t array_bytes = 0;
size_t pixel_bytes = 0;

void *allocate(size_t size, bool throwing)
void *allocate(size_t size, bool throwing, bool array = false)
{
++allocation_calls;
if (size > largest_request) largest_request = size;
void *p = (deny_allocations || size == fail_size || size > budget) ? 0 : std::malloc(size ? size : 1);
if (!p && throwing) throw std::bad_alloc();
if (p && capture_storage && size == 128 * small_frame_bytes) {
captured_storage = static_cast<unsigned char*>(p);
std::memset(p, 0xa5, size);
if (array) {
array_bytes += size;
if (array_calls++ == 0) {
pixel_bytes = size;
if (p && capture_storage) {
captured_storage = static_cast<unsigned char*>(p);
// A bounded prefix detects cycling without touching the whole reservation.
captured_bytes = size < 1024 * small_frame_bytes ? size : 1024 * small_frame_bytes;
std::memset(p, 0xa5, captured_bytes);
}
}
}
return p;
}
Expand All @@ -37,9 +53,9 @@ void parameter(f0r_instance_t instance, int index, double value)
} // namespace

void *operator new(size_t size) { return allocate(size, true); }
void *operator new[](size_t size) { return allocate(size, true); }
void *operator new[](size_t size) { return allocate(size, true, true); }
void *operator new(size_t size, const std::nothrow_t&) noexcept { return allocate(size, false); }
void *operator new[](size_t size, const std::nothrow_t&) noexcept { return allocate(size, false); }
void *operator new[](size_t size, const std::nothrow_t&) noexcept { return allocate(size, false, true); }
void operator delete(void *p) noexcept { std::free(p); }
void operator delete[](void *p) noexcept { std::free(p); }
void operator delete(void *p, size_t) noexcept { std::free(p); }
Expand All @@ -50,17 +66,24 @@ void operator delete[](void *p, const std::nothrow_t&) noexcept { std::free(p);
static int construction_budget(unsigned int width, unsigned int height)
{
const size_t frame_bytes = (size_t)width * height * sizeof(uint32_t);
const size_t frames = budget / frame_bytes < 128 ? budget / frame_bytes : 128;
largest_request = 0;
array_calls = array_bytes = pixel_bytes = 0;
f0r_instance_t instance = f0r_construct(width, height);
if (!instance || largest_request != frames * frame_bytes || largest_request > budget) {
std::fprintf(stderr, "%ux%u: unexpected buffer allocation %zu bytes\n",
width, height, largest_request);
if (instance) f0r_destruct(instance);
return 1;
if (frame_bytes > budget / 2) {
if (!instance && array_bytes == 0) return 0;
} else if (instance && array_calls == 2 && array_bytes <= budget &&
pixel_bytes % frame_bytes == 0) {
const size_t frames = pixel_bytes / frame_bytes;
// Account for observed metadata too; another complete frame must not fit.
if (frames >= 2 && budget - array_bytes < frame_bytes + (array_bytes - pixel_bytes) / frames) {
f0r_destruct(instance);
return 0;
}
}
f0r_destruct(instance);
return 0;
std::fprintf(stderr, "%ux%u: unexpected buffer allocation %zu bytes\n",
width, height, largest_request);
if (instance) f0r_destruct(instance);
return 1;
}

static int construction_failure()
Expand Down Expand Up @@ -90,16 +113,33 @@ static int construction_failure()
std::fprintf(stderr, "Constructor failure corrupted existing parameter metadata\n");
return 1;
}
fail_size = 128 * 8 * 8 * sizeof(uint32_t);
array_calls = array_bytes = pixel_bytes = 0;
instance = f0r_construct(8, 8);
if (!instance) return 1;
const size_t storage_size = pixel_bytes;
const size_t metadata_size = array_bytes - pixel_bytes;
f0r_destruct(instance);
fail_size = storage_size;
array_calls = array_bytes = pixel_bytes = 0;
instance = f0r_construct(8, 8);
fail_size = 0;
if (instance) {
f0r_destruct(instance);
if (instance || array_calls != 1) {
if (instance) f0r_destruct(instance);
std::fprintf(stderr, "Failed frame allocation was accepted\n");
return 1;
}
fail_size = metadata_size;
array_calls = array_bytes = pixel_bytes = 0;
instance = f0r_construct(8, 8);
fail_size = 0;
if (instance || array_calls != 2) {
if (instance) f0r_destruct(instance);
std::fprintf(stderr, "Failed history metadata allocation was accepted\n");
return 1;
}
largest_request = 0;
instance = f0r_construct(8192, 16385);
// Two pixel frames alone fill the budget, leaving no room for metadata.
instance = f0r_construct(8, (unsigned int)(budget / (2 * 8 * sizeof(uint32_t))));
if (instance || largest_request > budget) {
if (instance) f0r_destruct(instance);
std::fprintf(stderr, "Dimensions exceeding the two-frame budget were accepted\n");
Expand All @@ -111,23 +151,28 @@ static int construction_failure()
static int rendering_without_allocations()
{
alignas(16) uint32_t input[64], output[64];
array_calls = array_bytes = pixel_bytes = 0;
f0r_instance_t instance = f0r_construct(8, 8);
if (!instance) return 1;
const size_t capacity = pixel_bytes / sizeof(input);
parameter(instance, 0, 1.0);
parameter(instance, 1, 0.5);
const size_t before = allocation_calls;
bool failed = false;
deny_allocations = true;
try {
// Dense timestamps reach the frame-count limit before the two-second offset.
for (unsigned int frame = 0; frame < 1000; ++frame) {
const unsigned int oldest = frame < 128 ? 0 : frame - 127;
// Retain more than 128 dense frames; the small-budget build also fills
// the pool and checks eviction at its byte-derived capacity.
for (unsigned int frame = 0; frame < 3000; ++frame) {
const unsigned int by_time = frame < 2048 ? 0 : frame - 2048;
const unsigned int by_capacity = frame < capacity ? 0 : (unsigned int)(frame + 1 - capacity);
const unsigned int oldest = by_time > by_capacity ? by_time : by_capacity;
const unsigned int difference = (unsigned int)std::abs((int)(frame % 251) - (int)(oldest % 251));
for (size_t i = 0; i < 64; ++i) {
uint8_t *p = reinterpret_cast<uint8_t*>(&input[i]);
p[0] = (uint8_t)(frame % 251); p[1] = 7; p[2] = 9; p[3] = 200;
}
f0r_update(instance, frame / 1000.0, input, output);
f0r_update(instance, frame / 1024.0, input, output);
const uint8_t *p = reinterpret_cast<const uint8_t*>(&output[0]);
if (p[0] != difference || p[1] != 0 || p[2] != 0 || p[3] != 200) failed = true;
}
Expand All @@ -150,6 +195,7 @@ static int rendering_without_allocations()
static int working_set(double offset, size_t expected_slots)
{
captured_storage = 0;
array_calls = array_bytes = pixel_bytes = 0;
capture_storage = true;
f0r_instance_t instance = f0r_construct(8, 8);
capture_storage = false;
Expand All @@ -161,7 +207,7 @@ static int working_set(double offset, size_t expected_slots)
std::memset(input, 0x11, sizeof(input));
parameter(instance, 0, offset);
// Binary-exact timestamps make the expected history length unambiguous.
// Repeated wraparound must reuse released buffers, leaving unused ones intact.
// Repeated rendering must reuse released buffers, leaving unused ones intact.
for (unsigned int frame = 0; frame < 512; ++frame)
f0r_update(instance, frame / 32.0, input, output);
// Replacing the current frame and seeking must release their slots as well.
Expand All @@ -171,7 +217,7 @@ static int working_set(double offset, size_t expected_slots)
f0r_update(instance, time, input, output);
}
size_t touched = 0;
for (size_t slot = 0; slot < 128; ++slot) {
for (size_t slot = 0; slot < captured_bytes / small_frame_bytes; ++slot) {
for (size_t byte = 0; byte < small_frame_bytes; ++byte) {
if (captured_storage[slot * small_frame_bytes + byte] != 0xa5) {
++touched;
Expand All @@ -196,7 +242,7 @@ int main()
errors += construction_budget(1920, 1080);
errors += construction_budget(3840, 2160);
errors += construction_budget(4096, 4096);
errors += construction_budget(8192, 16384); // Exactly two frames fit in 1 GiB.
errors += construction_budget(8192, 16383); // Two frames plus metadata fit in 1 GiB.
errors += construction_failure();
errors += rendering_without_allocations();
errors += working_set(0.0, 2);
Expand Down
Loading