vf_hqdn3d.c 13 KB

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