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.

472 lines
19KB

  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. int coupling;
  31. float threshold[4];
  32. int range;
  33. int blur;
  34. float direction;
  35. int nb_components;
  36. int planewidth[4];
  37. int planeheight[4];
  38. int shift[2];
  39. int thr[4];
  40. int *x_pos;
  41. int *y_pos;
  42. int (*deband)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  43. } DebandContext;
  44. #define OFFSET(x) offsetof(DebandContext, x)
  45. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  46. static const AVOption deband_options[] = {
  47. { "1thr", "set 1st plane threshold", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  48. { "2thr", "set 2nd plane threshold", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  49. { "3thr", "set 3rd plane threshold", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  50. { "4thr", "set 4th plane threshold", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.02}, 0.00003, 0.5, FLAGS },
  51. { "range", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
  52. { "r", "set range", OFFSET(range), AV_OPT_TYPE_INT, {.i64=16}, INT_MIN, INT_MAX, FLAGS },
  53. { "direction", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
  54. { "d", "set direction", OFFSET(direction), AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI, 2*M_PI, FLAGS },
  55. { "blur", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  56. { "b", "set blur", OFFSET(blur), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  57. { "coupling", "set plane coupling", OFFSET(coupling), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  58. { "c", "set plane coupling", OFFSET(coupling), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(deband);
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. DebandContext *s = ctx->priv;
  65. static const enum AVPixelFormat pix_fmts[] = {
  66. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10,
  67. AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  68. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  69. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  70. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  71. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ440P,
  72. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
  73. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  74. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  75. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  76. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
  77. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  78. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  79. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  80. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  81. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  82. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  83. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  84. AV_PIX_FMT_NONE
  85. };
  86. static const enum AVPixelFormat cpix_fmts[] = {
  87. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  88. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P9,
  89. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10,
  90. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
  91. AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUVA444P16,
  92. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  93. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  94. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
  95. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  96. AV_PIX_FMT_NONE
  97. };
  98. AVFilterFormats *fmts_list = ff_make_format_list(s->coupling ? cpix_fmts : pix_fmts);
  99. if (!fmts_list)
  100. return AVERROR(ENOMEM);
  101. return ff_set_common_formats(ctx, fmts_list);
  102. }
  103. static float frand(int x, int y)
  104. {
  105. const float r = sinf(x * 12.9898 + y * 78.233) * 43758.545;
  106. return r - floorf(r);
  107. }
  108. static int inline get_avg(int ref0, int ref1, int ref2, int ref3)
  109. {
  110. return (ref0 + ref1 + ref2 + ref3) / 4;
  111. }
  112. typedef struct ThreadData {
  113. AVFrame *in, *out;
  114. } ThreadData;
  115. static int deband_8_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  116. {
  117. DebandContext *s = ctx->priv;
  118. ThreadData *td = arg;
  119. AVFrame *in = td->in;
  120. AVFrame *out = td->out;
  121. int x, y, p;
  122. for (p = 0; p < s->nb_components; p++) {
  123. const uint8_t *src_ptr = (const uint8_t *)in->data[p];
  124. uint8_t *dst_ptr = (uint8_t *)out->data[p];
  125. const int dst_linesize = out->linesize[p];
  126. const int src_linesize = in->linesize[p];
  127. const int thr = s->thr[p];
  128. const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
  129. const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
  130. const int w = s->planewidth[p] - 1;
  131. const int h = s->planeheight[p] - 1;
  132. for (y = start; y < end; y++) {
  133. const int pos = y * s->planewidth[0];
  134. for (x = 0; x < s->planewidth[p]; x++) {
  135. const int x_pos = s->x_pos[pos + x];
  136. const int y_pos = s->y_pos[pos + x];
  137. const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  138. const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  139. const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  140. const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  141. const int src0 = src_ptr[y * src_linesize + x];
  142. if (s->blur) {
  143. const int avg = get_avg(ref0, ref1, ref2, ref3);
  144. const int diff = FFABS(src0 - avg);
  145. dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
  146. } else {
  147. dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
  148. (FFABS(src0 - ref1) < thr) &&
  149. (FFABS(src0 - ref2) < thr) &&
  150. (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
  151. }
  152. }
  153. }
  154. }
  155. return 0;
  156. }
  157. static int deband_8_coupling_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  158. {
  159. DebandContext *s = ctx->priv;
  160. ThreadData *td = arg;
  161. AVFrame *in = td->in;
  162. AVFrame *out = td->out;
  163. const int start = (s->planeheight[0] * jobnr ) / nb_jobs;
  164. const int end = (s->planeheight[0] * (jobnr+1)) / nb_jobs;
  165. int x, y, p;
  166. for (y = start; y < end; y++) {
  167. const int pos = y * s->planewidth[0];
  168. for (x = 0; x < s->planewidth[0]; x++) {
  169. const int x_pos = s->x_pos[pos + x];
  170. const int y_pos = s->y_pos[pos + x];
  171. int avg[4], cmp[4] = { 0 }, src[4];
  172. for (p = 0; p < s->nb_components; p++) {
  173. const uint8_t *src_ptr = (const uint8_t *)in->data[p];
  174. const int src_linesize = in->linesize[p];
  175. const int thr = s->thr[p];
  176. const int w = s->planewidth[p] - 1;
  177. const int h = s->planeheight[p] - 1;
  178. const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  179. const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  180. const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  181. const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  182. const int src0 = src_ptr[y * src_linesize + x];
  183. src[p] = src0;
  184. avg[p] = get_avg(ref0, ref1, ref2, ref3);
  185. if (s->blur) {
  186. cmp[p] = FFABS(src0 - avg[p]) < thr;
  187. } else {
  188. cmp[p] = (FFABS(src0 - ref0) < thr) &&
  189. (FFABS(src0 - ref1) < thr) &&
  190. (FFABS(src0 - ref2) < thr) &&
  191. (FFABS(src0 - ref3) < thr);
  192. }
  193. }
  194. for (p = 0; p < s->nb_components; p++)
  195. if (!cmp[p])
  196. break;
  197. if (p == s->nb_components) {
  198. for (p = 0; p < s->nb_components; p++) {
  199. const int dst_linesize = out->linesize[p];
  200. out->data[p][y * dst_linesize + x] = avg[p];
  201. }
  202. } else {
  203. for (p = 0; p < s->nb_components; p++) {
  204. const int dst_linesize = out->linesize[p];
  205. out->data[p][y * dst_linesize + x] = src[p];
  206. }
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. static int deband_16_coupling_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  213. {
  214. DebandContext *s = ctx->priv;
  215. ThreadData *td = arg;
  216. AVFrame *in = td->in;
  217. AVFrame *out = td->out;
  218. const int start = (s->planeheight[0] * jobnr ) / nb_jobs;
  219. const int end = (s->planeheight[0] * (jobnr+1)) / nb_jobs;
  220. int x, y, p, z;
  221. for (y = start; y < end; y++) {
  222. const int pos = y * s->planewidth[0];
  223. for (x = 0; x < s->planewidth[0]; x++) {
  224. const int x_pos = s->x_pos[pos + x];
  225. const int y_pos = s->y_pos[pos + x];
  226. int avg[4], cmp[4] = { 0 }, src[4];
  227. for (p = 0; p < s->nb_components; p++) {
  228. const uint16_t *src_ptr = (const uint16_t *)in->data[p];
  229. const int src_linesize = in->linesize[p] / 2;
  230. const int thr = s->thr[p];
  231. const int w = s->planewidth[p] - 1;
  232. const int h = s->planeheight[p] - 1;
  233. const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  234. const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  235. const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  236. const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  237. const int src0 = src_ptr[y * src_linesize + x];
  238. src[p] = src0;
  239. avg[p] = get_avg(ref0, ref1, ref2, ref3);
  240. if (s->blur) {
  241. cmp[p] = FFABS(src0 - avg[p]) < thr;
  242. } else {
  243. cmp[p] = (FFABS(src0 - ref0) < thr) &&
  244. (FFABS(src0 - ref1) < thr) &&
  245. (FFABS(src0 - ref2) < thr) &&
  246. (FFABS(src0 - ref3) < thr);
  247. }
  248. }
  249. for (z = 0; z < s->nb_components; z++)
  250. if (!cmp[z])
  251. break;
  252. if (z == s->nb_components) {
  253. for (p = 0; p < s->nb_components; p++) {
  254. const int dst_linesize = out->linesize[p] / 2;
  255. uint16_t *dst = (uint16_t *)out->data[p] + y * dst_linesize + x;
  256. dst[0] = avg[p];
  257. }
  258. } else {
  259. for (p = 0; p < s->nb_components; p++) {
  260. const int dst_linesize = out->linesize[p] / 2;
  261. uint16_t *dst = (uint16_t *)out->data[p] + y * dst_linesize + x;
  262. dst[0] = src[p];
  263. }
  264. }
  265. }
  266. }
  267. return 0;
  268. }
  269. static int deband_16_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  270. {
  271. DebandContext *s = ctx->priv;
  272. ThreadData *td = arg;
  273. AVFrame *in = td->in;
  274. AVFrame *out = td->out;
  275. int x, y, p;
  276. for (p = 0; p < s->nb_components; p++) {
  277. const uint16_t *src_ptr = (const uint16_t *)in->data[p];
  278. uint16_t *dst_ptr = (uint16_t *)out->data[p];
  279. const int dst_linesize = out->linesize[p] / 2;
  280. const int src_linesize = in->linesize[p] / 2;
  281. const int thr = s->thr[p];
  282. const int start = (s->planeheight[p] * jobnr ) / nb_jobs;
  283. const int end = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
  284. const int w = s->planewidth[p] - 1;
  285. const int h = s->planeheight[p] - 1;
  286. for (y = start; y < end; y++) {
  287. const int pos = y * s->planewidth[0];
  288. for (x = 0; x < s->planewidth[p]; x++) {
  289. const int x_pos = s->x_pos[pos + x];
  290. const int y_pos = s->y_pos[pos + x];
  291. const int ref0 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  292. const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + x_pos, 0, w)];
  293. const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  294. const int ref3 = src_ptr[av_clip(y + y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
  295. const int src0 = src_ptr[y * src_linesize + x];
  296. if (s->blur) {
  297. const int avg = get_avg(ref0, ref1, ref2, ref3);
  298. const int diff = FFABS(src0 - avg);
  299. dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
  300. } else {
  301. dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
  302. (FFABS(src0 - ref1) < thr) &&
  303. (FFABS(src0 - ref2) < thr) &&
  304. (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
  305. }
  306. }
  307. }
  308. }
  309. return 0;
  310. }
  311. static int config_input(AVFilterLink *inlink)
  312. {
  313. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  314. AVFilterContext *ctx = inlink->dst;
  315. DebandContext *s = ctx->priv;
  316. const float direction = s->direction;
  317. const int range = s->range;
  318. int x, y;
  319. s->nb_components = desc->nb_components;
  320. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  321. s->planeheight[0] = s->planeheight[3] = inlink->h;
  322. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  323. s->planewidth[0] = s->planewidth[3] = inlink->w;
  324. s->shift[0] = desc->log2_chroma_w;
  325. s->shift[1] = desc->log2_chroma_h;
  326. if (s->coupling)
  327. s->deband = desc->comp[0].depth > 8 ? deband_16_coupling_c : deband_8_coupling_c;
  328. else
  329. s->deband = desc->comp[0].depth > 8 ? deband_16_c : deband_8_c;
  330. s->thr[0] = ((1 << desc->comp[0].depth) - 1) * s->threshold[0];
  331. s->thr[1] = ((1 << desc->comp[1].depth) - 1) * s->threshold[1];
  332. s->thr[2] = ((1 << desc->comp[2].depth) - 1) * s->threshold[2];
  333. s->thr[3] = ((1 << desc->comp[3].depth) - 1) * s->threshold[3];
  334. s->x_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->x_pos));
  335. s->y_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->y_pos));
  336. if (!s->x_pos || !s->y_pos)
  337. return AVERROR(ENOMEM);
  338. for (y = 0; y < s->planeheight[0]; y++) {
  339. for (x = 0; x < s->planewidth[0]; x++) {
  340. const float r = frand(x, y);
  341. const float dir = direction < 0 ? -direction : r * direction;
  342. const int dist = range < 0 ? -range : r * range;
  343. s->x_pos[y * s->planewidth[0] + x] = cosf(dir) * dist;
  344. s->y_pos[y * s->planewidth[0] + x] = sinf(dir) * dist;
  345. }
  346. }
  347. return 0;
  348. }
  349. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  350. {
  351. AVFilterContext *ctx = inlink->dst;
  352. AVFilterLink *outlink = ctx->outputs[0];
  353. DebandContext *s = ctx->priv;
  354. AVFrame *out;
  355. ThreadData td;
  356. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  357. if (!out) {
  358. av_frame_free(&in);
  359. return AVERROR(ENOMEM);
  360. }
  361. av_frame_copy_props(out, in);
  362. td.in = in; td.out = out;
  363. ctx->internal->execute(ctx, s->deband, &td, NULL, FFMIN3(s->planeheight[1],
  364. s->planeheight[2],
  365. ff_filter_get_nb_threads(ctx)));
  366. av_frame_free(&in);
  367. return ff_filter_frame(outlink, out);
  368. }
  369. static av_cold void uninit(AVFilterContext *ctx)
  370. {
  371. DebandContext *s = ctx->priv;
  372. av_freep(&s->x_pos);
  373. av_freep(&s->y_pos);
  374. }
  375. static const AVFilterPad avfilter_vf_deband_inputs[] = {
  376. {
  377. .name = "default",
  378. .type = AVMEDIA_TYPE_VIDEO,
  379. .config_props = config_input,
  380. .filter_frame = filter_frame,
  381. },
  382. { NULL }
  383. };
  384. static const AVFilterPad avfilter_vf_deband_outputs[] = {
  385. {
  386. .name = "default",
  387. .type = AVMEDIA_TYPE_VIDEO,
  388. },
  389. { NULL }
  390. };
  391. AVFilter ff_vf_deband = {
  392. .name = "deband",
  393. .description = NULL_IF_CONFIG_SMALL("Debands video."),
  394. .priv_size = sizeof(DebandContext),
  395. .priv_class = &deband_class,
  396. .uninit = uninit,
  397. .query_formats = query_formats,
  398. .inputs = avfilter_vf_deband_inputs,
  399. .outputs = avfilter_vf_deband_outputs,
  400. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  401. };