Repository navigation
Expand file tree
/
Copy pathVideoPreprocessor.cpp
More file actions
166 lines (140 loc) · 4.8 KB
/
Copy pathVideoPreprocessor.cpp
File metadata and controls
166 lines (140 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "warpr_includes.h"
#include "VideoPreprocessor.h"
#include "Graphics/GraphicsDevice.h"
#include "Core/WarpConfiguration.h"
using namespace Axodox::Infrastructure;
using namespace Warpr::Graphics;
using namespace std;
using namespace winrt;
namespace Warpr::Encoder
{
VideoPreprocessor::VideoPreprocessor(Axodox::Infrastructure::dependency_container* container)
{
_logger.log(log_severity::information, "Initializing...");
auto device = container->resolve<GraphicsDevice>();
_device = device->Device();
_videoDevice = _device.as<ID3D11VideoDevice>();
_context = device->Context().as<ID3D11VideoContext>();
_logger.log(log_severity::information, "Initialized.");
}
float VideoPreprocessor::ResolutionScale() const
{
return _resolutionScale;
}
void VideoPreprocessor::ResolutionScale(float value)
{
_resolutionScale = value;
}
void VideoPreprocessor::ProcessFrame(Capture::Frame& frame)
{
//Create targets
auto targetProperties = GetTargetProperties(frame);
EnsureTargets(targetProperties);
//Create source view
auto& inputView = _inputViews[frame.get()];
if (!inputView)
{
D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC description{
.FourCC = 0,
.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D,
.Texture2D = {
.MipSlice = 0,
.ArraySlice = 0
}
};
check_hresult(_videoDevice->CreateVideoProcessorInputView(frame.get(), _processorEnumerator.get(), &description, inputView.put()));
}
//Process frame
D3D11_VIDEO_PROCESSOR_STREAM stream{
.Enable = true,
.OutputIndex = 0,
.InputFrameOrField = 0,
.PastFrames = 0,
.FutureFrames = 0,
.ppPastSurfaces = nullptr,
.pInputSurface = inputView.get(),
.ppFutureSurfaces = nullptr,
.ppPastSurfacesRight = nullptr,
.pInputSurfaceRight = nullptr,
.ppFutureSurfacesRight = nullptr
};
//Run RGB => NV12 conversion
check_hresult(_context->VideoProcessorBlt(_processor.get(), _outputView.get(), 0, 1, &stream));
frame = _outputTexture;
}
VideoPreprocessor::TargetProperties VideoPreprocessor::GetTargetProperties(Capture::Frame& frame)
{
D3D11_TEXTURE2D_DESC description;
frame->GetDesc(&description);
return TargetProperties{
.Width = description.Width,
.Height = description.Height,
.ResolutionScale = _resolutionScale
};
}
void VideoPreprocessor::EnsureTargets(const TargetProperties& targetProperties)
{
if (_targetProperties == targetProperties) return;
_targetProperties = targetProperties;
_logger.log(log_severity::information, "Allocating resources...");
//Calculate output resolution
auto width = max(256u, MakeEven(uint32_t(targetProperties.Width * _targetProperties.ResolutionScale)));
auto height = max(256u, MakeEven(uint32_t(targetProperties.Height * _targetProperties.ResolutionScale)));
//Create video processor enumerator
{
D3D11_VIDEO_PROCESSOR_CONTENT_DESC description{
.InputFrameFormat = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE,
.InputFrameRate = {
.Numerator = 1,
.Denominator = 1
},
.InputWidth = targetProperties.Width,
.InputHeight = targetProperties.Height,
.OutputFrameRate = {
.Numerator = 1,
.Denominator = 1
},
.OutputWidth = width,
.OutputHeight = height,
.Usage = D3D11_VIDEO_USAGE_PLAYBACK_NORMAL
};
check_hresult(_videoDevice->CreateVideoProcessorEnumerator(&description, _processorEnumerator.put()));
}
//Create video processor
check_hresult(_videoDevice->CreateVideoProcessor(_processorEnumerator.get(), 0, _processor.put()));
//Create backing texture
{
D3D11_TEXTURE2D_DESC description{
.Width = width,
.Height = height,
.MipLevels = 1,
.ArraySize = 1,
.Format = DXGI_FORMAT_NV12,
.SampleDesc = {
.Count = 1,
.Quality = 0
},
.Usage = D3D11_USAGE_DEFAULT,
.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE,
.CPUAccessFlags = 0,
.MiscFlags = 0
};
check_hresult(_device->CreateTexture2D(&description, nullptr, _outputTexture.put()));
}
//Create video processor input view
{
D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC description{
.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D,
.Texture2D = {
.MipSlice = 0
}
};
check_hresult(_videoDevice->CreateVideoProcessorOutputView(_outputTexture.get(), _processorEnumerator.get(), &description, _outputView.put()));
}
_logger.log(log_severity::information, "Resources allocated.");
}
uint32_t VideoPreprocessor::MakeEven(uint32_t value)
{
return value % 2 == 0 ? value : value + 1;
}
}