vf_yadif_cuda.cu 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (C) 2018 Philip Langdale <philipl@overt.org>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. template<typename T>
  21. __inline__ __device__ T spatial_predictor(T a, T b, T c, T d, T e, T f, T g,
  22. T h, T i, T j, T k, T l, T m, T n)
  23. {
  24. int spatial_pred = (d + k)/2;
  25. int spatial_score = abs(c - j) + abs(d - k) + abs(e - l);
  26. int score = abs(b - k) + abs(c - l) + abs(d - m);
  27. if (score < spatial_score) {
  28. spatial_pred = (c + l)/2;
  29. spatial_score = score;
  30. score = abs(a - l) + abs(b - m) + abs(c - n);
  31. if (score < spatial_score) {
  32. spatial_pred = (b + m)/2;
  33. spatial_score = score;
  34. }
  35. }
  36. score = abs(d - i) + abs(e - j) + abs(f - k);
  37. if (score < spatial_score) {
  38. spatial_pred = (e + j)/2;
  39. spatial_score = score;
  40. score = abs(e - h) + abs(f - i) + abs(g - j);
  41. if (score < spatial_score) {
  42. spatial_pred = (f + i)/2;
  43. spatial_score = score;
  44. }
  45. }
  46. return spatial_pred;
  47. }
  48. __inline__ __device__ int max3(int a, int b, int c)
  49. {
  50. int x = max(a, b);
  51. return max(x, c);
  52. }
  53. __inline__ __device__ int min3(int a, int b, int c)
  54. {
  55. int x = min(a, b);
  56. return min(x, c);
  57. }
  58. template<typename T>
  59. __inline__ __device__ T temporal_predictor(T A, T B, T C, T D, T E, T F,
  60. T G, T H, T I, T J, T K, T L,
  61. T spatial_pred, bool skip_check)
  62. {
  63. int p0 = (C + H) / 2;
  64. int p1 = F;
  65. int p2 = (D + I) / 2;
  66. int p3 = G;
  67. int p4 = (E + J) / 2;
  68. int tdiff0 = abs(D - I);
  69. int tdiff1 = (abs(A - F) + abs(B - G)) / 2;
  70. int tdiff2 = (abs(K - F) + abs(G - L)) / 2;
  71. int diff = max3(tdiff0, tdiff1, tdiff2);
  72. if (!skip_check) {
  73. int maxi = max3(p2 - p3, p2 - p1, min(p0 - p1, p4 - p3));
  74. int mini = min3(p2 - p3, p2 - p1, max(p0 - p1, p4 - p3));
  75. diff = max3(diff, mini, -maxi);
  76. }
  77. if (spatial_pred > p2 + diff) {
  78. spatial_pred = p2 + diff;
  79. }
  80. if (spatial_pred < p2 - diff) {
  81. spatial_pred = p2 - diff;
  82. }
  83. return spatial_pred;
  84. }
  85. template<typename T>
  86. __inline__ __device__ void yadif_single(T *dst,
  87. cudaTextureObject_t prev,
  88. cudaTextureObject_t cur,
  89. cudaTextureObject_t next,
  90. int dst_width, int dst_height, int dst_pitch,
  91. int src_width, int src_height,
  92. int parity, int tff, bool skip_spatial_check)
  93. {
  94. // Identify location
  95. int xo = blockIdx.x * blockDim.x + threadIdx.x;
  96. int yo = blockIdx.y * blockDim.y + threadIdx.y;
  97. if (xo >= dst_width || yo >= dst_height) {
  98. return;
  99. }
  100. // Don't modify the primary field
  101. if (yo % 2 == parity) {
  102. dst[yo*dst_pitch+xo] = tex2D<T>(cur, xo, yo);
  103. return;
  104. }
  105. // Calculate spatial prediction
  106. T a = tex2D<T>(cur, xo - 3, yo - 1);
  107. T b = tex2D<T>(cur, xo - 2, yo - 1);
  108. T c = tex2D<T>(cur, xo - 1, yo - 1);
  109. T d = tex2D<T>(cur, xo - 0, yo - 1);
  110. T e = tex2D<T>(cur, xo + 1, yo - 1);
  111. T f = tex2D<T>(cur, xo + 2, yo - 1);
  112. T g = tex2D<T>(cur, xo + 3, yo - 1);
  113. T h = tex2D<T>(cur, xo - 3, yo + 1);
  114. T i = tex2D<T>(cur, xo - 2, yo + 1);
  115. T j = tex2D<T>(cur, xo - 1, yo + 1);
  116. T k = tex2D<T>(cur, xo - 0, yo + 1);
  117. T l = tex2D<T>(cur, xo + 1, yo + 1);
  118. T m = tex2D<T>(cur, xo + 2, yo + 1);
  119. T n = tex2D<T>(cur, xo + 3, yo + 1);
  120. T spatial_pred =
  121. spatial_predictor(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
  122. // Calculate temporal prediction
  123. int is_second_field = !(parity ^ tff);
  124. cudaTextureObject_t prev2 = prev;
  125. cudaTextureObject_t prev1 = is_second_field ? cur : prev;
  126. cudaTextureObject_t next1 = is_second_field ? next : cur;
  127. cudaTextureObject_t next2 = next;
  128. T A = tex2D<T>(prev2, xo, yo - 1);
  129. T B = tex2D<T>(prev2, xo, yo + 1);
  130. T C = tex2D<T>(prev1, xo, yo - 2);
  131. T D = tex2D<T>(prev1, xo, yo + 0);
  132. T E = tex2D<T>(prev1, xo, yo + 2);
  133. T F = tex2D<T>(cur, xo, yo - 1);
  134. T G = tex2D<T>(cur, xo, yo + 1);
  135. T H = tex2D<T>(next1, xo, yo - 2);
  136. T I = tex2D<T>(next1, xo, yo + 0);
  137. T J = tex2D<T>(next1, xo, yo + 2);
  138. T K = tex2D<T>(next2, xo, yo - 1);
  139. T L = tex2D<T>(next2, xo, yo + 1);
  140. spatial_pred = temporal_predictor(A, B, C, D, E, F, G, H, I, J, K, L,
  141. spatial_pred, skip_spatial_check);
  142. dst[yo*dst_pitch+xo] = spatial_pred;
  143. }
  144. template <typename T>
  145. __inline__ __device__ void yadif_double(T *dst,
  146. cudaTextureObject_t prev,
  147. cudaTextureObject_t cur,
  148. cudaTextureObject_t next,
  149. int dst_width, int dst_height, int dst_pitch,
  150. int src_width, int src_height,
  151. int parity, int tff, bool skip_spatial_check)
  152. {
  153. int xo = blockIdx.x * blockDim.x + threadIdx.x;
  154. int yo = blockIdx.y * blockDim.y + threadIdx.y;
  155. if (xo >= dst_width || yo >= dst_height) {
  156. return;
  157. }
  158. if (yo % 2 == parity) {
  159. // Don't modify the primary field
  160. dst[yo*dst_pitch+xo] = tex2D<T>(cur, xo, yo);
  161. return;
  162. }
  163. T a = tex2D<T>(cur, xo - 3, yo - 1);
  164. T b = tex2D<T>(cur, xo - 2, yo - 1);
  165. T c = tex2D<T>(cur, xo - 1, yo - 1);
  166. T d = tex2D<T>(cur, xo - 0, yo - 1);
  167. T e = tex2D<T>(cur, xo + 1, yo - 1);
  168. T f = tex2D<T>(cur, xo + 2, yo - 1);
  169. T g = tex2D<T>(cur, xo + 3, yo - 1);
  170. T h = tex2D<T>(cur, xo - 3, yo + 1);
  171. T i = tex2D<T>(cur, xo - 2, yo + 1);
  172. T j = tex2D<T>(cur, xo - 1, yo + 1);
  173. T k = tex2D<T>(cur, xo - 0, yo + 1);
  174. T l = tex2D<T>(cur, xo + 1, yo + 1);
  175. T m = tex2D<T>(cur, xo + 2, yo + 1);
  176. T n = tex2D<T>(cur, xo + 3, yo + 1);
  177. T spatial_pred;
  178. spatial_pred.x =
  179. spatial_predictor(a.x, b.x, c.x, d.x, e.x, f.x, g.x, h.x, i.x, j.x, k.x, l.x, m.x, n.x);
  180. spatial_pred.y =
  181. spatial_predictor(a.y, b.y, c.y, d.y, e.y, f.y, g.y, h.y, i.y, j.y, k.y, l.y, m.y, n.y);
  182. // Calculate temporal prediction
  183. int is_second_field = !(parity ^ tff);
  184. cudaTextureObject_t prev2 = prev;
  185. cudaTextureObject_t prev1 = is_second_field ? cur : prev;
  186. cudaTextureObject_t next1 = is_second_field ? next : cur;
  187. cudaTextureObject_t next2 = next;
  188. T A = tex2D<T>(prev2, xo, yo - 1);
  189. T B = tex2D<T>(prev2, xo, yo + 1);
  190. T C = tex2D<T>(prev1, xo, yo - 2);
  191. T D = tex2D<T>(prev1, xo, yo + 0);
  192. T E = tex2D<T>(prev1, xo, yo + 2);
  193. T F = tex2D<T>(cur, xo, yo - 1);
  194. T G = tex2D<T>(cur, xo, yo + 1);
  195. T H = tex2D<T>(next1, xo, yo - 2);
  196. T I = tex2D<T>(next1, xo, yo + 0);
  197. T J = tex2D<T>(next1, xo, yo + 2);
  198. T K = tex2D<T>(next2, xo, yo - 1);
  199. T L = tex2D<T>(next2, xo, yo + 1);
  200. spatial_pred.x =
  201. temporal_predictor(A.x, B.x, C.x, D.x, E.x, F.x, G.x, H.x, I.x, J.x, K.x, L.x,
  202. spatial_pred.x, skip_spatial_check);
  203. spatial_pred.y =
  204. temporal_predictor(A.y, B.y, C.y, D.y, E.y, F.y, G.y, H.y, I.y, J.y, K.y, L.y,
  205. spatial_pred.y, skip_spatial_check);
  206. dst[yo*dst_pitch+xo] = spatial_pred;
  207. }
  208. extern "C" {
  209. __global__ void yadif_uchar(unsigned char *dst,
  210. cudaTextureObject_t prev,
  211. cudaTextureObject_t cur,
  212. cudaTextureObject_t next,
  213. int dst_width, int dst_height, int dst_pitch,
  214. int src_width, int src_height,
  215. int parity, int tff, bool skip_spatial_check)
  216. {
  217. yadif_single(dst, prev, cur, next,
  218. dst_width, dst_height, dst_pitch,
  219. src_width, src_height,
  220. parity, tff, skip_spatial_check);
  221. }
  222. __global__ void yadif_ushort(unsigned short *dst,
  223. cudaTextureObject_t prev,
  224. cudaTextureObject_t cur,
  225. cudaTextureObject_t next,
  226. int dst_width, int dst_height, int dst_pitch,
  227. int src_width, int src_height,
  228. int parity, int tff, bool skip_spatial_check)
  229. {
  230. yadif_single(dst, prev, cur, next,
  231. dst_width, dst_height, dst_pitch,
  232. src_width, src_height,
  233. parity, tff, skip_spatial_check);
  234. }
  235. __global__ void yadif_uchar2(uchar2 *dst,
  236. cudaTextureObject_t prev,
  237. cudaTextureObject_t cur,
  238. cudaTextureObject_t next,
  239. int dst_width, int dst_height, int dst_pitch,
  240. int src_width, int src_height,
  241. int parity, int tff, bool skip_spatial_check)
  242. {
  243. yadif_double(dst, prev, cur, next,
  244. dst_width, dst_height, dst_pitch,
  245. src_width, src_height,
  246. parity, tff, skip_spatial_check);
  247. }
  248. __global__ void yadif_ushort2(ushort2 *dst,
  249. cudaTextureObject_t prev,
  250. cudaTextureObject_t cur,
  251. cudaTextureObject_t next,
  252. int dst_width, int dst_height, int dst_pitch,
  253. int src_width, int src_height,
  254. int parity, int tff, bool skip_spatial_check)
  255. {
  256. yadif_double(dst, prev, cur, next,
  257. dst_width, dst_height, dst_pitch,
  258. src_width, src_height,
  259. parity, tff, skip_spatial_check);
  260. }
  261. } /* extern "C" */