Products | Support | Send comments on this topic. | Email a link to this topic. | Back to Getting Started | Help Version 18.0.11.27
LEADTOOLS Media Foundation Help

Using Memory Conversions Example for C++

Show in webframe

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 handle
IltmfConvert* 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 dialog
BOOL CALLBACK ConvertDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
   TCHAR sz[256]; 
   BSTR bstr1; 
   BSTR bstr2; 
   HRESULT hr; 
   long state; 
   long type; 
   switch (msg) 
   {
   case WM_INITDIALOG: 
      // assign the notification window
      g_pConvert->SetNotifyWindow((long) hwnd, WM_CONVERTNOTIFY); 
      
      // set the abort button text
      SetDlgItemText(hwnd, IDC_USERABORT, _T("Abort"));
   
      // start the conversion
      hr = 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 window
      g_pConvert->SetNotifyWindow((long) NULL, 0); 
#ifdef _DEBUG
   {
      long state, err, pc; 
      TCHAR sz[1024]; 
      
      // get the current state
      g_pConvert->get_State(&state); 
      
      // get the current state
      g_pConvert->get_ConvertError(&err); 
      // get the amount converted
      g_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; 
   }
   return 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 MP4 g_pConvert->put_TargetFormat(ltmfConvert_TargetFormat_MP4); #ifdef _DEBUG { long v; g_pConvert->get_TargetFormat(&v); assert(v == ltmfConvert_TargetFormat_MP4); } #endif
// get target format
IltmfTargetFormats* pTargetFormats;
IltmfTargetFormat* pTargetFormat;
g_pConvert->get_TargetFormats(&pTargetFormats);

long v;
pTargetFormats->get_Selection(&v);
pTargetFormats->Item(v, &pTargetFormat);
pTargetFormats->Release();
// get target video formats
IltmfTargetVideoFormats* pTargetVideoFormats;
pTargetFormat->get_VideoFormats(&pTargetVideoFormats);
// select the H264 target video format
bstr = SysAllocString(L"{34363248-0000-0010-8000-00AA00389B71}");
pTargetVideoFormats->Find(bstr, &index);
SysFreeString(bstr);
if(index < 0) {
// video target format isn't registered
pTargetFormat->Release();
pTargetVideoFormats->Release();
return E_FAIL; }
pTargetVideoFormats->put_Selection(index);
pTargetVideoFormats->Release();
// get target audio formats
IltmfTargetAudioFormats* pTargetAudioFormats;
pTargetFormat->get_AudioFormats(&pTargetAudioFormats);
// select the AAC target audio format
bstr = SysAllocString(L"{00001610-0000-0010-8000-00AA00389B71}");
pTargetAudioFormats->Find(bstr, &index);
SysFreeString(bstr);
if(index < 0) {
// audio target format isn't registered
pTargetFormat->Release();
pTargetAudioFormats->Release();
return E_FAIL; }
pTargetAudioFormats->put_Selection(index);
pTargetAudioFormats->Release();
pTargetFormat->Release();
// set target format properties
g_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 file hfile = CreateFile(pszSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(hfile == INVALID_HANDLE_VALUE) return E_FAIL; // allocate same-sized SAFEARRAY size = GetFileSize(hfile, NULL); psaSource = SafeArrayCreateVector(VT_UI1, 0, size); if(!psaSource) { CloseHandle(hfile); return E_OUTOFMEMORY; } // read entire source into array SafeArrayAccessData(psaSource, (void**) &buffer); if(!ReadFile(hfile, buffer, size, &cb, NULL) || cb != size) { SafeArrayUnaccessData(psaSource); CloseHandle(hfile); SafeArrayDestroy(psaSource); return E_FAIL; } SafeArrayUnaccessData(psaSource); // close file CloseHandle(hfile); // assign the source array VariantInit(&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 _UNICODE bstr = SysAllocString(pszTarget); #else swprintf(wsz, L"%hs", pszTarget); bstr = SysAllocString(wsz); #endif hr = g_pConvert->put_TargetFile(bstr); SysFreeString(bstr); if(FAILED(hr)) { g_pConvert->ResetSource(); SafeArrayDestroy(psaSource); return hr; } // setup MP4 recompression hr = SetMP4Recompression(NULL); if(FAILED(hr)) { g_pConvert->ResetTarget(); g_pConvert->ResetSource(); SafeArrayDestroy(psaSource); return hr; } // do conversion hr = (HRESULT) DialogBox(g_hInstance, (LPCTSTR)IDD_CONVERTDLG, NULL, ConvertDlgProc); // kill the convert objects interest in the arrays g_pConvert->ResetTarget(); g_pConvert->ResetSource(); // don't need the source anymore SafeArrayDestroy(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 _UNICODE WCHAR wsz[MAX_PATH]; #endif // open the source file hfile = CreateFile(pszSource, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(hfile == INVALID_HANDLE_VALUE) return E_FAIL; // allocate same-sized buffer size = GetFileSize(hfile, NULL); hglobal = GlobalAlloc(GMEM_MOVEABLE, size); if(!hglobal) { CloseHandle(hfile); return E_OUTOFMEMORY; } // read entire source into buffer buffer = GlobalLock(hglobal); if(!ReadFile(hfile, buffer, size, &cb, NULL) || cb != size) { GlobalUnlock(hglobal); CloseHandle(hfile); GlobalFree(hglobal); return E_FAIL; } GlobalUnlock(hglobal); // close file CloseHandle(hfile); // assign the source buffer hr = 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 _UNICODE bstr = SysAllocString(pszTarget); #else swprintf(wsz, L"%hs", pszTarget); bstr = SysAllocString(wsz); #endif hr = g_pConvert->put_TargetFile(bstr); SysFreeString(bstr); if(FAILED(hr)) { g_pConvert->ResetSource(); GlobalFree(hglobal); return hr; } // setup MP4 recompression hr = SetMP4Recompression(NULL); if(FAILED(hr)) { g_pConvert->ResetSource(); GlobalFree(hglobal); return hr; } // do conversion hr = (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 library hr = CoInitialize(NULL); if(FAILED(hr)) goto error; // create the convert object hr = 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: // cleanup if(g_pConvert) g_pConvert->Release(); CoUninitialize(); return 0; }
Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.