summaryrefslogtreecommitdiff
path: root/libavfilter/vf_bwdif_cuda.cu
blob: 3d4c29d8c3926b2aa7ae697ea4272cce67b65bce (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (C) 2019 Philip Langdale <philipl@overt.org>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

__device__ static const int coef_lf[2] = { 4309, 213 };
__device__ static const int coef_hf[3] = { 5570, 3801, 1016 };
__device__ static const int coef_sp[2] = { 5077, 981 };

template<typename T>
__inline__ __device__ T max3(T a, T b, T c)
{
    T x = max(a, b);
    return max(x, c);
}

template<typename T>
__inline__ __device__ T min3(T a, T b, T c)
{
    T x = min(a, b);
    return min(x, c);
}

template<typename T>
__inline__ __device__ T clip(T a, T min, T max)
{
    if (a < min) {
        return min;
    } else if (a > max) {
        return max;
    } else {
        return a;
    }
}

template<typename T>
__inline__ __device__ T filter_intra(T cur_prefs3, T cur_prefs,
                                     T cur_mrefs, T cur_mrefs3,
                                     int clip_max)
{
    int final = (coef_sp[0] * (cur_mrefs + cur_prefs) -
                 coef_sp[1] * (cur_mrefs3 + cur_prefs3)) >> 13;
    return clip(final, 0, clip_max);
}

template<typename T>
__inline__ __device__ T filter(T cur_prefs3, T cur_prefs, T cur_mrefs, T cur_mrefs3,
                               T prev2_prefs4, T prev2_prefs2, T prev2_0, T prev2_mrefs2, T prev2_mrefs4,
                               T prev_prefs, T prev_mrefs, T next_prefs, T next_mrefs,
                               T next2_prefs4, T next2_prefs2, T next2_0, T next2_mrefs2, T next2_mrefs4,
                               int clip_max)
{
    T final;

    int c = cur_mrefs;
    int d = (prev2_0 + next2_0) >> 1;
    int e = cur_prefs;

    int temporal_diff0 = abs(prev2_0 - next2_0);
    int temporal_diff1 = (abs(prev_mrefs - c) + abs(prev_prefs - e)) >> 1;
    int temporal_diff2 = (abs(next_mrefs - c) + abs(next_prefs - e)) >> 1;
    int diff = max3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2);

    if (!diff) {
        final = d;
    } else {
        int b = ((prev2_mrefs2 + next2_mrefs2) >> 1) - c;
        int f = ((prev2_prefs2 + next2_prefs2) >> 1) - e;
        int dc = d - c;
        int de = d - e;
        int mmax = max3(de, dc, min(b, f));
        int mmin = min3(de, dc, max(b, f));
        diff = max3(diff, mmin, -mmax);

        int interpol;
        if (abs(c - e) > temporal_diff0) {
            interpol = (((coef_hf[0] * (prev2_0 + next2_0)
                - coef_hf[1] * (prev2_mrefs2 + next2_mrefs2 + prev2_prefs2 + next2_prefs2)
                + coef_hf[2] * (prev2_mrefs4 + next2_mrefs4 + prev2_prefs4 + next2_mrefs4)) >> 2)
                + coef_lf[0] * (c + e) - coef_lf[1] * (cur_mrefs3 + cur_prefs3)) >> 13;
        } else {
            interpol = (coef_sp[0] * (c + e) - coef_sp[1] * (cur_mrefs3 + cur_prefs3)) >> 13;
        }

        if (interpol > d + diff) {
            interpol = d + diff;
        } else if (interpol < d - diff) {
            interpol = d - diff;
        }
        final = clip(interpol, 0, clip_max);
    }

    return final;
}

