|
| 1 | +#include <inttypes.h> |
| 2 | +#include "wrapper/avs_filter.hpp" |
| 3 | +#include "version.hpp" |
| 4 | +#include "minideen_engine.hpp" |
| 5 | + |
| 6 | +class MiniDeenFilter: public AVSFilter { |
| 7 | + |
| 8 | +protected: |
| 9 | + int radius; |
| 10 | + int thrY, thrUV; |
| 11 | + int y, u, v; |
| 12 | + int bit_per_channel; |
| 13 | + uint16_t magic[MAX_PIXEL_COUNT]; |
| 14 | + decltype (MiniDeenEngine::process_plane_scalar<uint8_t>) *process_plane; |
| 15 | + |
| 16 | +public: |
| 17 | + virtual const char* name() const { return "MiniDeen"; } |
| 18 | + virtual void initialize() { |
| 19 | + radius = ArgAsInt( 1, "radius", 1); |
| 20 | + thrY = ArgAsInt( 2, "thrY", 10); |
| 21 | + thrUV = ArgAsInt( 3, "thrUV", 12); |
| 22 | + y = ArgAsInt( 4, "y", 3); |
| 23 | + u = ArgAsInt( 5, "u", 3); |
| 24 | + v = ArgAsInt( 6, "v", 3); |
| 25 | + bit_per_channel = vi.BitsPerComponent(); |
| 26 | + byte_per_channel = bit_per_channel > 8 ? 2 : 1; |
| 27 | + |
| 28 | + // Check input |
| 29 | + if (!vi.HasVideo()) |
| 30 | + throw("where's the video?"); |
| 31 | + if (thrY < 2 || thrY > 255) |
| 32 | + throw("threshold (Y) must be between 2 and 255 (inclusive)."); |
| 33 | + if (thrUV < 2 || thrUV > 255) |
| 34 | + throw("threshold (UV) must be between 2 and 255 (inclusive)."); |
| 35 | + if (!supported_pixel()) |
| 36 | + throw("only 8..16 bit integer clips with constant format are supported."); |
| 37 | + |
| 38 | + int pixel_max = (1 << bit_per_channel) - 1; |
| 39 | + |
| 40 | + thrY = thrY * pixel_max / 255; |
| 41 | + thrUV = thrUV * pixel_max / 255; |
| 42 | + memset(&magic, 0, sizeof(magic)); |
| 43 | + |
| 44 | + process_plane = (bit_per_channel == 8) ? MiniDeenEngine::process_plane_scalar<uint8_t> |
| 45 | + : MiniDeenEngine::process_plane_scalar<uint16_t>; |
| 46 | +#if defined (MINIDEEN_X86) |
| 47 | + process_plane = (bit_per_channel == 8) ? MiniDeenEngine::process_plane_sse2_8bit |
| 48 | + : MiniDeenEngine::process_plane_sse2_16bit; |
| 49 | + |
| 50 | + for (int i = 2; i < MAX_PIXEL_COUNT; i++) |
| 51 | + magic[i] = (unsigned)(65536.0 / i + 0.5); |
| 52 | +#endif |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + virtual AVSFilter::AFrame get(int n) { |
| 57 | + auto src = GetFrame(child, n); |
| 58 | + auto dst = NewFrame(); |
| 59 | + auto widthY = width(src, PLANAR_Y); |
| 60 | + auto widthUV = width(src, PLANAR_U); |
| 61 | + auto heightY = height(src, PLANAR_Y); |
| 62 | + auto heightUV = height(src, PLANAR_U); |
| 63 | + auto strideY = stride(src, PLANAR_Y); |
| 64 | + auto strideUV = stride(src, PLANAR_U); |
| 65 | + if (y == 3) |
| 66 | + process_plane(src->GetReadPtr(PLANAR_Y), dst->GetWritePtr(PLANAR_Y), 0, widthY, widthY, heightY, strideY, thrY, radius, magic); |
| 67 | + else if (y == 2) |
| 68 | + _env->BitBlt(dst->GetWritePtr(PLANAR_Y), strideY, src->GetReadPtr(PLANAR_Y), strideY, widthY * byte_per_channel, heightY); |
| 69 | + if (u == 3) |
| 70 | + process_plane(src->GetReadPtr(PLANAR_U), dst->GetWritePtr(PLANAR_U), 0, widthUV, widthUV, heightUV, strideUV, thrUV, radius, magic); |
| 71 | + else if (u == 2) |
| 72 | + _env->BitBlt(dst->GetWritePtr(PLANAR_U), strideUV, src->GetReadPtr(PLANAR_U), strideUV, widthUV * byte_per_channel, heightUV); |
| 73 | + if (v == 3) |
| 74 | + process_plane(src->GetReadPtr(PLANAR_V), dst->GetWritePtr(PLANAR_V), 0, widthUV, widthUV, heightUV, strideUV, thrUV, radius, magic); |
| 75 | + else if (v == 2) |
| 76 | + _env->BitBlt(dst->GetWritePtr(PLANAR_V), strideUV, src->GetReadPtr(PLANAR_V), strideUV, widthUV * byte_per_channel, heightUV); |
| 77 | + |
| 78 | + return dst; |
| 79 | + } |
| 80 | +public: |
| 81 | + using AVSFilter::AVSFilter; |
| 82 | +}; |
| 83 | + |
| 84 | + |
| 85 | +const AVS_Linkage *AVS_linkage = NULL; |
| 86 | + |
| 87 | +AVSValue __cdecl CreateAVSFilter(AVSValue args, void* user_data, IScriptEnvironment* env) |
| 88 | +{ |
| 89 | + auto filter = new MiniDeenFilter(args, env); |
| 90 | + try { |
| 91 | + filter->initialize(); |
| 92 | + } |
| 93 | + catch (const char *err) { |
| 94 | + env->ThrowError("%s %s: %s", filter->name(), PLUGIN_VERSION, err); |
| 95 | + } |
| 96 | + return filter; |
| 97 | +} |
| 98 | + |
| 99 | +extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* env, AVS_Linkage* vectors) |
| 100 | +{ |
| 101 | + AVS_linkage = vectors; |
| 102 | + env->AddFunction("MiniDeen", "c[radius]i[thrY]i[thrUV]i[y]i[u]i[v]i", CreateAVSFilter, 0); |
| 103 | + return "MiniDeen"; |
| 104 | +} |
0 commit comments