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.

394 lines
21KB

  1. /*
  2. * Copyright (c) 2017 Ming Yang
  3. * Copyright (c) 2019 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 all
  13. * 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/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. typedef struct BilateralContext {
  31. const AVClass *class;
  32. float sigmaS;
  33. float sigmaR;
  34. int planes;
  35. int nb_planes;
  36. int depth;
  37. int planewidth[4];
  38. int planeheight[4];
  39. float alpha;
  40. float range_table[65536];
  41. float *img_out_f;
  42. float *img_temp;
  43. float *map_factor_a;
  44. float *map_factor_b;
  45. float *slice_factor_a;
  46. float *slice_factor_b;
  47. float *line_factor_a;
  48. float *line_factor_b;
  49. } BilateralContext;
  50. #define OFFSET(x) offsetof(BilateralContext, x)
  51. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  52. static const AVOption bilateral_options[] = {
  53. { "sigmaS", "set spatial sigma", OFFSET(sigmaS), AV_OPT_TYPE_FLOAT, {.dbl=0.1}, 0.0, 512, FLAGS },
  54. { "sigmaR", "set range sigma", OFFSET(sigmaR), AV_OPT_TYPE_FLOAT, {.dbl=0.1}, 0.0, 1, FLAGS },
  55. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1}, 0, 0xF, FLAGS },
  56. { NULL }
  57. };
  58. AVFILTER_DEFINE_CLASS(bilateral);
  59. static int query_formats(AVFilterContext *ctx)
  60. {
  61. static const enum AVPixelFormat pix_fmts[] = {
  62. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  63. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  64. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  65. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  66. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  67. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  68. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  69. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  70. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  71. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  72. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  73. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  74. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  75. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  76. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  77. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  78. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  79. AV_PIX_FMT_NONE
  80. };
  81. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  82. }
  83. static int config_params(AVFilterContext *ctx)
  84. {
  85. BilateralContext *s = ctx->priv;
  86. float inv_sigma_range;
  87. inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
  88. s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
  89. //compute a lookup table
  90. for (int i = 0; i < (1 << s->depth); i++)
  91. s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
  92. return 0;
  93. }
  94. static int config_input(AVFilterLink *inlink)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. BilateralContext *s = ctx->priv;
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  99. s->depth = desc->comp[0].depth;
  100. config_params(ctx);
  101. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  102. s->planewidth[0] = s->planewidth[3] = inlink->w;
  103. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  104. s->planeheight[0] = s->planeheight[3] = inlink->h;
  105. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  106. s->img_out_f = av_calloc(inlink->w * inlink->h, sizeof(float));
  107. s->img_temp = av_calloc(inlink->w * inlink->h, sizeof(float));
  108. s->map_factor_a = av_calloc(inlink->w * inlink->h, sizeof(float));
  109. s->map_factor_b = av_calloc(inlink->w * inlink->h, sizeof(float));
  110. s->slice_factor_a = av_calloc(inlink->w, sizeof(float));
  111. s->slice_factor_b = av_calloc(inlink->w, sizeof(float));
  112. s->line_factor_a = av_calloc(inlink->w, sizeof(float));
  113. s->line_factor_b = av_calloc(inlink->w, sizeof(float));
  114. if (!s->img_out_f ||
  115. !s->img_temp ||
  116. !s->map_factor_a ||
  117. !s->map_factor_b ||
  118. !s->slice_factor_a ||
  119. !s->slice_factor_a ||
  120. !s->line_factor_a ||
  121. !s->line_factor_a)
  122. return AVERROR(ENOMEM);
  123. return 0;
  124. }
  125. #define BILATERAL(type, name) \
  126. static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *ddst, \
  127. float sigma_spatial, float sigma_range, \
  128. int width, int height, int src_linesize, int dst_linesize) \
  129. { \
  130. type *dst = (type *)ddst; \
  131. const type *src = (const type *)ssrc; \
  132. float *img_out_f = s->img_out_f, *img_temp = s->img_temp; \
  133. float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \
  134. float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \
  135. float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \
  136. const float *range_table = s->range_table; \
  137. const float alpha = s->alpha; \
  138. float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \
  139. const float inv_alpha_ = 1.f - alpha; \
  140. float *ycf, *ypf, *xcf, *in_factor; \
  141. const type *tcy, *tpy; \
  142. int h1; \
  143. \
  144. for (int y = 0; y < height; y++) { \
  145. float *temp_factor_x, *temp_x = &img_temp[y * width]; \
  146. const type *in_x = &src[y * src_linesize]; \
  147. const type *texture_x = &src[y * src_linesize]; \
  148. type tpr; \
  149. \
  150. *temp_x++ = ypr = *in_x++; \
  151. tpr = *texture_x++; \
  152. \
  153. temp_factor_x = &map_factor_a[y * width]; \
  154. *temp_factor_x++ = fp = 1; \
  155. \
  156. for (int x = 1; x < width; x++) { \
  157. float alpha_; \
  158. int range_dist; \
  159. type tcr = *texture_x++; \
  160. type dr = abs(tcr - tpr); \
  161. \
  162. range_dist = dr; \
  163. alpha_ = range_table[range_dist]; \
  164. *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \
  165. tpr = tcr; \
  166. ypr = ycr; \
  167. *temp_factor_x++ = fc = inv_alpha_ + alpha_ * fp; \
  168. fp = fc; \
  169. } \
  170. --temp_x; *temp_x = 0.5f*((*temp_x) + (*--in_x)); \
  171. tpr = *--texture_x; \
  172. ypr = *in_x; \
  173. \
  174. --temp_factor_x; *temp_factor_x = 0.5f*((*temp_factor_x) + 1); \
  175. fp = 1; \
  176. \
  177. for (int x = width - 2; x >= 0; x--) { \
  178. type tcr = *--texture_x; \
  179. type dr = abs(tcr - tpr); \
  180. int range_dist = dr; \
  181. float alpha_ = range_table[range_dist]; \
  182. \
  183. ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr; \
  184. --temp_x; *temp_x = 0.5f*((*temp_x) + ycr); \
  185. tpr = tcr; \
  186. ypr = ycr; \
  187. \
  188. fc = inv_alpha_ + alpha_*fp; \
  189. --temp_factor_x; \
  190. *temp_factor_x = 0.5f*((*temp_factor_x) + fc); \
  191. fp = fc; \
  192. } \
  193. } \
  194. memcpy(img_out_f, img_temp, sizeof(float) * width); \
  195. \
  196. in_factor = map_factor_a; \
  197. memcpy(map_factor_b, in_factor, sizeof(float) * width); \
  198. for (int y = 1; y < height; y++) { \
  199. tpy = &src[(y - 1) * src_linesize]; \
  200. tcy = &src[y * src_linesize]; \
  201. xcy = &img_temp[y * width]; \
  202. ypy = &img_out_f[(y - 1) * width]; \
  203. ycy = &img_out_f[y * width]; \
  204. \
  205. xcf = &in_factor[y * width]; \
  206. ypf = &map_factor_b[(y - 1) * width]; \
  207. ycf = &map_factor_b[y * width]; \
  208. for (int x = 0; x < width; x++) { \
  209. type dr = abs((*tcy++) - (*tpy++)); \
  210. int range_dist = dr; \
  211. float alpha_ = range_table[range_dist]; \
  212. \
  213. *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \
  214. *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \
  215. } \
  216. } \
  217. h1 = height - 1; \
  218. ycf = line_factor_a; \
  219. ypf = line_factor_b; \
  220. memcpy(ypf, &in_factor[h1 * width], sizeof(float) * width); \
  221. for (int x = 0; x < width; x++) \
  222. map_factor_b[h1 * width + x] = 0.5f*(map_factor_b[h1 * width + x] + ypf[x]); \
  223. \
  224. ycy = slice_factor_a; \
  225. ypy = slice_factor_b; \
  226. memcpy(ypy, &img_temp[h1 * width], sizeof(float) * width); \
  227. for (int x = 0, k = 0; x < width; x++) { \
  228. int idx = h1 * width + x; \
  229. img_out_f[idx] = 0.5f*(img_out_f[idx] + ypy[k++]) / map_factor_b[h1 * width + x]; \
  230. } \
  231. \
  232. for (int y = h1 - 1; y >= 0; y--) { \
  233. float *ycf_, *ypf_, *factor_; \
  234. float *ycy_, *ypy_, *out_; \
  235. \
  236. tpy = &src[(y + 1) * src_linesize]; \
  237. tcy = &src[y * src_linesize]; \
  238. xcy = &img_temp[y * width]; \
  239. ycy_ = ycy; \
  240. ypy_ = ypy; \
  241. out_ = &img_out_f[y * width]; \
  242. \
  243. xcf = &in_factor[y * width]; \
  244. ycf_ = ycf; \
  245. ypf_ = ypf; \
  246. factor_ = &map_factor_b[y * width]; \
  247. for (int x = 0; x < width; x++) { \
  248. type dr = abs((*tcy++) - (*tpy++)); \
  249. int range_dist = dr; \
  250. float alpha_ = range_table[range_dist]; \
  251. float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \
  252. \
  253. *ycf_++ = fcc; \
  254. *factor_ = 0.5f * (*factor_ + fcc); \
  255. \
  256. ycc = inv_alpha_*(*xcy++) + alpha_*(*ypy_++); \
  257. *ycy_++ = ycc; \
  258. *out_ = 0.5f * (*out_ + ycc) / (*factor_); \
  259. out_++; \
  260. factor_++; \
  261. } \
  262. \
  263. ypy = ycy; \
  264. ypf = ycf; \
  265. } \
  266. \
  267. for (int i = 0; i < height; i++) \
  268. for (int j = 0; j < width; j++) \
  269. dst[j + i * dst_linesize] = img_out_f[i * width + j]; \
  270. }
  271. BILATERAL(uint8_t, byte)
  272. BILATERAL(uint16_t, word)
  273. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  274. {
  275. AVFilterContext *ctx = inlink->dst;
  276. BilateralContext *s = ctx->priv;
  277. AVFilterLink *outlink = ctx->outputs[0];
  278. AVFrame *out;
  279. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  280. if (!out) {
  281. av_frame_free(&in);
  282. return AVERROR(ENOMEM);
  283. }
  284. av_frame_copy_props(out, in);
  285. for (int plane = 0; plane < s->nb_planes; plane++) {
  286. if (!(s->planes & (1 << plane))) {
  287. av_image_copy_plane(out->data[plane], out->linesize[plane],
  288. in->data[plane], in->linesize[plane],
  289. s->planewidth[plane] * ((s->depth + 7) / 8), s->planeheight[plane]);
  290. continue;
  291. }
  292. if (s->depth <= 8)
  293. bilateral_byte(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
  294. s->planewidth[plane], s->planeheight[plane],
  295. in->linesize[plane], out->linesize[plane]);
  296. else
  297. bilateral_word(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
  298. s->planewidth[plane], s->planeheight[plane],
  299. in->linesize[plane] / 2, out->linesize[plane] / 2);
  300. }
  301. av_frame_free(&in);
  302. return ff_filter_frame(outlink, out);
  303. }
  304. static av_cold void uninit(AVFilterContext *ctx)
  305. {
  306. BilateralContext *s = ctx->priv;
  307. av_freep(&s->img_out_f);
  308. av_freep(&s->img_temp);
  309. av_freep(&s->map_factor_a);
  310. av_freep(&s->map_factor_b);
  311. av_freep(&s->slice_factor_a);
  312. av_freep(&s->slice_factor_b);
  313. av_freep(&s->line_factor_a);
  314. av_freep(&s->line_factor_b);
  315. }
  316. static int process_command(AVFilterContext *ctx,
  317. const char *cmd,
  318. const char *arg,
  319. char *res,
  320. int res_len,
  321. int flags)
  322. {
  323. int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
  324. if (ret < 0)
  325. return ret;
  326. return config_params(ctx);
  327. }
  328. static const AVFilterPad bilateral_inputs[] = {
  329. {
  330. .name = "default",
  331. .type = AVMEDIA_TYPE_VIDEO,
  332. .config_props = config_input,
  333. .filter_frame = filter_frame,
  334. },
  335. { NULL }
  336. };
  337. static const AVFilterPad bilateral_outputs[] = {
  338. {
  339. .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO,
  341. },
  342. { NULL }
  343. };
  344. AVFilter ff_vf_bilateral = {
  345. .name = "bilateral",
  346. .description = NULL_IF_CONFIG_SMALL("Apply Bilateral filter."),
  347. .priv_size = sizeof(BilateralContext),
  348. .priv_class = &bilateral_class,
  349. .uninit = uninit,
  350. .query_formats = query_formats,
  351. .inputs = bilateral_inputs,
  352. .outputs = bilateral_outputs,
  353. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  354. .process_command = process_command,
  355. };