template<typename T>
__inline__ __device__ void bwdif_single(T *dst,
                                        cudaTextureObject_t prev,
                                        cudaTextureObject_t cur,
                                        cudaTextureObject_t next,
                                        int dst_width, int dst_height, int dst_pitch,
                                        int src_width, int src_height,
                                        int parity, int tff,
                                        int is_field_end, int clip_max)
{
    // Identify location
    int xo = blockIdx.x * blockDim.x + threadIdx.x;
    int yo = blockIdx.y * blockDim.y + threadIdx.y;

    if (xo >= dst_width || yo >= dst_height) {
        return;
    }

    // Don't modify the primary field
    if (yo % 2 == parity) {
      dst[yo*dst_pitch+xo] = tex2D<T>(cur, xo, yo);
      return;
    }

    T cur_prefs3 = tex2D<T>(cur, xo, yo + 3);
    T cur_prefs = tex2D<T>(cur, xo, yo + 1);
    T cur_mrefs = tex2D<T>(cur, xo, yo - 1);
    T cur_mrefs3 = tex2D<T>(cur, xo, yo - 3);

    if (is_field_end) {
        dst[yo*dst_pitch+xo] =
            filter_intra(cur_prefs3, cur_prefs, cur_mrefs, cur_mrefs3, clip_max);
        return;
    }

    // Calculate temporal prediction
    int is_second_field = !(parity ^ tff);

    cudaTextureObject_t prev2 = prev;
    cudaTextureObject_t prev1 = is_second_field ? cur : prev;
    cudaTextureObject_t next1 = is_second_field ? next : cur;
    cudaTextureObject_t next2 = next;

    T prev2_prefs4 = tex2D<T>(prev2, xo,  yo + 4);
    T prev2_prefs2 = tex2D<T>(prev2, xo,  yo + 2);
    T prev2_0 = tex2D<T>(prev2, xo,  yo + 0);
    T prev2_mrefs2 = tex2D<T>(prev2, xo,  yo - 2);
    T prev2_mrefs4 = tex2D<T>(prev2, xo,  yo - 4);
    T prev_prefs = tex2D<T>(prev1, xo,  yo + 1);
    T prev_mrefs = tex2D<T>(prev1, xo,  yo - 1);
    T next_prefs = tex2D<T>(next1, xo,  yo + 1);
    T next_mrefs = tex2D<T>(next1, xo,  yo - 1);
    T next2_prefs4 = tex2D<T>(next2, xo,  yo + 4);
    T next2_prefs2 = tex2D<T>(next2, xo,  yo + 2);
    T next2_0 = tex2D<T>(next2, xo,  yo + 0);
    T next2_mrefs2 = tex2D<T>(next2, xo,  yo - 2);
    T next2_mrefs4 = tex2D<T>(next2, xo,  yo - 4);

    dst[yo*dst_pitch+xo] = filter(cur_prefs3, cur_prefs, cur_mrefs, cur_mrefs3,
                                  prev2_prefs4, prev2_prefs2, prev2_0, prev2_mrefs2, prev2_mrefs4,
                                  prev_prefs, prev_mrefs, next_prefs, next_mrefs,
                                  next2_prefs4, next2_prefs2, next2_0, next2_mrefs2, next2_mrefs4,
                                  clip_max);
}

