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

Reply
 
Thread Tools Search this Thread Display Modes
Old 29th January 2009, 07:24   #1  |  Link
SAPikachu
Registered User
 
SAPikachu's Avatar
 
Join Date: Aug 2007
Posts: 218
AvsFilterNet - writing avisynth filter in .NET

I recently wrote a wrapper for avisynth in .NET so that you can write avisynth filter in any .NET language. Hope someone will find it useful.

You can download binaries and source code here. Any suggestion is appreciated.
SAPikachu is offline   Reply With Quote
Old 29th January 2009, 15:45   #2  |  Link
tin3tin
Registered User
 
tin3tin's Avatar
 
Join Date: Mar 2005
Posts: 365
This looks very interesting. Would it be possible for you to write a quick "Getting-started writing avisynth plugins in .net" tutorial, for people(like me) who never worked in .net before ?

And maybe with an example of a .net/avisynth plugin script?

Anyway thank you for your work.
__________________
DVD slideshow GUI(Freeware).

Last edited by tin3tin; 29th January 2009 at 19:47.
tin3tin is offline   Reply With Quote
Old 30th January 2009, 04:11   #3  |  Link
SAPikachu
Registered User
 
SAPikachu's Avatar
 
Join Date: Aug 2007
Posts: 218
You can see the SimpleSampleNet project included in the package. It is ported from the sample in avisynth filter SDK. There are many comments in the code, so you can use it as a starting point to create your own plugin.
SAPikachu is offline   Reply With Quote
Old 1st February 2009, 08:38   #4  |  Link
Fizick
AviSynth plugger
 
Fizick's Avatar
 
Join Date: Nov 2003
Location: Russia
Posts: 2,183
Thanks, added to the Wiki:
http://avisynth.org/mediawiki/Filter...ther_compilers
__________________
My Avisynth plugins are now at http://avisynth.org.ru and mirror at http://avisynth.nl/users/fizick
I usually do not provide a technical support in private messages.
Fizick is offline   Reply With Quote
Old 1st February 2009, 09:02   #5  |  Link
SAPikachu
Registered User
 
SAPikachu's Avatar
 
Join Date: Aug 2007
Posts: 218
Thanks!
SAPikachu is offline   Reply With Quote
Old 16th July 2013, 20:52   #6  |  Link
rvs75
Registered User
 
Join Date: Jul 2011
Posts: 18
AvsFilterNet is compatible with Avisynth 2.6 ?
rvs75 is offline   Reply With Quote
Old 17th July 2013, 01:06   #7  |  Link
SAPikachu
Registered User
 
SAPikachu's Avatar
 
Join Date: Aug 2007
Posts: 218
Quote:
Originally Posted by rvs75 View Post
AvsFilterNet is compatible with Avisynth 2.6 ?
Not currently, it needs to be recompiled with new headers to support avisynth 2.6.
__________________
f3kdb 1.5.1 / MP_Pipeline 0.18

ffms2 builds with 10bit output hack:
libav-9a60b1f / ffmpeg-1e4d049 / FFmbc-0.7.1
Built from ffms2 6e0d654 (hack a9fe004)

Mirrors: http://bit.ly/19TwDD3
SAPikachu is offline   Reply With Quote
Old 5th July 2015, 07:25   #8  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
This looks very interesting.

But if it doesn't support AviSynth 2.6, what does it support? I'm looking at maybe writing something for AviSynth 2.6 MT.

OK, it doesn't load in AviSynth 2.6. It doesn't load in 2.5 either.

I was thinking it might be easy to port the SuperRes, written in .NET, to AviSynth if it could also be written in .NET.
https://github.com/zachsaw/MPDN_Exte...ow.SuperRes.cs

But since AvsFilterNet was obviously not designed for AviSynth 2.6, will there be more to do than replacing header files to get it working? Would I be seeing all kinds of incompatibility issues and bugs?

Last edited by MysteryX; 5th July 2015 at 07:52.
MysteryX is offline   Reply With Quote
Old 9th July 2016, 05:17   #9  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
I went ahead and adapted the code for AviSynth 2.6, AviSynth+ (x86/x64) and Visual Studio 2015. I don't think any plugins were even written with this and looking at the code, AvsFilterNet was never finished. A few things were missing or done wrong; with comments in Chinese.

You can get the full source code here
https://github.com/mysteryx93/AvsFilterNet

C# can remove a lot of the complexity of plugin development. Here is the simplest LoadImage plugin on Earth: 45 lines of code.

