vf_hqdn3d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
  3. * Copyright (c) 2010 Baptiste Coudurier
  4. * Copyright (c) 2012 Loren Merritt
  5. *
  6. * This file is part of Libav, ported from MPlayer.
  7. *
  8. * Libav is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with Libav; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /**
  23. * @file
  24. * high quality 3d video denoiser, ported from MPlayer
  25. * libmpcodecs/vf_hqdn3d.c.
  26. */
  27. #include <float.h>
  28. #include "config.h"
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/opt.h"
  34. #include "avfilter.h"
  35. #include "formats.h"
  36. #include "internal.h"
  37. #include "video.h"
  38. #include "vf_hqdn3d.h"
  39. #define LUT_BITS (depth==16 ? 8 : 4)
  40. #define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
  41. + (((1 << (16 - depth)) - 1) >> 1))
  42. #define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
  43. AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
  44. av_always_inline
  45. static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
  46. {
  47. int d = (prev - cur) >> (8 - LUT_BITS);
  48. return cur + coef[d];
  49. }
  50. av_always_inline
  51. static void denoise_temporal(uint8_t *src, uint8_t *dst,
  52. uint16_t *frame_ant,
  53. int w, int h, int sstride, int dstride,
  54. int16_t *temporal, int depth)
  55. {
  56. long x, y;
  57. uint32_t tmp;
  58. temporal += 256 << LUT_BITS;
  59. for (y = 0; y < h; y++) {
  60. for (x = 0; x < w; x++) {
  61. frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
  62. STORE(x, tmp);
  63. }
  64. src += sstride;
  65. dst += dstride;
  66. frame_ant += w;
  67. }
  68. }
  69. av_always_inline
  70. static void denoise_spatial(HQDN3DContext *s,
  71. uint8_t *src, uint8_t *dst,
  72. uint16_t *line_ant, uint16_t *frame_ant,
  73. int w, int h, int sstride, int dstride,
  74. int16_t *spatial, int16_t *temporal, int depth)
  75. {
  76. long x, y;
  77. uint32_t pixel_ant;
  78. uint32_t tmp;
  79. spatial += 256 << LUT_BITS;
  80. temporal += 256 << LUT_BITS;
  81. /* First line has no top neighbor. Only left one for each tmp and
  82. * last frame */
  83. pixel_ant = LOAD(0);
  84. for (x = 0; x < w; x++) {
  85. line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
  86. frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
  87. STORE(x, tmp);
  88. }
  89. for (y = 1; y < h; y++) {
  90. src += sstride;
  91. dst += dstride;
  92. frame_ant += w;
  93. if (s->denoise_row[depth]) {
  94. s->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
  95. continue;
  96. }
  97. pixel_ant = LOAD(0);
  98. for (x = 0; x < w-1; x++) {
  99. line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
  100. pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
  101. frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
  102. STORE(x, tmp);
  103. }
  104. line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
  105. frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
  106. STORE(x, tmp);
  107. }
  108. }
  109. av_always_inline
  110. static int denoise_depth(HQDN3DContext *s,
  111. uint8_t *src, uint8_t *dst,
  112. uint16_t *line_ant, uint16_t **frame_ant_ptr,
  113. int w, int h, int sstride, int dstride,
  114. int16_t *spatial, int16_t *temporal, int depth)
  115. {
  116. // FIXME: For 16-bit depth, frame_ant could be a pointer to the previous
  117. // filtered frame rather than a separate buffer.
  118. long x, y;
  119. uint16_t *frame_ant = *frame_ant_ptr;
  120. if (!frame_ant) {
  121. uint8_t *frame_src = src;
  122. *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
  123. if (!frame_ant)
  124. return AVERROR(ENOMEM);
  125. for (y = 0; y < h; y++, src += sstride, frame_ant += w)
  126. for (x = 0; x < w; x++)
  127. frame_ant[x] = LOAD(x);
  128. src = frame_src;
  129. frame_ant = *frame_ant_ptr;
  130. }
  131. if (spatial[0])
  132. denoise_spatial(s, src, dst, line_ant, frame_ant,
  133. w, h, sstride, dstride, spatial, temporal, depth);
  134. else
  135. denoise_temporal(src, dst, frame_ant,
  136. w, h, sstride, dstride, temporal, depth);
  137. emms_c();
  138. return 0;
  139. }
  140. #define denoise(...) \
  141. do { \
  142. int ret = AVERROR_INVALIDDATA; \
  143. switch (s->depth) { \
  144. case 8: ret = denoise_depth(__VA_ARGS__, 8); break; \
  145. case 9: ret = denoise_depth(__VA_ARGS__, 9); break; \
  146. case 10: ret = denoise_depth(__VA_ARGS__, 10); break; \
  147. case 16: ret = denoise_depth(__VA_ARGS__, 16); break; \
  148. } \
  149. if (ret < 0) { \
  150. av_frame_free(&out); \
  151. if (!direct) \
  152. av_frame_free(&in); \
  153. return ret; \
  154. } \
  155. } while (0)
  156. static int16_t *precalc_coefs(double dist25, int depth)
  157. {
  158. int i;
  159. double gamma, simil, C;
  160. int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
  161. if (!ct)
  162. return NULL;
  163. gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
  164. for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
  165. double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
  166. simil = 1.0 - FFABS(f) / 255.0;
  167. C = pow(simil, gamma) * 256.0 * f;
  168. ct[(256<<LUT_BITS)+i] = lrint(C);
  169. }
  170. ct[0] = !!dist25;
  171. return ct;
  172. }
  173. #define PARAM1_DEFAULT 4.0
  174. #define PARAM2_DEFAULT 3.0
  175. #define PARAM3_DEFAULT 6.0
  176. static av_cold int init(AVFilterContext *ctx)
  177. {
  178. HQDN3DContext *s = ctx->priv;
  179. if (!s->strength[LUMA_SPATIAL])
  180. s->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
  181. if (!s->strength[CHROMA_SPATIAL])
  182. s->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
  183. if (!s->strength[LUMA_TMP])
  184. s->strength[LUMA_TMP] = PARAM3_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
  185. if (!s->strength[CHROMA_TMP])
  186. s->strength[CHROMA_TMP] = s->strength[LUMA_TMP] * s->strength[CHROMA_SPATIAL] / s->strength[LUMA_SPATIAL];
  187. av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
  188. s->strength[LUMA_SPATIAL], s->strength[CHROMA_SPATIAL],
  189. s->strength[LUMA_TMP], s->strength[CHROMA_TMP]);
  190. return 0;
  191. }
  192. static av_cold void uninit(AVFilterContext *ctx)
  193. {
  194. HQDN3DContext *s = ctx->priv;
  195. av_freep(&s->coefs[0]);
  196. av_freep(&s->coefs[1]);
  197. av_freep(&s->coefs[2]);
  198. av_freep(&s->coefs[3]);
  199. av_freep(&s->line);
  200. av_freep(&s->frame_prev[0]);
  201. av_freep(&s->frame_prev[1]);
  202. av_freep(&s->frame_prev[2]);
  203. }
  204. static int query_formats(AVFilterContext *ctx)
  205. {
  206. static const enum AVPixelFormat pix_fmts[] = {
  207. AV_PIX_FMT_YUV420P,
  208. AV_PIX_FMT_YUV422P,
  209. AV_PIX_FMT_YUV444P,
  210. AV_PIX_FMT_YUV410P,
  211. AV_PIX_FMT_YUV411P,
  212. AV_PIX_FMT_YUV440P,
  213. AV_PIX_FMT_YUVJ420P,
  214. AV_PIX_FMT_YUVJ422P,
  215. AV_PIX_FMT_YUVJ444P,
  216. AV_PIX_FMT_YUVJ440P,
  217. AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
  218. AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
  219. AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
  220. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  221. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  222. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  223. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  224. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  225. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  226. AV_PIX_FMT_NONE
  227. };
  228. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  229. return 0;
  230. }
  231. static int config_input(AVFilterLink *inlink)
  232. {
  233. HQDN3DContext *s = inlink->dst->priv;
  234. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  235. int i;
  236. uninit(inlink->dst);
  237. s->hsub = desc->log2_chroma_w;
  238. s->vsub = desc->log2_chroma_h;
  239. s->depth = desc->comp[0].depth;
  240. s->line = av_malloc(inlink->w * sizeof(*s->line));
  241. if (!s->line)
  242. return AVERROR(ENOMEM);
  243. for (i = 0; i < 4; i++) {
  244. s->coefs[i] = precalc_coefs(s->strength[i], s->depth);
  245. if (!s->coefs[i])
  246. return AVERROR(ENOMEM);
  247. }
  248. if (ARCH_X86)
  249. ff_hqdn3d_init_x86(s);
  250. return 0;
  251. }
  252. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  253. {
  254. HQDN3DContext *s = inlink->dst->priv;
  255. AVFilterLink *outlink = inlink->dst->outputs[0];
  256. AVFrame *out;
  257. int c, direct = av_frame_is_writable(in);
  258. if (direct) {
  259. out = in;
  260. } else {
  261. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  262. if (!out) {
  263. av_frame_free(&in);
  264. return AVERROR(ENOMEM);
  265. }
  266. av_frame_copy_props(out, in);
  267. out->width = outlink->w;
  268. out->height = outlink->h;
  269. }
  270. for (c = 0; c < 3; c++) {
  271. denoise(s, in->data[c], out->data[c],
  272. s->line, &s->frame_prev[c],
  273. in->width >> (!!c * s->hsub),
  274. in->height >> (!!c * s->vsub),
  275. in->linesize[c], out->linesize[c],
  276. s->coefs[c?2:0], s->coefs[c?3:1]);
  277. }
  278. if (!direct)
  279. av_frame_free(&in);
  280. return ff_filter_frame(outlink, out);
  281. }
  282. #define OFFSET(x) offsetof(HQDN3DContext, x)
  283. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  284. static const AVOption options[] = {
  285. { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  286. { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  287. { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  288. { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  289. { NULL },
  290. };
  291. static const AVClass hqdn3d_class = {
  292. .class_name = "hqdn3d",
  293. .item_name = av_default_item_name,
  294. .option = options,
  295. .version = LIBAVUTIL_VERSION_INT,
  296. };
  297. static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
  298. {
  299. .name = "default",
  300. .type = AVMEDIA_TYPE_VIDEO,
  301. .config_props = config_input,
  302. .filter_frame = filter_frame,
  303. },
  304. { NULL }
  305. };
  306. static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_VIDEO
  310. },
  311. { NULL }
  312. };
  313. AVFilter ff_vf_hqdn3d = {
  314. .name = "hqdn3d",
  315. .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
  316. .priv_size = sizeof(HQDN3DContext),
  317. .priv_class = &hqdn3d_class,
  318. .init = init,
  319. .uninit = uninit,
  320. .query_formats = query_formats,
  321. .inputs = avfilter_vf_hqdn3d_inputs,
  322. .outputs = avfilter_vf_hqdn3d_outputs,
  323. };