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.

361 lines
11KB

  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/common.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/intreadwrite.h"
  32. #include "libavutil/opt.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "video.h"
  37. #include "vf_hqdn3d.h"
  38. #define LUT_BITS (depth==16 ? 8 : 4)
  39. #define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
  40. + (((1 << (16 - depth)) - 1) >> 1))
  41. #define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
  42. AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
  43. av_always_inline
  44. static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
  45. {
  46. int d = (prev - cur) >> (8 - LUT_BITS);
  47. return cur + coef[d];
  48. }
  49. av_always_inline
  50. static void denoise_temporal(uint8_t *src, uint8_t *dst,
  51. uint16_t *frame_ant,
  52. int w, int h, int sstride, int dstride,
  53. int16_t *temporal, int depth)
  54. {
  55. long x, y;
  56. uint32_t tmp;
  57. temporal += 256 << LUT_BITS;
  58. for (y = 0; y < h; y++) {
  59. for (x = 0; x < w; x++) {
  60. frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
  61. STORE(x, tmp);
  62. }
  63. src += sstride;
  64. dst += dstride;
  65. frame_ant += w;
  66. }
  67. }
  68. av_always_inline
  69. static void denoise_spatial(HQDN3DContext *hqdn3d,
  70. uint8_t *src, uint8_t *dst,
  71. uint16_t *line_ant, uint16_t *frame_ant,
  72. int w, int h, int sstride, int dstride,
  73. int16_t *spatial, int16_t *temporal, int depth)
  74. {
  75. long x, y;
  76. uint32_t pixel_ant;
  77. uint32_t tmp;
  78. spatial += 256 << LUT_BITS;
  79. temporal += 256 << LUT_BITS;
  80. /* First line has no top neighbor. Only left one for each tmp and
  81. * last frame */
  82. pixel_ant = LOAD(0);
  83. for (x = 0; x < w; x++) {
  84. line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
  85. frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
  86. STORE(x, tmp);
  87. }
  88. for (y = 1; y < h; y++) {
  89. src += sstride;
  90. dst += dstride;
  91. frame_ant += w;
  92. if (hqdn3d->denoise_row[depth]) {
  93. hqdn3d->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
  94. continue;
  95. }
  96. pixel_ant = LOAD(0);
  97. for (x = 0; x < w-1; x++) {
  98. line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
  99. pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
  100. frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
  101. STORE(x, tmp);
  102. }
  103. line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
  104. frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
  105. STORE(x, tmp);
  106. }
  107. }
  108. av_always_inline
  109. static void denoise_depth(HQDN3DContext *hqdn3d,
  110. uint8_t *src, uint8_t *dst,
  111. uint16_t *line_ant, uint16_t **frame_ant_ptr,
  112. int w, int h, int sstride, int dstride,
  113. int16_t *spatial, int16_t *temporal, int depth)
  114. {
  115. // FIXME: For 16bit depth, frame_ant could be a pointer to the previous
  116. // filtered frame rather than a separate buffer.
  117. long x, y;
  118. uint16_t *frame_ant = *frame_ant_ptr;
  119. if (!frame_ant) {
  120. uint8_t *frame_src = src;
  121. *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
  122. for (y = 0; y < h; y++, src += sstride, frame_ant += w)
  123. for (x = 0; x < w; x++)
  124. frame_ant[x] = LOAD(x);
  125. src = frame_src;
  126. frame_ant = *frame_ant_ptr;
  127. }
  128. if (spatial[0])
  129. denoise_spatial(hqdn3d, src, dst, line_ant, frame_ant,
  130. w, h, sstride, dstride, spatial, temporal, depth);
  131. else
  132. denoise_temporal(src, dst, frame_ant,
  133. w, h, sstride, dstride, temporal, depth);
  134. }
  135. #define denoise(...) \
  136. switch (hqdn3d->depth) {\
  137. case 8: denoise_depth(__VA_ARGS__, 8); break;\
  138. case 9: denoise_depth(__VA_ARGS__, 9); break;\
  139. case 10: denoise_depth(__VA_ARGS__, 10); break;\
  140. case 16: denoise_depth(__VA_ARGS__, 16); break;\
  141. }
  142. static int16_t *precalc_coefs(double dist25, int depth)
  143. {
  144. int i;
  145. double gamma, simil, C;
  146. int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
  147. if (!ct)
  148. return NULL;
  149. gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
  150. for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
  151. double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0; // midpoint of the bin
  152. simil = 1.0 - FFABS(f) / 255.0;
  153. C = pow(simil, gamma) * 256.0 * f;
  154. ct[(256<<LUT_BITS)+i] = lrint(C);
  155. }
  156. ct[0] = !!dist25;
  157. return ct;
  158. }
  159. #define PARAM1_DEFAULT 4.0
  160. #define PARAM2_DEFAULT 3.0
  161. #define PARAM3_DEFAULT 6.0
  162. static int init(AVFilterContext *ctx)
  163. {
  164. HQDN3DContext *hqdn3d = ctx->priv;
  165. if (!hqdn3d->strength[LUMA_SPATIAL])
  166. hqdn3d->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
  167. if (!hqdn3d->strength[CHROMA_SPATIAL])
  168. hqdn3d->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * hqdn3d->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
  169. if (!hqdn3d->strength[LUMA_TMP])
  170. hqdn3d->strength[LUMA_TMP] = PARAM3_DEFAULT * hqdn3d->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
  171. if (!hqdn3d->strength[CHROMA_TMP])
  172. hqdn3d->strength[CHROMA_TMP] = hqdn3d->strength[LUMA_TMP] * hqdn3d->strength[CHROMA_SPATIAL] / hqdn3d->strength[LUMA_SPATIAL];
  173. av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
  174. hqdn3d->strength[LUMA_SPATIAL], hqdn3d->strength[CHROMA_SPATIAL],
  175. hqdn3d->strength[LUMA_TMP], hqdn3d->strength[CHROMA_TMP]);
  176. return 0;
  177. }
  178. static void uninit(AVFilterContext *ctx)
  179. {
  180. HQDN3DContext *hqdn3d = ctx->priv;
  181. av_freep(&hqdn3d->coefs[0]);
  182. av_freep(&hqdn3d->coefs[1]);
  183. av_freep(&hqdn3d->coefs[2]);
  184. av_freep(&hqdn3d->coefs[3]);
  185. av_freep(&hqdn3d->line);
  186. av_freep(&hqdn3d->frame_prev[0]);
  187. av_freep(&hqdn3d->frame_prev[1]);
  188. av_freep(&hqdn3d->frame_prev[2]);
  189. }
  190. static int query_formats(AVFilterContext *ctx)
  191. {
  192. static const enum AVPixelFormat pix_fmts[] = {
  193. AV_PIX_FMT_YUV420P,
  194. AV_PIX_FMT_YUV422P,
  195. AV_PIX_FMT_YUV444P,
  196. AV_PIX_FMT_YUV410P,
  197. AV_PIX_FMT_YUV411P,
  198. AV_PIX_FMT_YUV440P,
  199. AV_PIX_FMT_YUVJ420P,
  200. AV_PIX_FMT_YUVJ422P,
  201. AV_PIX_FMT_YUVJ444P,
  202. AV_PIX_FMT_YUVJ440P,
  203. AV_NE( AV_PIX_FMT_YUV420P9BE, AV_PIX_FMT_YUV420P9LE ),
  204. AV_NE( AV_PIX_FMT_YUV422P9BE, AV_PIX_FMT_YUV422P9LE ),
  205. AV_NE( AV_PIX_FMT_YUV444P9BE, AV_PIX_FMT_YUV444P9LE ),
  206. AV_NE( AV_PIX_FMT_YUV420P10BE, AV_PIX_FMT_YUV420P10LE ),
  207. AV_NE( AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10LE ),
  208. AV_NE( AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUV444P10LE ),
  209. AV_NE( AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_YUV420P16LE ),
  210. AV_NE( AV_PIX_FMT_YUV422P16BE, AV_PIX_FMT_YUV422P16LE ),
  211. AV_NE( AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16LE ),
  212. AV_PIX_FMT_NONE
  213. };
  214. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  215. return 0;
  216. }
  217. static int config_input(AVFilterLink *inlink)
  218. {
  219. HQDN3DContext *hqdn3d = inlink->dst->priv;
  220. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  221. int i;
  222. hqdn3d->hsub = desc->log2_chroma_w;
  223. hqdn3d->vsub = desc->log2_chroma_h;
  224. hqdn3d->depth = desc->comp[0].depth_minus1+1;
  225. hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
  226. if (!hqdn3d->line)
  227. return AVERROR(ENOMEM);
  228. for (i = 0; i < 4; i++) {
  229. hqdn3d->coefs[i] = precalc_coefs(hqdn3d->strength[i], hqdn3d->depth);
  230. if (!hqdn3d->coefs[i])
  231. return AVERROR(ENOMEM);
  232. }
  233. if (ARCH_X86)
  234. ff_hqdn3d_init_x86(hqdn3d);
  235. return 0;
  236. }
  237. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  238. {
  239. HQDN3DContext *hqdn3d = inlink->dst->priv;
  240. AVFilterLink *outlink = inlink->dst->outputs[0];
  241. AVFrame *out;
  242. int direct, c;
  243. if (av_frame_is_writable(in)) {
  244. direct = 1;
  245. out = in;
  246. } else {
  247. direct = 0;
  248. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  249. if (!out) {
  250. av_frame_free(&in);
  251. return AVERROR(ENOMEM);
  252. }
  253. av_frame_copy_props(out, in);
  254. }
  255. for (c = 0; c < 3; c++) {
  256. denoise(hqdn3d, in->data[c], out->data[c],
  257. hqdn3d->line, &hqdn3d->frame_prev[c],
  258. in->width >> (!!c * hqdn3d->hsub),
  259. in->height >> (!!c * hqdn3d->vsub),
  260. in->linesize[c], out->linesize[c],
  261. hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
  262. }
  263. if (!direct)
  264. av_frame_free(&in);
  265. return ff_filter_frame(outlink, out);
  266. }
  267. #define OFFSET(x) offsetof(HQDN3DContext, x)
  268. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
  269. static const AVOption options[] = {
  270. { "luma_spatial", "spatial luma strength", OFFSET(strength[LUMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  271. { "chroma_spatial", "spatial chroma strength", OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  272. { "luma_tmp", "temporal luma strength", OFFSET(strength[LUMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  273. { "chroma_tmp", "temporal chroma strength", OFFSET(strength[CHROMA_TMP]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
  274. { NULL },
  275. };
  276. static const AVClass hqdn3d_class = {
  277. .class_name = "hqdn3d",
  278. .item_name = av_default_item_name,
  279. .option = options,
  280. .version = LIBAVUTIL_VERSION_INT,
  281. };
  282. static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
  283. {
  284. .name = "default",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .config_props = config_input,
  287. .filter_frame = filter_frame,
  288. },
  289. { NULL }
  290. };
  291. static const AVFilterPad avfilter_vf_hqdn3d_outputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO
  295. },
  296. { NULL }
  297. };
  298. AVFilter avfilter_vf_hqdn3d = {
  299. .name = "hqdn3d",
  300. .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
  301. .priv_size = sizeof(HQDN3DContext),
  302. .priv_class = &hqdn3d_class,
  303. .init = init,
  304. .uninit = uninit,
  305. .query_formats = query_formats,
  306. .inputs = avfilter_vf_hqdn3d_inputs,
  307. .outputs = avfilter_vf_hqdn3d_outputs,
  308. };