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.

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