comimgrend.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:22k
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: RCSL 1.0/RPSL 1.0
- *
- * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
- *
- * The contents of this file, and the files included with this file, are
- * subject to the current version of the RealNetworks Public Source License
- * Version 1.0 (the "RPSL") available at
- * http://www.helixcommunity.org/content/rpsl unless you have licensed
- * the file under the RealNetworks Community Source License Version 1.0
- * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
- * in which case the RCSL will apply. You may also obtain the license terms
- * directly from RealNetworks. You may not use this file except in
- * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
- * applicable to this file, the RCSL. Please see the applicable RPSL or
- * RCSL for the rights, obligations and limitations governing use of the
- * contents of the file.
- *
- * This file is part of the Helix DNA Technology. RealNetworks is the
- * developer of the Original Code and owns the copyrights in the portions
- * it created.
- *
- * This file, and the files included with this file, is distributed and made
- * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- *
- * Technology Compatibility Kit Test Suite(s) Location:
- * http://www.helixcommunity.org/content/tck
- *
- * Contributor(s):
- *
- * ***** END LICENSE BLOCK ***** */
- #include "hxtypes.h"
- #include "hxwintyp.h"
- #include "hxcom.h"
- #include "hxcomm.h"
- #include "ihxpckts.h"
- #include "hxfiles.h"
- #include "hxcore.h"
- #include "hxrendr.h"
- #include "hxplugn.h"
- #include "hxwin.h"
- #include "hxevent.h"
- #include "hxvsurf.h"
- #include "hxslist.h"
- #include "hxver.h"
- #include "baseobj.h"
- #include "wbmphdr.h"
- #include "hxtick.h"
- #include "unifimg.h"
- #include "nativeimgdec.h"
- #include "comimgrend.h"
- #include "comimgrend.ver"
- #include "hxheap.h"
- #ifdef _DEBUG
- #undef HX_THIS_FILE
- static const char HX_THIS_FILE[] = __FILE__;
- #endif
- const char* const CCommonImageRenderer::m_pszName = "CommonImageRenderer";
- const char* const CCommonImageRenderer::m_pszDescription = "Helix Common Image Renderer Plugin";
- const char* const CCommonImageRenderer::m_pszCopyright = HXVER_COPYRIGHT;
- const char* const CCommonImageRenderer::m_pszMoreInfoURL = HXVER_MOREINFO;
- const char* const CCommonImageRenderer::m_ppszStreamMimeType[] = {IMAGE_MIMETYPE_JPEG,
- IMAGE_MIMETYPE_PNG,
- IMAGE_MIMETYPE_WBMP,
- IMAGE_MIMETYPE_JPEG_STREAMED,
- IMAGE_MIMETYPE_PNG_STREAMED,
- IMAGE_MIMETYPE_WBMP_STREAMED,
- NULL};
- CCommonImageRenderer::CCommonImageRenderer()
- {
- m_lRefCount = 0;
- m_pContext = NULL;
- m_pSite = NULL;
- m_pCommonClassFactory = NULL;
- m_pStreamHeader = NULL;
- m_pMimeTypeStr = NULL;
- m_ulImageWidth = 0;
- m_ulImageHeight = 0;
- m_pPacketList = NULL;
- m_bHasForceRedraw = FALSE;
- m_bDecodeComplete = FALSE;
- m_pBitmapInfoHeader = NULL;
- m_pOutputBuffer = NULL;
- m_pNativeImageDecoder = NULL;
- }
- CCommonImageRenderer::~CCommonImageRenderer()
- {
- ClearPacketList();
- HX_RELEASE(m_pContext);
- HX_RELEASE(m_pSite);
- HX_RELEASE(m_pCommonClassFactory);
- HX_RELEASE(m_pStreamHeader);
- HX_DELETE(m_pPacketList);
- HX_DELETE(m_pBitmapInfoHeader);
- HX_RELEASE(m_pOutputBuffer);
- HX_DELETE(m_pNativeImageDecoder);
- }
- STDMETHODIMP CCommonImageRenderer::QueryInterface(REFIID riid, void** ppvObj)
- {
- HX_RESULT retVal = HXR_OK;
- if (ppvObj)
- {
- // Set default
- *ppvObj = NULL;
- // Check for IID type
- if (IsEqualIID(riid, IID_IUnknown))
- {
- AddRef();
- *ppvObj = (IUnknown*) (CHXBaseCountingObject*) (IHXPlugin*) this;
- }
- else if (IsEqualIID(riid, IID_IHXPlugin))
- {
- AddRef();
- *ppvObj = (IHXPlugin*) this;
- }
- else if (IsEqualIID(riid, IID_IHXRenderer))
- {
- AddRef();
- *ppvObj = (IHXRenderer*) this;
- }
- else if (IsEqualIID(riid, IID_IHXSiteUser))
- {
- AddRef();
- *ppvObj = (IHXSiteUser*) this;
- }
- else
- {
- retVal = HXR_NOINTERFACE;
- }
- }
- else
- {
- retVal = HXR_FAIL;
- }
- return retVal;
- }
- STDMETHODIMP_(UINT32) CCommonImageRenderer::AddRef()
- {
- return InterlockedIncrement(&m_lRefCount);
- }
- STDMETHODIMP_(UINT32) CCommonImageRenderer::Release()
- {
- if (InterlockedDecrement(&m_lRefCount) > 0)
- {
- return m_lRefCount;
- }
- delete this;
- return 0;
- }
- STDMETHODIMP CCommonImageRenderer::GetPluginInfo(REF(BOOL) rbLoadMultiple,
- REF(const char*) rpszDescription,
- REF(const char*) rpszCopyright,
- REF(const char*) rpszMoreInfoURL,
- REF(UINT32) rulVersionNumber)
- {
- rbLoadMultiple = TRUE;
- rpszDescription = (const char*) m_pszDescription;
- rpszCopyright = (const char*) m_pszCopyright;
- rpszMoreInfoURL = (const char*) m_pszMoreInfoURL;
- rulVersionNumber = TARVER_ULONG32_VERSION;
- return HXR_OK;
- }
- STDMETHODIMP CCommonImageRenderer::InitPlugin(IUnknown* pContext)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (pContext)
- {
- // Save a copy of the calling context
- HX_RELEASE(m_pContext);
- m_pContext = pContext;
- m_pContext->AddRef();
- // Get a IHXCommonClassFactory interface
- HX_RELEASE(m_pCommonClassFactory);
- retVal = m_pContext->QueryInterface(IID_IHXCommonClassFactory,
- (void**) &m_pCommonClassFactory);
- }
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::GetRendererInfo(REF(const char**) rppszStreamMimeType,
- REF(UINT32) rulInitialGranularity)
- {
- rppszStreamMimeType = (const char**) m_ppszStreamMimeType;
- rulInitialGranularity = 100;
- return HXR_OK;
- }
- STDMETHODIMP CCommonImageRenderer::StartStream(IHXStream* pStream, IHXPlayer* pPlayer)
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnHeader(IHXValues* pHeader)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (pHeader)
- {
- // Parse the stream header
- retVal = ParseStreamHeader(pHeader, m_ulImageWidth,
- m_ulImageHeight, m_pMimeTypeStr);
- if (SUCCEEDED(retVal))
- {
- // Save the stream header
- HX_RELEASE(m_pStreamHeader);
- m_pStreamHeader = pHeader;
- m_pStreamHeader->AddRef();
- }
- }
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnBegin(UINT32 ulTimeAfterBegin)
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::GetDisplayType(REF(HX_DISPLAY_TYPE) rDisplayType,
- REF(IHXBuffer*) rpDisplayInfo)
- {
- HX_RESULT retVal = HXR_OK;
- rDisplayType = HX_DISPLAY_WINDOW |
- HX_DISPLAY_SUPPORTS_RESIZE |
- HX_DISPLAY_SUPPORTS_FULLSCREEN;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnPacket(IHXPacket* pPacket, INT32 lTimeOffset)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (pPacket)
- {
- if (!m_pPacketList)
- {
- m_pPacketList = new CHXSimpleList();
- }
- if (m_pPacketList)
- {
- // AddRef the packet before going on the list
- pPacket->AddRef();
- // Add the packet to the list
- m_pPacketList->AddTail((void*) pPacket);
- }
- // Clear the return value
- retVal = HXR_OK;
- }
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnTimeSync(UINT32 ulTime)
- {
- HX_RESULT retVal = HXR_OK;
- if (m_bDecodeComplete && !m_bHasForceRedraw )
- _ForceRedraw();
-
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnPreSeek(UINT32 ulTimeBefore, UINT32 ulTimeAfter)
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnPostSeek(UINT32 ulTimeBefore, UINT32 ulTimeAfter)
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnPause(UINT32 ulTimeBeforePause)
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnBuffering(UINT32 ulReason, UINT16 usPercentComplete)
- {
- HX_RESULT retVal = HXR_OK;
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::OnEndofPackets()
- {
- HX_RESULT retVal = HXR_OK;
- // Generate single image buffer from list of packets
- IHXBuffer* pBuffer = NULL;
- retVal = GenerateImageBuffer(m_pPacketList, m_pStreamHeader, pBuffer);
- if (SUCCEEDED(retVal))
- {
- // Now we can clear the packet list
- ClearPacketList();
- // Delete the list object
- HX_DELETE(m_pPacketList);
- // Create the native image decoder object
- HX_DELETE(m_pNativeImageDecoder);
- m_pNativeImageDecoder = CNativeImageDecoder::CreateNativeImageDecoder();
- if (m_pNativeImageDecoder)
- {
- // Decode the image asynchronously
- retVal = m_pNativeImageDecoder->Decode(m_pContext,
- this,
- pBuffer,
- m_pMimeTypeStr);
- }
- }
- HX_RELEASE(pBuffer);
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::EndStream()
- {
- HX_RESULT retVal = HXR_OK;
- ClearPacketList();
- HX_RELEASE(m_pContext);
- HX_RELEASE(m_pSite);
- HX_RELEASE(m_pCommonClassFactory);
- HX_DELETE(m_pPacketList);
- HX_DELETE(m_pBitmapInfoHeader);
- HX_RELEASE(m_pOutputBuffer);
- HX_DELETE(m_pNativeImageDecoder);
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::AttachSite(IHXSite* pSite)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (pSite && !m_pSite)
- {
- // Save the site
- m_pSite = pSite;
- m_pSite->AddRef();
- // Set the size of the site
- HXxSize cSize = {m_ulImageWidth, m_ulImageHeight};
- m_pSite->SetSize(cSize);
- // Clear the return value
- retVal = HXR_OK;
- }
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::DetachSite()
- {
- HX_RESULT retVal = HXR_OK;
- // Release our ref on the site
- HX_RELEASE(m_pSite);
- return retVal;
- }
- STDMETHODIMP CCommonImageRenderer::HandleEvent(HXxEvent* pEvent)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (pEvent)
- {
- // Set defaults
- pEvent->handled = FALSE;
- pEvent->result = 0;
- // Switch based on event type
- switch (pEvent->event)
- {
- case HX_SURFACE_UPDATE:
- {
- if (m_pSite)
- {
- // Get the site size
- HXxSize cSize = {0,0};
- m_pSite->GetSize(cSize);
- // Create src and dst rects
- HXxRect cSrcRect = {0, 0, m_ulImageWidth, m_ulImageHeight};
- HXxRect cDstRect = {0, 0, cSize.cx, cSize.cy};
- // Get the video surface
- IHXVideoSurface* pSurf = (IHXVideoSurface*) pEvent->param1;
- if( pSurf && m_pOutputBuffer )
- {
- pSurf->AddRef();
- // Blt to the surface
- pSurf->Blt(m_pOutputBuffer->GetBuffer(),
- m_pBitmapInfoHeader,
- cDstRect,
- cSrcRect);
- pSurf->Release();
- pEvent->handled = TRUE;
- }
- }
- }
- break;
- default:
- break;
- }
- // Clear the return value
- retVal = HXR_OK;
- }
- return retVal;
- }
- STDMETHODIMP_(BOOL) CCommonImageRenderer::NeedsWindowedSites()
- {
- return FALSE;
- }
- STDMETHODIMP CCommonImageRenderer::DecodeDone(HX_RESULT status,
- HXBitmapInfoHeader* pHeader,
- IHXBuffer* pBuffer)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (SUCCEEDED(status) && pHeader && pBuffer)
- {
- // Allocate an HXBitmapInfoHeader
- HX_DELETE(m_pBitmapInfoHeader);
- m_pBitmapInfoHeader = new HXBitmapInfoHeader;
- if (m_pBitmapInfoHeader)
- {
- // Copy the bitmap header
- memcpy(m_pBitmapInfoHeader, pHeader, sizeof(HXBitmapInfoHeader));
- // Save the buffer
- HX_RELEASE(m_pOutputBuffer);
- m_pOutputBuffer = pBuffer;
- m_pOutputBuffer->AddRef();
- // Clear the return value
- retVal = HXR_OK;
- }
- m_bDecodeComplete = TRUE;
- //If the HX_SURFACE_UPDATE event came in while we were still
- //waiting on the async decode then we need to blt now to
- //catch up.
- if( !m_bHasForceRedraw )
- _ForceRedraw();
- }
- return retVal;
- }
- void CCommonImageRenderer::_ForceRedraw()
- {
- if( m_pSite )
- {
- HXxSize cSize = {0, 0};
- m_pSite->GetSize(cSize);
- HXxRect cRect = {0, 0, cSize.cx, cSize.cy};
- m_pSite->DamageRect(cRect);
- m_pSite->ForceRedraw();
- m_bHasForceRedraw = TRUE;
- }
- }
- HX_RESULT STDAPICALLTYPE CCommonImageRenderer::HXCreateInstance(IUnknown** ppIUnknown)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (ppIUnknown)
- {
- // Set default
- *ppIUnknown = NULL;
- // Create the object
- CCommonImageRenderer *pObj = new CCommonImageRenderer();
- if (pObj)
- {
- // QI for IUnknown
- retVal = pObj->QueryInterface(IID_IUnknown, (void**) ppIUnknown);
- }
- if (FAILED(retVal))
- {
- HX_DELETE(pObj);
- }
- }
- return retVal;
- }
- HX_RESULT CCommonImageRenderer::GenerateImageBuffer(CHXSimpleList* pPacketList,
- IHXValues* pStreamHeader,
- REF(IHXBuffer*) rpBuffer)
- {
- HX_RESULT retVal = HXR_FAIL;
- // First run through and see how big a single buffer we need
- UINT32 ulTotal = SumCopyPacketList(pPacketList, pStreamHeader, NULL);
- if (ulTotal && m_pCommonClassFactory)
- {
- // Create the buffer
- IHXBuffer* pWholeBuffer = NULL;
- retVal = m_pCommonClassFactory->CreateInstance(CLSID_IHXBuffer,
- (void**) &pWholeBuffer);
- if (SUCCEEDED(retVal))
- {
- // Set the size
- retVal = pWholeBuffer->SetSize(ulTotal);
- if (SUCCEEDED(retVal))
- {
- SumCopyPacketList(pPacketList, pStreamHeader, pWholeBuffer);
- // Set the out parameter
- HX_RELEASE(rpBuffer);
- rpBuffer = pWholeBuffer;
- rpBuffer->AddRef();
- }
- }
- HX_RELEASE(pWholeBuffer);
- }
- return retVal;
- }
- UINT32 CCommonImageRenderer::SumCopyPacketList(CHXSimpleList* pPacketList,
- IHXValues* pStreamHeader,
- IHXBuffer* pWholeBuffer)
- {
- UINT32 ulRet = 0;
- if (pPacketList && pPacketList->GetCount() > 0 && pStreamHeader)
- {
- // Get the mime type
- IHXBuffer* pMimeTypeStr = NULL;
- pStreamHeader->GetPropertyCString("MimeType", pMimeTypeStr);
- if (pMimeTypeStr)
- {
- const char* pszMimeType = (const char*) pMimeTypeStr->GetBuffer();
- if (pszMimeType)
- {
- // Get the number of overhead bytes
- UINT32 ulOverheadBytes = 0;
- if (!strcmp(pszMimeType, IMAGE_MIMETYPE_JPEG_STREAMED))
- {
- // 20 bytes of overhead in JPEG packetization
- ulOverheadBytes = 20;
- }
- else if (!strcmp(pszMimeType, IMAGE_MIMETYPE_WBMP_STREAMED))
- {
- // 4 bytes of overhead in WBMP packetization
- ulOverheadBytes = 4;
- // If we are the WBMP wire format (coming from
- // the WBMP file format and not the unified
- // file format), then we need to add the value
- // of the opaque data.
- IHXBuffer* pOpaque = NULL;
- pStreamHeader->GetPropertyBuffer("OpaqueData", pOpaque);
- if (pOpaque)
- {
- if (pWholeBuffer)
- {
- memcpy(pWholeBuffer->GetBuffer() + ulRet,
- pOpaque->GetBuffer(),
- pOpaque->GetSize());
- }
- ulRet += pOpaque->GetSize();
- }
- HX_RELEASE(pOpaque);
- }
- // Loop through the packets, either adding up
- // or copying or both
- LISTPOSITION pos = pPacketList->GetHeadPosition();
- while (pos)
- {
- IHXPacket* pPacket = (IHXPacket*) pPacketList->GetNext(pos);
- if (pPacket)
- {
- IHXBuffer* pBuffer = pPacket->GetBuffer();
- if (pBuffer)
- {
- // Add up (and maybe copy) the actual
- // bytes minus the overhead
- if (pWholeBuffer)
- {
- memcpy(pWholeBuffer->GetBuffer() + ulRet,
- pBuffer->GetBuffer() + ulOverheadBytes,
- pBuffer->GetSize() - ulOverheadBytes);
- }
- ulRet += pBuffer->GetSize() - ulOverheadBytes;
- }
- HX_RELEASE(pBuffer);
- }
- }
- }
- }
- HX_RELEASE(pMimeTypeStr);
- }
- return ulRet;
- }
- void CCommonImageRenderer::ClearPacketList()
- {
- if (m_pPacketList)
- {
- LISTPOSITION pos = m_pPacketList->GetHeadPosition();
- while (pos)
- {
- IHXPacket* pPacket = (IHXPacket*) m_pPacketList->GetNext(pos);
- HX_RELEASE(pPacket);
- }
- m_pPacketList->RemoveAll();
- }
- }
- HX_RESULT CCommonImageRenderer::ParseStreamHeader(IHXValues* pHeader,
- REF(UINT32) rulImageWidth,
- REF(UINT32) rulImageHeight,
- REF(IHXBuffer*) rpMimeTypeStr)
- {
- HX_RESULT retVal = HXR_FAIL;
- if (pHeader)
- {
- // Get the mime type string buffer
- HX_RELEASE(rpMimeTypeStr);
- retVal = pHeader->GetPropertyCString("MimeType", rpMimeTypeStr);
- if (SUCCEEDED(retVal))
- {
- // Get the mime type string
- const char* pszMimeType = (const char*) rpMimeTypeStr->GetBuffer();
- // Get the opaque data
- IHXBuffer* pOpaque = NULL;
- pHeader->GetPropertyBuffer("OpaqueData", pOpaque);
- // Default the width and height
- rulImageWidth = 0;
- rulImageHeight = 0;
- // Parse based on mime type
- if (!strcmp(pszMimeType, IMAGE_MIMETYPE_JPEG) ||
- !strcmp(pszMimeType, IMAGE_MIMETYPE_PNG) ||
- !strcmp(pszMimeType, IMAGE_MIMETYPE_WBMP))
- {
- // This header came from unified fileformat
- pHeader->GetPropertyULONG32("ImageWidth", rulImageWidth);
- pHeader->GetPropertyULONG32("ImageHeight", rulImageHeight);
- }
- else if (!strcmp(pszMimeType, IMAGE_MIMETYPE_PNG_STREAMED) ||
- !strcmp(pszMimeType, IMAGE_MIMETYPE_JPEG_STREAMED))
- {
- if (pOpaque && pOpaque->GetSize() >= 8)
- {
- BYTE* pBuf = (BYTE*) pOpaque->GetBuffer();
- rulImageWidth = (pBuf[0] << 24) | (pBuf[1] << 16) | (pBuf[2] << 8) | pBuf[3];
- rulImageHeight = (pBuf[4] << 24) | (pBuf[5] << 16) | (pBuf[6] << 8) | pBuf[7];
- }
- }
- else if (!strcmp(pszMimeType, IMAGE_MIMETYPE_WBMP_STREAMED))
- {
- if (pOpaque)
- {
- // Unpack the stream header
- UINT32 ulHdrSize = 0;
- ParseWBMPHeader((BYTE*) pOpaque->GetBuffer(),
- pOpaque->GetSize(),
- rulImageWidth,
- rulImageHeight,
- ulHdrSize);
- }
- }
- HX_RELEASE(pOpaque);
- if (!rulImageWidth || !rulImageHeight)
- {
- retVal = HXR_FAIL;
- }
- }
- }
- return retVal;
- }