template <typename T>
__inline__ __device__ void bwdif_double(T *dst,
                                        cudaTextureObject_t prev,
                                        cudaTextureObject_t cur,
                                        cudaTextureObject_t next,
                                        int dst_width, int dst_height, int dst_pitch,
                                        int src_width, int src_height,
                                        int parity, int tff,
                                        int is_field_end, int clip_max)
{
    int xo = blockIdx.x * blockDim.x + threadIdx.x;
    int yo = blockIdx.y * blockDim.y + threadIdx.y;

    if (xo >= dst_width || yo >= dst_height) {
        return;
    }

    if (yo % 2 == parity) {
      // Don't modify the primary field
      dst[yo*dst_pitch+xo] = tex2D<T>(cur, xo, yo);
      return;
    }

    T cur_prefs3 = tex2D<T>(cur, xo, yo + 3);
    T cur_prefs = tex2D<T>(cur, xo, yo + 1);
    T cur_mrefs = tex2D<T>(cur, xo, yo - 1);
    T cur_mrefs3 = tex2D<T>(cur, xo, yo - 3);

    if (is_field_end) {
        T final;
        final.x = filter_intra(cur_prefs3.x, cur_prefs.x, cur_mrefs.x, cur_mrefs3.x,
                               clip_max);
        final.y = filter_intra(cur_prefs3.y, cur_prefs.y, cur_mrefs.y, cur_mrefs3.y,
                               clip_max);
        dst[yo*dst_pitch+xo] = final;
        return;
    }

    int is_second_field = !(parity ^ tff);

    cudaTextureObject_t prev2 = prev;
    cudaTextureObject_t prev1 = is_second_field ? cur : prev;
    cudaTextureObject_t next1 = is_second_field ? next : cur;
    cudaTextureObject_t next2 = next;

    T prev2_prefs4 = tex2D<T>(prev2, xo,  yo + 4);
    T prev2_prefs2 = tex2D<T>(prev2, xo,  yo + 2);
    T prev2_0 = tex2D<T>(prev2, xo,  yo + 0);
    T prev2_mrefs2 = tex2D<T>(prev2, xo,  yo - 2);
    T prev2_mrefs4 = tex2D<T>(prev2, xo,  yo - 4);
    T prev_prefs = tex2D<T>(prev1, xo,  yo + 1);
    T prev_mrefs = tex2D<T>(prev1, xo,  yo - 1);
    T next_prefs = tex2D<T>(next1, xo,  yo + 1);
    T next_mrefs = tex2D<T>(next1, xo,  yo - 1);
    T next2_prefs4 = tex2D<T>(next2, xo,  yo + 4);
    T next2_prefs2 = tex2D<T>(next2, xo,  yo + 2);
    T next2_0 = tex2D<T>(next2, xo,  yo + 0);
    T next2_mrefs2 = tex2D<T>(next2, xo,  yo - 2);
    T next2_mrefs4 = tex2D<T>(next2, xo,  yo - 4);

    T final;
    final.x = filter(cur_prefs3.x, cur_prefs.x, cur_mrefs.x, cur_mrefs3.x,
                     prev2_prefs4.x, prev2_prefs2.x, prev2_0.x, prev2_mrefs2.x, prev2_mrefs4.x,
                     prev_prefs.x, prev_mrefs.x, next_prefs.x, next_mrefs.x,
                     next2_prefs4.x, next2_prefs2.x, next2_0.x, next2_mrefs2.x, next2_mrefs4.x,
                     clip_max);
    final.y = filter(cur_prefs3.y, cur_prefs.y, cur_mrefs.y, cur_mrefs3.y,
                     prev2_prefs4.y, prev2_prefs2.y, prev2_0.y, prev2_mrefs2.y, prev2_mrefs4.y,
                     prev_prefs.y, prev_mrefs.y, next_prefs.y, next_mrefs.y,
                     next2_prefs4.y, next2_prefs2.y, next2_0.y, next2_mrefs2.y, next2_mrefs4.y,
                     clip_max);

    dst[yo*dst_pitch+xo] = final;
}

extern "C" {

__global__ void bwdif_uchar(unsigned char *dst,
                            cudaTextureObject_t prev,
                            cudaTextureObject_t cur,
                            cudaTextureObject_t next,
                            int dst_width, int dst_height, int dst_pitch,
                            int src_width, int src_height,
                            int parity, int tff, int is_field_end, int clip_max)
{
    bwdif_single(dst, prev, cur, next,
                 dst_width, dst_height, dst_pitch,
                 src_width, src_height,
                 parity, tff, is_field_end, clip_max);
}

__global__ void bwdif_ushort(unsigned short *dst,
                            cudaTextureObject_t prev,
                            cudaTextureObject_t cur,
                            cudaTextureObject_t next,
                            int dst_width, int dst_height, int dst_pitch,
                            int src_width, int src_height,
                            int parity, int tff, int is_field_end, int clip_max)
{
    bwdif_single(dst, prev, cur, next,
                 dst_width, dst_height, dst_pitch,
                 src_width, src_height,
                 parity, tff, is_field_end, clip_max);
}

__global__ void bwdif_uchar2(uchar2 *dst,
                            cudaTextureObject_t prev,
                            cudaTextureObject_t cur,
                            cudaTextureObject_t next,
                            int dst_width, int dst_height, int dst_pitch,
                            int src_width, int src_height,
                            int parity, int tff, int is_field_end, int clip_max)
{
    bwdif_double(dst, prev, cur, next,
                 dst_width, dst_height, dst_pitch,
                 src_width, src_height,
                 parity, tff, is_field_end, clip_max);
}

__global__ void bwdif_ushort2(ushort2 *dst,
                            cudaTextureObject_t prev,
                            cudaTextureObject_t cur,
                            cudaTextureObject_t next,
                            int dst_width, int dst_height, int dst_pitch,
                            int src_width, int src_height,
                            int parity, int tff, int is_field_end, int clip_max)
{
    bwdif_double(dst, prev, cur, next,
                 dst_width, dst_height, dst_pitch,
                 src_width, src_height,
                 parity, tff, is_field_end, clip_max);
}

} /* extern "C" */