The following code demonstrates how to implement memory conversions:
#define MAKE_MEDIA_PATH(pFileName) (TEXT("C:\\LEADTOOLS\\Media\\")TEXT(pFileName))// include the LEAD Media Foundation TOOLKIT header#include "ltmf.h"#include "resource.h"#include <tchar.h>#include <stdio.h>#include <assert.h>HINSTANCE g_hInstance; // application instance handleIltmfConvert* g_pConvert; // convert object's interface pointer// user defined message id used for conversion events#define WM_CONVERTNOTIFY (WM_USER + 1000)//// ConvertDlgProc// starts the conversion process and provides status feedback//// controls:// IDC_CONVERTSTATUS - static control used for status messages// IDC_CONVERTPROGRESS - static control used for conversion progress// IDC_USERABORT - button control used to abort the conversion or exit the dialogBOOL CALLBACK ConvertDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){TCHAR sz[256];HRESULT hr;switch (msg){case WM_INITDIALOG:// assign the notification windowg_pConvert->SetNotifyWindow((long) hwnd, WM_CONVERTNOTIFY);// set the abort button textSetDlgItemText(hwnd, IDC_USERABORT, _T("Abort"));// start the conversionhr = g_pConvert->StartConvert();if(FAILED(hr)){_stprintf(sz, _T("conversion error 0x%.8X"), lParam);SetDlgItemText(hwnd, IDC_CONVERTSTATUS, sz);UpdateWindow(GetDlgItem(hwnd, IDC_CONVERTSTATUS));SetDlgItemText(hwnd, IDC_USERABORT, _T("Exit"));MessageBeep(0);}return TRUE;break;case WM_DESTROY:// reset the notification windowg_pConvert->SetNotifyWindow((long) NULL, 0);#ifdef _DEBUG{long state, err, pc;TCHAR sz[1024];// get the current stateg_pConvert->get_State(&state);// get the current stateg_pConvert->get_ConvertError(&err);// get the amount convertedg_pConvert->get_PercentComplete(&pc);_stprintf(sz, _T("state = %d, error = 0x%.8X, complete = %d complete"), lParam);SetDlgItemText(hwnd, IDC_CONVERTPROGRESS, sz);UpdateWindow(GetDlgItem(hwnd, IDC_CONVERTPROGRESS));break;}return TRUE;break;}#endifreturn FALSE;}//// SetMP4Recompression// sets up H264 target video format, AAC target audio, and MP4 file output//// hwndParent = parent window for target format property dialog boxes//HRESULT SetMP4Recompression(HWND hwndParent){long index;VARIANT_BOOL f;BSTR bstr;// set output format to MP4g_pConvert->put_TargetFormat(ltmfConvert_TargetFormat_MP4);#ifdef _DEBUG{ long v;g_pConvert->get_TargetFormat(&v);assert(v == ltmfConvert_TargetFormat_MP4);}#endif// get target formatIltmfTargetFormats* pTargetFormats;IltmfTargetFormat* pTargetFormat;g_pConvert->get_TargetFormats(&pTargetFormats);long v;pTargetFormats->get_Selection(&v);pTargetFormats->Item(v, &pTargetFormat);pTargetFormats->Release();// get target video formatsIltmfTargetVideoFormats* pTargetVideoFormats;pTargetFormat->get_VideoFormats(&pTargetVideoFormats);// select the H264 target video formatbstr = SysAllocString(L"{34363248-0000-0010-8000-00AA00389B71}");pTargetVideoFormats->Find(bstr, &index);SysFreeString(bstr);if(index < 0){// video target format isn't registeredpTargetFormat->Release();pTargetVideoFormats->Release();return E_FAIL;}pTargetVideoFormats->put_Selection(index);pTargetVideoFormats->Release();// get target audio formatsIltmfTargetAudioFormats* pTargetAudioFormats;pTargetFormat->get_AudioFormats(&pTargetAudioFormats);// select the AAC target audio formatbstr = SysAllocString(L"{00001610-0000-0010-8000-00AA00389B71}");pTargetAudioFormats->Find(bstr, &index);SysFreeString(bstr);if(index < 0){// audio target format isn't registeredpTargetFormat->Release();pTargetAudioFormats->Release();return E_FAIL;}pTargetAudioFormats->put_Selection(index);pTargetAudioFormats->Release();pTargetFormat->Release();// set target format propertiesg_pConvert->HasDialog(ltmfConvert_Dlg_TargetFormat, &f);if(f)g_pConvert->ShowDialog(ltmfConvert_Dlg_TargetFormat, (long) hwndParent);return S_OK;}//// RecompressArray//// 1. the source file is preloaded into a source array// 2. the source array is recompressed to a target file//// pszSource = source file path// pszTarget = target file path//HRESULT RecompressArray(LPCTSTR pszSource, LPCTSTR pszTarget){HRESULT hr;HANDLE hfile;DWORD size, cb;void* buffer;VARIANT var;SAFEARRAY* psaSource;BSTR bstr;// open the source filehfile = CreateFile(pszSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);if(hfile == INVALID_HANDLE_VALUE)return E_FAIL;// allocate same-sized SAFEARRAYsize = GetFileSize(hfile, NULL);psaSource = SafeArrayCreateVector(VT_UI1, 0, size);if(!psaSource){CloseHandle(hfile);return E_OUTOFMEMORY;}// read entire source into arraySafeArrayAccessData(psaSource, (void**) &buffer);if(!ReadFile(hfile, buffer, size, &cb, NULL) || cb != size){SafeArrayUnaccessData(psaSource);CloseHandle(hfile);SafeArrayDestroy(psaSource);return E_FAIL;}SafeArrayUnaccessData(psaSource);// close fileCloseHandle(hfile);// assign the source arrayVariantInit(&var);V_VT(&var) = (VT_ARRAY | VT_UI1);V_ARRAY(&var) = psaSource;hr = g_pConvert->put_SourceArray(var);if(FAILED(hr)){SafeArrayDestroy(psaSource);return hr;}#ifdef _DEBUG{VARIANT var;VariantInit(&var);g_pConvert->get_SourceArray(&var);assert(var.vt == (VT_ARRAY | VT_UI1));}#endif// set target file#ifdef _UNICODEbstr = SysAllocString(pszTarget);#else swprintf(wsz, L"%hs", pszTarget);bstr = SysAllocString(wsz);#endifhr = g_pConvert->put_TargetFile(bstr);SysFreeString(bstr);if(FAILED(hr)){g_pConvert->ResetSource();SafeArrayDestroy(psaSource);return hr;}// setup MP4 recompressionhr = SetMP4Recompression(NULL);if(FAILED(hr)){g_pConvert->ResetTarget();g_pConvert->ResetSource();SafeArrayDestroy(psaSource);return hr;}// do conversionhr = (HRESULT) DialogBox(g_hInstance, (LPCTSTR)IDD_CONVERTDLG, NULL, ConvertDlgProc);// kill the convert objects interest in the arraysg_pConvert->ResetTarget();g_pConvert->ResetSource();// don't need the source anymoreSafeArrayDestroy(psaSource);return hr;}//// RecompressHGlobal// recompresses from global memory to a file using the LEAD video and MP3 audio compressors//// 1. the source file is preloaded into global memory// 2. the source memory is recompressed to the target file//// pszSource = source file path// pszTarget = target file path//HRESULT RecompressHGlobal(LPCTSTR pszSource, LPCTSTR pszTarget){HRESULT hr;HANDLE hfile;BSTR bstr;DWORD size, cb;void* buffer;HGLOBAL hglobal;#ifndef _UNICODEWCHAR wsz[MAX_PATH];#endif// open the source filehfile = CreateFile(pszSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);if(hfile == INVALID_HANDLE_VALUE)return E_FAIL;// allocate same-sized buffersize = GetFileSize(hfile, NULL);hglobal = GlobalAlloc(GMEM_MOVEABLE, size);if(!hglobal){CloseHandle(hfile);return E_OUTOFMEMORY;}// read entire source into bufferbuffer = GlobalLock(hglobal);if(!ReadFile(hfile, buffer, size, &cb, NULL) || cb != size){GlobalUnlock(hglobal);CloseHandle(hfile);GlobalFree(hglobal);return E_FAIL;}GlobalUnlock(hglobal);// close fileCloseHandle(hfile);// assign the source bufferhr = g_pConvert->put_SourceHGlobal((long) hglobal);if(FAILED(hr)){GlobalFree(hglobal);return hr;}#ifdef _DEBUG{HGLOBAL hGlobal;g_pConvert->get_SourceHGlobal((long*) &hGlobal);assert(hGlobal != NULL);}#endif// set target file#ifdef _UNICODEbstr = SysAllocString(pszTarget);#elseswprintf(wsz, L"%hs", pszTarget);bstr = SysAllocString(wsz);#endifhr = g_pConvert->put_TargetFile(bstr);SysFreeString(bstr);if(FAILED(hr)){g_pConvert->ResetSource();GlobalFree(hglobal);return hr;}// setup MP4 recompressionhr = SetMP4Recompression(NULL);if(FAILED(hr)){g_pConvert->ResetSource();GlobalFree(hglobal);return hr;}// do conversionhr = (HRESULT) DialogBox(g_hInstance, (LPCTSTR)IDD_CONVERTDLG, NULL, ConvertDlgProc);g_pConvert->ResetSource();GlobalFree(hglobal);return hr;}int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){HRESULT hr;g_hInstance = hInstance;// initialize COM libraryhr = CoInitialize(NULL);if(FAILED(hr))goto error;// create the convert objecthr = CoCreateInstance(CLSID_ltmfConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmfConvert, (void**) &g_pConvert);if(FAILED(hr))goto error;hr = RecompressArray(MAKE_MEDIA_PATH("source.avi"), MAKE_MEDIA_PATH("target1.mp4"));if(FAILED(hr))goto error;hr = RecompressHGlobal(MAKE_MEDIA_PATH("source.avi"), MAKE_MEDIA_PATH("target2.mp4"));if(FAILED(hr))goto error;error:// cleanupif(g_pConvert)g_pConvert->Release();CoUninitialize();return 0;}