vf_deband.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2015 Niklas Haas
  3. * Copyright (c) 2015 Paul B Mahol
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct DebandContext {
  29. const AVClass *class;
  30. float threshold[4];
  31. int range;
  32. int blur;
  33. float direction;
  34. int nb_components;
  35. int planewidth[4];
  36. int planeheight[4];
  37. int thr[4];
  38. int *x_pos;
  39. int *y_pos;
  40. int (*deband)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  41. } DebandContext;
  42. #define OFFSET(x) offsetof(DebandContext, x)
  43. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  44. static const AVOption deband_options[] = {
  45. { "1thr", "set 1st plane threshold", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  46. { "2thr", "set 2nd plane threshold", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  47. { "3thr", "set 3rd plane threshold", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  48. { "4thr", "set 4th plane threshold", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  49. { "range", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
  50. { "r", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
  51. { "direction", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
  52. { "d", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
  53. { "blur", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  54. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(deband);
  57. static int query_formats(AVFilterContext *ctx)
  58. {
  59. static const enum AVPixelFormat pix_fmts[] = {
  60. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  61. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  62. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  63. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  64. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ440P,
  65. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  66. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  67. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  68. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  69. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  70. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  71. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  72. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  73. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  74. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  75. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  76. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  77. AV_PIX_FMT_NONE
  78. };
  79. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  80. if (!fmts_list)
  81. return AVERROR(ENOMEM);
  82. return ff_set_common_formats(ctx, fmts_list);
  83. }
  84. static float frand(int x, int y)
  85. {
  86. const float r = sinf(x * 12.9898 + y * 78.233) * 43758.545;
  87. return r - floorf(r);
  88. }
  89. static int inline get_avg(int ref0, int ref1, int ref2, int ref3)
  90. {
  91. return (ref0 + ref1 + ref2 + ref3) / 4;
  92. }
  93. typedef struct ThreadData {
  94. AVFrame *in, *out;
  95. } ThreadData;
  96. static int deband_8_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  97. {
  98. DebandContext *s = ctx->priv;
  99. ThreadData *td = arg;
  100. AVFrame *in = td->in;
  101. AVFrame *out = td->out;
  102. int x, y, p;
  103. for (p = 0; p < s->nb_components; p++) {
  104. const uint8_t *src_ptr = (const uint8_t *)in->data[p];
  105. uint8_t *dst_ptr = (uint8_t *)out->data[p];
  106. const int dst_linesize = out->linesize[p];
  107. const int src_linesize = in->linesize[p];
  108. const int thr = s->thr[p];
  109. const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
  110. const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
  111. const int w = s->planewidth[p] - 1;
  112. const int h = s->planeheight[p] - 1;
  113. for (y = start; y < end; y++) {
  114. const int pos = y * s->planeheight[0];
  115. for (x = 0; x < s->planewidth[p]; x++) {
  116. const int x_pos = s->x_pos[pos + x];
  117. const int y_pos = s->y_pos[pos + x];
  118. const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  119. const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  120. const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  121. const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  122. const int src0 = src_ptr[y * src_linesize + x];
  123. if (s->blur) {
  124. const int avg = get_avg(ref0, ref1, ref2, ref3);
  125. const int diff = FFABS(src0 - avg);
  126. dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
  127. } else {
  128. dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
  129. (FFABS(src0 - ref1) < thr) &&
  130. (FFABS(src0 - ref2) < thr) &&
  131. (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
  132. }
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. static int deband_16_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  139. {
  140. DebandContext *s = ctx->priv;
  141. ThreadData *td = arg;
  142. AVFrame *in = td->in;
  143. AVFrame *out = td->out;
  144. int x, y, p;
  145. for (p = 0; p < s->nb_components; p++) {
  146. const uint16_t *src_ptr = (const uint16_t *)in->data[p];
  147. uint16_t *dst_ptr = (uint16_t *)out->data[p];
  148. const int dst_linesize = out->linesize[p] / 2;
  149. const int src_linesize = in->linesize[p] / 2;
  150. const int thr = s->thr[p];
  151. const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
  152. const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
  153. const int w = s->planewidth[p] - 1;
  154. const int h = s->planeheight[p] - 1;
  155. for (y = start; y < end; y++) {
  156. const int pos = y * s->planeheight[0];
  157. for (x = 0; x < s->planewidth[p]; x++) {
  158. const int x_pos = s->x_pos[pos + x];
  159. const int y_pos = s->y_pos[pos + x];
  160. const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  161. const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  162. const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  163. const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  164. const int src0 = src_ptr[y * src_linesize + x];
  165. if (s->blur) {
  166. const int avg = get_avg(ref0, ref1, ref2, ref3);
  167. const int diff = FFABS(src0 - avg);
  168. dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
  169. } else {
  170. dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
  171. (FFABS(src0 - ref1) < thr) &&
  172. (FFABS(src0 - ref2) < thr) &&
  173. (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
  174. }
  175. }
  176. }
  177. }
  178. return 0;
  179. }
  180. static int config_input(AVFilterLink *inlink)
  181. {
  182. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  183. AVFilterContext *ctx = inlink->dst;
  184. DebandContext *s = ctx->priv;
  185. const float direction = s->direction;
  186. const int range = s->range;
  187. int x, y;
  188. s->nb_components = desc->nb_components;
  189. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  190. s->planeheight[0] = s->planeheight[3] = inlink->h;
  191. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  192. s->planewidth[0] = s->planewidth[3] = inlink->w;
  193. s->deband = desc->comp[0].depth > 8 ? deband_16_c : deband_8_c;
  194. s->thr[0] = ((1 << desc->comp[0].depth) - 1) * s->threshold[0];
  195. s->thr[1] = ((1 << desc->comp[1].depth) - 1) * s->threshold[1];
  196. s->thr[2] = ((1 << desc->comp[2].depth) - 1) * s->threshold[2];
  197. s->thr[3] = ((1 << desc->comp[3].depth) - 1) * s->threshold[3];
  198. s->x_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->x_pos));
  199. s->y_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->y_pos));
  200. if (!s->x_pos || !s->y_pos)
  201. return AVERROR(ENOMEM);
  202. for (y = 0; y < s->planeheight[0]; y++) {
  203. for (x = 0; x < s->planewidth[0]; x++) {
  204. const float r = frand(x, y);
  205. const float dir = direction < 0 ? -direction : r * direction;
  206. const int dist = range < 0 ? -range : r * range;
  207. s->x_pos[y * s->planeheight[0] + x] = cosf(dir) * dist;
  208. s->y_pos[y * s->planeheight[0] + x] = sinf(dir) * dist;
  209. }
  210. }
  211. return 0;
  212. }
  213. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  214. {
  215. AVFilterContext *ctx = inlink->dst;
  216. AVFilterLink *outlink = ctx->outputs[0];
  217. DebandContext *s = ctx->priv;
  218. AVFrame *out;
  219. ThreadData td;
  220. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  221. if (!out) {
  222. av_frame_free(&in);
  223. return AVERROR(ENOMEM);
  224. }
  225. av_frame_copy_props(out, in);
  226. td.in = in; td.out = out;
  227. ctx->internal->execute(ctx, s->deband, &td, NULL, FFMIN3(s->planeheight[1],
  228. s->planeheight[2],
  229. ctx->graph->nb_threads));
  230. av_frame_free(&in);
  231. return ff_filter_frame(outlink, out);
  232. }
  233. static av_cold void uninit(AVFilterContext *ctx)
  234. {
  235. DebandContext *s = ctx->priv;
  236. av_freep(&s->x_pos);
  237. av_freep(&s->y_pos);
  238. }
  239. static const AVFilterPad avfilter_vf_deband_inputs[] = {
  240. {
  241. .name = "default",
  242. .type = AVMEDIA_TYPE_VIDEO,
  243. .config_props = config_input,
  244. .filter_frame = filter_frame,
  245. },
  246. { NULL }
  247. };
  248. static const AVFilterPad avfilter_vf_deband_outputs[] = {
  249. {
  250. .name = "default",
  251. .type = AVMEDIA_TYPE_VIDEO,
  252. },
  253. { NULL }
  254. };
  255. AVFilter ff_vf_deband = {
  256. .name = "deband",
  257. .description = NULL_IF_CONFIG_SMALL("Debands video."),
  258. .priv_size = sizeof(DebandContext),
  259. .priv_class = &deband_class,
  260. .uninit = uninit,
  261. .query_formats = query_formats,
  262. .inputs = avfilter_vf_deband_inputs,
  263. .outputs = avfilter_vf_deband_outputs,
  264. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  265. };