Path: blob/master/samples/winrt/ImageManipulations/MediaExtensions/Common/BufferLock.h
16349 views
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF1// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO2// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A3// PARTICULAR PURPOSE.4//5// Copyright (c) Microsoft Corporation. All rights reserved678#pragma once91011//////////////////////////////////////////////////////////////////////////12// VideoBufferLock13//14// Description:15// Locks a video buffer that might or might not support IMF2DBuffer.16//17//////////////////////////////////////////////////////////////////////////1819class VideoBufferLock20{21public:22VideoBufferLock(IMFMediaBuffer *pBuffer) : m_p2DBuffer(NULL)23{24m_pBuffer = pBuffer;25m_pBuffer->AddRef();2627// Query for the 2-D buffer interface. OK if this fails.28m_pBuffer->QueryInterface(IID_PPV_ARGS(&m_p2DBuffer));29}3031~VideoBufferLock()32{33UnlockBuffer();34SafeRelease(&m_pBuffer);35SafeRelease(&m_p2DBuffer);36}3738// LockBuffer:39// Locks the buffer. Returns a pointer to scan line 0 and returns the stride.4041// The caller must provide the default stride as an input parameter, in case42// the buffer does not expose IMF2DBuffer. You can calculate the default stride43// from the media type.4445HRESULT LockBuffer(46LONG lDefaultStride, // Minimum stride (with no padding).47DWORD dwHeightInPixels, // Height of the image, in pixels.48BYTE **ppbScanLine0, // Receives a pointer to the start of scan line 0.49LONG *plStride // Receives the actual stride.50)51{52HRESULT hr = S_OK;5354// Use the 2-D version if available.55if (m_p2DBuffer)56{57hr = m_p2DBuffer->Lock2D(ppbScanLine0, plStride);58}59else60{61// Use non-2D version.62BYTE *pData = NULL;6364hr = m_pBuffer->Lock(&pData, NULL, NULL);65if (SUCCEEDED(hr))66{67*plStride = lDefaultStride;68if (lDefaultStride < 0)69{70// Bottom-up orientation. Return a pointer to the start of the71// last row *in memory* which is the top row of the image.72*ppbScanLine0 = pData + abs(lDefaultStride) * (dwHeightInPixels - 1);73}74else75{76// Top-down orientation. Return a pointer to the start of the77// buffer.78*ppbScanLine0 = pData;79}80}81}82return hr;83}8485HRESULT UnlockBuffer()86{87if (m_p2DBuffer)88{89return m_p2DBuffer->Unlock2D();90}91else92{93return m_pBuffer->Unlock();94}95}9697private:98IMFMediaBuffer *m_pBuffer;99IMF2DBuffer *m_p2DBuffer;100};101102103