]> git.sesse.net Git - ffmpeg/blob - libavfilter/cuda/vector_helpers.cuh
avfilter/vf_pseudocolor: add two more presets
[ffmpeg] / libavfilter / cuda / vector_helpers.cuh
1 /*
2  * This file is part of FFmpeg.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #ifndef AVFILTER_CUDA_VECTORHELPERS_H
24 #define AVFILTER_CUDA_VECTORHELPERS_H
25
26 typedef unsigned char uchar;
27 typedef unsigned short ushort;
28
29 template<typename T> struct vector_helper { };
30 template<> struct vector_helper<uchar>   { typedef float  ftype; typedef int  itype; };
31 template<> struct vector_helper<uchar2>  { typedef float2 ftype; typedef int2 itype; };
32 template<> struct vector_helper<uchar4>  { typedef float4 ftype; typedef int4 itype; };
33 template<> struct vector_helper<ushort>  { typedef float  ftype; typedef int  itype; };
34 template<> struct vector_helper<ushort2> { typedef float2 ftype; typedef int2 itype; };
35 template<> struct vector_helper<ushort4> { typedef float4 ftype; typedef int4 itype; };
36 template<> struct vector_helper<int>     { typedef float  ftype; typedef int  itype; };
37 template<> struct vector_helper<int2>    { typedef float2 ftype; typedef int2 itype; };
38 template<> struct vector_helper<int4>    { typedef float4 ftype; typedef int4 itype; };
39
40 #define floatT typename vector_helper<T>::ftype
41 #define intT typename vector_helper<T>::itype
42
43 template<typename T, typename V> inline __device__ V to_floatN(const T &a) { return (V)a; }
44 template<typename T, typename V> inline __device__ T from_floatN(const V &a) { return (T)a; }
45
46 #define OPERATORS2(T) \
47     template<typename V> inline __device__ T operator+(const T &a, const V &b) { return make_ ## T (a.x + b.x, a.y + b.y); } \
48     template<typename V> inline __device__ T operator-(const T &a, const V &b) { return make_ ## T (a.x - b.x, a.y - b.y); } \
49     template<typename V> inline __device__ T operator*(const T &a, V b) { return make_ ## T (a.x * b, a.y * b); } \
50     template<typename V> inline __device__ T operator/(const T &a, V b) { return make_ ## T (a.x / b, a.y / b); } \
51     template<typename V> inline __device__ T operator>>(const T &a, V b) { return make_ ## T (a.x >> b, a.y >> b); } \
52     template<typename V> inline __device__ T operator<<(const T &a, V b) { return make_ ## T (a.x << b, a.y << b); } \
53     template<typename V> inline __device__ T &operator+=(T &a, const V &b) { a.x += b.x; a.y += b.y; return a; } \
54     template<typename V> inline __device__ void vec_set(T &a, const V &b) { a.x = b.x; a.y = b.y; } \
55     template<typename V> inline __device__ void vec_set_scalar(T &a, V b) { a.x = b; a.y = b; } \
56     template<> inline __device__ float2 to_floatN<T, float2>(const T &a) { return make_float2(a.x, a.y); } \
57     template<> inline __device__ T from_floatN<T, float2>(const float2 &a) { return make_ ## T(a.x, a.y); }
58 #define OPERATORS4(T) \
59     template<typename V> inline __device__ T operator+(const T &a, const V &b) { return make_ ## T (a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); } \
60     template<typename V> inline __device__ T operator-(const T &a, const V &b) { return make_ ## T (a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); } \
61     template<typename V> inline __device__ T operator*(const T &a, V b) { return make_ ## T (a.x * b, a.y * b, a.z * b, a.w * b); } \
62     template<typename V> inline __device__ T operator/(const T &a, V b) { return make_ ## T (a.x / b, a.y / b, a.z / b, a.w / b); } \
63     template<typename V> inline __device__ T operator>>(const T &a, V b) { return make_ ## T (a.x >> b, a.y >> b, a.z >> b, a.w >> b); } \
64     template<typename V> inline __device__ T operator<<(const T &a, V b) { return make_ ## T (a.x << b, a.y << b, a.z << b, a.w << b); } \
65     template<typename V> inline __device__ T &operator+=(T &a, const V &b) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; return a; } \
66     template<typename V> inline __device__ void vec_set(T &a, const V &b) { a.x = b.x; a.y = b.y; a.z = b.z; a.w = b.w; } \
67     template<typename V> inline __device__ void vec_set_scalar(T &a, V b) { a.x = b; a.y = b; a.z = b; a.w = b; } \
68     template<> inline __device__ float4 to_floatN<T, float4>(const T &a) { return make_float4(a.x, a.y, a.z, a.w); } \
69     template<> inline __device__ T from_floatN<T, float4>(const float4 &a) { return make_ ## T(a.x, a.y, a.z, a.w); }
70
71 OPERATORS2(int2)
72 OPERATORS2(uchar2)
73 OPERATORS2(ushort2)
74 OPERATORS2(float2)
75 OPERATORS4(int4)
76 OPERATORS4(uchar4)
77 OPERATORS4(ushort4)
78 OPERATORS4(float4)
79
80 template<typename V> inline __device__ void vec_set(int &a, V b) { a = b; }
81 template<typename V> inline __device__ void vec_set(float &a, V b) { a = b; }
82 template<typename V> inline __device__ void vec_set(uchar &a, V b) { a = b; }
83 template<typename V> inline __device__ void vec_set(ushort &a, V b) { a = b; }
84 template<typename V> inline __device__ void vec_set_scalar(int &a, V b) { a = b; }
85 template<typename V> inline __device__ void vec_set_scalar(float &a, V b) { a = b; }
86 template<typename V> inline __device__ void vec_set_scalar(uchar &a, V b) { a = b; }
87 template<typename V> inline __device__ void vec_set_scalar(ushort &a, V b) { a = b; }
88
89 template<typename T>
90 inline __device__ T lerp_scalar(T v0, T v1, float t) {
91     return t*v1 + (1.0f - t)*v0;
92 }
93
94 template<>
95 inline __device__ float2 lerp_scalar<float2>(float2 v0, float2 v1, float t) {
96     return make_float2(
97         lerp_scalar(v0.x, v1.x, t),
98         lerp_scalar(v0.y, v1.y, t)
99     );
100 }
101
102 template<>
103 inline __device__ float4 lerp_scalar<float4>(float4 v0, float4 v1, float t) {
104     return make_float4(
105         lerp_scalar(v0.x, v1.x, t),
106         lerp_scalar(v0.y, v1.y, t),
107         lerp_scalar(v0.z, v1.z, t),
108         lerp_scalar(v0.w, v1.w, t)
109     );
110 }
111
112 #endif