Skip to content

Commit

Permalink
Add prop arg to PlaneAverage/PlaneMinMax
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjulek committed Apr 16, 2024
1 parent fc9ca17 commit 8a08a34
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 59 deletions.
46 changes: 37 additions & 9 deletions src/filters/planeaverage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const PlaneAverageData = struct {
dt: helper.DataType,
peak: f32,
planes: [3]bool,
prop_buff: ?[]u8,
prop: ?StringProp,
};

const StringProp = struct {
d: []u8,
a: []u8,
};

export fn planeAverageGetFrame(n: c_int, activation_reason: vs.ActivationReason, instance_data: ?*anyopaque, frame_data: ?*?*anyopaque, frame_ctx: ?*vs.FrameContext, core: ?*vs.Core, vsapi: ?*const vs.API) callconv(.C) ?*const vs.Frame {
Expand All @@ -27,9 +34,7 @@ export fn planeAverageGetFrame(n: c_int, activation_reason: vs.ActivationReason,

if (activation_reason == .Initial) {
vsapi.?.requestFrameFilter.?(n, d.node1, frame_ctx);
if (d.node2) |node| {
vsapi.?.requestFrameFilter.?(n, node, frame_ctx);
}
if (d.node2) |node| vsapi.?.requestFrameFilter.?(n, node, frame_ctx);
} else if (activation_reason == .AllFramesReady) {
var src = zapi.Frame.init(d.node1, n, frame_ctx, core, vsapi);
defer src.deinit();
Expand Down Expand Up @@ -66,10 +71,10 @@ export fn planeAverageGetFrame(n: c_int, activation_reason: vs.ActivationReason,
.U16 => process.averageRef(u16, srcp, refp, stride, w, h, d.exclude, d.peak),
.F32 => process.averageRef(f32, srcp, refp, stride, w, h, d.exclude, d.peak),
};
_ = vsapi.?.mapSetFloat.?(props, "psmDiff", stats.diff, .Append);
_ = vsapi.?.mapSetFloat.?(props, if (d.prop != null) d.prop.?.d.ptr else "psmDiff", stats.diff, .Append);
avg = stats.avg;
}
_ = vsapi.?.mapSetFloat.?(props, "psmAvg", avg, .Append);
_ = vsapi.?.mapSetFloat.?(props, if (d.prop != null) d.prop.?.a.ptr else "psmAvg", avg, .Append);
}

return dst.frame;
Expand All @@ -86,10 +91,8 @@ export fn planeAverageFree(instance_data: ?*anyopaque, core: ?*vs.Core, vsapi: ?
.f => allocator.free(d.exclude.f),
}

if (d.node2) |node| {
vsapi.?.freeNode.?(node);
}

if (d.node2) |node| vsapi.?.freeNode.?(node);
if (d.prop_buff) |buff| allocator.free(buff);
vsapi.?.freeNode.?(d.node1);
allocator.destroy(d);
}
Expand All @@ -110,6 +113,7 @@ pub export fn planeAverageCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?
helper.mapGetPlanes(in, out, &nodes, &planes, d.vi.format.numPlanes, filter_name, vsapi) catch return;
d.planes = planes;

