vf_hqdn3d.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 FFmpeg, ported from MPlayer.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 void 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 16bit 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_array(w, h*sizeof(uint16_t));
  123. for (y = 0; y < h; y++, src += sstride, frame_ant += w)
  124. for (x = 0; x < w; x++)
  125. frame_ant[x] = LOAD(x);
  126. src = frame_src;
  127. frame_ant = *frame_ant_ptr;
  128. }
  129. if (spatial[0])
  130. denoise_spatial(s, src, dst, line_ant, frame_ant,
  131. w, h, sstride, dstride, spatial, temporal, depth);
  132. else
  133. denoise_temporal(src, dst, frame_ant,
  134. w, h, sstride, dstride, temporal, depth);
  135. emms_c();
  136. }
  137. #define denoise(...) \
  138. switch (s->depth) {\
  139. case 8: denoise_depth(__VA_ARGS__, 8); break;\
  140. case 9: denoise_depth(__VA_ARGS__, 9); break;\
  141. case 10: denoise_depth(__VA_ARGS__, 10); break;\
  142. case 16: denoise_depth(__VA_ARGS__, 16); break;\
  143. }
  144. static int16_t *precalc_coefs(double dist25, int depth)
  145. {
  146. int i;
  147. double gamma, simil, C;
  148. int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
  149. if (!ct)
  150. return NULL;
  151. gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
  152. for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
  153. double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
  154. simil = 1.0 - FFABS(f) / 255.0;
  155. C = pow(simil, gamma) * 256.0 * f;
  156. ct[(256<<LUT_BITS)+i] = lrint(C);
  157. }
  158. ct[0] = !!dist25;
  159. return ct;
  160. }
  161. #define PARAM1_DEFAULT 4.0
  162. #define PARAM2_DEFAULT 3.0
  163. #define PARAM3_DEFAULT 6.0
  164. static av_cold int init(AVFilterContext *ctx)
  165. {
  166. HQDN3DContext *s = ctx->priv;
  167. if (!s->strength[LUMA_SPATIAL])
  168. s->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
  169. if (!s->strength[CHROMA_SPATIAL])
  170. s->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
  171. if (!s->strength[LUMA_TMP])
  172. s->strength[LUMA_TMP] = PARAM3_DEFAULT * s->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
  173. if (!s->strength[CHROMA_TMP])
  174. s->strength[CHROMA_TMP] = s->strength[LUMA_TMP] * s->strength[CHROMA_SPATIAL] / s->strength[LUMA_SPATIAL];
  175. av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
  176. s->strength[LUMA_SPATIAL], s->strength[CHROMA_SPATIAL],
  177. s->strength[LUMA_TMP], s->strength[CHROMA_TMP]);
  178. return 0;
  179. }
  180. static av_cold void uninit(AVFilterContext *ctx)
  181. {
  182. HQDN3DContext *s = ctx->priv;
  183. av_freep(&s->coefs[0]);
  184. av_freep(&s->coefs[1]);
  185. av_freep(&s->coefs[2]);
  186. av_freep(&s->coefs[3]);
  187. av_freep(&s->line);
  188. av_freep(&s->frame_prev[0]);
  189. av_freep(&s->frame_prev[1]);
  190. av_freep(&s->frame_prev[2]);
  191. }
  192. static int query_formats(AVFilterContext *ctx)
  193. {
  194. static const enum AVPixelFormat pix_fmts[] = {
  195. AV_PIX_FMT_YUV420P,
  196. AV_PIX_FMT_YUV422P,
  197. AV_PIX_FMT_YUV444P,
  198. AV_PIX_FMT_YUV410P,
  199. AV_PIX_FMT_YUV411P,
  200. AV_PIX_FMT_YUV440P,
  201. AV_PIX_FMT_YUVJ420P,
  202. AV_PIX_FMT_YUVJ422P,
  203. AV_PIX_FMT_YUVJ444P,
  204. AV_PIX_FMT_YUVJ440P,
  205. AV_PIX_FMT_YUV420P9,
  206. AV_PIX_FMT_YUV422P9,
  207. AV_PIX_FMT_YUV444P9,
  208. AV_PIX_FMT_YUV420P10,
  209. AV_PIX_FMT_YUV422P10,
  210. AV_PIX_FMT_YUV444P10,
  211. AV_PIX_FMT_YUV420P16,
  212. AV_PIX_FMT_YUV422P16,
  213. AV_PIX_FMT_YUV444P16,
  214. AV_PIX_FMT_NONE
  215. };
  216. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  217. return 0;
  218. }
  219. static int config_input(AVFilterLink *inlink)
  220. {
  221. HQDN3DContext *s = inlink->dst->priv;
  222. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  223. int i;
  224. uninit(inlink->dst);
  225. s->hsub = desc->log2_chroma_w;
  226. s->vsub = desc->log2_chroma_h;
  227. s->depth = desc->comp[0].depth_minus1+1;
  228. s->line = av_malloc_array(inlink->w, sizeof(*s->line));
  229. if (!s->line)
  230. return AVERROR(ENOMEM);
  231. for (i = 0; i < 4; i++) {
  232. s->coefs[i] = precalc_coefs(s->strength[i], s->depth);
  233. if (!s->coefs[i])
  234. return AVERROR(ENOMEM);
  235. }
  236. if (ARCH_X86)
  237. ff_hqdn3d_init_x86(s);
  238. return 0;
  239. }
  240. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  241. {
  242. AVFilterContext *ctx = inlink->dst;
  243. HQDN3DContext *s = ctx->priv;
  244. AVFilterLink *outlink = ctx->outputs[0];
  245. AVFrame *out;
  246. int direct, c;
  247. if (av_frame_is_writable(in) && !ctx->is_disabled) {
  248. direct = 1;
  249. out = in;
  250. } else {
  251. direct = 0;
  252. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  253. if (!out) {
  254. av_frame_free(&in);
  255. return AVERROR(ENOMEM);
  256. }
  257. av_frame_copy_props(out, in);
  258. }
  259. for (c = 0; c < 3; c++) {
  260. denoise(s, in->data[c], out->data[c],
  261. s->line, &s->frame_prev[c],
  262. FF_CEIL_RSHIFT(in->width, (!!c * s->hsub)),
  263. FF_CEIL_RSHIFT(in->height, (!!c * s->vsub)),
  264. in->linesize[c], out->linesize[c],
  265. s->coefs[c ? CHROMA_SPATIAL : LUMA_SPATIAL],
  266. s->coefs[c ? CHROMA_TMP : LUMA_TMP]);
  267. }
  268. if (ctx->is_disabled) {
  269. av_frame_free(&out);
  270. return ff_filter_frame(outlink, in);
  271. }
  272. if (!direct)
  273. av_frame_free(&in);
  274. return ff_filter_frame(outlink, out);
  275. }
  276. #define OFFSET(x) offsetof(HQDN3DContext, x)
  277. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  278. static const AVOption hqdn3d_options[] = {
  279. { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  280. { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  281. { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  282. { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  283. { NULL }
  284. };
  285. AVFILTER_DEFINE_CLASS(hqdn3d);
  286. static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
  287. {
  288. .name = "default",
  289. .type = AVMEDIA_TYPE_VIDEO,
  290. .config_props = config_input,
  291. .filter_frame = filter_frame,
  292. },
  293. { NULL }
  294. };
  295. static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
  296. {
  297. .name = "default",
  298. .type = AVMEDIA_TYPE_VIDEO
  299. },
  300. { NULL }
  301. };
  302. AVFilter ff_vf_hqdn3d = {
  303. .name = "hqdn3d",
  304. .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
  305. .priv_size = sizeof(HQDN3DContext),
  306. .priv_class = &hqdn3d_class,
  307. .init = init,
  308. .uninit = uninit,
  309. .query_formats = query_formats,
  310. .inputs = avfilter_vf_hqdn3d_inputs,
  311. .outputs = avfilter_vf_hqdn3d_outputs,
  312. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  313. };