Here are some benefits of C# plugin development.
- Simplicity of code. All the implementation complexity is handled automatically.
- The plugin creation and registration is handled automatically.
- The plugin's source code is self-contained within a single code file.
- Debugging works much better.
- C# code works for both x86 and x64 platforms.
- C# is natively cross-platform compatible and will work in Linux or MacOS if AviSynth ever support those.
- C# can benefit from CPU-specific advanced instruction sets as it compiles on-demand
- No random memory access violation issues. C# won't allow you to work with memory data outside the managed scope.
- No threading "undefined" behaviors. C# has much more thread-awareness, many objects will notify you if you violate their thread affinity and managing threads is a whole lot easier.
- No worries about releasing objects. C# uses the Gargage Collector to release memory. Microsoft determined that this resulted in about 3% performance cost and decided to migrate their whole products into using this.
- Everything regarding multi-threading and multi-processes become much simpler
- Complex tasks become a whole lot easier

Drawbacks of C#:
- To use assembly code, you must link to a C++ library containing that code.
- Raw pointer access and data manipulation remains easier with C++'s direct pointer access.

Performance-wise, this is a wrapper around the C++ functions. Besides the lack of assembly code, I think the performance cost will be minimal. As for working with data within a Stream vs using C++ pointers, the performance would have to be tested.

In terms of memory, because C# uses the Garbage Collector, the memory usage may "appear" considerably higher because it won't release the memory until it considers it needs it. Some people try to override the GC's logic but as a rule of thumb, don't try to re-invent the wheel and let the GC do its job. Just know the memory usage you see may be inaccurate.

It is built with the headers of AviSynth+ r2022 which are incompatible with previous builds of AviSynth+. There are currently some issues in MT mode and I will wait for Ultim to come out with an updated version of AviSynth+. It is working perfectly fine in single-threaded mode. In MT, the calls might not come from the thread you are expecting.
MysteryX is offline   Reply With Quote
Old 9th July 2016, 10:07   #10  |  Link
amayra
Quality Checker
 
amayra's Avatar
 
Join Date: Aug 2013
Posts: 175
indeed this have issues MT mod
__________________
I love Doom9
amayra is offline   Reply With Quote
Old 9th July 2016, 11:14   #11  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
The MT issues I saw may simply be because I was using a class with thread affinity that cared from which thread it was being called. Without using such a class, then it might work just the same as with C++. Have to test more.

In terms of performance, often low performance doesn't come from lack of assembly code and raw execution speed, but rather from poor code structure due to a huge amount of unreadable code that piled up over time. On this point, C# wins major points. It allows for much better and simpler code structure that can be maintained a lot more easily.

I've done a lot of database applications, and the best practice for such application is to split the solution in 3 layers: UI, Business Logic and Data Access. In the same way, it would make sense to completely separate the plugin logic from the raw frame data operations. The plugin logic can be maintained and simplified in a C# project, while the raw calculations are being done in a separate layer. Such functions generally need a code splitter and several implementations: CPU, SSE2, AVX.

Separating the plugin logic from the raw operations also might be the easiest way to port the plugins to other platforms in the future. The plugin logic is cross-platform compatible, and all that needs to be rewritten is the assembly in the raw operations layer. Porting then becomes very easy. The only downside: it requires 2 DLLs instead of 1; plus AviFilterNet.dll.

That being said, the first person to develop a plugin with this may need to still fix a few bugs and/or implement a few missing features while using AvsFilterNet.
MysteryX is offline   Reply With Quote
Old 9th July 2016, 21:15   #12  |  Link
FranceBB
FranceBB
 
FranceBB's Avatar
 
Join Date: Nov 2013
Location: Germany
Posts: 196
Avisynth and C#, I knew this day would have come! :')
FranceBB is offline   Reply With Quote
Old 10th July 2016, 11:26   #13  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
I was worried about MT and error handling, and after testing, both are working fine. There are however a few issues left.

Version 1.0.1 is ready.
- Tested multi-threading and error handling. Both are working fine.
- Renamed the new Finalize function to ExecuteAfter.
- Added ExecuteBefore function with CancelLoad parameter.
- If CancelLoad is specified during ExecuteBefore, the filter will be discarded and other filters can be executed instead.
- env.Invoke can now take null as arguments parameters.
- Added generation of intellisense documentation.
- Fixed env2.GetProperty by adding managed enumeration.
- Added InterProcessNet sample which performs a frame operation in a different process.

Known issues:
- ExecuteBefore result will be ignored; calling SetChild with its result causes an infinite loop with SetCacheHints
- LoadPluginNet must point to a path known to the assembly (same folder) or it may not recognize the DLL
- If ExecuteAfter returns another filter, the filter will execute with the MT mode of that last filter (known issue within AviSynth+). Best solution is to make your filter MT_NICE_FILTER. Otherwise you can return a dummy filter having the desired MT mode. Or wait for Ultim to release a fix.


v1.0
- Transfered the code to GitHub
- Ported old code to Visual Studio 2015
- Replaced AviSynth.h headers with the one from AviSynth+
- Adapted the code for the new headers
- Env was being stored by the filter class (bad), removed that
- Removed GenericVideoFilter base class of AvisynthFilter and made sure it doesn't crash when first parameter isn't a clip
- Removed SAPStudio namespace
- Cleaned up formatting and removed a lot of commented code
- Added support for AviSynth+'s env2
- Added auto-registration of MT mode via the class attibute
- Added LoadImageNet sample
- Removed the constructor parameters and added them to Initialize
- Added functions to allow executing other filters before and after
- Added env.bitblt overloads to support C# byte[]

