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.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Development
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
Old 25th January 2023, 03:03   #1  |  Link
Ceppo
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.
Ceppo is offline   Reply With Quote
Old 25th January 2023, 04:00   #2  |  Link
kedautinh12
Registered User
 
Join Date: Jan 2018
Posts: 2,156
You can ask professional coder Asd-g
kedautinh12 is offline   Reply With Quote
Old 25th January 2023, 04:03   #3  |  Link
Ceppo
Registered User
 
Join Date: Feb 2016
Location: Nonsense land
Posts: 339
On GitHub?
Ceppo is offline   Reply With Quote
Old 25th January 2023, 04:29   #4  |  Link
kedautinh12
Registered User
 
Join Date: Jan 2018
Posts: 2,156
Quote:
Originally Posted by Ceppo View Post
On GitHub?
Yes
https://github.com/Asd-g/AviSynthPlus-Scripts/issues
kedautinh12 is offline   Reply With Quote
Old 25th January 2023, 19:03   #5  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,496
Quote:
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).
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 25th January 2023, 19:07   #6  |  Link
Ceppo
Registered User
 
Join Date: Feb 2016
Location: Nonsense land
Posts: 339
It compiles, but AVSPMod crashes because something strange is going on, if I debug my RAM got consumed to the MAX
Ceppo is offline   Reply With Quote
Old 25th January 2023, 19:19   #7  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,496
Quote:
It compiles
Not with Visual Studio it doesn't...

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

Code:
min(row_size, x + 1)
(three instances) which should be this:

Code:
min(row_size - 1, x + 1)
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 25th January 2023, 19:27   #8  |  Link
Ceppo
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;
    }
};
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:
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";
}
So I reduced everything to call the frame and return and it doesn't work...

Last edited by Ceppo; 25th January 2023 at 19:29.
Ceppo is offline   Reply With Quote
Old 25th January 2023, 19:37   #9  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
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";
}
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 25th January 2023, 19:50   #10  |  Link
Ceppo
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;
            }
        }
    }
Ceppo is offline   Reply With Quote
Old 25th January 2023, 22:42   #11  |  Link
Ceppo
Registered User
 
Join Date: Feb 2016
Location: Nonsense land
Posts: 339
Update Now it Works
Ceppo is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:16.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.