Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/BufferLock.h
3185 views
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
//
10
//////////////////////////////////////////////////////////////////////////
11
12
13
14
#pragma once
15
16
#include "CaptureDevice.h"
17
#include <wrl/client.h>
18
19
20
//-------------------------------------------------------------------
21
// VideoBufferLock class
22
//
23
// Locks a video buffer that might or might not support IMF2DBuffer.
24
//
25
//-------------------------------------------------------------------
26
27
class VideoBufferLock
28
{
29
public:
30
VideoBufferLock(IMFMediaBuffer *pBuffer) : m_p2DBuffer(NULL), m_bLocked(FALSE)
31
{
32
m_pBuffer = pBuffer;
33
34
// Query for the 2-D buffer interface. OK if this fails.
35
(void)m_pBuffer->QueryInterface(IID_PPV_ARGS(&m_p2DBuffer));
36
}
37
38
~VideoBufferLock()
39
{
40
UnlockBuffer();
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
102
void UnlockBuffer()
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
118
private:
119
Microsoft::WRL::ComPtr<IMFMediaBuffer> m_pBuffer;
120
Microsoft::WRL::ComPtr<IMF2DBuffer> m_p2DBuffer;
121
122
BOOL m_bLocked;
123
};
124
125