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.

367 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
  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_input(AVFilterLink *inlink)
  84. {
  85. BilateralContext *s = inlink->dst->priv;
  86. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  87. float inv_sigma_range;
  88. s->depth = desc->comp[0].depth;
  89. inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
  90. s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
  91. //compute a lookup table
  92. for (int i = 0; i < (1 << s->depth); i++)
  93. s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
  94. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  95. s->planewidth[0] = s->planewidth[3] = inlink->w;
  96. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  97. s->planeheight[0] = s->planeheight[3] = inlink->h;
  98. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  99. s->img_out_f = av_calloc(inlink->w * inlink->h, sizeof(float));
  100. s->img_temp = av_calloc(inlink->w * inlink->h, sizeof(float));
  101. s->map_factor_a = av_calloc(inlink->w * inlink->h, sizeof(float));
  102. s->map_factor_b = av_calloc(inlink->w * inlink->h, sizeof(float));
  103. s->slice_factor_a = av_calloc(inlink->w, sizeof(float));
  104. s->slice_factor_b = av_calloc(inlink->w, sizeof(float));
  105. s->line_factor_a = av_calloc(inlink->w, sizeof(float));
  106. s->line_factor_b = av_calloc(inlink->w, sizeof(float));
  107. if (!s->img_out_f ||
  108. !s->img_temp ||
  109. !s->map_factor_a ||
  110. !s->map_factor_b ||
  111. !s->slice_factor_a ||
  112. !s->slice_factor_a ||
  113. !s->line_factor_a ||
  114. !s->line_factor_a)
  115. return AVERROR(ENOMEM);
  116. return 0;
  117. }
  118. #define BILATERAL(type, name) \
  119. static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *ddst, \
  120. float sigma_spatial, float sigma_range, \
  121. int width, int height, int src_linesize, int dst_linesize) \
  122. { \
  123. type *dst = (type *)ddst; \
  124. const type *src = (const type *)ssrc; \
  125. float *img_out_f = s->img_out_f, *img_temp = s->img_temp; \
  126. float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \
  127. float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \
  128. float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \
  129. const float *range_table = s->range_table; \
  130. const float alpha = s->alpha; \
  131. float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \
  132. const float inv_alpha_ = 1.f - alpha; \
  133. float *ycf, *ypf, *xcf, *in_factor; \
  134. const type *tcy, *tpy; \
  135. int h1; \
  136. \
  137. for (int y = 0; y < height; y++) { \
  138. float *temp_factor_x, *temp_x = &img_temp[y * width]; \
  139. const type *in_x = &src[y * src_linesize]; \
  140. const type *texture_x = &src[y * src_linesize]; \
  141. type tpr; \
  142. \
  143. *temp_x++ = ypr = *in_x++; \
  144. tpr = *texture_x++; \
  145. \
  146. temp_factor_x = &map_factor_a[y * width]; \
  147. *temp_factor_x++ = fp = 1; \
  148. \
  149. for (int x = 1; x < width; x++) { \
  150. float alpha_; \
  151. int range_dist; \
  152. type tcr = *texture_x++; \
  153. type dr = abs(tcr - tpr); \
  154. \
  155. range_dist = dr; \
  156. alpha_ = range_table[range_dist]; \
  157. *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \
  158. tpr = tcr; \
  159. ypr = ycr; \
  160. *temp_factor_x++ = fc = inv_alpha_ + alpha_ * fp; \
  161. fp = fc; \
  162. } \
  163. --temp_x; *temp_x = 0.5f*((*temp_x) + (*--in_x)); \
  164. tpr = *--texture_x; \
  165. ypr = *in_x; \
  166. \
  167. --temp_factor_x; *temp_factor_x = 0.5f*((*temp_factor_x) + 1); \
  168. fp = 1; \
  169. \
  170. for (int x = width - 2; x >= 0; x--) { \
  171. type tcr = *--texture_x; \
  172. type dr = abs(tcr - tpr); \
  173. int range_dist = dr; \
  174. float alpha_ = range_table[range_dist]; \
  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. in_factor = map_factor_a; \
  190. memcpy(map_factor_b, in_factor, sizeof(float) * width); \
  191. for (int y = 1; y < height; y++) { \
  192. tpy = &src[(y - 1) * src_linesize]; \
  193. tcy = &src[y * src_linesize]; \
  194. xcy = &img_temp[y * width]; \
  195. ypy = &img_out_f[(y - 1) * width]; \
  196. ycy = &img_out_f[y * width]; \
  197. \
  198. xcf = &in_factor[y * width]; \
  199. ypf = &map_factor_b[(y - 1) * width]; \
  200. ycf = &map_factor_b[y * width]; \
  201. for (int x = 0; x < width; x++) { \
  202. type dr = abs((*tcy++) - (*tpy++)); \
  203. int range_dist = dr; \
  204. float alpha_ = range_table[range_dist]; \
  205. \
  206. *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \
  207. *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \
  208. } \
  209. } \
  210. h1 = height - 1; \
  211. ycf = line_factor_a; \
  212. ypf = line_factor_b; \
  213. memcpy(ypf, &in_factor[h1 * width], sizeof(float) * width); \
  214. for (int x = 0; x < width; x++) \
  215. map_factor_b[h1 * width + x] = 0.5f*(map_factor_b[h1 * width + x] + ypf[x]); \
  216. \
  217. ycy = slice_factor_a; \
  218. ypy = slice_factor_b; \
  219. memcpy(ypy, &img_temp[h1 * width], sizeof(float) * width); \
  220. for (int x = 0, k = 0; x < width; x++) { \
  221. int idx = h1 * width + x; \
  222. img_out_f[idx] = 0.5f*(img_out_f[idx] + ypy[k++]) / map_factor_b[h1 * width + x]; \
  223. } \
  224. \
  225. for (int y = h1 - 1; y >= 0; y--) { \
  226. float *ycf_, *ypf_, *factor_; \
  227. float *ycy_, *ypy_, *out_; \
  228. \
  229. tpy = &src[(y + 1) * src_linesize]; \
  230. tcy = &src[y * src_linesize]; \
  231. xcy = &img_temp[y * width]; \
  232. ycy_ = ycy; \
  233. ypy_ = ypy; \
  234. out_ = &img_out_f[y * width]; \
  235. \
  236. xcf = &in_factor[y * width]; \
  237. ycf_ = ycf; \
  238. ypf_ = ypf; \
  239. factor_ = &map_factor_b[y * width]; \
  240. for (int x = 0; x < width; x++) { \
  241. type dr = abs((*tcy++) - (*tpy++)); \
  242. int range_dist = dr; \
  243. float alpha_ = range_table[range_dist]; \
  244. float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \
  245. \
  246. *ycf_++ = fcc; \
  247. *factor_ = 0.5f * (*factor_ + fcc); \
  248. \
  249. ycc = inv_alpha_*(*xcy++) + alpha_*(*ypy_++); \
  250. *ycy_++ = ycc; \
  251. *out_ = 0.5f * (*out_ + ycc) / (*factor_); \
  252. out_++; \
  253. factor_++; \
  254. } \
  255. \
  256. ypy = ycy; \
  257. ypf = ycf; \
  258. } \
  259. \
  260. for (int i = 0; i < height; i++) \
  261. for (int j = 0; j < width; j++) \
  262. dst[j + i * dst_linesize] = img_out_f[i * width + j]; \
  263. }
  264. BILATERAL(uint8_t, byte)
  265. BILATERAL(uint16_t, word)
  266. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  267. {
  268. AVFilterContext *ctx = inlink->dst;
  269. BilateralContext *s = ctx->priv;
  270. AVFilterLink *outlink = ctx->outputs[0];
  271. AVFrame *out;
  272. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  273. if (!out) {
  274. av_frame_free(&in);
  275. return AVERROR(ENOMEM);
  276. }
  277. av_frame_copy_props(out, in);
  278. for (int plane = 0; plane < s->nb_planes; plane++) {
  279. if (!(s->planes & (1 << plane))) {
  280. av_image_copy_plane(out->data[plane], out->linesize[plane],
  281. in->data[plane], in->linesize[plane],
  282. s->planewidth[plane] * ((s->depth + 7) / 8), s->planeheight[plane]);
  283. continue;
  284. }
  285. if (s->depth <= 8)
  286. bilateral_byte(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
  287. s->planewidth[plane], s->planeheight[plane],
  288. in->linesize[plane], out->linesize[plane]);
  289. else
  290. bilateral_word(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
  291. s->planewidth[plane], s->planeheight[plane],
  292. in->linesize[plane] / 2, out->linesize[plane] / 2);
  293. }
  294. av_frame_free(&in);
  295. return ff_filter_frame(outlink, out);
  296. }
  297. static av_cold void uninit(AVFilterContext *ctx)
  298. {
  299. BilateralContext *s = ctx->priv;
  300. av_freep(&s->img_out_f);
  301. av_freep(&s->img_temp);
  302. av_freep(&s->map_factor_a);
  303. av_freep(&s->map_factor_b);
  304. av_freep(&s->slice_factor_a);
  305. av_freep(&s->slice_factor_b);
  306. av_freep(&s->line_factor_a);
  307. av_freep(&s->line_factor_b);
  308. }
  309. static const AVFilterPad bilateral_inputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .config_props = config_input,
  314. .filter_frame = filter_frame,
  315. },
  316. { NULL }
  317. };
  318. static const AVFilterPad bilateral_outputs[] = {
  319. {
  320. .name = "default",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. },
  323. { NULL }
  324. };
  325. AVFilter ff_vf_bilateral = {
  326. .name = "bilateral",
  327. .description = NULL_IF_CONFIG_SMALL("Apply Bilateral filter."),
  328. .priv_size = sizeof(BilateralContext),
  329. .priv_class = &bilateral_class,
  330. .uninit = uninit,
  331. .query_formats = query_formats,
  332. .inputs = bilateral_inputs,
  333. .outputs = bilateral_outputs,
  334. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  335. };