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 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 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_array(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_BUG; \
  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 = -256<<LUT_BITS; i < 256<<LUT_BITS; i++) {
  165. double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
  166. simil = FFMAX(0, 1.0 - fabs(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_PIX_FMT_YUV420P9,
  218. AV_PIX_FMT_YUV422P9,
  219. AV_PIX_FMT_YUV444P9,
  220. AV_PIX_FMT_YUV420P10,
  221. AV_PIX_FMT_YUV422P10,
  222. AV_PIX_FMT_YUV444P10,
  223. AV_PIX_FMT_YUV420P16,
  224. AV_PIX_FMT_YUV422P16,
  225. AV_PIX_FMT_YUV444P16,
  226. AV_PIX_FMT_NONE
  227. };
  228. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  229. if (!fmts_list)
  230. return AVERROR(ENOMEM);
  231. return ff_set_common_formats(ctx, fmts_list);
  232. }
  233. static int config_input(AVFilterLink *inlink)
  234. {
  235. HQDN3DContext *s = inlink->dst->priv;
  236. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  237. int i;
  238. uninit(inlink->dst);
  239. s->hsub = desc->log2_chroma_w;
  240. s->vsub = desc->log2_chroma_h;
  241. s->depth = desc->comp[0].depth;
  242. s->line = av_malloc_array(inlink->w, sizeof(*s->line));
  243. if (!s->line)
  244. return AVERROR(ENOMEM);
  245. for (i = 0; i < 4; i++) {
  246. s->coefs[i] = precalc_coefs(s->strength[i], s->depth);
  247. if (!s->coefs[i])
  248. return AVERROR(ENOMEM);
  249. }
  250. if (ARCH_X86)
  251. ff_hqdn3d_init_x86(s);
  252. return 0;
  253. }
  254. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  255. {
  256. AVFilterContext *ctx = inlink->dst;
  257. HQDN3DContext *s = ctx->priv;
  258. AVFilterLink *outlink = ctx->outputs[0];
  259. AVFrame *out;
  260. int c, direct = av_frame_is_writable(in) && !ctx->is_disabled;
  261. if (direct) {
  262. out = in;
  263. } else {
  264. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  265. if (!out) {
  266. av_frame_free(&in);
  267. return AVERROR(ENOMEM);
  268. }
  269. av_frame_copy_props(out, in);
  270. }
  271. for (c = 0; c < 3; c++) {
  272. denoise(s, in->data[c], out->data[c],
  273. s->line, &s->frame_prev[c],
  274. AV_CEIL_RSHIFT(in->width, (!!c * s->hsub)),
  275. AV_CEIL_RSHIFT(in->height, (!!c * s->vsub)),
  276. in->linesize[c], out->linesize[c],
  277. s->coefs[c ? CHROMA_SPATIAL : LUMA_SPATIAL],
  278. s->coefs[c ? CHROMA_TMP : LUMA_TMP]);
  279. }
  280. if (ctx->is_disabled) {
  281. av_frame_free(&out);
  282. return ff_filter_frame(outlink, in);
  283. }
  284. if (!direct)
  285. av_frame_free(&in);
  286. return ff_filter_frame(outlink, out);
  287. }
  288. #define OFFSET(x) offsetof(HQDN3DContext, x)
  289. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  290. static const AVOption hqdn3d_options[] = {
  291. { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  292. { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  293. { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  294. { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  295. { NULL }
  296. };
  297. AVFILTER_DEFINE_CLASS(hqdn3d);
  298. static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
  299. {
  300. .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. .config_props = config_input,
  303. .filter_frame = filter_frame,
  304. },
  305. { NULL }
  306. };
  307. static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
  308. {
  309. .name = "default",
  310. .type = AVMEDIA_TYPE_VIDEO
  311. },
  312. { NULL }
  313. };
  314. AVFilter ff_vf_hqdn3d = {
  315. .name = "hqdn3d",
  316. .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
  317. .priv_size = sizeof(HQDN3DContext),
  318. .priv_class = &hqdn3d_class,
  319. .init = init,
  320. .uninit = uninit,
  321. .query_formats = query_formats,
  322. .inputs = avfilter_vf_hqdn3d_inputs,
  323. .outputs = avfilter_vf_hqdn3d_outputs,
  324. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  325. };