]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale_cuda_bicubic.cu
avfilter/scale_cuda: add lanczos algorithm
[ffmpeg] / libavfilter / vf_scale_cuda_bicubic.cu
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 #include "cuda/vector_helpers.cuh"
24
25 typedef float4 (*coeffs_function_t)(float);
26
27 __device__ inline float4 lanczos_coeffs(float x)
28 {
29     const float pi = 3.141592654f;
30
31     float4 res = make_float4(
32         pi * (x + 1),
33         pi * x,
34         pi * (x - 1),
35         pi * (x - 2));
36
37     res.x = res.x == 0.0f ? 1.0f :
38         __sinf(res.x) * __sinf(res.x / 2.0f) / (res.x * res.x / 2.0f);
39     res.y = res.y == 0.0f ? 1.0f :
40         __sinf(res.y) * __sinf(res.y / 2.0f) / (res.y * res.y / 2.0f);
41     res.z = res.z == 0.0f ? 1.0f :
42         __sinf(res.z) * __sinf(res.z / 2.0f) / (res.z * res.z / 2.0f);
43     res.w = res.w == 0.0f ? 1.0f :
44         __sinf(res.w) * __sinf(res.w / 2.0f) / (res.w * res.w / 2.0f);
45
46     return res / (res.x + res.y + res.z + res.w);
47 }
48
49 __device__ inline float4 bicubic_coeffs(float x)
50 {
51     const float A = -0.75f;
52
53     float4 res;
54     res.x = ((A * (x + 1) - 5 * A) * (x + 1) + 8 * A) * (x + 1) - 4 * A;
55     res.y = ((A + 2) * x - (A + 3)) * x * x + 1;
56     res.z = ((A + 2) * (1 - x) - (A + 3)) * (1 - x) * (1 - x) + 1;
57     res.w = 1.0f - res.x - res.y - res.z;
58
59     return res;
60 }
61
62 __device__ inline void derived_fast_coeffs(float4 coeffs, float x, float *h0, float *h1, float *s)
63 {
64     float g0 = coeffs.x + coeffs.y;
65     float g1 = coeffs.z + coeffs.w;
66
67     *h0 = coeffs.y / g0 - 0.5f;
68     *h1 = coeffs.w / g1 + 1.5f;
69     *s  = g0 / (g0 + g1);
70 }
71
72 template<typename V>
73 __device__ inline V apply_coeffs(float4 coeffs, V c0, V c1, V c2, V c3)
74 {
75     V res = c0 * coeffs.x;
76     res  += c1 * coeffs.y;
77     res  += c2 * coeffs.z;
78     res  += c3 * coeffs.w;
79
80     return res;
81 }
82
83 template<typename T>
84 __device__ inline void Subsample_Bicubic(coeffs_function_t coeffs_function,
85                                          cudaTextureObject_t src_tex,
86                                          T *dst,
87                                          int dst_width, int dst_height, int dst_pitch,
88                                          int src_width, int src_height,
89                                          int bit_depth)
90 {
91     int xo = blockIdx.x * blockDim.x + threadIdx.x;
92     int yo = blockIdx.y * blockDim.y + threadIdx.y;
93
94     if (yo < dst_height && xo < dst_width)
95     {
96         float hscale = (float)src_width / (float)dst_width;
97         float vscale = (float)src_height / (float)dst_height;
98         float xi = (xo + 0.5f) * hscale - 0.5f;
99         float yi = (yo + 0.5f) * vscale - 0.5f;
100         float px = floor(xi);
101         float py = floor(yi);
102         float fx = xi - px;
103         float fy = yi - py;
104
105         float factor = bit_depth > 8 ? 0xFFFF : 0xFF;
106
107         float4 coeffsX = coeffs_function(fx);
108         float4 coeffsY = coeffs_function(fy);
109
110 #define PIX(x, y) tex2D<floatT>(src_tex, (x), (y))
111
112         dst[yo * dst_pitch + xo] = from_floatN<T, floatT>(
113             apply_coeffs<floatT>(coeffsY,
114                 apply_coeffs<floatT>(coeffsX, PIX(px - 1, py - 1), PIX(px, py - 1), PIX(px + 1, py - 1), PIX(px + 2, py - 1)),
115                 apply_coeffs<floatT>(coeffsX, PIX(px - 1, py    ), PIX(px, py    ), PIX(px + 1, py    ), PIX(px + 2, py    )),
116                 apply_coeffs<floatT>(coeffsX, PIX(px - 1, py + 1), PIX(px, py + 1), PIX(px + 1, py + 1), PIX(px + 2, py + 1)),
117                 apply_coeffs<floatT>(coeffsX, PIX(px - 1, py + 2), PIX(px, py + 2), PIX(px + 1, py + 2), PIX(px + 2, py + 2))
118             ) * factor
119         );
120
121 #undef PIX
122     }
123 }
124
125 /* This does not yield correct results. Most likely because of low internal precision in tex2D linear interpolation */
126 template<typename T>
127 __device__ inline void Subsample_FastBicubic(coeffs_function_t coeffs_function,
128                                              cudaTextureObject_t src_tex,
129                                              T *dst,
130                                              int dst_width, int dst_height, int dst_pitch,
131                                              int src_width, int src_height,
132                                              int bit_depth)
133 {
134     int xo = blockIdx.x * blockDim.x + threadIdx.x;
135     int yo = blockIdx.y * blockDim.y + threadIdx.y;
136
137     if (yo < dst_height && xo < dst_width)
138     {
139         float hscale = (float)src_width / (float)dst_width;
140         float vscale = (float)src_height / (float)dst_height;
141         float xi = (xo + 0.5f) * hscale - 0.5f;
142         float yi = (yo + 0.5f) * vscale - 0.5f;
143         float px = floor(xi);
144         float py = floor(yi);
145         float fx = xi - px;
146         float fy = yi - py;
147
148         float factor = bit_depth > 8 ? 0xFFFF : 0xFF;
149
150         float4 coeffsX = coeffs_function(fx);
151         float4 coeffsY = coeffs_function(fy);
152
153         float h0x, h1x, sx;
154         float h0y, h1y, sy;
155         derived_fast_coeffs(coeffsX, fx, &h0x, &h1x, &sx);
156         derived_fast_coeffs(coeffsY, fy, &h0y, &h1y, &sy);
157
158 #define PIX(x, y) tex2D<floatT>(src_tex, (x), (y))
159
160         floatT pix[4] = {
161             PIX(px + h0x, py + h0y),
162             PIX(px + h1x, py + h0y),
163             PIX(px + h0x, py + h1y),
164             PIX(px + h1x, py + h1y)
165         };
166
167 #undef PIX
168
169         dst[yo * dst_pitch + xo] = from_floatN<T, floatT>(
170             lerp_scalar(
171                 lerp_scalar(pix[3], pix[2], sx),
172                 lerp_scalar(pix[1], pix[0], sx),
173                 sy) * factor
174         );
175     }
176 }
177
178 extern "C" {
179
180 #define BICUBIC_KERNEL(T) \
181     __global__ void Subsample_Bicubic_ ## T(cudaTextureObject_t src_tex,                  \
182                                             T *dst,                                       \
183                                             int dst_width, int dst_height, int dst_pitch, \
184                                             int src_width, int src_height,                \
185                                             int bit_depth)                                \
186     {                                                                                     \
187         Subsample_Bicubic<T>(&bicubic_coeffs, src_tex, dst,                               \
188                              dst_width, dst_height, dst_pitch,                            \
189                              src_width, src_height,                                       \
190                              bit_depth);                                                  \
191     }
192
193 BICUBIC_KERNEL(uchar)
194 BICUBIC_KERNEL(uchar2)
195 BICUBIC_KERNEL(uchar4)
196
197 BICUBIC_KERNEL(ushort)
198 BICUBIC_KERNEL(ushort2)
199 BICUBIC_KERNEL(ushort4)
200
201
202 #define LANCZOS_KERNEL(T) \
203     __global__ void Subsample_Lanczos_ ## T(cudaTextureObject_t src_tex,                  \
204                                             T *dst,                                       \
205                                             int dst_width, int dst_height, int dst_pitch, \
206                                             int src_width, int src_height,                \
207                                             int bit_depth)                                \
208     {                                                                                     \
209         Subsample_Bicubic<T>(&lanczos_coeffs, src_tex, dst,                               \
210                              dst_width, dst_height, dst_pitch,                            \
211                              src_width, src_height,                                       \
212                              bit_depth);                                                  \
213     }
214
215 LANCZOS_KERNEL(uchar)
216 LANCZOS_KERNEL(uchar2)
217 LANCZOS_KERNEL(uchar4)
218
219 LANCZOS_KERNEL(ushort)
220 LANCZOS_KERNEL(ushort2)
221 LANCZOS_KERNEL(ushort4)
222
223 }