Quantcast C++ DLL Wrapper for Delphi, Code Design Thoughts? - digitalFAQ.com Forums [Archives]
  #1  
02-08-2003, 01:18 AM
vhelp vhelp is offline
Free Member
 
Join Date: Jan 2003
Posts: 1,009
Thanks: 0
Thanked 0 Times in 0 Posts
Hello everyone..

I'm opening up this thread w/ a C++ topic
Well, here goes nothing.. if anything, some new threads will begin
to poor in, in addtion to this and so on..


Flow for DLL Wrapper Design..

Code:
C++ DLL Wrapper for Delphi
----------------------------------

STEP 1
------

C++ --> Create a "template" C++ DLL - (follow stdcall 
convension)
  |
  * Delphi --> (import C++ DLL above)
     |
     * do our coding w/ imported C++ DLL
     * save/create a NEW Delphi DLL for use w/ AVIsynth
       scripts but be sure that it follows AVIsynth's
       calling convensions else DLL wont work properly or
       at all.  Our Wrapper is complete.


STEP 2
------

AVIsynth script coding..
   |
   * load plugin DLL (C++ and/or our new DELPHI DLL)
   * more AVS script commands
   * save final AVS script


STEP 3
------

Frameserve the AVS script into (vdub or tmpg)
..the end..


--> The PROCESS:
* we start with C++
* in C++, start w/ an image or video frame structure
* setup the struture parameters
* ie, GetFrame(PCLIP frame, INT width, INT height) 
* ... more code.. ..
NOTES
-----------------------------------
*** Mind you, this is VERY rough.
1 * if we could use the frame as a guide.. get the frame to be
... read in our DLL imported into Delphi, and as a test, under
... delphi, place an Image control on the form. Passing the
... C++ frame structure DLL, Delphi reads in the contents, and
... if all goes well, our Image control should NOW have an video
... image in our frame (our movie, yaeh)
2 * if an image IS finally displayed, we have passed the first
... test!!
3 * NOW, we can do what we want, ie, use our home-made Delphi
... noise remover, cropper, or whatever..
4 * next, after processing the frame, we continue by releasing
... the massaged data back into the DLL for further parameter passing
... into AVIsynth's own processing engine.. moving on..

Come on guys, I can't think of everything..

Correct me if I'm wrong, but isn't the mojority of the frameserving
dealing with Video BITMAP Images ??
If I'm missing any important steps/methods etc., please advise.
This is a learning experience.

I believe that it is very important to undertand the AVI stream
structure or RIFF, followed by the Header and Chunks etc. That is
another topic for discussion, but deserves to be mentioned here,
followed by other items of interest.. perhaps in later threads,
if not topics.

Start your CODE snips engines..

Well, class is OPEN !!
Shall we begin !!
-vhelp
Reply With Quote
Someday, 12:01 PM
admin's Avatar
Site Staff / Ad Manager
 
Join Date: Dec 2002
Posts: 42
Thanks: ∞
Thanked 42 Times in 42 Posts
  #2  
02-08-2003, 02:58 AM
MrTibs MrTibs is offline
Free Member
 
Join Date: Aug 2002
Location: Canada
Posts: 200
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, I'm in.

I don't know much about C++ but I've been doing some reading. I already have a new filter prototyped in Delphi that I want/need in Avisynth.

By tomorrow, I'll have a copy of MSVC to play with (so far we can only make dll's for Avisynth in MSVC) and I'll take a quick and dirty crack at the problem.

My thoughts about this very thing was to make a Delphi DLL loader. This way, the MSVC DLL would be compiled once and free for anyone to build Delphi plugins.

The avs script would look like this:

--------------------------------------------

LoadPlugin("C:\AviSynth\DelphiPlugin.dll")

bla bla bla.....

DelphiPlugin("myDelphifilter.dll","param1,param2,p aram3")

----------------------------------------------

Anyway, which ever system is best, I'm all for. While we are at it, lets make one for Borland C++ and MinGW.
Reply With Quote
  #3  
02-08-2003, 03:35 AM
vhelp vhelp is offline
Free Member
 
Join Date: Jan 2003
Posts: 1,009
Thanks: 0
Thanked 0 Times in 0 Posts
hay MrTibs...

I think you got the idea going.. he, he...

..hence, the Wrapper.

Yes, this is my approach. If we can figure on a good C++ template,
perhaps to come up with a few for certain conditions or approaches,
or something like that, once the C++ DLL is finally inside Delphi,
the coder is ready for any sort of action.

What I mean by tempalte, is a common format or structure if you call it.
For example, I'm working on a simple snipplet,
(remember, we want to first debug the DLL to make sure that it is accepted
by C++ or AVIsynth..

Code:
type
 TGetFrame = record
               Width  : dword;
               Height : dword;
               sSize  : Cardinal;
             end;

function fGetFrame(var Buf : TGetFrame) : Integer; stdcall;
begin
 if @Buf <> NIL then
   begin
     with Buffer do
       begin
         Width     := 528;
         Height    := 480;
         StrucSize := sizeof(buffer);
       end;
     Result := 0;
   end
end;
I know, my code needs work.. it's rough, only to give you an idea.

If the above could be passed back and forth in C/C++ then we are
headed in the right direction. And, finally, to be tested in AVIsynth.

Notice how I'm not making anything too complex. No sense on going over
board w/ complexities.. less debugging. If the above code (give or
take a few more coding and whatnot) works, the next step is to try a
pass w/ an image (another buffer, but with actual data, like a chunk
from a BITMAP or Frame or something.

#include <avisynth.h>

..has the vfw.h which has ALL the AVI structures, which is what we need
for these DLL demos. Once we have a better understanding (calling too)
we can get closer to a final outcome. vfw.h has ALL the AVI
structures, and it's not a pretty site. You really have to know what
all the structure and functions mean and do and how to call them and
with proper parameters etc. Add to that, the color
conversion process ie YUV/Pallette table etc. Did I leave anything out.

Code:
 TBuffer = ^TGetFrame;
 TGetFrame = record
               Width  : dword;
               Height : dword;
               sSize  : Cardinal;
               data   : ^vbuffer;
             end;
As I've said, the code needs work, and it IS just a rough idea to get your
self a little excited.

But, if we can get the DLL's to talk w/out issues, we're just accross
the street from success.

And, once in, it's no holds bar!! quite exciting, ain't it though ?

I'm actually thinking re-imaging my 2nd pc as a 2nd setup just for:
* Visual Basic, and
* C++

What I would do is write my code on my main pc here in Delphi, and then,
through LAN connects, debug the DLL's on my 2nd pc, but since the 2nd pc is
ONLY for Visual Basic and C++, there is no chance of any issues w/ Delphi.
Hmm.. Let me think about some more.. I'll sleep on it to night..
I'm exhausted w/ all this DLL endeavors. I love this hobby though.

I sense we are just around the corner from actually coding this.
Could be tomorrow !! We shall see.
-vhelp
Reply With Quote
  #4  
02-08-2003, 03:46 AM
vhelp vhelp is offline
Free Member
 
Join Date: Jan 2003
Posts: 1,009
Thanks: 0
Thanked 0 Times in 0 Posts
I will try and stay up just a wee be more, so I can throw up some code
snips. Just some small and simple excercises.

what I want to do is pass a Delphi DLL, and see if you C/C++ guys can
use the DLL.

Small steps. Well, let me see if I can come up with some.

If I do, I'll post in the Delphi thread.

-vhelp
Reply With Quote
  #5  
02-08-2003, 04:27 AM
vhelp vhelp is offline
Free Member
 
Join Date: Jan 2003
Posts: 1,009
Thanks: 0
Thanked 0 Times in 0 Posts
Well, as promised, here is it is, and I'm pooped.

Ok, here is the simple and small Delphi DLL excercise I was talking about
in my last post. The Delphi Source Code is included for your reference.

Download: 33.(dll)-simpledll.rar

Files you should see in your .RAR kit:
1, mathminmax.dpr
2, mathminmax.dll
3, mathminmax.cfg
4, mathminmax.dof


DELPHI SOURCE CODE:

Code:
Library mathminmax;

Function Min(X, Y: Integer): Integer; export;
begin
  if X < Y then Min := X else Min := Y;
end;

Function Max(X, Y: Integer): Integer; export;
begin
  if X > Y then Max := X else Max := Y;
end;

Exports
 Min index 1,
 Max index 2;

begin
end.
For you C/C++ 'ers out there.. please see if you can call these
two functions, following these instructions.
Note, these snips were taken via the net, so I'm saving some typing here:

Excercise #1: C/C++ utilizing Delphi's DLL routines

Quote:
To Call min() and max() functions in your C Code:

1 - In your .DEF File add:

IMPORTS
Min =MINMAX.Min
Max =MINMAX.Max

2. In your C application, you must prototype the functions as:

int FAR PASCAL Min(int x, y);
int FAR PASCAL Min(int x, y);

3. Call the Min or Max anywhere in your application.
I woundn't mind trying this Excecise out, but I don't have everything
up and running on my 2nd pc (haven't gone to sleep yet)

Please let me know if they worked.
Thanks. Good night all.
-vhelp
Reply With Quote
  #6  
02-08-2003, 06:17 AM
GFR GFR is offline
Free Member
 
Join Date: May 2002
Posts: 438
Thanks: 0
Thanked 0 Times in 0 Posts
I guess the problem is not only porting the Avisynth headers to Delphi - tht would be relatively easy!

The big catch is that Avisynth needs MSVC, not just any C++ compiler!! If you try it with Borland C++ Builder or Dev-C++ it does not compile It seems MSVC provides some extra stuff you need.

From Doom9:

Quote:
Registered: Jan 2002
Location: Chicago, Illinois, USA
Posts: 1514

It's not that the whole VC7 is needed; it is rather that many Windows platform SDK headers are needed! It is just the case that VC7 provides those for you.

Have some kind soul give you the include directory from VC7 or get the headers from the Microsoft web site by downloading the appropriate platform SDK.


__________________
Donald Graft
Filters for Avisynth and VirtualDub
http://shelob.mordor.net/dgraft/

milan
Senior Member

Registered: Apr 2002
Location: Slovakia
Posts: 164

Is it even possible to compile AviSynth filters with GCC?
Sorry, I don't know much about AviSynth API (I haven't developed or compiled any filter yet), but AFAIK it uses C++ classes. But I think MSVC and GCC have different C++ ABIs. For example many of AviSynth classes contains virtual functions and if MSVC and C++ uses different virtual method tables layout AviSynth will not be able to call the right methods.
It's interesting for me, becuase I'm thinking about creating AviSynth interface to ffdshow's image processing filters and as always I would like to be able to compile it with GCC too.


Last edited by milan on 4th October 2002 at 08:30

Below there's a Virtualdub filter compiled with BC++, this may be a good read:

http://wwwtcs.inf.tu-dresden.de/~dc1/conditional/
Reply With Quote
  #7  
02-10-2003, 01:31 AM
vhelp vhelp is offline
Free Member
 
Join Date: Jan 2003
Posts: 1,009
Thanks: 0
Thanked 0 Times in 0 Posts
Hay GFR,
Sorry for taking so long to respond to your post.
I've ben busy w/ many ideas and things. Anyways..

Yes, I agree w/ you. I don't doubt the validity of your statement..
that being, porting AVIsynth's header "avysnth.h" file. It would
be nice (great) if someone had ported it to Delphi. I'm going to
do some searches on this (perhaps some exhausted ones, when I go to
work tomorrow, while using my fast connects) These 56k phone lines
suck big-time.. ruins the moments, if you know what I mean.

DLL's. . .
I've ben playing around (or more like hacking around) w/ some of my
DLL's that I created in Delphi. I've also included other DLL's as
well, from other users and apps. Trying to bring them into AVIsynth
to see what would happen.
Most of my attempts would produce an error message or too.
But, yesterday (2.8.03) I had a few of mine working when I opened in
vdub. Problem is, today, I can't get those same DLL's to load in
AVIsynth anymore. I don't know what had happend since yesterday,
but for all I know, my system could have ben sick, (down to 7% CPU)
and just took everything in w/out any warning messages popping up.
But, I'm more curious then ever, because for whatever it's worth,
those DLL's got in w/ out issues.

Vdub would be nice too, to write DLL's for (mainly filters) and I
know it is equipt to share it's VBitmap structure but you still have
to be C/C++ user. But, maybe it's more forgiving. Requires MS C.

Anyways, I don't have any graphical C/C++, only those GPL/GNU versions
which are DOS/COMMANDline only versions (as far as I know) as I did
try and compile a few short .h header files, but all I got was a
bunch of errors.
So, I may have to go on-line tomorrow (today by now) and order a copy
of either MS or Borland C++.. Don't know what the price is on
those two popular ones $$ Well, I'll look into them anyways..

Quote:
Below there's a Virtualdub filter compiled with BC++, this may be a good read:
http://wwwtcs.inf.tu-dresden.de/~dc1/conditional/
Oh, and thanks for the good read.

Well, I've ben working on a few more DLL ideas. I've also posted
a few DLL excercises under the Delphi section of this FORUM.
I've managed to develop a simple DLL for Delphi and VB/VBA, and
the apps are avail for D/L, as well as the source code to them.
These are just simple excercises if anything. Actually, I saved myself
a bit of headacke, by not installing VB 3.0 (again) By using VBA
included in Office 97, I was able to get the Excercise to work (but
not w/out some headackes, as I forgot about the proper param
passing syntax, etc) But, they both work. So, anyways..

Well, have a good night, and see you tomorrow or so.
-vhelp
Reply With Quote
  #8  
02-10-2003, 10:54 AM
MrTibs MrTibs is offline
Free Member
 
Join Date: Aug 2002
Location: Canada
Posts: 200
Thanks: 0
Thanked 0 Times in 0 Posts
I did find someone on doom9 who said that they had ported the Avisynth.h file to Delphi but I haven't been able to find him/her again.

One option for us is to modify the "LoadPlugin" routine in Avisynth to accept native Delphi, BCC and MinGW dlls. (I've posted a feature request on avisynth.org.)

I'm sorry I haven't been active in the thread but I decided to upgrade my CPU (on my video development machine) and well...it didn't go well. Right now my Asus A7V MB won't boot [insert 7 page rant on Asus motherboards here] but I'll do some work on my laptop for a while.
Reply With Quote
  #9  
02-10-2003, 09:49 PM
vhelp vhelp is offline
Free Member
 
Join Date: Jan 2003
Posts: 1,009
Thanks: 0
Thanked 0 Times in 0 Posts
hay MrTibs..

bummer about your "video development machine" issues

Hope you got it all straighten out.

Today, I made my first doom9 post. When I first joined, someone had
posted an issue about a Demux for VOB files (something I am also very
interested in [opening an MPEG2 or VOB and reading a frame]) , and he
wanted to have a few peoples try it out. I was one of them. Anyways, I
went to post a response, and shore enought, blastid FORUM had a 5 day
hold over. Anyways, I've sense moved on.

I did mention my DLL Wrapper theory there as well. I tried to keep to the
same script (for what it's worth) but it was hard, as I always have ideas
going on upstairs he, he..

When I begin to get tired of this, I look for something else to do. I'm
always juggling multiple projects and things, quite routinely.

Anyways, hope you got your pc issues straighten out, if not, post your
issues here, and I'm sure someone will get back to you w/ some assistance. Maybe I can help.. (ADVC) ..Good luck with that pc though.
-vhelp
Reply With Quote
  #10  
02-11-2003, 10:56 AM
MrTibs MrTibs is offline
Free Member
 
Join Date: Aug 2002
Location: Canada
Posts: 200
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the offer for help on my PC. Basically, I flashed my MB and now it won't boot. It looks like I'll have to do a "hot-flash" to get it working again.

As for this issue, here is an article I found on the web that talks about using C++ objects in Delphi (complete with example code):

http://rvelthuis.bei.t-online.de/art...es-cppobjs.htm
Reply With Quote
  #11  
02-25-2003, 05:25 PM
SansGrip SansGrip is offline
Free Member
 
Join Date: Nov 2002
Location: Ontario, Canada
Posts: 1,135
Thanks: 0
Thanked 0 Times in 0 Posts
This is an interesting idea, but you must remember that a Delphi filter would almost certainly be extremely slow. C++ is slow enough when you get even slightly algorithmically complicated. To match the performance of, say, Flux you'd end up with about 10% of the code in Delphi and 90% in assembler, which kinda negates the point .

That said, I do think it's a worthwhile endeavour.
Reply With Quote
  #12  
02-25-2003, 06:16 PM
MrTibs MrTibs is offline
Free Member
 
Join Date: Aug 2002
Location: Canada
Posts: 200
Thanks: 0
Thanked 0 Times in 0 Posts
You are probably right on the speed issue with Delphi but I haven't done any tests so I can't say for sure.

I think you eluded to the bigger issue which is expanding the plugin support of Avisynth beyond MSVC. There is no reason that the Delphi "loader" couldn't be used with a Borland C++ plugin or a Dev-C++ plugin either.

I don't know C++ so my code is quite brutal but I'm hoping to have the DLL "loader" in beta in a few weeks. Presently I am writing a [my first] filter to fix the VHS over-sharp problem. Perhaps when I am done, someone who knows C++ can improve the code to make it faster and better.
Reply With Quote
  #13  
02-25-2003, 09:31 PM
SansGrip SansGrip is offline
Free Member
 
Join Date: Nov 2002
Location: Ontario, Canada
Posts: 1,135
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by MrTibs
Perhaps when I am done, someone who knows C++ can improve the code to make it faster and better.
I could certainly take a look at the source when it's complete.

The most important thing to remember when writing an Avisynth filter is that if your frame is, say, 528x480, your main loop will be processing roughly 400,000 bytes per frame (at least with YUY2), so do as much work outside the loop as possible (initialization of variables, tables, etc.).

Also be careful with your pointers, and remember to use GetReadPtr for read-only access and GetWritePtr otherwise. Adding asserts to your code can be very helpful in detecting buffer overruns, which crop up an awful lot when you're new to this kind of coding.

When I was first learning I spent quite a bit of time reading other peoples' source code. Stay away from anything involving MMX at first (it's very confusing for the Avisynth/C++ beginner) -- stick to tracing through the C++ versions of all the filter source you can find.

Other than that it's definitely worth learning assembler/general optimization techniques if you intend to release your filters to the public, since you'll find a great number of people will complain about the speed .

If you have any questions about filter coding then post them in here and I'll try my best to help.
Reply With Quote
Reply




Similar Threads
Thread Thread Starter Forum Replies Last Post
Delphi: Pascal conversion of avisynth_c.h GFR Avisynth Scripting 6 09-16-2005 12:46 PM
DVD Burner: Thoughts about lite-on drives? horta Blank Media / Burning Discs 1 07-28-2004 05:56 AM
Making a Delphi GUI need some help Zyphon Avisynth Scripting 8 07-06-2004 02:05 AM
Avisynth: The WHYs of the optimal script design FredThompson Avisynth Scripting 0 07-03-2003 04:35 AM
Delphi: PicSwitch v1.0.4 (beta) vhelp Avisynth Scripting 11 03-11-2003 10:41 PM

Thread Tools



 
All times are GMT -5. The time now is 01:34 PM  —  vBulletin © Jelsoft Enterprises Ltd