Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. |
25th January 2023, 03:03 | #1 | Link |
Registered User
Join Date: Feb 2016
Location: Nonsense land
Posts: 339
|
My computer explodes when I run this plugin.
Can someone teach me what I'm doing wrong, please
Everytime I write on a clip inside a struct or a class nothing works :\. Code:
#pragma once #include <cmath> #include <windows.h> #include <avisynth.h> using namespace std; struct CSharpenFilter { PVideoFrame src; unsigned char* srcp; int height, row_size, src_pitch; //int* vector_srcp = new int[height * row_size]; //int* vector_blur = new int[height * row_size]; void GetFrame(PClip child, int n, IScriptEnvironment* env) { src = child->GetFrame(n, env); env->MakeWritable(&src); } void GetPlaneY() { srcp = src->GetWritePtr(PLANAR_Y); src_pitch = src->GetPitch(PLANAR_Y); height = src->GetHeight(PLANAR_Y); row_size = src->GetRowSize(PLANAR_Y); } void GetPlaneU() { srcp = src->GetWritePtr(PLANAR_U); src_pitch = src->GetPitch(PLANAR_U); height = src->GetHeight(PLANAR_U); row_size = src->GetRowSize(PLANAR_U); } void GetPlaneV() { srcp = src->GetWritePtr(PLANAR_V); src_pitch = src->GetPitch(PLANAR_V); height = src->GetHeight(PLANAR_V); row_size = src->GetRowSize(PLANAR_V); } void CoreFilter(int nt, int mode) { //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // vector_srcp[y * row_size + x] = srcp[x]; // } // srcp += src_pitch; //} //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // vector_blur[y * row_size + x] += vector_srcp[y * row_size + x] * 4; // vector_blur[y * row_size + x] += vector_srcp[y * row_size + max(0, x - 1)] * 2; // vector_blur[y * row_size + x] += vector_srcp[y * row_size + min(row_size, x + 1)] * 2; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + x] * 2; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + max(0, x - 1)]; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + min(row_size, x + 1)]; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + x] * 2; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + max(0, x - 1)]; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + min(row_size, x + 1)]; // vector_blur[y * row_size + x] = static_cast<int64_t>(vector_blur[y * row_size + x] / 16.0f + 0.5f); // } //} int i, j, k; for (int y = 0; y < height; y++) { for (int x = 0; x < row_size; x++) { i = (srcp + y * src_pitch)[x] * 4; i += (srcp + y * src_pitch)[max(0, x - 1)] * 2; i += (srcp + y * src_pitch)[min(row_size, x + 1)] * 2; i += (srcp + max(0, y - 1) * src_pitch)[x] * 2; i += (srcp + max(0, y - 1) * src_pitch)[max(0, x - 1)]; i += (srcp + max(0, y - 1) * src_pitch)[min(row_size, x + 1)]; i += (srcp + min(height - 1, y + 1) * src_pitch)[x] * 2; i += (srcp + min(height - 1, y + 1) * src_pitch)[max(0, x - 1)]; i += (srcp + min(height - 1, y + 1) * src_pitch)[min(row_size, x + 1)]; i = srcp[x] - static_cast<int>(i / 16.0f + 0.5f); j = abs(i); k = (i > 0) - (i < 0); i = j < nt ? 0 : i; i = mode > 0 ? (int)sqrt(j) : i; i = mode > 1 ? (int)atan(j) * i : i; i = i * k; i = max(0, min(255, (srcp + y * src_pitch)[x] + i)); (srcp + y * src_pitch)[x] = i; } } } }; class CSharpen : public GenericVideoFilter { int nt, mode; bool Y, U, V; public: CSharpen::CSharpen(PClip _child, int _nt, int _mode, bool _Y, bool _U, bool _V, IScriptEnvironment* env) : GenericVideoFilter(_child), nt(_nt), mode(_mode), Y(_Y), U(_U), V(_V) { if (!vi.IsYUV()) { env->ThrowError("CSharpen: supported colorspaces are Y8, YV12, YV16, YV24!"); } else if (nt < 0 || nt > 255) { env->ThrowError("CSharpen: nt avaible range is [0, 255]!"); } else if (mode < 0 || mode > 2) { env->ThrowError("CSharpen: mode avaible mode values are 0, 1, 2!"); } } PVideoFrame __stdcall CSharpen::GetFrame(int n, IScriptEnvironment* env) { CSharpenFilter Frame; Frame.GetFrame(child, n, env); Y ? Frame.GetPlaneY() : NULL; Y ? Frame.CoreFilter(nt, mode) : NULL; !vi.IsY8() && U ? Frame.GetPlaneU() : NULL; !vi.IsY8() && U ? Frame.CoreFilter(nt, mode) : NULL; !vi.IsY8() && V ? Frame.GetPlaneV() : NULL; !vi.IsY8() && V ? Frame.CoreFilter(nt, mode) : NULL; return Frame.src; } }; Last edited by Ceppo; 25th January 2023 at 04:03. |
25th January 2023, 04:29 | #4 | Link |
Registered User
Join Date: Jan 2018
Posts: 2,156
|
|
25th January 2023, 19:19 | #7 | Link | |
Formerly davidh*****
Join Date: Jan 2004
Posts: 2,496
|
Quote:
The one Avisynth coding error that jumps out at me though is this: Code:
min(row_size, x + 1) Code:
min(row_size - 1, x + 1) |
|
25th January 2023, 19:27 | #8 | Link |
Registered User
Join Date: Feb 2016
Location: Nonsense land
Posts: 339
|
Thanks, broken logic I guess. BUT even if I comment out everything, it compiles but doesn't work, like that:
Code:
#pragma once #include <cmath> #include <windows.h> #include <avisynth.h> using namespace std; struct CSharpenFilter { PVideoFrame src; const unsigned char* srcp; int height, row_size, src_pitch; //int* vector_srcp = new int[height * row_size]; //int* vector_blur = new int[height * row_size]; void GetFrame(PClip child, int n, IScriptEnvironment* env) { src = child->GetFrame(n, env); } void GetPlaneY() { srcp = src->GetReadPtr(PLANAR_Y); src_pitch = src->GetPitch(PLANAR_Y); height = src->GetHeight(PLANAR_Y); row_size = src->GetRowSize(PLANAR_Y); } void GetPlaneU() { srcp = src->GetReadPtr(PLANAR_U); src_pitch = src->GetPitch(PLANAR_U); height = src->GetHeight(PLANAR_U); row_size = src->GetRowSize(PLANAR_U); } void GetPlaneV() { srcp = src->GetReadPtr(PLANAR_V); src_pitch = src->GetPitch(PLANAR_V); height = src->GetHeight(PLANAR_V); row_size = src->GetRowSize(PLANAR_V); } void CoreFilter(int nt, int mode) { //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // vector_srcp[y * row_size + x] = srcp[x]; // } // srcp += src_pitch; //} //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // vector_blur[y * row_size + x] += vector_srcp[y * row_size + x] * 4; // vector_blur[y * row_size + x] += vector_srcp[y * row_size + max(0, x - 1)] * 2; // vector_blur[y * row_size + x] += vector_srcp[y * row_size + min(row_size, x + 1)] * 2; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + x] * 2; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + max(0, x - 1)]; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + min(row_size, x + 1)]; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + x] * 2; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + max(0, x - 1)]; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + min(row_size, x + 1)]; // vector_blur[y * row_size + x] = static_cast<int64_t>(vector_blur[y * row_size + x] / 16.0f + 0.5f); // } //} //int i, j, k; //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // i = (srcp + y * src_pitch)[x] * 4; // i += (srcp + y * src_pitch)[max(0, x - 1)] * 2; // i += (srcp + y * src_pitch)[min(row_size, x + 1)] * 2; // i += (srcp + max(0, y - 1) * src_pitch)[x] * 2; // i += (srcp + max(0, y - 1) * src_pitch)[max(0, x - 1)]; // i += (srcp + max(0, y - 1) * src_pitch)[min(row_size, x + 1)]; // i += (srcp + min(height - 1, y + 1) * src_pitch)[x] * 2; // i += (srcp + min(height - 1, y + 1) * src_pitch)[max(0, x - 1)]; // i += (srcp + min(height - 1, y + 1) * src_pitch)[min(row_size, x + 1)]; // i = (srcp + y * src_pitch)[x] - static_cast<int>(i / 16.0f + 0.5f); // // j = abs(i); // k = (i > 0) - (i < 0); // i = j < nt ? 0 : i; // i = mode > 0 ? (int)sqrt(j) : i; // i = mode > 1 ? (int)atan(j) * i : i; // i = i * k; // i = max(0, min(255, (srcp + y * src_pitch)[x] + i)); // (srcp + y * src_pitch)[x] = i; // } //} } }; class CSharpen : public GenericVideoFilter { int nt, mode; bool Y, U, V; public: CSharpen::CSharpen(PClip _child, int _nt, int _mode, bool _Y, bool _U, bool _V, IScriptEnvironment* env) : GenericVideoFilter(_child), nt(_nt), mode(_mode), Y(_Y), U(_U), V(_V) { if (!vi.IsYUV()) { env->ThrowError("CSharpen: supported colorspaces are Y8, YV12, YV16, YV24!"); } else if (nt < 0 || nt > 255) { env->ThrowError("CSharpen: nt avaible range is [0, 255]!"); } else if (mode < 0 || mode > 2) { env->ThrowError("CSharpen: mode avaible mode values are 0, 1, 2!"); } } PVideoFrame __stdcall CSharpen::GetFrame(int n, IScriptEnvironment* env) { CSharpenFilter Frame; Frame.GetFrame(child, n, env); //Y ? Frame.GetPlaneY() : NULL; //Y ? Frame.CoreFilter(nt, mode) : NULL; //!vi.IsY8() && U ? Frame.GetPlaneU() : NULL; //!vi.IsY8() && U ? Frame.CoreFilter(nt, mode) : NULL; //!vi.IsY8() && V ? Frame.GetPlaneV() : NULL; //!vi.IsY8() && V ? Frame.CoreFilter(nt, mode) : NULL; return Frame.src; } }; Code:
AVSValue __cdecl Create_SimpleSample(AVSValue args, void* user_data, IScriptEnvironment* env) { return new SimpleSample(args[0].AsClip(), args[1].AsInt(100), env); } const AVS_Linkage *AVS_linkage = 0; extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* env, const AVS_Linkage* const vectors) { AVS_linkage = vectors; env->AddFunction("SimpleSample", "c[size]i", Create_SimpleSample, 0); return "SimpleSample plugin"; } Last edited by Ceppo; 25th January 2023 at 19:29. |
25th January 2023, 19:37 | #9 | Link |
Formerly davidh*****
Join Date: Jan 2004
Posts: 2,496
|
This "works" (with the caveat that I don't know what it's supposed to do):
Code:
#pragma once #include <cmath> #include <windows.h> #include <avisynth.h> using namespace std; struct CSharpenFilter { PVideoFrame src; unsigned char* srcp; int height, row_size, src_pitch; //int* vector_srcp = new int[height * row_size]; //int* vector_blur = new int[height * row_size]; void GetFrame(PClip child, int n, IScriptEnvironment* env) { src = child->GetFrame(n, env); env->MakeWritable(&src); } void GetPlaneY() { srcp = src->GetWritePtr(PLANAR_Y); src_pitch = src->GetPitch(PLANAR_Y); height = src->GetHeight(PLANAR_Y); row_size = src->GetRowSize(PLANAR_Y); } void GetPlaneU() { srcp = src->GetWritePtr(PLANAR_U); src_pitch = src->GetPitch(PLANAR_U); height = src->GetHeight(PLANAR_U); row_size = src->GetRowSize(PLANAR_U); } void GetPlaneV() { srcp = src->GetWritePtr(PLANAR_V); src_pitch = src->GetPitch(PLANAR_V); height = src->GetHeight(PLANAR_V); row_size = src->GetRowSize(PLANAR_V); } void CoreFilter(int nt, int mode) { //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // vector_srcp[y * row_size + x] = srcp[x]; // } // srcp += src_pitch; //} //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < row_size; x++) // { // vector_blur[y * row_size + x] += vector_srcp[y * row_size + x] * 4; // vector_blur[y * row_size + x] += vector_srcp[y * row_size + max(0, x - 1)] * 2; // vector_blur[y * row_size + x] += vector_srcp[y * row_size + min(row_size, x + 1)] * 2; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + x] * 2; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + max(0, x - 1)]; // vector_blur[y * row_size + x] += vector_srcp[max(0, y - 1) * row_size + min(row_size, x + 1)]; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + x] * 2; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + max(0, x - 1)]; // vector_blur[y * row_size + x] += vector_srcp[min(height - 1, y + 1) * row_size + min(row_size, x + 1)]; // vector_blur[y * row_size + x] = static_cast<int64_t>(vector_blur[y * row_size + x] / 16.0f + 0.5f); // } //} int i, j, k; for (int y = 0; y < height; y++) { for (int x = 0; x < row_size; x++) { i = (srcp + y * src_pitch)[x] * 4; i += (srcp + y * src_pitch)[max(0, x - 1)] * 2; i += (srcp + y * src_pitch)[min(row_size - 1, x + 1)] * 2; i += (srcp + max(0, y - 1) * src_pitch)[x] * 2; i += (srcp + max(0, y - 1) * src_pitch)[max(0, x - 1)]; i += (srcp + max(0, y - 1) * src_pitch)[min(row_size - 1, x + 1)]; i += (srcp + min(height - 1, y + 1) * src_pitch)[x] * 2; i += (srcp + min(height - 1, y + 1) * src_pitch)[max(0, x - 1)]; i += (srcp + min(height - 1, y + 1) * src_pitch)[min(row_size - 1, x + 1)]; i = srcp[x] - static_cast<int>(i / 16.0f + 0.5f); j = abs(i); k = (i > 0) - (i < 0); i = j < nt ? 0 : i; i = mode > 0 ? (int)sqrt(j) : i; i = mode > 1 ? (int)atan(j) * i : i; i = i * k; i = max(0, min(255, (srcp + y * src_pitch)[x] + i)); (srcp + y * src_pitch)[x] = i; } } } }; class CSharpen : public GenericVideoFilter { int nt, mode; bool Y, U, V; public: CSharpen(PClip _child, int _nt, int _mode, bool _Y, bool _U, bool _V, IScriptEnvironment* env) : GenericVideoFilter(_child), nt(_nt), mode(_mode), Y(_Y), U(_U), V(_V) { if (!vi.IsYUV()) { env->ThrowError("CSharpen: supported colorspaces are Y8, YV12, YV16, YV24!"); } else if (nt < 0 || nt > 255) { env->ThrowError("CSharpen: nt avaible range is [0, 255]!"); } else if (mode < 0 || mode > 2) { env->ThrowError("CSharpen: mode avaible mode values are 0, 1, 2!"); } } PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* env) { CSharpenFilter Frame; Frame.GetFrame(child, n, env); if (Y) Frame.GetPlaneY(); if (Y) Frame.CoreFilter(nt, mode); if (!vi.IsY8() && U) Frame.GetPlaneU(); if (!vi.IsY8() && U) Frame.CoreFilter(nt, mode); if (!vi.IsY8() && V) Frame.GetPlaneV(); if (!vi.IsY8() && V) Frame.CoreFilter(nt, mode); return Frame.src; } }; const AVS_Linkage *AVS_linkage = 0; AVSValue __cdecl Create_CSharpen(AVSValue args, void* user_data, IScriptEnvironment* env) { return new CSharpen( args[0].AsClip(), args[1].AsInt(), args[2].AsInt(), args[3].AsBool(true), args[4].AsBool(true), args[5].AsBool(true), env ); } extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* env, const AVS_Linkage* const vectors) { AVS_linkage = vectors; env->AddFunction("CSharpen", "ciibbb", Create_CSharpen, 0); return "CSharpen"; } |
25th January 2023, 19:50 | #10 | Link |
Registered User
Join Date: Feb 2016
Location: Nonsense land
Posts: 339
|
Wow THANKS. Now it doesn't crash anymore. However, this part of the code is supposed to make an approximated gaussin blur and add back the sqrt of the difference to get sharpening. But the image is left untouched...
Code:
void CoreFilter(int nt, int mode) { int i, j, k; for (int y = 0; y < height; y++) { for (int x = 0; x < row_size; x++) { i = (srcp + y * src_pitch)[x] * 4; i += (srcp + y * src_pitch)[max(0, x - 1)] * 2; i += (srcp + y * src_pitch)[min(row_size - 1, x + 1)] * 2; i += (srcp + max(0, y - 1) * src_pitch)[x] * 2; i += (srcp + max(0, y - 1) * src_pitch)[max(0, x - 1)]; i += (srcp + max(0, y - 1) * src_pitch)[min(row_size - 1, x + 1)]; i += (srcp + min(height - 1, y + 1) * src_pitch)[x] * 2; i += (srcp + min(height - 1, y + 1) * src_pitch)[max(0, x - 1)]; i += (srcp + min(height - 1, y + 1) * src_pitch)[min(row_size - 1, x + 1)]; i = (srcp + y * src_pitch)[x] - static_cast<int>(i / 16.0f + 0.5f); j = abs(i); k = (i > 0) - (i < 0); i = j < nt ? 0 : i; i = mode > 0 ? (int)sqrt(j) : i; i = mode > 1 ? (int)atan(j) * i : i; i = i * k; i = max(0, min(255, (srcp + y * src_pitch)[x] + i)); (srcp + y * src_pitch)[x] = i; } } } |
Thread Tools | Search this Thread |
Display Modes | |
|
|