Use ltmmMemory for Memory Conversion Example for C
1. |
Create a convert object, source and target objects: |
// create a convert object, source and target memory objects
CoCreateInstance(CLSID_ltmmConvert,
NULL,
CLSCTX_INPROC_SERVER,
IID_IltmmConvert,
(void**) &pConvert);
CoCreateInstance(CLSID_ltmmMemory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IltmmMemory,
(void**) &source);
CoCreateInstance(CLSID_ltmmMemory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IltmmMemory,
(void**) &target);
// set notify window
IltmmConvert_SetNotifyWindow(pConvert, (long)m_hWnd, WM_CONVERTNOTIFY);
2. |
Setup the source object, preload it with data: |
// Set the buffer size
IltmmMemory__put_BufferSize (source, fl);
SAFEARRAY sa;
VARIANT var;
// pass data from the arr BYTE array...
memset(&sa, 0, sizeof(sa));
sa.cbElements = sizeof(unsigned char);
sa.cDims = 1;
sa.fFeatures = (FADF_AUTO | FADF_FIXEDSIZE);
sa.pvData = arr; //data to fill
sa.rgsabound[0].cElements = fl;
VariantInit(&var);
V_VT(&var) = (VT_ARRAY | VT_UI1);
V_ARRAY(&var) = &sa;
IltmmMemory_SetData (source, 0, fl, var);
// Set input memory object
IltmmConvert__put_SourceStream(pConvert, source);
// Set output memory object
IltmmConvert__put_TargetStream(pConvert, target);
// Set recompression
SetAVIRecompression(NULL, pConvert);
// Start conversion
IltmmConvert_StartConvert (pConvert);
3. |
Handle the notification window to plot the data and save it to a file: |
LRESULT OnConvertNotify(WPARAM wParam, LPARAM lParam)
{
switch(wParam)
{
case ltmmConvert_Notify_Complete:
{
// Indicates a successful completion of the conversion process.
HANDLE hFile = NULL;
// write the data to a file
hFile = CreateFile(TARGET, // open file name
GENERIC_WRITE, // open for writing
FILE_SHARE_READ, // share for reading
NULL, // no security
CREATE_ALWAYS, // re-create the file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
long fl = 0;
unsigned long lBytesWritten;
IltmmMemory *pTarget = NULL;
VARIANT varData;
unsigned char *pBuffer = NULL;
SAFEARRAY sa;
IltmmConvert__get_TargetStream(pConvert, (IUnknown**)&pTarget);
if(!pTarget)
break;
IltmmMemory__get_BufferSize(pTarget, &fl);
VariantInit(&varData);
// pass data
memset(&sa, 0, sizeof(sa));
sa.cbElements = sizeof(unsigned char);
sa.cDims = 1;
sa.fFeatures = (FADF_AUTO | FADF_FIXEDSIZE);
sa.pvData = new UCHAR[fl];
sa.rgsabound[0].cElements = fl;
V_VT(&varData) = (VT_ARRAY | VT_UI1);
V_ARRAY(&varData) = &sa;
//
// To lock the buffer for synchronization, use the following
// line instead of the line below :
// IltmmMemory_LockBuffer (pTarget, &varData);
// or
// IltmmMemory_LockRegion (pTarget, 0, fl, &varData);
// You may also use the IltmmMemory__getData() function
// To get the data without allocating a SAFEARRAY :
// IltmmMemory__getData (pTarget, 0, fl, &varData);
//
IltmmMemory_CopyData(pTarget, 0, fl, &varData);
SafeArrayAccessData(V_ARRAY(&varData), (void**)&pBuffer);
WriteFile(hFile, pBuffer, fl, &lBytesWritten, NULL);
// Plot data on screen
string dataPlot;
char data[20];
short sData;
for(int i=0; i<fl ; i++)
{
IltmmMemory__getByte (pTarget, i, &sData);
dataPlot += "0x";
dataPlot += ltoa(sData, data, 16);
dataPlot += " ";
}
OutputText(dataPlot);
// if IltmmMemory_LockBuffer() or
// IltmmMemory_LockRegion() is used,
// IltmmMemory_Unloack() must be called :
// IltmmMemory_Unlock (pTarget);
SafeArrayUnaccessData(V_ARRAY(&varData));
VariantClear(&varData);
CloseHandle(hFile);
IltmmMemory_Release(pTarget);
}
break;
}
return 0;
}
//
// SetAVIRecompression
// sets up LEAD video compression, MP3 audio compression, and AVI file output
//
// hwndParent = parent window for compressor property dialog boxes
//
HRESULT SetAVIRecompression(HWND hwndParent, IltmmConvert *pConvert)
{
ltmmCompressors* pCompressors;
long index;
VARIANT_BOOL f;
BSTR bstr;
// select the LEAD video compressor
IltmmConvert__get_VideoCompressors(pConvert, &pCompressors);
bstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD
MCMP/MJPEG Codec A COmpressor combined with a DECompressor, or encoder and a decoder, which allows you to both compress and decompress that same data.COmpressor Also known as an encoder, this is a module or algorithm to compress data. Playing that data back requires a decompressor, or decoder. combined with a DECompressor, or encoder Also known as compressor, this is a module or algorithm to compress data. Playing that data back requires a decompressor, or decoder. and a decoder Also known as a decompressor, this is a module or algorithm to decompress data., which allows you to both compress and decompress that same data. (2.0)");
IltmmCompressors_Find (pCompressors , bstr, &index);
SysFreeString(bstr);
if(index < 0)
{
// compressor isn't registered
IltmmCompressors_Release (pCompressors);
return E_FAIL;
}
IltmmCompressors__put_Selection (pCompressors, index);
IltmmCompressors_Release(pCompressors);
// select the MP3 audio video compressor
IltmmConvert__get_AudioCompressors(pConvert, &pCompressors);
bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");
IltmmCompressors_Find(pCompressors, bstr, &index);
SysFreeString(bstr);
if(index < 0)
{
// compressor isn't registered
IltmmCompressors_Release (pCompressors);
return E_FAIL;
}
IltmmCompressors__put_Selection (pCompressors, index);
IltmmCompressors_Release(pCompressors);
// set output format to AVI
IltmmConvert_put_TargetFormat (pConvert, ltmmConvert_TargetFormat_Avi);
// set video compressor properties
IltmmConvert_HasDialog(pConvert, ltmmConvert_Dlg_VideoCompressor, &f);
if(f)
IltmmConvert_ShowDialog (pConvert, ltmmConvert_Dlg_VideoCompressor, (long) hwndParent);
// set audio compressor properties
IltmmConvert_HasDialog(pConvert, ltmmConvert_Dlg_AudioCompressor, &f);
if(f)
IltmmConvert_ShowDialog(pConvert, ltmmConvert_Dlg_AudioCompressor, (long) hwndParent);
return S_OK;
}