|
|
Hi everyone,
I am trying to make a video out of images. And following is the full code for doing so. But trying to write only one image. The video file is getting created but with no video only some ezzzing sound. The image specification is 640 X 480 and bit depth is 24.
#include <stdio.h> #include <windows.h> #include <wmsdk.h> #include <wmsysprf.h>
#ifndef GOTO_EXIT_IF_FAILED #define GOTO_EXIT_IF_FAILED(hr) if(FAILED(hr)) goto Exit; #endif
#ifndef SAFE_RELEASE #define SAFE_RELEASE(x) \ if(x != NULL) \ { \ x->Release(); \ x = NULL; \ } #endif
#ifndef SAFE_ARRAY_DELETE #define SAFE_ARRAY_DELETE(x) \ if(x != NULL) \ { \ delete[] x; \ x = NULL; \ } #endif
HRESULT FindInputFormat(IWMWriter* pWriter, DWORD dwInput, GUID guidSubType, IWMInputMediaProps** ppProps) { HRESULT hr = S_OK; DWORD cFormats = 0; DWORD cbSize = 0; DWORD formatIndex = 0;
WM_MEDIA_TYPE* pType = NULL; IWMInputMediaProps* pProps = NULL;
// Set the ppProps parameter to point to NULL. This will // be used to check the results of the function later. *ppProps = NULL;
// Find the number of formats supported by this input. hr = pWriter->GetInputFormatCount(dwInput, &cFormats); GOTO_EXIT_IF_FAILED(hr);
// Loop through all of the supported formats. for (formatIndex = 0; formatIndex < cFormats; formatIndex++) { // Get the input media properties for the input format. hr = pWriter->GetInputFormat(dwInput, formatIndex, &pProps); GOTO_EXIT_IF_FAILED(hr);
// Get the size of the media type structure. hr = pProps->GetMediaType(NULL, &cbSize); GOTO_EXIT_IF_FAILED(hr);
// Allocate memory for the media type structure. pType = (WM_MEDIA_TYPE*) new BYTE[cbSize]; if (pType == NULL) { hr = E_OUTOFMEMORY; goto Exit; }
// Get the media type structure. hr = pProps->GetMediaType(pType, &cbSize); GOTO_EXIT_IF_FAILED(hr);
if(pType->subtype == guidSubType) { ppProps = &pProps; (*ppProps)->AddRef(); goto Exit; }
// Clean up for next iteration. SAFE_ARRAY_DELETE(pType); SAFE_RELEASE(pProps); } // End for formatIndex.
// If execution made it to this point, no matching format was found. hr = NS_E_INVALID_INPUT_FORMAT;
Exit: SAFE_ARRAY_DELETE(pType); SAFE_RELEASE(pProps); return hr; }
HRESULT ConfigureVideoInput(IWMWriter* pWriter, DWORD dwInput, GUID guidSubType, LONG lFrameWidth, LONG lFrameHeight) { HRESULT hr = S_OK; DWORD cbSize = 0; LONG lStride = 0;
IWMInputMediaProps* pProps = NULL; WM_MEDIA_TYPE* pType = NULL; WMVIDEOINFOHEADER* pVidHdr = NULL; BITMAPINFOHEADER* pBMHdr = NULL;
// Get the base input format for the required subtype. hr = FindInputFormat(pWriter, dwInput, guidSubType, &pProps); GOTO_EXIT_IF_FAILED(hr);
// Get the size of the media type structure. hr = pProps->GetMediaType(NULL, &cbSize); GOTO_EXIT_IF_FAILED(hr);
// Allocate memory for the media type structure. pType = (WM_MEDIA_TYPE*) new BYTE[cbSize]; if (pType == NULL) { hr = E_OUTOFMEMORY; goto Exit; }
// Get the media type structure. hr = pProps->GetMediaType(pType, &cbSize); GOTO_EXIT_IF_FAILED(hr);
// Adjust the format to match your source images. ///// pType->majortype = WMMEDIATYPE_Image; pType->subtype = guidSubType; pType->bFixedSizeSamples = FALSE; pType->bTemporalCompression = FALSE; pType->lSampleSize = 0; pType->formattype = WMFORMAT_VideoInfo; pType->pUnk = NULL; pType->cbFormat = sizeof(WMVIDEOINFOHEADER); ////// // First set pointers to the video structures. pVidHdr = (WMVIDEOINFOHEADER*) pType->pbFormat; pBMHdr = &(pVidHdr->bmiHeader); //// pVidHdr->rcSource.left = pVidHdr->rcTarget.left = 0; pVidHdr->rcSource.top = pVidHdr->rcTarget.top = 0; pVidHdr->rcSource.right = pVidHdr->rcTarget.right = lFrameWidth; pVidHdr->rcSource.bottom = pVidHdr->rcTarget.bottom = lFrameHeight; pVidHdr->dwBitErrorRate = 0; pVidHdr->dwBitRate = 100; pVidHdr->dwBitErrorRate = 0; pVidHdr->AvgTimePerFrame = 0; //////
pBMHdr->biWidth = lFrameWidth; pBMHdr->biHeight = lFrameHeight; ///// pBMHdr->biBitCount = 24; pBMHdr->biPlanes = 1; pBMHdr->biCompression = BI_RGB; pBMHdr->biClrImportant = 0; pBMHdr->biClrUsed = 0; pBMHdr->biXPelsPerMeter = 0; pBMHdr->biYPelsPerMeter = 0; ////// // Stride = (width * bytes/pixel), rounded to the next DWORD boundary. lStride = (lFrameWidth * (pBMHdr->biBitCount / 8) + 3) & ~3;
// Image size = stride * height. pBMHdr->biSizeImage = lFrameHeight * lStride;
// Apply the adjusted type to the video input. hr = pProps->SetMediaType(pType); GOTO_EXIT_IF_FAILED(hr);
hr = pWriter->SetInputProps(dwInput, pProps);
Exit: SAFE_ARRAY_DELETE(pType); SAFE_RELEASE(pProps); pVidHdr = NULL; pBMHdr = NULL; return hr; }
int main(void) { HRESULT hr = NULL; char *szInputFile = "D:\\BitmapSet1.bmp"; unsigned short *szOutFile = L"D:\\vishy.wmv"; HANDLE hFile;
IWMWriterAdvanced2 *pWriterAdv = NULL; IWMWriter *pWriter = NULL; IWMProfile *pProfile = NULL; IWMProfileManager *pProfileManager = NULL; IWMStreamConfig *pStreamCon = NULL; INSSBuffer *pBuffer = NULL;
hr = CoInitialize(NULL);
if(FAILED(hr)) { printf("CoInitialize failed\n"); return hr; }
hr = WMCreateWriter(NULL, &pWriter); if(FAILED(hr)) { printf("WmCreateWriter failed\n"); return hr; }
hr = pWriter->SetProfileByID( WMProfile_V70_768Video); if(FAILED(hr)) { printf("SetProfileByID failed\n"); return hr; }
//////
pWriter->SetOutputFilename(szOutFile);
hr = pWriter->BeginWriting(); if(FAILED(hr)) { printf("BeginWriting failed\n"); return hr; }
hFile = (HANDLE)CreateFile(szInputFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); DWORD size = GetFileSize(hFile, &size); BYTE *buffer = (BYTE *)malloc(size); DWORD read; if(ReadFile(hFile, buffer, size, &read, NULL) == 0) { printf("ReadFile(%s) Failed\n", szInputFile); return 1; } int cbFileHeader = sizeof(BITMAPFILEHEADER);
// Store the size of the BITMAPINFO BITMAPFILEHEADER *pBm = (BITMAPFILEHEADER*)buffer; DWORD cbBitmapInfo = pBm->bfOffBits - cbFileHeader;
// Store a pointer to the BITMAPINFO BITMAPINFO *pBmi = (BITMAPINFO*)(buffer + cbFileHeader); BITMAPINFOHEADER *pBmiHdr = (BITMAPINFOHEADER*)(buffer + cbFileHeader);
printf("%d %d X %d\n", pBmiHdr->biBitCount,pBmiHdr->biWidth, pBmiHdr-
[Quoted Text] >biHeight);
ConfigureVideoInput(pWriter, 0, WMMEDIASUBTYPE_RGB24, pBmiHdr- >biWidth,
pBmiHdr->biHeight);
// Store a pointer to the starting address of the pixel bits BYTE * Image = buffer + cbFileHeader + cbBitmapInfo;
// Close and invalidate the file handle, since we have copied its bitmap data CloseHandle(hFile); hFile = INVALID_HANDLE_VALUE;
hr = pWriter->AllocateSample(size - (cbFileHeader + cbBitmapInfo) , &pBuffer);
char *pBuf; DWORD length; hr = pBuffer->GetBufferAndLength((BYTE **)&pBuf, &length);
printf("Lenght: %d\n", length);
if(FAILED(hr)) { printf("GetBuffer failed\n"); return hr; }
memcpy(pBuf, Image, length);
(strncmp( pBuf, (char *)Image, length) == 0)?printf("equal: %s\n", pBuf):printf("not equal: %d\n",
length);
if(FAILED(hr)) { printf("SetInputSetting failed\n"); return hr; }
hr = pWriter->WriteSample(0, 1, 0, pBuffer); if(FAILED(hr)) { printf("WriteSample failed\n"); return hr; }
hr = pWriter->EndWriting(); if(FAILED(hr)) { printf("EndWriting failed\n"); return hr; }
SAFE_RELEASE ( pWriterAdv ); SAFE_RELEASE ( pWriter ); SAFE_RELEASE ( pProfile); SAFE_RELEASE ( pProfileManager ); return hr; }
|
|
|