Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Thu, 24 Jul 2008 06:11:00 -0700,    group: microsoft.public.win32.programmer.mmedia        back       


How to encode video using VFW?   
Hi,

I've some problems with Video For Windows library, I looked previous posts 
and couldn't find any relevant post...

I want to compress an uncompressed video file using mpeg2. After some search 
it seems that Video For Windows (VFW) is suitable for the job. (I'm using 
Visual c++ 6)

I have implemented some piece of code according to below pattern, 

First, please have a look at the pattern and inform me if there is any 
conceptual mistake.

1. select codec 
2. open input video
3. get input video stream
4. get input video's stream format
5. get output video's stream format (by ICCompressGetFormat)
6. create output file
7. create output stream (how to fill avistreaminfo struct correctly?)
8. get frames by one by, compress frame, write compressed frame to 
outputStream
9. Close streams and files...


I have written some piece of code but I have some diffuculties in filling 
lots of header/information headers.
As a result my code compressed an uncompressed avi of 162 mbs into 165 mbs 
and the video quality is awful::(

Here is the code, 
What is the bug(s) ?
--------------------------------------

#define RETURN_IF_ERR(hr)  if(hr!=0) \
void CVideoCapDlg::OnCodecSelect() 
{
	_pc.cbSize = sizeof(COMPVARS);

	if (TRUE == ICCompressorChoose( NULL, ICMF_CHOOSE_ALLCOMPRESSORS, NULL, 
NULL, &_pc, "Select Compressor"))
	{


	}else
	{
		// Codec did not selected
	}
	
}


void CVideoCapDlg::OnCompress() 
{
	LONG hr; 

	char * cpInFileName = "video.avi";
	char * cpOutFileName = "videoOut.avi";
	PAVIFILE		pInFile;
	PAVIFILE		pOutFile; 
	AVIFILEINFO		pInFileInf;
	PAVISTREAM		pInStream;
    PAVISTREAM		pOutStream; 
    AVISTREAMINFO	pOutStreamInf; 

	// open AVIFile library
	AVIFileInit();           

	// open input avi file
	hr = AVIFileOpen(&pInFile, cpInFileName, OF_SHARE_DENY_WRITE, NULL); 
	RETURN_IF_ERR(hr)

	// get file info
	hr = AVIFileInfo(pInFile, &pInFileInf, sizeof(AVIFILEINFO)); 
	RETURN_IF_ERR(hr)


	// get video stream
	hr = AVIFileGetStream( pInFile, &pInStream, streamtypeVIDEO, 0);
	RETURN_IF_ERR(hr)

	// fill input stream bitmapInfoHeader 
	// size is set as sizeof(BITMAPINFOHEADER) bec we only want header info
	
	LPBITMAPINFO  pBitmapInfoIn = new BITMAPINFO();
	long iHeaderSize = sizeof(BITMAPINFO);
	hr = AVIStreamReadFormat( pInStream ,  0,(void*) pBitmapInfoIn, 
&iHeaderSize);
	RETURN_IF_ERR(hr)

	// get compressor output format
	LPBITMAPINFO  pBitmapInfoOut = new BITMAPINFO(); 
	hr = ICCompressGetFormat(_pc.hic, pBitmapInfoIn, pBitmapInfoOut);			  // 
get format
	RETURN_IF_ERR(hr)

	// is conversion possible?
	DWORD res;
	res = ICCompressQuery(_pc.hic, (LPBITMAPINFO) pBitmapInfoIn, 
(LPBITMAPINFO)pBitmapInfoOut);
	if (res==ICERR_BADFORMAT)
		return;

	// get frame start and count

	int iFrameStart = AVIStreamStart(pInStream);
	int iFrameCount = AVIStreamLength(pInStream);
	int iFrameOffset = 0;

	// open video stream for frame grab
	PGETFRAME pFrameHandler ;
	pFrameHandler = AVIStreamGetFrameOpen(pInStream, NULL);

	// COMPRESS VIDEO STREAMS

	// Find the worst-case buffer size used in compression. 
	DWORD dwCompressBufferSize = ICCompressGetSize(_pc.hic, pBitmapInfoIn, 
pBitmapInfoOut); 
 

	// create output avi file
	AVISTREAMINFO  si;

    hr = AVIFileOpen(&pOutFile, cpOutFileName, OF_WRITE | OF_CREATE, NULL); 
	RETURN_IF_ERR(hr)

	ZeroMemory((LPVOID)&pOutStreamInf,sizeof(pOutStreamInf));
	pOutStreamInf.fccType = streamtypeVIDEO;
	pOutStreamInf.fccHandler = _pc.fccHandler;
	pOutStreamInf.dwScale = 10000;
	pOutStreamInf.dwRate = 300000;
	pOutStreamInf.dwQuality = 10000;
	pOutStreamInf.dwLength = 375;
	pOutStreamInf.dwSuggestedBufferSize	= dwCompressBufferSize ;

	hr = AVIFileCreateStream(pOutFile, &pOutStream, &pOutStreamInf); 
    RETURN_IF_ERR(hr) 

	hr = AVIStreamSetFormat( pOutStream,0,pBitmapInfoOut, 
sizeof(BITMAPINFOHEADER));
    RETURN_IF_ERR(hr)

	AVIStreamInfo( pOutStream, &si, sizeof(si));


	// Allocate a buffer and get lpOutput to point to it. 
	HGLOBAL h = GlobalAlloc(GHND, dwCompressBufferSize); 
	LPVOID lpOutputBuffer = (LPVOID)GlobalLock(h); 

	LPVOID frame;
	DWORD dwCkID; 
	DWORD dwCompFlags; 
	DWORD dwFlags = 0; 

	LPVOID lpNew = GlobalAlloc(GMEM_MOVEABLE, 
pBitmapInfoOut->bmiHeader.biSizeImage); 


	if (ICCompressBegin(_pc.hic, pBitmapInfoIn, pBitmapInfoOut) == ICERR_OK)
	{ 

		while( (frame = AVIStreamGetFrame(pFrameHandler,iFrameStart + 
iFrameOffset)) != NULL)
		{


			if (ICCompress(	_pc.hic, dwFlags , &pBitmapInfoOut->bmiHeader, 
							lpOutputBuffer,&pBitmapInfoIn->bmiHeader , frame, 
							&dwCkID, &dwCompFlags, iFrameOffset, 
							0, 0, NULL, NULL) == ICERR_OK)
			{ 

				
		       hr = AVIStreamWrite(pOutStream, iFrameOffset , 1,lpOutputBuffer , 
									dwCompressBufferSize, 0 , 
									NULL, NULL); 

			} 
			else 
			{ 
				return;
			} 

			iFrameOffset++;
		}
		
		AVIStreamRelease(pOutStream); 
		AVIFileRelease(pOutFile); 

		
		ICCompressEnd(_pc.hic);    // terminate compression 
	} 
	else 
	{ 
    // Handle the error identifying the unsupported format. 
	} 
	AVIStreamRelease(pInStream); 
	AVIFileRelease(pInFile); 
	delete pBitmapInfoIn;
	delete pBitmapInfoOut;
}
date: Thu, 24 Jul 2008 06:11:00 -0700   author:   yavuz gok yavuz

Re: How to encode video using VFW?   
From: "yavuz gok"

> I want to compress an uncompressed video file using
> mpeg2. After some search it seems that Video For Windows
> (VFW) is suitable for the job. (I'm using Visual c++ 6)

VFW can only output AVI 1.0 files and compress using 
VCM/ICM/VFW codecs. There are no MPEG-2 video codecs (well, 
not exactly, but MPEG-2 video codecs that work in AVI are 
not seen as MPEG-2 video codecs from other components and 
are usually limited) and AVI files are non MPEG-2 files.

If you really want to write an AVI 1.0 file that contains 
MPEG-2-like video (and you have such an encoder from a 
third-party), you can use VFW, otherwise you need to se 
another framework, like DirectShow. If you use DirectShow, 
you need third-party MPEG-2 audio/video encoders and an 
MPEG-2 muxer/writer.

-- 
// Alessandro Angeli
// MVP :: DirectShow / MediaFoundation
// mvpnews at riseoftheants dot com
// http://www.riseoftheants.com/mmx/faq.htm
date: Thu, 24 Jul 2008 17:04:07 -0400   author:   Alessandro Angeli

Re: How to encode video using VFW?   
thanks for the reply,

let say I want an mpeg-2 like avi, (I must use VFW, I have no chance to use 
DirectShow or else...)

What is wrong with my pattern/code?
date: Thu, 24 Jul 2008 22:50:00 -0700   author:   yavuz gok

Re: How to encode video using VFW?   
From: "yavuz gok"

> let say I want an mpeg-2 like avi, (I must use VFW, I
> have no chance to use DirectShow or else...)

Why not? Unless your target is Win95 or older, DirectShow is 
supported wherever VFW is supported.

> What is wrong with my pattern/code?

How would I know? Assuming your code is correct, you left 
out the most important information, that is the contents of 
the input and output BITMAPINFOHEADERs (including any extra 
data, that is the BITMAPINFOHEADER::cbSize - 
sizeof(BITMAPINFOHEADER) BYTEs that follow the fixed 
structure, if any) and the name of the selected codec.

-- 
// Alessandro Angeli
// MVP :: DirectShow / MediaFoundation
// mvpnews at riseoftheants dot com
// http://www.riseoftheants.com/mmx/faq.htm
date: Fri, 25 Jul 2008 03:16:12 -0400   author:   Alessandro Angeli

Re: How to encode video using VFW?   
> Why not? Unless your target is Win95 or older, DirectShow is 
> supported wherever VFW is supported.
> 

I'am using visual studio 6.0, is it possible to use directshow in VS. 6.0
date: Fri, 25 Jul 2008 06:21:03 -0700   author:   yavuz gok

Re: How to encode video using VFW?   
On Fri, 25 Jul 2008 06:21:03 -0700, yavuz gok wrote:

> I'am using visual studio 6.0, is it possible to use directshow in VS. 6.0

Yes.  Get the DirectX 2004 Summer Update SDK for VS6.  See my Links page.

-- 
Please read this before replying:
1. Dshow & posting help:  http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others:  follow up if you are helped or you found a solution
date: Fri, 25 Jul 2008 09:07:18 -0600   author:   The March Hare [MVP] erland

Re: How to encode video using VFW?   
> > I'am using visual studio 6.0, is it possible to use directshow in VS. 6.0
> 
> Yes.  Get the DirectX 2004 Summer Update SDK for VS6.  See my Links page.

I download the SDK[1] but when I try to include dshow.h in an project I get 
the below error:

c:\program files\microsoft directx 9.0 sdk (summer 
2004)\include\strmif.h(1018) : error C2146: syntax error : missing ';' before 
identifier 'HSEMAPHORE'
c:\program files\microsoft directx 9.0 sdk (summer 
2004)\include\strmif.h(1018) : fatal error C1004: unexpected end of file found

compiler does not recognize DWORD_PTR...

I found a message[2] about the error but link of solution is dead 
unfortunately
It seems that there are some compability problems with vs 6.0 + directx 9.0 
sumer update 2004 
Any advice?



[1] sdk: 
http://www.microsoft.com/downloads/thankyou.aspx?familyId=fd044a42-9912-42a3-9a9e-d857199f888e&displayLang=en&hash=F3xGj4sDz90vcGg3Vh9PDUfQX1kffwwGlzj1fszyYgODSzX2T3p1ReXFzxZYy8uT%2f3xF0Kj97GzPepNTLQDvoA%3d%3d
[2] solution: http://osdir.com/ml/lib.opencv/2006-01/msg00659.html
date: Mon, 28 Jul 2008 05:43:00 -0700   author:   yavuz gok

Re: How to encode video using VFW?   
On Mon, 28 Jul 2008 05:43:00 -0700, yavuz gok wrote:

> I download the SDK[1] but when I try to include dshow.h in an project I get 
> the below error:
> 
> c:\program files\microsoft directx 9.0 sdk (summer 
> 2004)\include\strmif.h(1018) : error C2146: syntax error : missing ';' before 
> identifier 'HSEMAPHORE'
> c:\program files\microsoft directx 9.0 sdk (summer 
> 2004)\include\strmif.h(1018) : fatal error C1004: unexpected end of file found
> 
> compiler does not recognize DWORD_PTR...

I thought that the Summer 2004 SDK supported VC6.  I checked its release
notes and it isn't made clear.

You will also need the corresponding version of the Platform SDK installed
and you will need its headers in your include path.

Or, you may need to go back to the Summer 2003 update for VC6 support:

http://www.microsoft.com/downloads/details.aspx?FamilyID=9216652f-51e0-402e-b7b5-feb68d00f298&DisplayLang=en


-- 
Please read this before replying:
1. Dshow & posting help:  http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others:  follow up if you are helped or you found a solution
date: Mon, 28 Jul 2008 07:23:55 -0600   author:   The March Hare [MVP] erland

Re: How to encode video using VFW?   
> 
> Or, you may need to go back to the Summer 2003 update for VC6 support:
> 

it works, 
thanks...
date: Mon, 28 Jul 2008 07:41:00 -0700   author:   yavuz gok

Re: How to encode video using VFW?   
On Mon, 28 Jul 2008 07:41:00 -0700, yavuz gok wrote:

>> 
>> Or, you may need to go back to the Summer 2003 update for VC6 support:
>> 
> 
> it works, 
> thanks...

Thank you for following up.  It has been a while since I looked at VC6
support (that version is 10 years old) and I thought the 2004 SDK supported
it.

-- 
Please read this before replying:
1. Dshow & posting help:  http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others:  follow up if you are helped or you found a solution
date: Mon, 28 Jul 2008 13:31:30 -0600   author:   The March Hare [MVP] erland

Re: How to encode video using VFW?   
After environment problems I started to investigate directshow,
but ,I think, it is very hard to understand the api ,
as I said before I want to compress an uncompressed video using mpeg2,
where can I start? What is your advice...
date: Tue, 29 Jul 2008 04:30:01 -0700   author:   yavuz gok

Re: How to encode video using VFW?   
The March Hare [MVP] wrote:
> On Mon, 28 Jul 2008 07:41:00 -0700, yavuz gok wrote:
> 
>>> Or, you may need to go back to the Summer 2003 update for VC6 support:
>>>
>> it works, 
>> thanks...
> 
> Thank you for following up.  It has been a while since I looked at VC6
> support (that version is 10 years old) and I thought the 2004 SDK supported
> it.
> 

FWIW the Summer 2004 SDK does work with VC6 but I believe MS removed the 
BASETSD.H file from the DXSDK Include directory in this distribution. So 
you have add the BASETSD.H file from the Platform SDK into the 
..\DX9SDK\Include directory - or else install the Platform SDK and add 
its include path to the VC6 directory options (which I think TMH 
mentioned) if you want to correct the compiler's DWORD_PTR problem.


Joe Flynn
date: Wed, 30 Jul 2008 19:26:58 -0500   author:   Joe Flynn am

Re: How to encode video using VFW?   
On Wed, 30 Jul 2008 19:26:58 -0500, Joe Flynn wrote:

> FWIW the Summer 2004 SDK does work with VC6 but I believe MS removed the 
> BASETSD.H file from the DXSDK Include directory in this distribution. So 
> you have add the BASETSD.H file...

Thanks for the tip, Joe.  An advantage to using the Summer 2003 update is
that is has the VC6 project files (including one great big one that
includes all the samples).  One of the main advantages to Summer 2004,
though, is that it contains a standalone dshow CHM file.  That's usually
what I use for dshow help since not much has changed since then.


-- 
Please read this before replying:
1. Dshow & posting help:  http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others:  follow up if you are helped or you found a solution
date: Wed, 30 Jul 2008 19:40:11 -0600   author:   The March Hare [MVP] erland

Re: How to encode video using VFW?   
On Tue, 29 Jul 2008 04:30:01 -0700, yavuz gok wrote:

> I want to compress an uncompressed video using mpeg2,
> where can I start? What is your advice...

Have a look at the CompressView sample in the Windows SDK (or DirectX
Summer 2004 Update SDK, see my site below for lots of dshow resources).

Also the best group for dshow questions is:

  	microsoft.public.win32.programmer.directx.video 

-- 
Please read this before replying:
1. Dshow & posting help:  http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others:  follow up if you are helped or you found a solution
date: Thu, 31 Jul 2008 17:59:58 -0600   author:   The March Hare [MVP] erland

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us