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.

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