You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
11KB

  1. /*
  2. * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
  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. /**
  21. * @file
  22. * unsharp input video
  23. */
  24. #include "unsharp_opencl.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/opencl_internal.h"
  27. #define PLANE_NUM 3
  28. static inline void add_mask_counter(uint32_t *dst, uint32_t *counter1, uint32_t *counter2, int len)
  29. {
  30. int i;
  31. for (i = 0; i < len; i++) {
  32. dst[i] = counter1[i] + counter2[i];
  33. }
  34. }
  35. static int compute_mask(int step, uint32_t *mask)
  36. {
  37. int i, z, ret = 0;
  38. int counter_size = sizeof(uint32_t) * (2 * step + 1);
  39. uint32_t *temp1_counter, *temp2_counter, **counter;
  40. temp1_counter = av_mallocz(counter_size);
  41. if (!temp1_counter) {
  42. ret = AVERROR(ENOMEM);
  43. goto end;
  44. }
  45. temp2_counter = av_mallocz(counter_size);
  46. if (!temp2_counter) {
  47. ret = AVERROR(ENOMEM);
  48. goto end;
  49. }
  50. counter = av_mallocz(sizeof(uint32_t *) * (2 * step + 1));
  51. if (!counter) {
  52. ret = AVERROR(ENOMEM);
  53. goto end;
  54. }
  55. for (i = 0; i < 2 * step + 1; i++) {
  56. counter[i] = av_mallocz(counter_size);
  57. if (!counter[i]) {
  58. ret = AVERROR(ENOMEM);
  59. goto end;
  60. }
  61. }
  62. for (i = 0; i < 2 * step + 1; i++) {
  63. memset(temp1_counter, 0, counter_size);
  64. temp1_counter[i] = 1;
  65. for (z = 0; z < step * 2; z += 2) {
  66. add_mask_counter(temp2_counter, counter[z], temp1_counter, step * 2);
  67. memcpy(counter[z], temp1_counter, counter_size);
  68. add_mask_counter(temp1_counter, counter[z + 1], temp2_counter, step * 2);
  69. memcpy(counter[z + 1], temp2_counter, counter_size);
  70. }
  71. }
  72. memcpy(mask, temp1_counter, counter_size);
  73. end:
  74. av_freep(&temp1_counter);
  75. av_freep(&temp2_counter);
  76. for (i = 0; i < 2 * step + 1; i++) {
  77. av_freep(&counter[i]);
  78. }
  79. av_freep(&counter);
  80. return ret;
  81. }
  82. static int compute_mask_matrix(cl_mem cl_mask_matrix, int step_x, int step_y)
  83. {
  84. int i, j, ret = 0;
  85. uint32_t *mask_matrix, *mask_x, *mask_y;
  86. size_t size_matrix = sizeof(uint32_t) * (2 * step_x + 1) * (2 * step_y + 1);
  87. mask_x = av_mallocz(sizeof(uint32_t) * (2 * step_x + 1));
  88. if (!mask_x) {
  89. ret = AVERROR(ENOMEM);
  90. goto end;
  91. }
  92. mask_y = av_mallocz(sizeof(uint32_t) * (2 * step_y + 1));
  93. if (!mask_y) {
  94. ret = AVERROR(ENOMEM);
  95. goto end;
  96. }
  97. mask_matrix = av_mallocz(size_matrix);
  98. if (!mask_matrix) {
  99. ret = AVERROR(ENOMEM);
  100. goto end;
  101. }
  102. ret = compute_mask(step_x, mask_x);
  103. if (ret < 0)
  104. goto end;
  105. ret = compute_mask(step_y, mask_y);
  106. if (ret < 0)
  107. goto end;
  108. for (j = 0; j < 2 * step_y + 1; j++) {
  109. for (i = 0; i < 2 * step_x + 1; i++) {
  110. mask_matrix[i + j * (2 * step_x + 1)] = mask_y[j] * mask_x[i];
  111. }
  112. }
  113. ret = av_opencl_buffer_write(cl_mask_matrix, (uint8_t *)mask_matrix, size_matrix);
  114. end:
  115. av_freep(&mask_x);
  116. av_freep(&mask_y);
  117. av_freep(&mask_matrix);
  118. return ret;
  119. }
  120. static int generate_mask(AVFilterContext *ctx)
  121. {
  122. UnsharpContext *unsharp = ctx->priv;
  123. int i, ret = 0, step_x[2], step_y[2];
  124. cl_mem mask_matrix[2];
  125. mask_matrix[0] = unsharp->opencl_ctx.cl_luma_mask;
  126. mask_matrix[1] = unsharp->opencl_ctx.cl_chroma_mask;
  127. step_x[0] = unsharp->luma.steps_x;
  128. step_x[1] = unsharp->chroma.steps_x;
  129. step_y[0] = unsharp->luma.steps_y;
  130. step_y[1] = unsharp->chroma.steps_y;
  131. if (!mask_matrix[0] || !mask_matrix[1]) {
  132. av_log(ctx, AV_LOG_ERROR, "Luma mask and chroma mask should not be NULL\n");
  133. return AVERROR(EINVAL);
  134. }
  135. for (i = 0; i < 2; i++) {
  136. ret = compute_mask_matrix(mask_matrix[i], step_x[i], step_y[i]);
  137. if (ret < 0)
  138. return ret;
  139. }
  140. return ret;
  141. }
  142. int ff_opencl_apply_unsharp(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
  143. {
  144. int ret;
  145. AVFilterLink *link = ctx->inputs[0];
  146. UnsharpContext *unsharp = ctx->priv;
  147. cl_int status;
  148. int cw = FF_CEIL_RSHIFT(link->w, unsharp->hsub);
  149. int ch = FF_CEIL_RSHIFT(link->h, unsharp->vsub);
  150. const size_t global_work_size = link->w * link->h + 2 * ch * cw;
  151. FFOpenclParam opencl_param = {0};
  152. opencl_param.ctx = ctx;
  153. opencl_param.kernel = unsharp->opencl_ctx.kernel_env.kernel;
  154. ret = ff_opencl_set_parameter(&opencl_param,
  155. FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_inbuf),
  156. FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_outbuf),
  157. FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_luma_mask),
  158. FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_chroma_mask),
  159. FF_OPENCL_PARAM_INFO(unsharp->luma.amount),
  160. FF_OPENCL_PARAM_INFO(unsharp->chroma.amount),
  161. FF_OPENCL_PARAM_INFO(unsharp->luma.steps_x),
  162. FF_OPENCL_PARAM_INFO(unsharp->luma.steps_y),
  163. FF_OPENCL_PARAM_INFO(unsharp->chroma.steps_x),
  164. FF_OPENCL_PARAM_INFO(unsharp->chroma.steps_y),
  165. FF_OPENCL_PARAM_INFO(unsharp->luma.scalebits),
  166. FF_OPENCL_PARAM_INFO(unsharp->chroma.scalebits),
  167. FF_OPENCL_PARAM_INFO(unsharp->luma.halfscale),
  168. FF_OPENCL_PARAM_INFO(unsharp->chroma.halfscale),
  169. FF_OPENCL_PARAM_INFO(in->linesize[0]),
  170. FF_OPENCL_PARAM_INFO(in->linesize[1]),
  171. FF_OPENCL_PARAM_INFO(out->linesize[0]),
  172. FF_OPENCL_PARAM_INFO(out->linesize[1]),
  173. FF_OPENCL_PARAM_INFO(link->h),
  174. FF_OPENCL_PARAM_INFO(link->w),
  175. FF_OPENCL_PARAM_INFO(ch),
  176. FF_OPENCL_PARAM_INFO(cw),
  177. NULL);
  178. if (ret < 0)
  179. return ret;
  180. status = clEnqueueNDRangeKernel(unsharp->opencl_ctx.kernel_env.command_queue,
  181. unsharp->opencl_ctx.kernel_env.kernel, 1, NULL,
  182. &global_work_size, NULL, 0, NULL, NULL);
  183. if (status != CL_SUCCESS) {
  184. av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
  185. return AVERROR_EXTERNAL;
  186. }
  187. clFinish(unsharp->opencl_ctx.kernel_env.command_queue);
  188. return av_opencl_buffer_read_image(out->data, unsharp->opencl_ctx.out_plane_size,
  189. unsharp->opencl_ctx.plane_num, unsharp->opencl_ctx.cl_outbuf,
  190. unsharp->opencl_ctx.cl_outbuf_size);
  191. }
  192. int ff_opencl_unsharp_init(AVFilterContext *ctx)
  193. {
  194. int ret = 0;
  195. UnsharpContext *unsharp = ctx->priv;
  196. ret = av_opencl_init(NULL);
  197. if (ret < 0)
  198. return ret;
  199. ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_luma_mask,
  200. sizeof(uint32_t) * (2 * unsharp->luma.steps_x + 1) * (2 * unsharp->luma.steps_y + 1),
  201. CL_MEM_READ_ONLY, NULL);
  202. if (ret < 0)
  203. return ret;
  204. ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_chroma_mask,
  205. sizeof(uint32_t) * (2 * unsharp->chroma.steps_x + 1) * (2 * unsharp->chroma.steps_y + 1),
  206. CL_MEM_READ_ONLY, NULL);
  207. if (ret < 0)
  208. return ret;
  209. ret = generate_mask(ctx);
  210. if (ret < 0)
  211. return ret;
  212. unsharp->opencl_ctx.plane_num = PLANE_NUM;
  213. if (!unsharp->opencl_ctx.kernel_env.kernel) {
  214. ret = av_opencl_create_kernel(&unsharp->opencl_ctx.kernel_env, "unsharp");
  215. if (ret < 0) {
  216. av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel with name 'unsharp'\n");
  217. return ret;
  218. }
  219. }
  220. return ret;
  221. }
  222. void ff_opencl_unsharp_uninit(AVFilterContext *ctx)
  223. {
  224. UnsharpContext *unsharp = ctx->priv;
  225. av_opencl_buffer_release(&unsharp->opencl_ctx.cl_inbuf);
  226. av_opencl_buffer_release(&unsharp->opencl_ctx.cl_outbuf);
  227. av_opencl_buffer_release(&unsharp->opencl_ctx.cl_luma_mask);
  228. av_opencl_buffer_release(&unsharp->opencl_ctx.cl_chroma_mask);
  229. av_opencl_release_kernel(&unsharp->opencl_ctx.kernel_env);
  230. av_opencl_uninit();
  231. }
  232. int ff_opencl_unsharp_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
  233. {
  234. int ret = 0;
  235. AVFilterLink *link = ctx->inputs[0];
  236. UnsharpContext *unsharp = ctx->priv;
  237. int ch = FF_CEIL_RSHIFT(link->h, unsharp->vsub);
  238. if ((!unsharp->opencl_ctx.cl_inbuf) || (!unsharp->opencl_ctx.cl_outbuf)) {
  239. unsharp->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
  240. unsharp->opencl_ctx.in_plane_size[1] = (in->linesize[1] * ch);
  241. unsharp->opencl_ctx.in_plane_size[2] = (in->linesize[2] * ch);
  242. unsharp->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
  243. unsharp->opencl_ctx.out_plane_size[1] = (out->linesize[1] * ch);
  244. unsharp->opencl_ctx.out_plane_size[2] = (out->linesize[2] * ch);
  245. unsharp->opencl_ctx.cl_inbuf_size = unsharp->opencl_ctx.in_plane_size[0] +
  246. unsharp->opencl_ctx.in_plane_size[1] +
  247. unsharp->opencl_ctx.in_plane_size[2];
  248. unsharp->opencl_ctx.cl_outbuf_size = unsharp->opencl_ctx.out_plane_size[0] +
  249. unsharp->opencl_ctx.out_plane_size[1] +
  250. unsharp->opencl_ctx.out_plane_size[2];
  251. if (!unsharp->opencl_ctx.cl_inbuf) {
  252. ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_inbuf,
  253. unsharp->opencl_ctx.cl_inbuf_size,
  254. CL_MEM_READ_ONLY, NULL);
  255. if (ret < 0)
  256. return ret;
  257. }
  258. if (!unsharp->opencl_ctx.cl_outbuf) {
  259. ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_outbuf,
  260. unsharp->opencl_ctx.cl_outbuf_size,
  261. CL_MEM_READ_WRITE, NULL);
  262. if (ret < 0)
  263. return ret;
  264. }
  265. }
  266. return av_opencl_buffer_write_image(unsharp->opencl_ctx.cl_inbuf,
  267. unsharp->opencl_ctx.cl_inbuf_size,
  268. 0, in->data, unsharp->opencl_ctx.in_plane_size,
  269. unsharp->opencl_ctx.plane_num);
  270. }