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.

586 lines
19KB

  1. /*
  2. * Copyright (c) 2003 LeFunGus, lefungus@altern.org
  3. *
  4. * This file is part of FFmpeg
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <float.h>
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/attributes.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct VagueDenoiserContext {
  32. const AVClass *class;
  33. float threshold;
  34. float percent;
  35. int method;
  36. int nsteps;
  37. int planes;
  38. int depth;
  39. int bpc;
  40. int peak;
  41. int nb_planes;
  42. int planeheight[4];
  43. int planewidth[4];
  44. float *block;
  45. float *in;
  46. float *out;
  47. float *tmp;
  48. int hlowsize[4][32];
  49. int hhighsize[4][32];
  50. int vlowsize[4][32];
  51. int vhighsize[4][32];
  52. void (*thresholding)(float *block, const int width, const int height,
  53. const int stride, const float threshold,
  54. const float percent, const int nsteps);
  55. } VagueDenoiserContext;
  56. #define OFFSET(x) offsetof(VagueDenoiserContext, x)
  57. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  58. static const AVOption vaguedenoiser_options[] = {
  59. { "threshold", "set filtering strength", OFFSET(threshold), AV_OPT_TYPE_FLOAT, {.dbl=2.}, 0,DBL_MAX, FLAGS },
  60. { "method", "set filtering method", OFFSET(method), AV_OPT_TYPE_INT, {.i64=2 }, 0, 2, FLAGS, "method" },
  61. { "hard", "hard thresholding", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "method" },
  62. { "soft", "soft thresholding", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "method" },
  63. { "garrote", "garotte thresholding", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "method" },
  64. { "nsteps", "set number of steps", OFFSET(nsteps), AV_OPT_TYPE_INT, {.i64=6 }, 1, 32, FLAGS },
  65. { "percent", "set percent of full denoising", OFFSET(percent),AV_OPT_TYPE_FLOAT, {.dbl=85}, 0,100, FLAGS },
  66. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15 }, 0, 15, FLAGS },
  67. { NULL }
  68. };
  69. AVFILTER_DEFINE_CLASS(vaguedenoiser);
  70. #define NPAD 10
  71. static const float analysis_low[9] = {
  72. 0.037828455506995f, -0.023849465019380f, -0.110624404418423f, 0.377402855612654f,
  73. 0.852698679009403f, 0.377402855612654f, -0.110624404418423f, -0.023849465019380f, 0.037828455506995f
  74. };
  75. static const float analysis_high[7] = {
  76. -0.064538882628938f, 0.040689417609558f, 0.418092273222212f, -0.788485616405664f,
  77. 0.418092273222212f, 0.040689417609558f, -0.064538882628938f
  78. };
  79. static const float synthesis_low[7] = {
  80. -0.064538882628938f, -0.040689417609558f, 0.418092273222212f, 0.788485616405664f,
  81. 0.418092273222212f, -0.040689417609558f, -0.064538882628938f
  82. };
  83. static const float synthesis_high[9] = {
  84. -0.037828455506995f, -0.023849465019380f, 0.110624404418423f, 0.377402855612654f,
  85. -0.852698679009403f, 0.377402855612654f, 0.110624404418423f, -0.023849465019380f, -0.037828455506995f
  86. };
  87. static int query_formats(AVFilterContext *ctx)
  88. {
  89. static const enum AVPixelFormat pix_fmts[] = {
  90. AV_PIX_FMT_GRAY8,
  91. AV_PIX_FMT_GRAY16,
  92. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  93. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  94. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  95. AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  96. AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
  97. AV_PIX_FMT_YUVJ411P,
  98. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  99. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  100. AV_PIX_FMT_YUV440P10,
  101. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  102. AV_PIX_FMT_YUV440P12,
  103. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  104. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  105. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  106. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  107. AV_PIX_FMT_NONE
  108. };
  109. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  110. if (!fmts_list)
  111. return AVERROR(ENOMEM);
  112. return ff_set_common_formats(ctx, fmts_list);
  113. }
  114. static int config_input(AVFilterLink *inlink)
  115. {
  116. VagueDenoiserContext *s = inlink->dst->priv;
  117. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  118. int p, i, nsteps_width, nsteps_height, nsteps_max;
  119. s->depth = desc->comp[0].depth;
  120. s->bpc = (s->depth + 7) / 8;
  121. s->nb_planes = desc->nb_components;
  122. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  123. s->planeheight[0] = s->planeheight[3] = inlink->h;
  124. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  125. s->planewidth[0] = s->planewidth[3] = inlink->w;
  126. s->block = av_malloc_array(inlink->w * inlink->h, sizeof(*s->block));
  127. s->in = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->in));
  128. s->out = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->out));
  129. s->tmp = av_malloc_array(32 + FFMAX(inlink->w, inlink->h), sizeof(*s->tmp));
  130. if (!s->block || !s->in || !s->out || !s->tmp)
  131. return AVERROR(ENOMEM);
  132. s->threshold *= 1 << (s->depth - 8);
  133. s->peak = (1 << s->depth) - 1;
  134. nsteps_width = ((s->planes & 2 || s->planes & 4) && s->nb_planes > 1) ? s->planewidth[1] : s->planewidth[0];
  135. nsteps_height = ((s->planes & 2 || s->planes & 4) && s->nb_planes > 1) ? s->planeheight[1] : s->planeheight[0];
  136. for (nsteps_max = 1; nsteps_max < 15; nsteps_max++) {
  137. if (pow(2, nsteps_max) >= nsteps_width || pow(2, nsteps_max) >= nsteps_height)
  138. break;
  139. }
  140. s->nsteps = FFMIN(s->nsteps, nsteps_max - 2);
  141. for (p = 0; p < 4; p++) {
  142. s->hlowsize[p][0] = (s->planewidth[p] + 1) >> 1;
  143. s->hhighsize[p][0] = s->planewidth[p] >> 1;
  144. s->vlowsize[p][0] = (s->planeheight[p] + 1) >> 1;
  145. s->vhighsize[p][0] = s->planeheight[p] >> 1;
  146. for (i = 1; i < s->nsteps; i++) {
  147. s->hlowsize[p][i] = (s->hlowsize[p][i - 1] + 1) >> 1;
  148. s->hhighsize[p][i] = s->hlowsize[p][i - 1] >> 1;
  149. s->vlowsize[p][i] = (s->vlowsize[p][i - 1] + 1) >> 1;
  150. s->vhighsize[p][i] = s->vlowsize[p][i - 1] >> 1;
  151. }
  152. }
  153. return 0;
  154. }
  155. static inline void copy(const float *p1, float *p2, const int length)
  156. {
  157. memcpy(p2, p1, length * sizeof(float));
  158. }
  159. static inline void copyv(const float *p1, const int stride1, float *p2, const int length)
  160. {
  161. int i;
  162. for (i = 0; i < length; i++) {
  163. p2[i] = *p1;
  164. p1 += stride1;
  165. }
  166. }
  167. static inline void copyh(const float *p1, float *p2, const int stride2, const int length)
  168. {
  169. int i;
  170. for (i = 0; i < length; i++) {
  171. *p2 = p1[i];
  172. p2 += stride2;
  173. }
  174. }
  175. // Do symmetric extension of data using prescribed symmetries
  176. // Original values are in output[npad] through output[npad+size-1]
  177. // New values will be placed in output[0] through output[npad] and in output[npad+size] through output[2*npad+size-1] (note: end values may not be filled in)
  178. // extension at left bdry is ... 3 2 1 0 | 0 1 2 3 ...
  179. // same for right boundary
  180. // if right_ext=1 then ... 3 2 1 0 | 1 2 3
  181. static void symmetric_extension(float *output, const int size, const int left_ext, const int right_ext)
  182. {
  183. int first = NPAD;
  184. int last = NPAD - 1 + size;
  185. const int originalLast = last;
  186. int i, nextend, idx;
  187. if (left_ext == 2)
  188. output[--first] = output[NPAD];
  189. if (right_ext == 2)
  190. output[++last] = output[originalLast];
  191. // extend left end
  192. nextend = first;
  193. for (i = 0; i < nextend; i++)
  194. output[--first] = output[NPAD + 1 + i];
  195. idx = NPAD + NPAD - 1 + size;
  196. // extend right end
  197. nextend = idx - last;
  198. for (i = 0; i < nextend; i++)
  199. output[++last] = output[originalLast - 1 - i];
  200. }
  201. static void transform_step(float *input, float *output, const int size, const int low_size, VagueDenoiserContext *s)
  202. {
  203. int i;
  204. symmetric_extension(input, size, 1, 1);
  205. for (i = NPAD; i < NPAD + low_size; i++) {
  206. const float a = input[2 * i - 14] * analysis_low[0];
  207. const float b = input[2 * i - 13] * analysis_low[1];
  208. const float c = input[2 * i - 12] * analysis_low[2];
  209. const float d = input[2 * i - 11] * analysis_low[3];
  210. const float e = input[2 * i - 10] * analysis_low[4];
  211. const float f = input[2 * i - 9] * analysis_low[3];
  212. const float g = input[2 * i - 8] * analysis_low[2];
  213. const float h = input[2 * i - 7] * analysis_low[1];
  214. const float k = input[2 * i - 6] * analysis_low[0];
  215. output[i] = a + b + c + d + e + f + g + h + k;
  216. }
  217. for (i = NPAD; i < NPAD + low_size; i++) {
  218. const float a = input[2 * i - 12] * analysis_high[0];
  219. const float b = input[2 * i - 11] * analysis_high[1];
  220. const float c = input[2 * i - 10] * analysis_high[2];
  221. const float d = input[2 * i - 9] * analysis_high[3];
  222. const float e = input[2 * i - 8] * analysis_high[2];
  223. const float f = input[2 * i - 7] * analysis_high[1];
  224. const float g = input[2 * i - 6] * analysis_high[0];
  225. output[i + low_size] = a + b + c + d + e + f + g;
  226. }
  227. }
  228. static void invert_step(const float *input, float *output, float *temp, const int size, VagueDenoiserContext *s)
  229. {
  230. const int low_size = (size + 1) >> 1;
  231. const int high_size = size >> 1;
  232. int left_ext = 1, right_ext, i;
  233. int findex;
  234. memcpy(temp + NPAD, input + NPAD, low_size * sizeof(float));
  235. right_ext = (size % 2 == 0) ? 2 : 1;
  236. symmetric_extension(temp, low_size, left_ext, right_ext);
  237. memset(output, 0, (NPAD + NPAD + size) * sizeof(float));
  238. findex = (size + 2) >> 1;
  239. for (i = 9; i < findex + 11; i++) {
  240. const float a = temp[i] * synthesis_low[0];
  241. const float b = temp[i] * synthesis_low[1];
  242. const float c = temp[i] * synthesis_low[2];
  243. const float d = temp[i] * synthesis_low[3];
  244. output[2 * i - 13] += a;
  245. output[2 * i - 12] += b;
  246. output[2 * i - 11] += c;
  247. output[2 * i - 10] += d;
  248. output[2 * i - 9] += c;
  249. output[2 * i - 8] += b;
  250. output[2 * i - 7] += a;
  251. }
  252. memcpy(temp + NPAD, input + NPAD + low_size, high_size * sizeof(float));
  253. left_ext = 2;
  254. right_ext = (size % 2 == 0) ? 1 : 2;
  255. symmetric_extension(temp, high_size, left_ext, right_ext);
  256. for (i = 8; i < findex + 11; i++) {
  257. const float a = temp[i] * synthesis_high[0];
  258. const float b = temp[i] * synthesis_high[1];
  259. const float c = temp[i] * synthesis_high[2];
  260. const float d = temp[i] * synthesis_high[3];
  261. const float e = temp[i] * synthesis_high[4];
  262. output[2 * i - 13] += a;
  263. output[2 * i - 12] += b;
  264. output[2 * i - 11] += c;
  265. output[2 * i - 10] += d;
  266. output[2 * i - 9] += e;
  267. output[2 * i - 8] += d;
  268. output[2 * i - 7] += c;
  269. output[2 * i - 6] += b;
  270. output[2 * i - 5] += a;
  271. }
  272. }
  273. static void hard_thresholding(float *block, const int width, const int height,
  274. const int stride, const float threshold,
  275. const float percent, const int unused)
  276. {
  277. const float frac = 1.f - percent * 0.01f;
  278. int y, x;
  279. for (y = 0; y < height; y++) {
  280. for (x = 0; x < width; x++) {
  281. if (FFABS(block[x]) <= threshold)
  282. block[x] *= frac;
  283. }
  284. block += stride;
  285. }
  286. }
  287. static void soft_thresholding(float *block, const int width, const int height, const int stride,
  288. const float threshold, const float percent, const int nsteps)
  289. {
  290. const float frac = 1.f - percent * 0.01f;
  291. const float shift = threshold * 0.01f * percent;
  292. int w = width;
  293. int h = height;
  294. int y, x, l;
  295. for (l = 0; l < nsteps; l++) {
  296. w = (w + 1) >> 1;
  297. h = (h + 1) >> 1;
  298. }
  299. for (y = 0; y < height; y++) {
  300. const int x0 = (y < h) ? w : 0;
  301. for (x = x0; x < width; x++) {
  302. const float temp = FFABS(block[x]);
  303. if (temp <= threshold)
  304. block[x] *= frac;
  305. else
  306. block[x] = (block[x] < 0.f ? -1.f : (block[x] > 0.f ? 1.f : 0.f)) * (temp - shift);
  307. }
  308. block += stride;
  309. }
  310. }
  311. static void qian_thresholding(float *block, const int width, const int height,
  312. const int stride, const float threshold,
  313. const float percent, const int unused)
  314. {
  315. const float percent01 = percent * 0.01f;
  316. const float tr2 = threshold * threshold * percent01;
  317. const float frac = 1.f - percent01;
  318. int y, x;
  319. for (y = 0; y < height; y++) {
  320. for (x = 0; x < width; x++) {
  321. const float temp = FFABS(block[x]);
  322. if (temp <= threshold) {
  323. block[x] *= frac;
  324. } else {
  325. const float tp2 = temp * temp;
  326. block[x] *= (tp2 - tr2) / tp2;
  327. }
  328. }
  329. block += stride;
  330. }
  331. }
  332. static void filter(VagueDenoiserContext *s, AVFrame *in, AVFrame *out)
  333. {
  334. int p, y, x, i, j;
  335. for (p = 0; p < s->nb_planes; p++) {
  336. const int height = s->planeheight[p];
  337. const int width = s->planewidth[p];
  338. const uint8_t *srcp8 = in->data[p];
  339. const uint16_t *srcp16 = (const uint16_t *)in->data[p];
  340. uint8_t *dstp8 = out->data[p];
  341. uint16_t *dstp16 = (uint16_t *)out->data[p];
  342. float *output = s->block;
  343. int h_low_size0 = width;
  344. int v_low_size0 = height;
  345. int nsteps_transform = s->nsteps;
  346. int nsteps_invert = s->nsteps;
  347. const float *input = s->block;
  348. if (!((1 << p) & s->planes)) {
  349. av_image_copy_plane(out->data[p], out->linesize[p], in->data[p], in->linesize[p],
  350. s->planewidth[p] * s->bpc, s->planeheight[p]);
  351. continue;
  352. }
  353. if (s->depth <= 8) {
  354. for (y = 0; y < height; y++) {
  355. for (x = 0; x < width; x++)
  356. output[x] = srcp8[x];
  357. srcp8 += in->linesize[p];
  358. output += width;
  359. }
  360. } else {
  361. for (y = 0; y < height; y++) {
  362. for (x = 0; x < width; x++)
  363. output[x] = srcp16[x];
  364. srcp16 += in->linesize[p] / 2;
  365. output += width;
  366. }
  367. }
  368. while (nsteps_transform--) {
  369. int low_size = (h_low_size0 + 1) >> 1;
  370. float *input = s->block;
  371. for (j = 0; j < v_low_size0; j++) {
  372. copy(input, s->in + NPAD, h_low_size0);
  373. transform_step(s->in, s->out, h_low_size0, low_size, s);
  374. copy(s->out + NPAD, input, h_low_size0);
  375. input += width;
  376. }
  377. low_size = (v_low_size0 + 1) >> 1;
  378. input = s->block;
  379. for (j = 0; j < h_low_size0; j++) {
  380. copyv(input, width, s->in + NPAD, v_low_size0);
  381. transform_step(s->in, s->out, v_low_size0, low_size, s);
  382. copyh(s->out + NPAD, input, width, v_low_size0);
  383. input++;
  384. }
  385. h_low_size0 = (h_low_size0 + 1) >> 1;
  386. v_low_size0 = (v_low_size0 + 1) >> 1;
  387. }
  388. s->thresholding(s->block, width, height, width, s->threshold, s->percent, s->nsteps);
  389. while (nsteps_invert--) {
  390. const int idx = s->vlowsize[p][nsteps_invert] + s->vhighsize[p][nsteps_invert];
  391. const int idx2 = s->hlowsize[p][nsteps_invert] + s->hhighsize[p][nsteps_invert];
  392. float * idx3 = s->block;
  393. for (i = 0; i < idx2; i++) {
  394. copyv(idx3, width, s->in + NPAD, idx);
  395. invert_step(s->in, s->out, s->tmp, idx, s);
  396. copyh(s->out + NPAD, idx3, width, idx);
  397. idx3++;
  398. }
  399. idx3 = s->block;
  400. for (i = 0; i < idx; i++) {
  401. copy(idx3, s->in + NPAD, idx2);
  402. invert_step(s->in, s->out, s->tmp, idx2, s);
  403. copy(s->out + NPAD, idx3, idx2);
  404. idx3 += width;
  405. }
  406. }
  407. if (s->depth <= 8) {
  408. for (y = 0; y < height; y++) {
  409. for (x = 0; x < width; x++)
  410. dstp8[x] = av_clip_uint8(input[x] + 0.5f);
  411. input += width;
  412. dstp8 += out->linesize[p];
  413. }
  414. } else {
  415. for (y = 0; y < height; y++) {
  416. for (x = 0; x < width; x++)
  417. dstp16[x] = av_clip(input[x] + 0.5f, 0, s->peak);
  418. input += width;
  419. dstp16 += out->linesize[p] / 2;
  420. }
  421. }
  422. }
  423. }
  424. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  425. {
  426. AVFilterContext *ctx = inlink->dst;
  427. VagueDenoiserContext *s = ctx->priv;
  428. AVFilterLink *outlink = ctx->outputs[0];
  429. AVFrame *out;
  430. int direct = av_frame_is_writable(in);
  431. if (direct) {
  432. out = in;
  433. } else {
  434. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  435. if (!out) {
  436. av_frame_free(&in);
  437. return AVERROR(ENOMEM);
  438. }
  439. av_frame_copy_props(out, in);
  440. }
  441. filter(s, in, out);
  442. if (!direct)
  443. av_frame_free(&in);
  444. return ff_filter_frame(outlink, out);
  445. }
  446. static av_cold int init(AVFilterContext *ctx)
  447. {
  448. VagueDenoiserContext *s = ctx->priv;
  449. switch (s->method) {
  450. case 0:
  451. s->thresholding = hard_thresholding;
  452. break;
  453. case 1:
  454. s->thresholding = soft_thresholding;
  455. break;
  456. case 2:
  457. s->thresholding = qian_thresholding;
  458. break;
  459. }
  460. return 0;
  461. }
  462. static av_cold void uninit(AVFilterContext *ctx)
  463. {
  464. VagueDenoiserContext *s = ctx->priv;
  465. av_freep(&s->block);
  466. av_freep(&s->in);
  467. av_freep(&s->out);
  468. av_freep(&s->tmp);
  469. }
  470. static const AVFilterPad vaguedenoiser_inputs[] = {
  471. {
  472. .name = "default",
  473. .type = AVMEDIA_TYPE_VIDEO,
  474. .config_props = config_input,
  475. .filter_frame = filter_frame,
  476. },
  477. { NULL }
  478. };
  479. static const AVFilterPad vaguedenoiser_outputs[] = {
  480. {
  481. .name = "default",
  482. .type = AVMEDIA_TYPE_VIDEO
  483. },
  484. { NULL }
  485. };
  486. AVFilter ff_vf_vaguedenoiser = {
  487. .name = "vaguedenoiser",
  488. .description = NULL_IF_CONFIG_SMALL("Apply a Wavelet based Denoiser."),
  489. .priv_size = sizeof(VagueDenoiserContext),
  490. .priv_class = &vaguedenoiser_class,
  491. .init = init,
  492. .uninit = uninit,
  493. .query_formats = query_formats,
  494. .inputs = vaguedenoiser_inputs,
  495. .outputs = vaguedenoiser_outputs,
  496. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  497. };