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.

377 lines
12KB

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