-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
246 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Examples | ||
|
||
[example](/example/) shows how to use the C binding only.\ | ||
[example-zigapi](/example-zigapi/) shows how to use the C binding with extra Zig struct/methods. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# invert_example | ||
|
||
To use this module in your project you will need: | ||
1. A [build.zig.zon](/examples/example-zigapi/build.zig.zon) file like this. | ||
2. These lines in your [build.zig](/examples/example-zigapi/build.zig): | ||
|
||
```zig | ||
const vapoursynth_dep = b.dependency("vapoursynth", .{ | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
lib.root_module.addImport("vapoursynth", vapoursynth_dep.module("vapoursynth")); | ||
``` | ||
|
||
The [invert_example.zig](/examples/example-zigapi/src/invert_example.zig) is based on [invert_example.c](https://github.com/vapoursynth/vapoursynth/blob/master/sdk/invert_example.c), from the VapourSynth SDK, I recommend checking it out first if you don't know the framework. | ||
|
||
## Building | ||
Zig version should be the master, 0.11.0 not supported. | ||
|
||
``zig build -Doptimize=ReleaseFast`` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//! https://github.com/vapoursynth/vapoursynth/blob/master/sdk/invert_example.c | ||
|
||
const std = @import("std"); | ||
const vapoursynth = @import("vapoursynth"); | ||
|
||
const math = std.math; | ||
const vs = vapoursynth.vapoursynth4; | ||
const vsh = vapoursynth.vshelper; | ||
const zapi = vapoursynth.zigapi; | ||
|
||
// https://ziglang.org/documentation/master/#Choosing-an-Allocator | ||
const allocator = std.heap.c_allocator; | ||
|
||
const InvertData = struct { | ||
node: ?*vs.Node, | ||
vi: *const vs.VideoInfo, | ||
enabled: bool, | ||
}; | ||
|
||
export fn invertGetFrame(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 { | ||
_ = frame_data; | ||
const d: *InvertData = @ptrCast(@alignCast(instance_data)); | ||
|
||
if (activation_reason == .Initial) { | ||
vsapi.?.requestFrameFilter.?(n, d.node, frame_ctx); | ||
} else if (activation_reason == .AllFramesReady) { | ||
var src = zapi.Frame.init(d.node, n, frame_ctx, core, vsapi); | ||
defer src.deinit(); | ||
var dst = src.newVideoFrame(); | ||
|
||
var plane: u32 = 0; | ||
while (plane < d.vi.format.numPlanes) : (plane += 1) { | ||
var srcp = src.getReadPtr(plane); | ||
var dstp = dst.getWritePtr(plane); | ||
|
||
// getDimensions returns a tuple with [width, height, stride], | ||
// use getDimensions2 if you want a struct. | ||
const w, const h, const stride = src.getDimensions(plane); | ||
|
||
var y: u32 = 0; | ||
while (y < h) : (y += 1) { | ||
var x: u32 = 0; | ||
while (x < w) : (x += 1) { | ||
dstp[x] = if (d.enabled) ~(srcp[x]) else srcp[x]; | ||
} | ||
|
||
dstp = dstp[stride..]; | ||
srcp = srcp[stride..]; | ||
} | ||
} | ||
|
||
return dst.frame; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export fn invertFree(instance_data: ?*anyopaque, core: ?*vs.Core, vsapi: ?*const vs.API) callconv(.C) void { | ||
_ = core; | ||
const d: *InvertData = @ptrCast(@alignCast(instance_data)); | ||
vsapi.?.freeNode.?(d.node); | ||
allocator.destroy(d); | ||
} | ||
|
||
export fn invertCreate(in: ?*const vs.Map, out: ?*vs.Map, user_data: ?*anyopaque, core: ?*vs.Core, vsapi: ?*const vs.API) callconv(.C) void { | ||
_ = user_data; | ||
var d: InvertData = undefined; | ||
var map = zapi.Map.init(in, out, vsapi); | ||
|
||
// getNodeVi returns a tuple with [vs.Node, vs.VideoInfo], | ||
// use getNodeVi2 if you want a struct. | ||
d.node, d.vi = map.getNodeVi("clip"); | ||
|
||
if (!vsh.isConstantVideoFormat(d.vi) or (d.vi.format.sampleType != .Integer) or (d.vi.format.bitsPerSample != 8)) { | ||
map.setError("Invert: only constant format 8bit integer input supported"); | ||
vsapi.?.freeNode.?(d.node); | ||
return; | ||
} | ||
|
||
// https://ziglang.org/documentation/master/#Optionals | ||
const enabled = map.getInt(i32, "enabled") orelse 1; | ||
|
||
if ((enabled < 0) or (enabled > 1)) { | ||
map.setError("Invert: enabled must be 0 or 1"); | ||
vsapi.?.freeNode.?(d.node); | ||
return; | ||
} | ||
|
||
d.enabled = enabled == 1; | ||
|
||
const data: *InvertData = allocator.create(InvertData) catch unreachable; | ||
data.* = d; | ||
|
||
var deps = [_]vs.FilterDependency{ | ||
vs.FilterDependency{ | ||
.source = d.node, | ||
.requestPattern = .StrictSpatial, | ||
}, | ||
}; | ||
|
||
vsapi.?.createVideoFilter.?(out, "Invert", d.vi, invertGetFrame, invertFree, .Parallel, &deps, deps.len, data, core); | ||
} | ||
|
||
export fn VapourSynthPluginInit2(plugin: *vs.Plugin, vspapi: *const vs.PLUGINAPI) void { | ||
_ = vspapi.configPlugin.?("com.example.zinvert", "zinvert", "VapourSynth Invert Example", vs.makeVersion(1, 0), vs.VAPOURSYNTH_API_VERSION, 0, plugin); | ||
_ = vspapi.registerFunction.?("Filter", "clip:vnode;enabled:int:opt;", "clip:vnode;", invertCreate, null, plugin); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# invert_example | ||
|
||
To use this module in your project you will need: | ||
1. A [build.zig.zon](/examples/example/build.zig.zon) file like this. | ||
2. These lines in your [build.zig](/examples/example/build.zig): | ||
|
||
```zig | ||
const vapoursynth_dep = b.dependency("vapoursynth", .{ | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
lib.root_module.addImport("vapoursynth", vapoursynth_dep.module("vapoursynth")); | ||
``` | ||
|
||
The [invert_example.zig](/examples/example/src/invert_example.zig) is based on [invert_example.c](https://github.com/vapoursynth/vapoursynth/blob/master/sdk/invert_example.c), from the VapourSynth SDK, I recommend checking it out first if you don't know the framework. | ||
|
||
## Building | ||
Zig version should be the master. | ||
|
||
``zig build -Doptimize=ReleaseFast`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const std = @import("std"); | ||
|
||
pub const min_zig_version = std.SemanticVersion{ .major = 0, .minor = 12, .patch = 0, .pre = "dev.2158" }; | ||
|
||
pub fn build(b: *std.Build) void { | ||
ensureZigVersion() catch return; | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const lib = b.addSharedLibrary(.{ | ||
.name = "invert_example", | ||
.root_source_file = .{ .path = "src/invert_example.zig" }, | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
const vapoursynth_dep = b.dependency("vapoursynth", .{ | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
lib.root_module.addImport("vapoursynth", vapoursynth_dep.module("vapoursynth")); | ||
lib.linkLibC(); | ||
|
||
if (lib.root_module.optimize == .ReleaseFast) { | ||
lib.root_module.strip = true; | ||
} | ||
|
||
b.installArtifact(lib); | ||
} | ||
|
||
fn ensureZigVersion() !void { | ||
var installed_ver = @import("builtin").zig_version; | ||
installed_ver.build = null; | ||
|
||
if (installed_ver.order(min_zig_version) == .lt) { | ||
std.log.err("\n" ++ | ||
\\--------------------------------------------------------------------------- | ||
\\ | ||
\\Installed Zig compiler version is too old. | ||
\\ | ||
\\Min. required version: {any} | ||
\\Installed version: {any} | ||
\\ | ||
\\Please install newer version and try again. | ||
\\Latest version can be found here: https://ziglang.org/download/ | ||
\\ | ||
\\--------------------------------------------------------------------------- | ||
\\ | ||
, .{ min_zig_version, installed_ver }); | ||
return error.ZigIsTooOld; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.{ | ||
.name = "invert_example", | ||
.version = "1.0.0", | ||
.paths = .{""}, | ||
.dependencies = .{ | ||
.vapoursynth = .{ | ||
.url = "https://github.com/dnjulek/vapoursynth-zig/archive/2f6a59a6cccf23cec7ca8a3abb3f316593c5ed7b.tar.gz", | ||
.hash = "12201cb4befda1224caa03a57a2188d5d5e985a7fdd7a8b3277d385dba4cb0e272cb", | ||
}, | ||
}, | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters