View Full Version : VB Code to Display Mpeg2Dec frame Aspect Ratio, Resizing, Cropping
DoC hEx
31st March 2003, 14:52
New version now includes support for the new 'DGDecode.dll'. But it has a special version of the DLL that is compatible with the VB6 IDE Development (see This Doom9 Topic (http://forum.doom9.org/showthread.php?s=&threadid=78589) for more details). I recompiled the DLL with wrapper function for VB6, this version of the DLL IS NOT FOR RE-DISTRIBUTION but only to allow VB programmers develop with the IDE. So please no included this DLL in any releases, use the normal one.
New Link: SampleD2V_DG.rar (http://www.ilovephilosophy.com/SampleD2V_DG.rar)
-----
Edit 26 June 2004
** This file now includes the fix for the playback problem **
I’ve updated the code for the last time… Well I think so… coz I know this version will be fast enough for everybody!!!
I’ve completely changed how the pixels are drawn to the screen; I’ve removed the api_SetPixelV as this is what’s really slowing down the process, and am using a direct memory write to the DC.hDC. This means no time is lost on silly unneeded function calls. The only thing slowing this version down is a loop to swap the R and B of the RGB as they are in the reverse order, other then that it should all be VERY fast!!!
Link is the good old reliable… SampleD2V.Zip (http://indigo.ie/~wilsonj/SampleD2V.zip)
-----
Edit 2003-9-19
Okay one more update, I hope you like it!
This new version now includes Aspect ratio correction, resizing, and cropping. So should do almost everything anybody could want. If you have any ideas on other functions you would like let me know. I’m not promising anything, but I’ll try to add it if it’s a good idea.
The file is at the same old link.
-----
Edit 2003-4-7
I've changed the code to make it run a bit faster. If your interested you should download the zip again, as I've updated the Zip file same link below.
Alain_French was right about the loops, I had a bug which I didn't notice so I added code to fix the problem, which lead to the slowing down of the code by adding more loop.
------
Edit 2003-4-6
Here is some code that uses VB and Mpeg2Dec.dll to get information about a D2V file. It will retrieve the Video information and display a selected frame from the D2V file to a VB PictureBox control. It’s a bit slow at the moment, as it’s almost 100% VB code.
SampleD2V.Zip (http://indigo.ie/~wilsonj/SampleD2V.zip) (fixed link)
Note: (SP3 of the VB is needed)
All feedback welcome!
cfm
31st March 2003, 15:01
There is a small problem with your link. It points to a .zip.zip file.
Thanks for this. It comes at the exact time when I need it. Except maybe that it should be in C++, but can't get everything... :D
Alain_French
5th April 2003, 13:53
Hy,
I tested your program and thats true this is very slow :(
I think, this is a problem with VB langage, because you do a loop 1.000.000 with the pixelV function. Somebody may make test with C++.
If not, perharps the news dll mpeg2dec3.dll is faster than the other.
I already try to make this function with the windowmediaplayer control. I can select the frame that i want, but i can't show it !
If, you know how show a selected frame with this OCX, please tell me :)
Bye
Alain
NiTroGen
5th April 2003, 23:40
I've tried to use similar code to call vidframe.dll (see this thread (http://forum.doom9.org/showthread.php?s=&threadid=49332)), but I had no success. Can anyone adapt this code to be used with vidframe.dll? With this dll more video file types can be opened, not only D2V.
DoC hEx
6th April 2003, 06:21
I've changed the code to make it run a bit faster. If your interested you should download the zip again, as I've updated the Zip file same link below.
Alain_French was right about the loops, I had a bug which I didn't notice so I added code to fix the problem, which lead to the slowing down of the code by adding more loop.
PS. I've added this info to my original posting.
DoC hEx
6th April 2003, 06:32
Originally posted by NiTroGen
I've tried to use similar code to call vidframe.dll (see this thread (http://forum.doom9.org/showthread.php?s=&threadid=49332)), but I had no success. Can anyone adapt this code to be used with vidframe.dll? With this dll more video file types can be opened, not only D2V. I found the problem is with VB it only returns LONG types form an DLL calls. That's why I use the LONG returned as a Point in memory to copy over my user Type with the code:
Private mTVideoInfo As TVideoInfo
'Open the Video, and copy into Type Def
api_CopyMemory ByVal VarPtr(mTVideoInfo), ByVal dll_OpenMPEG(mFileName), mTVideoInfoSize
This will copy mTVideoInfoSize number of byte from the DLL dll_OpenMPEG return LONG which is a mem pointer, to the VB VarPtr of my Type TVideoInfo variable called mTVideoInfo.
Also I don't know what by of format your Function returns, as the GetFrame from Mpeg2Dec.dll was YUV 4:2:2, which needs to be convered into RGB, which is what the GetFrameRGB takes care of.
Alain_French
6th April 2003, 08:05
AH yes !
Thank you for this correction... Now your code is fast :)
I will use it in my little program !
Thanks
Alain
DoC hEx
7th April 2003, 01:38
Okay one more update, I hope you like it!
This new version now includes Aspect ratio correction, resizing, and cropping. So should do almost everything anybody could want. If you have any ideas on other functions you would like let me know. I’m not promising anything, but I’ll try to add it if it’s a good idea.
The file is at the same old link.
Alain_French
7th April 2003, 06:51
Hy,
Thank you for your update :D
I think there is a problem with aspect ratio function.
You put 1.094 for 4:3 format. But i think that it is 1.368... My 4:3 sources don't have the good aspect.
The 16:9 seems to be good. But if the 4:3 is not ok, it will not be good too. Perhaps this is 1.823 for 16:9 sources. I haven't 16:9 source to test this morning.
If not, for the speed is better. 1second against 3s before you last last update. Do you think you can do this faster ?
I dont know what it will be impoved.
Bye
Alain
DoC hEx
7th April 2003, 07:35
Yeah! :( The Aspect ratios are a little off, I’ve only used a NTSC 16:9 for testing. From what I can see there are slightly different values based of the format (PAL/NTSC) and the resolution of the source video (720x576/768x576/there are lots more), I’m going by GKnot’s list. But the cropping and resizing do stay close to the aspect ratio used, so it’s purely an aspect ration problem.
But the VB Class should work fine, as the variable mAspectRatio can be set to whatever value is required by the programmer using it. So can the ‘const’ that store the default values for 16:9 and 4:3. I’m going to have to read up on it to find out for sure, and then I’ll update the VB Class, I’ll post it as soon as I make the changes.
As for the speed, I don’t know. I’ve being reading up on different ways to write into the picture box control. I might have to find a different control if I want it to run faster. Currently it’s almost fast enough for what I need the Class to do, so I don’t know if I’ll be looking at making it run faster anytime soon, it all depends on what I can find out about how picture box works. The perfect solution would be a straight memory copy from the DLL to the Box, but so far anytime I’ve tried this it crashes VB.
Alain_French
18th September 2003, 14:19
Hi,
After a few month, i retested this code. Yes for the speed, this is always a problem... But i don't know new method. Perhaps make de new external dll. I must do test to see where are the low sequence. (GKnot is so quick :( but not in VB)
Another thing, the picturebox borderstyle must be in 0-size if not we can t see the whole frame and it s a problem to crop properly :)
Alain_French
18th September 2003, 15:56
I made few test for the speed and this is the "GetFrame" fonction which take the time. It makes 1000ms.
This is normal :
For yLoop = 0 To mTVideoInfo.Height
CurByte = mTVideoInfo.Width * 3 * yLoop
For xLoop = 0 To mTVideoInfo.Width
Xout = CurByte + (xLoop * 3)
'Display Pixel
api_SetPixelV myDC.hDC, xLoop, mTVideoInfo.Height - yLoop, RGB(Abs(frameByte(Xout)), Abs(frameByte(Xout + 1)), Abs(frameByte(Xout + 2)))
Next xLoop
Next yLoop
There is 420000 loops so we can t improve it... (in VB).
The only solution is making a dll in C++ for this function but we can t have a picture in resultat or argument... I have no idea...( AMD 3000000000+ ?)
DoC hEx
19th September 2003, 05:50
Hi Alain_French,
I’ve updated the code for the last time… Well I think so… coz I know this version will be fast enough for everybody!!!
I’ve completely changed how the pixels are drawn to the screen; I’ve removed the api_SetPixelV as this is what’s really slowing down the process, and am using a direct memory write to the DC.hDC. This means no time is lost on silly unneeded function calls. The only thing slowing this version down is a loop to swap the R and B of the RGB as they are in the reverse order, other then that it should all be VERY fast!!!
Link is the good old reliable… See the first post off this topic
Alain_French
19th September 2003, 10:10
Yes, the execution is down by 5 ! This is a great improvement :)
In my code, i put a play feature so i "getframe" in a loop. But in the 175th frame, display are not ok. The picturebox seems to be dead. I must restart VB to have new images...
Have you got an idea ? If not, this is not very important for me, the normal display is very good :)
Thanks you for this release.
DoC hEx
19th September 2003, 13:08
Doh! Sorry about that, I didn’t test it to play a long series of frames, I did only a couple of seeks. I’ve updated the program again… you know the link.
I should have been deleting the hBMP -> CreateDIBitmap. So that’s been fixed it was a two liner!
See the GetFrame function and added the DeleteObject API call to the defs.
Alain_French
19th September 2003, 17:10
Yes, it works now :)
Thank you very much for this program.
DoC hEx
26th June 2004, 21:06
New version now includes support for the new 'DGDecode.dll'. But it has a special version of the DLL that is compatible with the VB6 IDE Development (see This Doom9 Topic (http://forum.doom9.org/showthread.php?s=&threadid=78589) for more details). I recompiled the DLL with wrapper function for VB6, this version of the DLL IS NOT FOR RE-DISTRIBUTION but only to allow VB programmers develop with the IDE. So please no included this DLL in any releases, use the normal one.
New Link: SampleD2V_DG.rar (http://www.ilovephilosophy.com/SampleD2V_DG.rar)
Alain_French
2nd May 2005, 12:39
Hi thank you again for your stuff. I tried to use dgdecode and i have the same problem that you explain in your thread "Using DGDecode.dll with VB6".
However i can t download your file, is the link down ?
Alain
If I can help by adding code to DGMPGDec to avoid you needing to modify it, I'd be willing to consider it. Please advise.
Alain_French
2nd May 2005, 16:18
I ll be happy if you can add this modification but i don t know exactly what DoC hEx did.
So please come back Doc hEx :)
vion11
3rd May 2005, 16:55
Here (http://www.innerphase.de/files/SampleD2V_DG.zip) is an copy of the files.
There might be some experimental changes in source,
can't find original rar. :(
Extracting bitmaps from 'd2v' files could be faster
with native support of dgdecode.dll instead of wrapping
with avisynth.
To use it without doc hex altered dll, function
exports have to use __stdcall instead of __cdecl.
I don't know of any other solution
to access bitmap data of mpeg2 with visual basic.
Turning existing functions into __stdcall will alert
c developper using dgdecode. Maybe openMPEG2Source,
closeVideo, getRGBFrame and getFrame will get a second
'interface' for use with vb.
Originally posted by vion11
Maybe openMPEG2Source,
closeVideo, getRGBFrame and getFrame will get a second
'interface' for use with vb. That sounds reasonable. How about if I make wrapper functions renamed with _SC at the end (for "standard call")?
Alain_French
3rd May 2005, 18:03
It will be great neuron2 if you could do that :)
Does this version of Sampled2v works with you ?
I try to read an d2v created by dvd2avi1.76, dgindex 1.30b5 from an mpeg file and the pva source...
I don t know if this is important but there is @4 at the end of openmpeg2source (_openMPEG2Source_VB@4) and getrgbframe (_getRGBFrame_VB@4)
vion11
3rd May 2005, 18:04
Yes this would be great. If there are any functions
that give information about mpeg2 stream, please include
them in your vb interface.
Can't help with compiling, but will test and give feedback.
These @4 can be avoid by using .def files
Sh0dan has had success on with them on Avisynth api. Link (http://forum.doom9.org/showthread.php?s=&postid=429583&highlight=The+dll+exports+have+quite+nice+names+by+now#post429583)
Alain_French
3rd May 2005, 19:49
This is not a compiling problem because I took his executable...
Perhaps this is a problem of compatibilty between dgindex Vxx and dgdecode Vyy. I don t know.
Why do I develop my little GUI in VB ? :p
I think I could put the modify 's neuron 's version in my tool to test and I hope it will work better. But if there is a problem, i don t know if i could feedback to find solutions...
DoC hEx
5th May 2005, 01:18
All I had to do was add the following lines to the AVISynthAPI.cpp. I didn't map the close function, so I don't know if this will cause a problem. But I haven't done anything with this in a longtime. The version of the code I created the original VB DLL with was 'dgdecode1012src', which is very old.
//VB Version of the Export
extern "C" __declspec(dllexport) VideoInfo* _stdcall openMPEG2Source_VB(char* file)
{
return openMPEG2Source(file);
}
//VB Version of the Export
extern "C" __declspec(dllexport) unsigned char* _stdcall getRGBFrame_VB(int frame)
{
return getRGBFrame(frame);
}
Hope this helps.
I've added the wrappers. They'll be in the next beta version and thereafter.
Alain_French
5th May 2005, 07:08
Hi,
When will the next release be ?
I failed to compile the project with mvc++6 :rolleyes:
Sorry, I can't predict when a beta will come right now.
What are your compiling problems?
Alain_French
5th May 2005, 13:31
First of all i don t know if i have to compile with vc6 or vc.net...
I downloaded nasm (nasm-0.98.39-djgpp.zip) from sourceforge and ml from i don t remember. ml install itself n program files, and i have several .bat to use it, so i put "ml-burg.bat" into system32.
When i compile i have this :
For debug :
Deleting intermediate files and output files for project 'DGDecode - Win32 Debug'.
--------------------Configuration: DGDecode - Win32 Debug--------------------
Performing Custom Build Step on .\skl_dct_sse.asm
Performing Custom Build Step on .\simple_idct_mmx.asm
Performing Custom Build Step on .\idctmmx.asm
Io: openIn failed on "/c", Win32TextPrimIO:openRd: failed
Error executing c:\windows\system32\cmd.exe.
DGDecode.dll - 1 error(s), 0 warning(s)
For release :
Deleting intermediate files and output files for project 'DGDecode - Win32 Release'.
--------------------Configuration: DGDecode - Win32 Release--------------------
Performing Custom Build Step on .\skl_dct_sse.asm
Le chemin d'accŠs sp‚cifi‚ est introuvable.
Error executing c:\windows\system32\cmd.exe.
DGDecode.dll - 1 error(s), 0 warning(s)
If I understand, VC makes .bat and run it, but i dont know where they are and the error message is not really clear for me.
On vc.net, it make a command line with command "cl" and the error is "cl : Ligne de commande error D2016 : options de ligne de commande '/O2' et '/RTC1' non compatibles"
Please don t laugh of me :D
Perhaps I forget to install something...
I put a beta up for you:
http://neuron2.net/dgmpgdec/DGDecode131b1.zip
The wrapper functions are:
openMPEG2Source_SC
getFrame_SC
getRGBFrame_SC
closeVideo_SC
This works with the DGMPGDec 1.3.0 distribution.
For the compile issue, with VC6++ you need service pack 3 and the processor pack.
Alain_French
5th May 2005, 15:40
Hum I have "no entry for openMPEG2Source_SC in DGDecode.dll" message... But the closeVideo_SC seems to be ok.
For compiling, I going to updating
Thank you
Don't forget the @4 complication.
I used a hex editor to look at the uploaded DLL, and it has an entry for _openMPEG2Source_SC@4.
This was discussed in another thread I believe.
vion11
5th May 2005, 16:25
Beta DLL works instantly with VB example From DocHex with the following lines and without special DLL:
Private Declare Function dll_GetRGBFrame Lib "DGDecode.dll" Alias "_getRGBFrame_SC@4" (ByVal lngFrameNum As Long) As Long
Private Declare Function dll_OpenMPEG2 Lib "DGDecode.dll" Alias "_openMPEG2Source_SC@4" (ByVal strFileName As String) As Long
Private Declare Function dll_CloseMPEG2 Lib "DGDecode.dll" Alias "_closeVideo_SC@0" () As Long
Private Declare Function dll_GetFrame Lib "DGDecode.dll" Alias "_getFrame_SC@4" (ByVal lngFrameNum As Long) As Long
As always full paths work better in IDE.
With this 'underscore@4' names look a bit hmmmm strange.
Anyway it works.
Thanks
Alain_French
5th May 2005, 16:59
Yes I put the _ @4 and it works...
This is the modified sample D2V :
SampleD2V_DG (http://alain.2005.free.fr/SampleD2V_DG.rar)
Thank you. Now it will be ok with the next release of dgmpgdec :)
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.