ARX  1.0
The next-generation open source augmented reality toolkit.
Loading...
Searching...
No Matches
BufferLock.h
Go to the documentation of this file.
1
2//
3// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
4// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
5// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
6// PARTICULAR PURPOSE.
7//
8// Copyright (c) Microsoft Corporation. All rights reserved.
9//
11
12
13
14#pragma once
15
16
17//-------------------------------------------------------------------
18// VideoBufferLock class
19//
20// Locks a video buffer that might or might not support IMF2DBuffer.
21//
22//-------------------------------------------------------------------
23
25{
26public:
27 VideoBufferLock(IMFMediaBuffer *pBuffer) : m_p2DBuffer(NULL), m_bLocked(FALSE)
28 {
29 m_pBuffer = pBuffer;
30 m_pBuffer->AddRef();
31
32 // Query for the 2-D buffer interface. OK if this fails.
33 (void)m_pBuffer->QueryInterface(IID_PPV_ARGS(&m_p2DBuffer));
34 }
35
37 {
39 SafeRelease(&m_pBuffer);
40 SafeRelease(&m_p2DBuffer);
41 }
42
43 //-------------------------------------------------------------------
44 // LockBuffer
45 //
46 // Locks the buffer. Returns a pointer to scan line 0 and returns the stride.
47 //
48 // The caller must provide the default stride as an input parameter, in case
49 // the buffer does not expose IMF2DBuffer. You can calculate the default stride
50 // from the media type.
51 //-------------------------------------------------------------------
52
53 HRESULT LockBuffer(
54 LONG lDefaultStride, // Minimum stride (with no padding).
55 DWORD dwHeightInPixels, // Height of the image, in pixels.
56 BYTE **ppbScanLine0, // Receives a pointer to the start of scan line 0.
57 LONG *plStride // Receives the actual stride.
58 )
59 {
60 HRESULT hr = S_OK;
61
62 // Use the 2-D version if available.
63 if (m_p2DBuffer)
64 {
65 hr = m_p2DBuffer->Lock2D(ppbScanLine0, plStride);
66 }
67 else
68 {
69 // Use non-2D version.
70 BYTE *pData = NULL;
71
72 hr = m_pBuffer->Lock(&pData, NULL, NULL);
73 if (SUCCEEDED(hr))
74 {
75 *plStride = lDefaultStride;
76 if (lDefaultStride < 0)
77 {
78 // Bottom-up orientation. Return a pointer to the start of the
79 // last row *in memory* which is the top row of the image.
80 *ppbScanLine0 = pData + abs(lDefaultStride) * (dwHeightInPixels - 1);
81 }
82 else
83 {
84 // Top-down orientation. Return a pointer to the start of the
85 // buffer.
86 *ppbScanLine0 = pData;
87 }
88 }
89 }
90
91 m_bLocked = (SUCCEEDED(hr));
92
93 return hr;
94 }
95
96 //-------------------------------------------------------------------
97 // UnlockBuffer
98 //
99 // Unlocks the buffer. Called automatically by the destructor.
100 //-------------------------------------------------------------------
101
103 {
104 if (m_bLocked)
105 {
106 if (m_p2DBuffer)
107 {
108 (void)m_p2DBuffer->Unlock2D();
109 }
110 else
111 {
112 (void)m_pBuffer->Unlock();
113 }
114 m_bLocked = FALSE;
115 }
116 }
117
118private:
119 IMFMediaBuffer *m_pBuffer;
120 IMF2DBuffer *m_p2DBuffer;
121
122 BOOL m_bLocked;
123};
#define FALSE
Definition: ar.h:106
Definition: BufferLock.h:25
HRESULT LockBuffer(LONG lDefaultStride, DWORD dwHeightInPixels, BYTE **ppbScanLine0, LONG *plStride)
Definition: BufferLock.h:53
VideoBufferLock(IMFMediaBuffer *pBuffer)
Definition: BufferLock.h:27
void UnlockBuffer()
Definition: BufferLock.h:102
~VideoBufferLock()
Definition: BufferLock.h:36