Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjulek committed Jan 24, 2025
1 parent 06d6ebf commit 6d8db52
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 70 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
.paths = .{""},
.dependencies = .{
.vapoursynth = .{
.url = "git+https://github.com/dnjulek/vapoursynth-zig.git#94da91b6229a20c1b8d49825773becead339c1e9",
.hash = "12200b2cb28aa0c1e7eb62f23c05c40da6a8c1b4a80f81a187159c9a65cf7e2e7580",
.url = "git+https://github.com/dnjulek/vapoursynth-zig.git#f555540e1852bee98a1b9d01de0acea4ce9cc662",
.hash = "12206056aace85f8d3c09cc3e4cc6c147e21be5ad4080bb4fda4b710fdae861c8c3b",
},
},
}
53 changes: 43 additions & 10 deletions src/helper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn absDiff(x: anytype, y: anytype) @TypeOf(x) {
return if (x > y) (x - y) else (y - x);
}

pub fn mapGetPlanes(in: zapi.ZMapRO, out: zapi.ZMapRW, nodes: []?*vs.Node, process: []bool, num_planes: c_int, comptime name: []const u8, vsapi: ?*const vs.API) !void {
pub fn mapGetPlanes(in: zapi.ZMapRO, out: zapi.ZMapRW, nodes: []const ?*vs.Node, process: []bool, num_planes: c_int, comptime name: []const u8, vsapi: ?*const vs.API) !void {
const num_e = in.numElements("planes") orelse return;
@memset(process, false);

Expand Down Expand Up @@ -118,21 +118,54 @@ pub fn mapGetPlanes(in: zapi.ZMapRO, out: zapi.ZMapRW, nodes: []?*vs.Node, proce
}
}

pub fn compareNodes(out: zapi.ZMapRW, node1: ?*vs.Node, node2: ?*vs.Node, vi1: *const vs.VideoInfo, vi2: *const vs.VideoInfo, comptime name: []const u8, vsapi: ?*const vs.API) !void {
if (node2 == null) {
return;
}
pub const ClipLen = enum {
SAME_LEN,
BIGGER_THAN,
MISMATCH,
};

pub fn compareNodes(out: zapi.ZMapRW, nodes: []const ?*vs.Node, len: ClipLen, comptime name: []const u8, vsapi: ?*const vs.API) !void {
const vi0 = vsapi.?.getVideoInfo.?(nodes[0]);
var err_msg: ?[]const u8 = null;
errdefer {
out.setError(err_msg.?);
vsapi.?.freeNode.?(node1);
vsapi.?.freeNode.?(node2);
for (nodes) |node| vsapi.?.freeNode.?(node);
}

if (!vsh.isSameVideoInfo(vi1, vi2) or !vsh.isConstantVideoFormat(vi2)) {
err_msg = name ++ ": both input clips must have the same format.";
return error.node;
for (nodes[1..]) |node| {
const vi = vsapi.?.getVideoInfo.?(node);
if (!vsh.isConstantVideoFormat(vi)) {
err_msg = name ++ ": all input clips must have constant format.";
return error.constant_format;
}
if ((vi0.width != vi.width) or (vi0.height != vi.height)) {
err_msg = name ++ ": all input clips must have the same width and height.";
return error.width_height;
}
if (vi0.format.colorFamily != vi.format.colorFamily) {
err_msg = name ++ ": all input clips must have the same color family.";
return error.color_family;
}
if ((vi0.format.subSamplingW != vi.format.subSamplingW) or (vi0.format.subSamplingH != vi.format.subSamplingH)) {
err_msg = name ++ ": all input clips must have the same subsampling.";
return error.subsampling;
}
if (vi0.format.bitsPerSample != vi.format.bitsPerSample) {
err_msg = name ++ ": all input clips must have the same bit depth.";
return error.bit_depth;
}

switch (len) {
.SAME_LEN => if (vi0.numFrames != vi.numFrames) {
err_msg = name ++ ": all input clips must have the same length.";
return error.length;
},
.BIGGER_THAN => if (vi0.numFrames > vi.numFrames) {
err_msg = name ++ ": second clip has less frames than input clip.";
return error.length;
},
.MISMATCH => {},
}
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/vapoursynth/adaptive_binarize.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,18 @@ pub export fn adaptiveBinarizeCreate(in: ?*const vs.Map, out: ?*vs.Map, user_dat
const map_out = zapi.ZMap.init(out, vsapi);

d.node, d.vi = map_in.getNodeVi("clip");
d.node2, const vi2 = map_in.getNodeVi("clip2");
d.node2 = map_in.getNode("clip2");

const nodes = [_]?*vs.Node{ d.node, d.node2 };
helper.compareNodes(map_out, &nodes, .BIGGER_THAN, filter_name, vsapi) catch return;

helper.compareNodes(map_out, d.node, d.node2, d.vi, vi2, filter_name, vsapi) catch return;
if ((d.vi.format.sampleType != .Integer) or (d.vi.format.bitsPerSample != 8)) {
map_out.setError(filter_name ++ ": only 8 bit int format supported.");
vsapi.?.freeNode.?(d.node);
vsapi.?.freeNode.?(d.node2);
return;
}

if (d.vi.numFrames != vi2.numFrames) {
vsapi.?.mapSetError.?(out, filter_name ++ " : clips must have the same length.");
vsapi.?.freeNode.?(d.node);
vsapi.?.freeNode.?(d.node2);
return;
}

const c_param = map_in.getInt(i32, "c") orelse 3;
for (&d.tab, 0..) |*i, n| {
i.* = if (@as(i32, @intCast(n)) - 255 <= -c_param) 255 else 0;
Expand Down
34 changes: 5 additions & 29 deletions src/vapoursynth/bilateral.zig
Original file line number Diff line number Diff line change
Expand Up @@ -217,35 +217,11 @@ pub export fn bilateralCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*an
}
}

d.join = false;
d.node2 = map_in.getNode("ref");
if (d.node2 != null) {
d.join = true;
const rvi: *const vs.VideoInfo = vsapi.?.getVideoInfo.?(d.node2);
if ((d.vi.width != rvi.width) or (d.vi.height != rvi.height)) {
map_out.setError("Bilateral: input clip and clip \"ref\" must be of the same size");
vsapi.?.freeNode.?(d.node1);
vsapi.?.freeNode.?(d.node2);
return;
}
if (d.vi.format.colorFamily != rvi.format.colorFamily) {
map_out.setError("Bilateral: input clip and clip \"ref\" must be of the same color family");
vsapi.?.freeNode.?(d.node1);
vsapi.?.freeNode.?(d.node2);
return;
}
if ((d.vi.format.subSamplingH != rvi.format.subSamplingH) or (d.vi.format.subSamplingW != rvi.format.subSamplingW)) {
map_out.setError("Bilateral: input clip and clip \"ref\" must be of the same subsampling");
vsapi.?.freeNode.?(d.node1);
vsapi.?.freeNode.?(d.node2);
return;
}
if (d.vi.format.bitsPerSample != rvi.format.bitsPerSample) {
map_out.setError("Bilateral: input clip and clip \"ref\" must be of the same bit depth");
vsapi.?.freeNode.?(d.node1);
vsapi.?.freeNode.?(d.node2);
return;
}
d.join = d.node2 != null;
if (d.join) {
const nodes = [_]?*vs.Node{ d.node1, d.node2 };
helper.compareNodes(map_out, &nodes, .BIGGER_THAN, filter_name, vsapi) catch return;
}

i = 0;
Expand Down Expand Up @@ -336,7 +312,7 @@ pub export fn bilateralCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*an
deps1[0],
vs.FilterDependency{
.source = d.node2,
.requestPattern = if (d.vi.numFrames <= vsapi.?.getVideoInfo.?(d.node2).numFrames) .StrictSpatial else .General,
.requestPattern = .StrictSpatial,
},
};

Expand Down
13 changes: 7 additions & 6 deletions src/vapoursynth/planeaverage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,17 @@ pub export fn planeAverageCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?
_ = user_data;
var d: Data = undefined;

const map_in = zapi.ZMap.init(in, vsapi);
const map_out = zapi.ZMap.init(out, vsapi);
const map_in = zapi.ZMapRO.init(in, vsapi);
const map_out = zapi.ZMapRW.init(out, vsapi);
d.node1, d.vi = map_in.getNodeVi("clipa");
const dt = helper.DataType.select(map_out, d.node1, d.vi, filter_name) catch return;

d.node2, const vi2 = map_in.getNodeVi("clipb");
helper.compareNodes(map_out, d.node1, d.node2, d.vi, vi2, filter_name, vsapi) catch return;
d.node2 = map_in.getNode("clipb");
const nodes = [_]?*vs.Node{ d.node1, d.node2 };
helper.compareNodes(map_out, &nodes, .BIGGER_THAN, filter_name, vsapi) catch return;

d.peak = @floatFromInt(helper.getPeak(d.vi));
var nodes = [_]?*vs.Node{ d.node1, d.node2 };

var planes = [3]bool{ true, false, false };
helper.mapGetPlanes(map_in, map_out, &nodes, &planes, d.vi.format.numPlanes, filter_name, vsapi) catch return;
d.planes = planes;
Expand Down Expand Up @@ -145,7 +146,7 @@ pub export fn planeAverageCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?
deps1[0],
vs.FilterDependency{
.source = d.node2,
.requestPattern = if (d.vi.numFrames <= vi2.numFrames) .StrictSpatial else .General,
.requestPattern = .StrictSpatial,
},
};

Expand Down
10 changes: 5 additions & 5 deletions src/vapoursynth/planeminmax.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ pub export fn planeMinMaxCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*
d.node1, d.vi = map_in.getNodeVi("clipa");
const dt = helper.DataType.select(map_out, d.node1, d.vi, filter_name) catch return;

d.node2, const vi2 = map_in.getNodeVi("clipb");
helper.compareNodes(map_out, d.node1, d.node2, d.vi, vi2, filter_name, vsapi) catch return;
d.node2 = map_in.getNode("clipb");
const nodes = [_]?*vs.Node{ d.node1, d.node2 };
helper.compareNodes(map_out, &nodes, .BIGGER_THAN, filter_name, vsapi) catch return;

var nodes = [_]?*vs.Node{ d.node1, d.node2 };
var planes = [3]bool{ true, false, false };
helper.mapGetPlanes(map_in, map_out, &nodes, &planes, d.vi.format.numPlanes, filter_name, vsapi) catch return;
d.planes = planes;
Expand Down Expand Up @@ -145,7 +145,7 @@ pub export fn planeMinMaxCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*
deps1[0],
vs.FilterDependency{
.source = d.node2,
.requestPattern = if (d.vi.numFrames <= vi2.numFrames) .StrictSpatial else .General,
.requestPattern = .StrictSpatial,
},
};

Expand All @@ -164,7 +164,7 @@ pub export fn planeMinMaxCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*
vsapi.?.createVideoFilter.?(out, filter_name, d.vi, getFrame, planeMinMaxFree, .Parallel, deps, deps_len, data, core);
}

pub fn getThr(in: zapi.ZMapRO, out: zapi.ZMapRW, nodes: []?*vs.Node, comptime key: []const u8, vsapi: ?*const vs.API) !f32 {
pub fn getThr(in: zapi.ZMapRO, out: zapi.ZMapRW, nodes: []const ?*vs.Node, comptime key: []const u8, vsapi: ?*const vs.API) !f32 {
var err_msg: ?[]const u8 = null;
errdefer {
out.setError(err_msg.?);
Expand Down
18 changes: 9 additions & 9 deletions src/vapoursynth/rfs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const allocator = std.heap.c_allocator;
pub const filter_name = "RFS";

const Data = struct {
node1: *vs.Node,
node2: *vs.Node,
node1: ?*vs.Node,
node2: ?*vs.Node,
replace: []bool,
};

Expand Down Expand Up @@ -46,11 +46,11 @@ pub export fn rfsCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*anyopaqu
const map_in = zapi.ZMapRO.init(in, vsapi);
const map_out = zapi.ZMapRW.init(out, vsapi);

d.node1 = vsapi.?.mapGetNode.?(in, "clipa", 0, &node_err).?;
d.node2 = vsapi.?.mapGetNode.?(in, "clipb", 0, &node_err).?;
var vi = vsapi.?.getVideoInfo.?(d.node1).*;
d.node1, const vi = map_in.getNodeVi("clipa");
d.node2 = map_in.getNode("clipb");
var vi_out = vi.*;
const mismatch = map_in.getBool("mismatch") orelse false;
rfsValidateInput(out.?, d.node1, d.node2, &vi, mismatch, vsapi.?) catch return;
rfsValidateInput(out.?, d.node1, d.node2, &vi_out, mismatch, vsapi.?) catch return;
d.replace = allocator.alloc(bool, @intCast(vi.numFrames)) catch unreachable;

const np = vi.format.numPlanes;
Expand All @@ -59,7 +59,7 @@ pub export fn rfsCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*anyopaqu

if ((ne > 0) and (np > 1)) {
var process = [3]bool{ false, false, false };
var nodes = [3]*vs.Node{ d.node1, d.node1, d.node1 };
var nodes = [3]?*vs.Node{ d.node1, d.node1, d.node1 };
i = 0;
while (i < ne) : (i += 1) {
const e = map_in.getInt2(i32, "planes", i).?;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub export fn rfsCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*anyopaqu
},
};

vsapi.?.createVideoFilter.?(out, filter_name, &vi, rfsGetFrame, rfsFree, .Parallel, &deps, deps.len, data, core);
vsapi.?.createVideoFilter.?(out, filter_name, &vi_out, rfsGetFrame, rfsFree, .Parallel, &deps, deps.len, data, core);
}

const rfsInputError = error{
Expand All @@ -124,7 +124,7 @@ const rfsInputError = error{
FrameRate,
};

fn rfsValidateInput(out: *vs.Map, node1: *vs.Node, node2: *vs.Node, outvi: *vs.VideoInfo, mismatch: bool, vsapi: *const vs.API) rfsInputError!void {
fn rfsValidateInput(out: *vs.Map, node1: ?*vs.Node, node2: ?*vs.Node, outvi: *vs.VideoInfo, mismatch: bool, vsapi: *const vs.API) rfsInputError!void {
const vi2 = vsapi.getVideoInfo.?(node2);
var err_msg: ?[*]const u8 = null;

Expand Down

0 comments on commit 6d8db52

Please sign in to comment.