d.prop = getPropData(map, "prop", allocator, &d.prop_buff);
const exclude_in = map.getIntArray("exclude");
if (exclude_in) |ein| {
if (d.dt == .F32) {
Expand Down Expand Up @@ -152,3 +156,27 @@ pub export fn planeAverageCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?

vsapi.?.createVideoFilter.?(out, filter_name, d.vi, planeAverageGetFrame, planeAverageFree, .Parallel, deps, deps_len, data, core);
}

pub fn getPropData(self: zapi.Map, comptime key: []const u8, data_allocator: std.mem.Allocator, data_buff: *?[]u8) ?StringProp {
var err: vs.MapPropertyError = undefined;
data_buff.* = null;
const data_ptr = self.vsapi.?.mapGetData.?(self.in, key.ptr, 0, &err);
if (err != .Success) {
return null;
}

const data_len = self.vsapi.?.mapGetDataSize.?(self.in, key.ptr, 0, &err);
if ((err != .Success) or (data_len < 1)) {
return null;
}

const udata_len: u32 = @bitCast(data_len);
const diff_len = udata_len + 5;
const avg_len = udata_len + 4;
const data = data_ptr[0..udata_len];
data_buff.* = data_allocator.alloc(u8, diff_len + avg_len) catch unreachable;
return .{
.d = std.fmt.bufPrint(data_buff.*.?[0..diff_len], "{s}Diff\x00", .{data}) catch unreachable,
.a = std.fmt.bufPrint(data_buff.*.?[diff_len..], "{s}Avg\x00", .{data}) catch unreachable,
};
}
61 changes: 45 additions & 16 deletions src/filters/planeminmax.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub const PlaneMinMaxData = struct {
hist_size: u32,
planes: [3]bool,
dt: helper.DataType,
prop_buff: ?[]u8,
prop: ?StringProp,
};

const StringProp = struct {
d: []u8,
ma: []u8,
mi: []u8,
};

export fn planeMinMaxGetFrame(n: c_int, activation_reason: vs.ActivationReason, instance_data: ?*anyopaque, frame_data: ?*?*anyopaque, frame_ctx: ?*vs.FrameContext, core: ?*vs.Core, vsapi: ?*const vs.API) callconv(.C) ?*const vs.Frame {
Expand All @@ -29,9 +37,7 @@ export fn planeMinMaxGetFrame(n: c_int, activation_reason: vs.ActivationReason,

if (activation_reason == .Initial) {
vsapi.?.requestFrameFilter.?(n, d.node1, frame_ctx);
if (d.node2) |node| {
vsapi.?.requestFrameFilter.?(n, node, frame_ctx);
}
if (d.node2) |node| vsapi.?.requestFrameFilter.?(n, node, frame_ctx);
} else if (activation_reason == .AllFramesReady) {
var src = zapi.Frame.init(d.node1, n, frame_ctx, core, vsapi);
defer src.deinit();
Expand Down Expand Up @@ -69,19 +75,19 @@ export fn planeMinMaxGetFrame(n: c_int, activation_reason: vs.ActivationReason,
};

_ = switch (stats) {
.i => vsapi.?.mapSetFloat.?(props, "psmDiff", stats.i.diff, .Append),
.f => vsapi.?.mapSetFloat.?(props, "psmDiff", stats.f.diff, .Append),
.i => vsapi.?.mapSetFloat.?(props, if (d.prop != null) d.prop.?.d.ptr else "psmDiff", stats.i.diff, .Append),
.f => vsapi.?.mapSetFloat.?(props, if (d.prop != null) d.prop.?.d.ptr else "psmDiff", stats.f.diff, .Append),
};
}

switch (stats) {
.i => {
_ = vsapi.?.mapSetInt.?(props, "psmMax", stats.i.max, .Append);
_ = vsapi.?.mapSetInt.?(props, "psmMin", stats.i.min, .Append);
_ = vsapi.?.mapSetInt.?(props, if (d.prop != null) d.prop.?.ma.ptr else "psmMax", stats.i.max, .Append);
_ = vsapi.?.mapSetInt.?(props, if (d.prop != null) d.prop.?.mi.ptr else "psmMin", stats.i.min, .Append);
},
.f => {
_ = vsapi.?.mapSetFloat.?(props, "psmMax", stats.f.max, .Append);
_ = vsapi.?.mapSetFloat.?(props, "psmMin", stats.f.min, .Append);
_ = vsapi.?.mapSetFloat.?(props, if (d.prop != null) d.prop.?.ma.ptr else "psmMax", stats.f.max, .Append);
_ = vsapi.?.mapSetFloat.?(props, if (d.prop != null) d.prop.?.mi.ptr else "psmMin", stats.f.min, .Append);
},
}
}
Expand All @@ -96,10 +102,8 @@ export fn planeMinMaxFree(instance_data: ?*anyopaque, core: ?*vs.Core, vsapi: ?*
_ = core;
const d: *PlaneMinMaxData = @ptrCast(@alignCast(instance_data));

if (d.node2) |node| {
vsapi.?.freeNode.?(node);
}

if (d.node2) |node| vsapi.?.freeNode.?(node);
if (d.prop_buff) |buff| allocator.free(buff);
vsapi.?.freeNode.?(d.node1);
allocator.destroy(d);
}
Expand All @@ -123,6 +127,7 @@ pub export fn planeMinMaxCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*
d.peak = @intCast(d.hist_size - 1);
d.maxthr = getThr(in, out, &nodes, "maxthr", vsapi) catch return;
d.minthr = getThr(in, out, &nodes, "minthr", vsapi) catch return;
d.prop = getPropData(map, "prop", allocator, &d.prop_buff);

const data: *PlaneMinMaxData = allocator.create(PlaneMinMaxData) catch unreachable;
data.* = d;
Expand Down Expand Up @@ -156,9 +161,7 @@ pub fn getThr(in: ?*const vs.Map, out: ?*vs.Map, nodes: []?*vs.Node, comptime ke
var err_msg: ?[*]const u8 = null;
errdefer {
vsapi.?.mapSetError.?(out, err_msg.?);
for (nodes) |node| {
vsapi.?.freeNode.?(node);
}
for (nodes) |node| vsapi.?.freeNode.?(node);
}

const thr = vsh.mapGetN(f32, in, key.ptr, 0, vsapi) orelse 0;
Expand All @@ -169,3 +172,29 @@ pub fn getThr(in: ?*const vs.Map, out: ?*vs.Map, nodes: []?*vs.Node, comptime ke

return thr;
}

pub fn getPropData(self: zapi.Map, comptime key: []const u8, data_allocator: std.mem.Allocator, data_buff: *?[]u8) ?StringProp {
var err: vs.MapPropertyError = undefined;
data_buff.* = null;
const data_ptr = self.vsapi.?.mapGetData.?(self.in, key.ptr, 0, &err);
if (err != .Success) {
return null;
}

const data_len = self.vsapi.?.mapGetDataSize.?(self.in, key.ptr, 0, &err);
if ((err != .Success) or (data_len < 1)) {
return null;
}

const udata_len: u32 = @bitCast(data_len);
const diff_len = udata_len + 5;
const max_len = udata_len + 4;
const min_len = udata_len + 4;
const data = data_ptr[0..udata_len];
data_buff.* = data_allocator.alloc(u8, diff_len + max_len + min_len) catch unreachable;
return .{
.d = std.fmt.bufPrint(data_buff.*.?[0..diff_len], "{s}Diff\x00", .{data}) catch unreachable,
.ma = std.fmt.bufPrint(data_buff.*.?[diff_len..], "{s}Max\x00", .{data}) catch unreachable,
.mi = std.fmt.bufPrint(data_buff.*.?[(diff_len + max_len)..], "{s}Min\x00", .{data}) catch unreachable,
};
}
35 changes: 3 additions & 32 deletions src/helper.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const std = @import("std");
const vszip = @import("vszip.zig");
const vs = vszip.vs;
const vsh = vszip.vsh;
const zapi = vszip.zapi;

pub const DataType = enum(c_int) {
U8 = 1,
Expand All @@ -23,9 +25,7 @@ pub fn mapGetPlanes(in: ?*const vs.Map, out: ?*vs.Map, nodes: []?*vs.Node, proce
var err_msg: ?[*]const u8 = null;
errdefer {
vsapi.?.mapSetError.?(out, err_msg.?);
for (nodes) |node| {
vsapi.?.freeNode.?(node);
}
for (nodes) |node| vsapi.?.freeNode.?(node);
}

var i: c_int = 0;
Expand Down Expand Up @@ -63,32 +63,3 @@ pub fn compareNodes(out: ?*vs.Map, node1: ?*vs.Node, node2: ?*vs.Node, vi1: *con
return error.node;
}
}

pub fn newVideoFrame(src: ?*const vs.Frame, core: ?*vs.Core, vsapi: ?*const vs.API) ?*vs.Frame {
return vsapi.?.newVideoFrame.?(
vsapi.?.getVideoFrameFormat.?(src),
vsapi.?.getFrameWidth.?(src, 0),
vsapi.?.getFrameHeight.?(src, 0),
src,
core,
);
}

pub fn newVideoFrame2(src: ?*const vs.Frame, process: []bool, core: ?*vs.Core, vsapi: ?*const vs.API) ?*vs.Frame {
var planes = [_]c_int{ 0, 1, 2 };
var cp_planes = [_]?*const vs.Frame{
if (process[0]) null else src,
if (process[1]) null else src,
if (process[2]) null else src,
};

return vsapi.?.newVideoFrame2.?(
vsapi.?.getVideoFrameFormat.?(src),
vsapi.?.getFrameWidth.?(src, 0),
vsapi.?.getFrameHeight.?(src, 0),
&cp_planes,
&planes,
src,
core,
);
}
4 changes: 2 additions & 2 deletions src/vszip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export fn VapourSynthPluginInit2(plugin: *vs.Plugin, vspapi: *const vs.PLUGINAPI
_ = vspapi.configPlugin.?("com.julek.vszip", "vszip", "VapourSynth Zig Image Process", vs.makeVersion(1, 0), vs.VAPOURSYNTH_API_VERSION, 0, plugin);
_ = vspapi.registerFunction.?(bilateral.filter_name, "clip:vnode;ref:vnode:opt;sigmaS:float[]:opt;sigmaR:float[]:opt;planes:int[]:opt;algorithm:int[]:opt;PBFICnum:int[]:opt", "clip:vnode;", bilateral.bilateralCreate, null, plugin);
_ = vspapi.registerFunction.?(boxblur.filter_name, "clip:vnode;planes:int[]:opt;hradius:int:opt;hpasses:int:opt;vradius:int:opt;vpasses:int:opt", "clip:vnode;", boxblur.boxBlurCreate, null, plugin);
_ = vspapi.registerFunction.?(pavg.filter_name, "clipa:vnode;exclude:int[];clipb:vnode:opt;planes:int[]:opt;", "clip:vnode;", pavg.planeAverageCreate, null, plugin);
_ = vspapi.registerFunction.?(pmm.filter_name, "clipa:vnode;minthr:float:opt;maxthr:float:opt;clipb:vnode:opt;planes:int[]:opt;", "clip:vnode;", pmm.planeMinMaxCreate, null, plugin);
_ = vspapi.registerFunction.?(pavg.filter_name, "clipa:vnode;exclude:int[];clipb:vnode:opt;planes:int[]:opt;prop:data:opt;", "clip:vnode;", pavg.planeAverageCreate, null, plugin);
_ = vspapi.registerFunction.?(pmm.filter_name, "clipa:vnode;minthr:float:opt;maxthr:float:opt;clipb:vnode:opt;planes:int[]:opt;prop:data:opt;", "clip:vnode;", pmm.planeMinMaxCreate, null, plugin);
_ = vspapi.registerFunction.?(rfs.filter_name, "clipa:vnode;clipb:vnode;frames:int[];mismatch:int:opt;planes:int[]:opt;", "clip:vnode;", rfs.rfsCreate, null, plugin);
}

0 comments on commit 8a08a34

Please sign in to comment.