Log in

View Full Version : My computer explodes when I run this plugin.


Ceppo
25th January 2023, 03:03
Can someone teach me what I'm doing wrong, please :confused:
Everytime I write on a clip inside a struct or a class nothing works :\.


#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;
}
};

kedautinh12
25th January 2023, 04:00
You can ask professional coder Asd-g :D

Ceppo
25th January 2023, 04:03
On GitHub?

kedautinh12
25th January 2023, 04:29
On GitHub?

Yes
https://github.com/Asd-g/AviSynthPlus-Scripts/issues

wonkey_monkey
25th January 2023, 19:03
nothing works :\.

You may need to be more specific... if you're getting compiler errors (which I suspect you are) you should refer to those to find the problem(s).

Ceppo
25th January 2023, 19:07
It compiles, but AVSPMod crashes because something strange is going on, if I debug my RAM got consumed to the MAX :scared:

wonkey_monkey
25th January 2023, 19:19
It compiles

Not with Visual Studio it doesn't...

The one Avisynth coding error that jumps out at me though is this:

min(row_size, x + 1)

(three instances) which should be this:

min(row_size - 1, x + 1)

Ceppo
25th January 2023, 19:27
Thanks, broken logic I guess. BUT even if I comment out everything, it compiles but doesn't work, like that:

#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;
}
};


I guess it doesn't compile, since it lacks a part to the code, as example, since I don't know what magic it does, so I don't know how to call it:

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";
}


So I reduced everything to call the frame and return and it doesn't work... :(

wonkey_monkey
25th January 2023, 19:37
This "works" (with the caveat that I don't know what it's supposed to do):

#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";
}

Ceppo
25th January 2023, 19:50
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...

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;
}
}
}

Ceppo
25th January 2023, 22:42
Update Now it Works :)