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.

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