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.

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