History

avfilter/scale_cuda: add nearest neighbour algorithm

Timo Rothenpieler committed 6 years ago

Commit 4ad7af01 parent 15c0e032 files · +50 −0Browse files

Showing the changes to libavfilter (2 of 2 files). Show all

Unified · Split

2 files
  1. libavfilter/vf_scale_cuda.c +8 −0
  2. libavfilter/vf_scale_cuda.cu +42 −0
libavfilter/vf_scale_cuda.c+8−0
@@ -56,6 +56,7 @@ static const enum AVPixelFormat supported_formats[] = { · 55 unmodified lines
5656 enum {
5757 INTERP_ALGO_DEFAULT,
5858
59+ INTERP_ALGO_NEAREST,
5960 INTERP_ALGO_BILINEAR,
6061 INTERP_ALGO_BICUBIC,
6162
@@ -273,6 +274,12 @@ static av_cold int cudascale_config_props(AVFilterLink *outlink) · 211 unmodified lines
273274 extern char vf_scale_cuda_bicubic_ptx[];
274275
275276 switch(s->interp_algo) {
277+ case INTERP_ALGO_NEAREST:
278+ scaler_ptx = vf_scale_cuda_ptx;
279+ function_infix = "_Nearest";
280+ s->interp_use_linear = 0;
281+ s->interp_as_integer = 1;
282+ break;
276283 case INTERP_ALGO_BILINEAR:
277284 scaler_ptx = vf_scale_cuda_ptx;
278285 function_infix = "_Bilinear";
@@ -591,6 +598,7 @@ static const AVOption options[] = { · 312 unmodified lines
591598 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
592599 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
593600 { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = INTERP_ALGO_DEFAULT }, 0, INTERP_ALGO_COUNT - 1, FLAGS, "interp_algo" },
601+ { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_NEAREST }, 0, 0, FLAGS, "interp_algo" },
594602 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BILINEAR }, 0, 0, FLAGS, "interp_algo" },
595603 { "bicubic", "bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BICUBIC }, 0, 0, FLAGS, "interp_algo" },
596604 { "passthrough", "Do not process frames at all if parameters match", OFFSET(passthrough), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
libavfilter/vf_scale_cuda.cu+42−0
@@ -22,6 +22,27 @@ · 21 unmodified lines
2222
2323 #include "cuda/vector_helpers.cuh"
2424
25+template<typename T>
26+__device__ inline void Subsample_Nearest(cudaTextureObject_t tex,
27+ T *dst,
28+ int dst_width, int dst_height, int dst_pitch,
29+ int src_width, int src_height,
30+ int bit_depth)
31+{
32+ int xo = blockIdx.x * blockDim.x + threadIdx.x;
33+ int yo = blockIdx.y * blockDim.y + threadIdx.y;
34+
35+ if (yo < dst_height && xo < dst_width)
36+ {
37+ float hscale = (float)src_width / (float)dst_width;
38+ float vscale = (float)src_height / (float)dst_height;
39+ float xi = (xo + 0.5f) * hscale;
40+ float yi = (yo + 0.5f) * vscale;
41+
42+ dst[yo*dst_pitch+xo] = tex2D<T>(tex, xi, yi);
43+ }
44+}
45+
2546 template<typename T>
2647 __device__ inline void Subsample_Bilinear(cudaTextureObject_t tex,
2748 T *dst,
@@ -57,6 +78,27 @@ __device__ inline void Subsample_Bilinear(cudaTextureObject_t tex, · 29 unmodified lines
5778
5879 extern "C" {
5980
81+#define NEAREST_KERNEL(T) \
82+ __global__ void Subsample_Nearest_ ## T(cudaTextureObject_t src_tex, \
83+ T *dst, \
84+ int dst_width, int dst_height, int dst_pitch, \
85+ int src_width, int src_height, \
86+ int bit_depth) \
87+ { \
88+ Subsample_Nearest<T>(src_tex, dst, \
89+ dst_width, dst_height, dst_pitch, \
90+ src_width, src_height, \
91+ bit_depth); \
92+ }
93+
94+NEAREST_KERNEL(uchar)
95+NEAREST_KERNEL(uchar2)
96+NEAREST_KERNEL(uchar4)
97+
98+NEAREST_KERNEL(ushort)
99+NEAREST_KERNEL(ushort2)
100+NEAREST_KERNEL(ushort4)
101+
60102 #define BILINEAR_KERNEL(T) \
61103 __global__ void Subsample_Bilinear_ ## T(cudaTextureObject_t src_tex, \
62104 T *dst, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t