-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
108 lines (88 loc) · 4.46 KB
/
common.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
/*
* 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 <stdexcept>
#include <string>
#include <assert.h>
#include <boost/numeric/conversion/cast.hpp>
using namespace std::literals::string_literals;
#define setup_xy int x = threadIdx.x + blockIdx.x * blockDim.x, y = threadIdx.y + blockIdx.y * blockDim.y
#define line(p) ((p) + (pitch / sizeof(T)) * y)
#define lineOff(p, off) ((p) + int(pitch / sizeof(T)) * (y + (off)))
#define point(p) ((p)[(pitch / sizeof(T)) * y + x])
struct VideoInfo {
int width, height, subSampling;
bool operator==(VideoInfo other) const {
return width == other.width && height == other.height && subSampling == other.subSampling;
}
bool operator!=(VideoInfo other) const {
return !(*this == other);
}
};
class CUDAError : public std::runtime_error {
using std::runtime_error::runtime_error;
};
#define try_cuda(expr) \
do { \
cudaError_t __err = expr; \
if (__err != cudaSuccess) { \
throw CUDAError("'"s + #expr + " failed: " + cudaGetErrorString(__err)); \
} \
} while (0)
[[noreturn]] static inline void unreachable() {
assert(false);
#ifdef _MSC_VER
__assume(false);
#else
__builtin_unreachable();
#endif
}
template <typename Td, typename Ts> void numeric_cast_to(Td &dst, Ts src) { dst = boost::numeric_cast<Td>(src); }
template <typename Td, typename Ts> void narrow_cast_to(Td &dst, Ts src) { dst = static_cast<Td>(src); }
template <typename T> struct Pass {
virtual ~Pass() = default;
virtual T *getSrcDevPtr() { throw std::logic_error("not implemented"); }
virtual unsigned getSrcPitch() { throw std::logic_error("not implemented"); }
virtual const T *getDstDevPtr() const { throw std::logic_error("not implemented"); }
virtual unsigned getDstPitch() { throw std::logic_error("not implemented"); }
virtual void process(int n, int plane, cudaStream_t stream) = 0;
[[nodiscard]] virtual Pass *dup() const = 0;
virtual void setSrcDevPtr(T *) { throw std::logic_error("this variable is readonly"); }
virtual void setSrcPitch(unsigned) { throw std::logic_error("this variable is readonly"); }
virtual void setDstDevPtr(T *) { throw std::logic_error("this variable is readonly"); };
virtual void setDstPitch(unsigned) { throw std::logic_error("this variable is readonly"); }
VideoInfo getOutputVI() const { return vi2; };
Pass(VideoInfo vi, VideoInfo vi2) : vi(vi), vi2(vi2) {}
protected:
VideoInfo vi, vi2;
};
template <typename T> struct BridgePass : public Pass<T> {
using Pass<T>::Pass;
BridgePass(const BridgePass &other) : Pass<T>(other) {}
protected:
T *src = nullptr, *dst = nullptr;
unsigned d_pitch_src, d_pitch_dst;
public:
void setSrcDevPtr(T *p) final { src = p; }
void setSrcPitch(unsigned p) final { d_pitch_src = p; }
void setDstDevPtr(T *p) final { dst = p; }
void setDstPitch(unsigned p) final { d_pitch_dst = p; }
T *getSrcDevPtr() final { return src; }
unsigned getSrcPitch() final { return d_pitch_src; }
const T *getDstDevPtr() const final { return dst; }
unsigned getDstPitch() final { return d_pitch_dst; }
};