-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.h
293 lines (253 loc) · 9.65 KB
/
pipeline.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* EEDI2CUDA: EEDI2 filter using CUDA
*
* Copyright (C) 2005-2006 Kevin Stone
* Copyright (C) 2014-2019 HolyWu
* Copyright (C) 2021 Misaki Kasumi
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
#include "eedi2.cuh"
#include "utils.cuh"
static inline void bitblt(void *dstp, int dst_stride, const void *srcp, int src_stride, size_t row_size, size_t height) {
// Copyright (C) 2012-2015 Fredrik Mellbin
if (height) {
if (src_stride == dst_stride && src_stride == (int)row_size) {
memcpy(dstp, srcp, row_size * height);
} else {
const uint8_t *srcp8 = (const uint8_t *)srcp;
uint8_t *dstp8 = (uint8_t *)dstp;
size_t i;
for (i = 0; i < height; i++) {
memcpy(dstp8, srcp8, row_size);
srcp8 += src_stride;
dstp8 += dst_stride;
}
}
}
}
struct PropsMap : public std::multimap<std::string_view, int64_t> {
using std::multimap<std::string_view, int64_t>::multimap;
std::optional<mapped_type> get(const key_type &key, const size_type idx = static_cast<size_type>(-1)) const {
auto casual = idx == static_cast<size_type>(-1);
auto [bg, ed] = casual ? std::make_pair(find(key), end()) : equal_range(key);
if (bg == ed)
return std::nullopt;
if (!casual)
for (size_type i = 0; i < idx; ++i)
if (++bg == ed)
return std::nullopt;
return std::make_optional(bg->second);
}
};
template <typename T> class BasePipeline {
std::vector<std::unique_ptr<Pass<T>>> passes;
VideoInfo vi;
int device_id;
cudaStream_t stream;
T *h_src, *h_dst;
std::vector<T *> fbs;
unsigned plane_mask = 0;
protected:
VideoInfo getOutputVI() const { return passes.back()->getOutputVI(); }
BasePipeline(std::string_view filterName, const PropsMap &props, VideoInfo vi) : vi(vi) {
using invalid_arg = std::invalid_argument;
auto vi2 = vi;
EEDI2Param d;
unsigned map, pp, fieldS;
if (vi.width < 8 || vi.height < 7)
throw invalid_arg("clip resolution too low");
if (filterName == "EEDI2")
numeric_cast_to(fieldS, props.get("field").value());
else
fieldS = 1;
numeric_cast_to(d.mthresh, props.get("mthresh").value_or(10));
numeric_cast_to(d.lthresh, props.get("lthresh").value_or(20));
numeric_cast_to(d.vthresh, props.get("vthresh").value_or(20));
numeric_cast_to(d.estr, props.get("estr").value_or(2));
numeric_cast_to(d.dstr, props.get("dstr").value_or(4));
numeric_cast_to(d.maxd, props.get("maxd").value_or(24));
numeric_cast_to(map, props.get("map").value_or(0));
numeric_cast_to(pp, props.get("pp").value_or(1));
unsigned nt;
numeric_cast_to(nt, props.get("nt").value_or(50));
numeric_cast_to(device_id, props.get("device_id").value_or(-1));
if (fieldS > 3)
throw invalid_arg("field must be 0, 1, 2 or 3");
if (d.maxd < 1 || d.maxd > 29)
throw invalid_arg("maxd must be between 1 and 29 (inclusive)");
if (map > 3)
throw invalid_arg("map must be 0, 1, 2 or 3");
if (pp > 1)
throw invalid_arg("only pp=0 or 1 is implemented");
if (map == 0 || map == 3)
vi2.height *= 2;
d.mthresh *= d.mthresh;
d.vthresh *= 81;
nt <<= sizeof(T) * 8 - 8;
d.nt4 = nt * 4;
d.nt7 = nt * 7;
d.nt8 = nt * 8;
d.nt13 = nt * 13;
d.nt19 = nt * 19;
passes.emplace_back(new EEDI2Pass<T>(vi, vi2, d, map, pp, fieldS));
if (filterName != "EEDI2") {
auto vi3 = vi2;
std::swap(vi3.width, vi3.height); // XXX: this is correct for 420 & 444 only
passes.emplace_back(new TransposePass<T>(vi2, vi3));
auto vi4 = vi3;
if (filterName == "AA2") {
vi4.width /= 2;
passes.emplace_back(new ScaleDownWPass<T>(vi3, vi4));
} else {
passes.emplace_back(new ShiftWPass<T>(vi3, vi4));
}
auto vi5 = vi4;
vi5.height *= 2;
passes.emplace_back(new EEDI2Pass<T>(vi4, vi5, d, map, pp, fieldS));
auto vi6 = vi5;
std::swap(vi6.width, vi6.height);
passes.emplace_back(new TransposePass<T>(vi5, vi6));
auto vi7 = vi6;
if (filterName == "AA2") {
vi7.width /= 2;
passes.emplace_back(new ScaleDownWPass<T>(vi6, vi7));
} else {
passes.emplace_back(new ShiftWPass<T>(vi6, vi7));
}
}
PropsMap::size_type i = 0;
try {
for (;; ++i) {
auto plane = props.get("planes", i).value();
plane_mask |= 1 << plane;
}
} catch (const std::bad_optional_access &) {
if (i == 0)
plane_mask = 7;
}
passes.shrink_to_fit();
initCuda();
}
BasePipeline(const BasePipeline &other) : vi(other.vi), device_id(other.device_id), plane_mask(other.plane_mask) {
passes.reserve(other.passes.size());
for (const auto &step : other.passes)
passes.emplace_back(step->dup());
initCuda();
}
public:
~BasePipeline() {
try_cuda(cudaFreeHost(h_src));
try_cuda(cudaFreeHost(h_dst));
for (auto fb : fbs)
try_cuda(cudaFree(fb));
}
private:
void initCuda() {
try {
try_cuda(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
} catch (const CUDAError &exc) {
throw CUDAError(exc.what() + " Please upgrade your driver."s);
}
if (auto &firstStep = *passes.front(); !firstStep.getSrcDevPtr()) {
size_t pitch;
T *fb_d_src;
try_cuda(cudaMallocPitch(&fb_d_src, &pitch, vi.width * sizeof(T), vi.height));
firstStep.setSrcDevPtr(fb_d_src);
firstStep.setSrcPitch(static_cast<unsigned>(pitch));
fbs.push_back(fb_d_src);
}
if (auto &lastStep = *passes.back(); !lastStep.getDstDevPtr()) {
auto vi2 = lastStep.getOutputVI();
size_t pitch;
T *fb_d_dst;
try_cuda(cudaMallocPitch(&fb_d_dst, &pitch, vi2.width * sizeof(T), vi2.height));
lastStep.setDstDevPtr(fb_d_dst);
lastStep.setDstPitch(static_cast<unsigned>(pitch));
fbs.push_back(fb_d_dst);
}
auto d_pitch_src = passes.front()->getSrcPitch();
auto d_pitch_dst = passes.back()->getDstPitch();
auto src_height = vi.height;
auto dst_height = passes.back()->getOutputVI().height;
try_cuda(cudaHostAlloc(&h_src, d_pitch_src * src_height, cudaHostAllocWriteCombined));
try_cuda(cudaHostAlloc(&h_dst, d_pitch_dst * dst_height, cudaHostAllocDefault));
}
protected:
void processPlane(int n, int plane, int src_width, int src_height, int dst_width, int dst_height, int s_pitch_src, int s_pitch_dst,
const void *s_src, void *s_dst) {
auto src_width_bytes = src_width * sizeof(T);
auto dst_width_bytes = dst_width * sizeof(T);
auto d_src = passes.front()->getSrcDevPtr();
auto d_dst = passes.back()->getDstDevPtr();
auto d_pitch_src = passes.front()->getSrcPitch() >> !!plane * vi.subSampling;
auto d_pitch_dst = passes.back()->getDstPitch() >> !!plane * getOutputVI().subSampling;
if (!((1u << plane) & plane_mask)) return;
// upload
bitblt(h_src, d_pitch_src, s_src, s_pitch_src, src_width_bytes, src_height);
try_cuda(cudaMemcpy2DAsync(d_src, d_pitch_src, h_src, d_pitch_src, src_width_bytes, src_height, cudaMemcpyHostToDevice, stream));
// process
for (unsigned i = 0; i < passes.size(); ++i) {
auto &cur = *passes[i];
if (i) {
auto &last = *passes[i - 1];
auto &next = *passes[i + 1];
auto last_vi = last.getOutputVI();
auto ss = !!plane * last_vi.subSampling;
if (!cur.getSrcDevPtr()) {
cur.setSrcDevPtr(const_cast<T *>(last.getDstDevPtr()));
cur.setSrcPitch(last.getDstPitch());
}
if (!cur.getDstDevPtr()) {
cur.setDstDevPtr(next.getSrcDevPtr());
cur.setDstPitch(next.getSrcPitch());
}
if (!cur.getDstDevPtr()) {
auto vi = cur.getOutputVI();
size_t pitch;
T *fb;
try_cuda(cudaMallocPitch(&fb, &pitch, vi.width * sizeof(T), vi.height));
cur.setDstDevPtr(fb);
next.setSrcDevPtr(fb);
cur.setDstPitch(static_cast<unsigned>(pitch));
next.setSrcPitch(static_cast<unsigned>(pitch));
fbs.push_back(fb);
}
auto curPtr = cur.getSrcDevPtr();
auto lastPtr = last.getDstDevPtr();
if (curPtr != lastPtr)
try_cuda(cudaMemcpy2DAsync(curPtr, cur.getSrcPitch() >> ss, lastPtr, last.getDstPitch() >> ss, last_vi.width * sizeof(T) >> ss,
last_vi.height >> ss, cudaMemcpyDeviceToDevice, stream));
}
cur.process(n, plane, stream);
}
// download
try_cuda(cudaMemcpy2DAsync(h_dst, d_pitch_dst, d_dst, d_pitch_dst, dst_width_bytes, dst_height, cudaMemcpyDeviceToHost, stream));
try_cuda(cudaStreamSynchronize(stream));
bitblt(s_dst, s_pitch_dst, h_dst, d_pitch_dst, dst_width_bytes, dst_height);
}
void prepare() {
if (device_id != -1)
try_cuda(cudaSetDevice(device_id));
}
unsigned getPlaneBypassMask() const {
const auto vi2 = getOutputVI();
if (vi != vi2) return 0;
else return ~plane_mask & 7;
}
};