Last edited by MysteryX; 10th July 2016 at 11:48.
MysteryX is offline   Reply With Quote
Old 10th July 2016, 14:19   #14  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
The bug with ExecuteBefore is now fixed in the code repository.

Also... why is it necessary to Dispose the VideoFrame after use? That normally shouldn't be necessary, but it crashes when closing the script if you don't.
https://github.com/mysteryx93/AvsFil...cessNet.cs#L50

Overall, it's working. Areas for improvement: parameters could be better validated to avoid erratic behaviors, and occasionally errors aren't being handled in a nice way.

Edit: For VideoFrame.Dispose, considering it's working fine during execution and only fails during the destructor, perhaps if we let the Garbage Collector release it, it doesn't care whether the Clip or VideoFrame is released first, and if it releases the Clip first, then obviously the VideoFrame is screwed up. In that case, explicitely calling VideoFrame.Dispose may be a good idea as I don't see any other way of controlling the order.

Last edited by MysteryX; 10th July 2016 at 14:42.
MysteryX is offline   Reply With Quote
Old 11th July 2016, 02:53   #15  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
There is one issue with VideoFrame releasing that I'm having a hard time with. It would be appreciated is some C++ expert could take a look at this one.

A lot of what I said about the Garbage Collector doesn't directly apply in this case because we're still dealing with unmanaged resources that must still be freed manually; and it must play nicely with the Garbage Collector.

Putting this code in GetFrame (C#), it will crash on exit if we don't call Src.Dispose. If we throw an exception, then we must call Dst.Dispose as well. Otherwise we get an ugly memory corruption error.

This code works but it's not addressing the core of the problem.

Code:
VideoFrame Src = Child.GetFrame(n, env);
VideoFrame Dst = NewVideoFrame(env);
try {
    throw new Exception("BOOM!");
} catch (Exception ex) {
    Dst.Dispose();
    throw ex;
} finally {
    Src.Dispose();
}
return Dst;
See the InterProcessSample.
MysteryX is offline   Reply With Quote
Old 11th July 2016, 15:43   #16  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
Version 1.0.2 is ready, fixing the rough edges.

What's new:
- Fixed ExecuteBefore
- Fixed VideoFrame not being properly released

These are all the known bugs.

There is still a limitation that LoadFilterNet must point to a known folder, such as in the same folder as AvsFilterNet.dll
MysteryX is offline   Reply With Quote
Old 12th July 2016, 09:24   #17  |  Link
amayra
Quality Checker
 
amayra's Avatar
 
Join Date: Aug 2013
Posts: 175
thanks AFN 1.0.2 work like a charm i have no issues until now
__________________
I love Doom9
amayra is offline   Reply With Quote
Old 20th July 2016, 11:16   #18  |  Link
amayra
Quality Checker
 
amayra's Avatar
 
Join Date: Aug 2013
Posts: 175
MysteryX Just out of curiosity, why were you started(forked) this project ?
__________________
I love Doom9
amayra is offline   Reply With Quote
Old 22nd July 2016, 09:24   #19  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
In case someone needs it. Having it before would have made it a lot easier to get started with plugin development, yet after digging through the code, there's no way I would have got it to a functional state without first learning to develop filters in C++. Now that I have the knowledge, it wasn't too hard to fix something that those who'd most need it don't have.
MysteryX is offline   Reply With Quote
Old 23rd July 2016, 17:16   #20  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,000
There are also a couple more reasons; one of which is that I'm a C# programmer in my blood.

The idea came when discussing MP_Pipeline and some of its current limitations. This sort of plugin dealing with multiple processes would be a whole lot easier to code and maintain in C#, and the other processes could then get an input instead of having to load the source within those processes. It also could be written with probably 5x less code.

The other reason is that C++ and C# have very different strengths. C++ is good for low-level work on data by using raw pointers and assembly optimization. C# is excellent for... pretty much everything else. Especially multi-threading and multi-processing. Any utility functions such as WriteToAvi or MP_Pipeline would be much easier to write that way.

Another reason is that C++ and C# programmers generally think differently. This may allow for different types of development outside of the box of what is currently being done. One thing I'm thinking is that if one would want to use some sort of advanced neural network algorithm, or some sort of AI, for highly advanced processing, these sorts of processing could be more easily done in a higher-level language like C# and linking to a library to do the neural processing (or whatever that would be). (That example is hypothetical)

Then it can lower the learning curve for .NET programmers (like me) who want to get started with plugin development.

So whatever the reasons, now it's here. I don't need it myself right now but I may use it if I need to develop something else in the future.
MysteryX is offline   Reply With Quote
Reply

Tags
.net, avisynth, filter

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 03:35.


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