vf_hqdn3d.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
  3. * Copyright (c) 2010 Baptiste Coudurier
  4. *
  5. * This file is part of FFmpeg, ported from MPlayer.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * high quality 3d video denoiser, ported from MPlayer
  24. * libmpcodecs/vf_hqdn3d.c.
  25. */
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. typedef struct {
  29. int Coefs[4][512*16];
  30. unsigned int *Line;
  31. unsigned short *Frame[3];
  32. int hsub, vsub;
  33. } HQDN3DContext;
  34. static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef)
  35. {
  36. // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
  37. int dMul= PrevMul-CurrMul;
  38. unsigned int d=((dMul+0x10007FF)>>12);
  39. return CurrMul + Coef[d];
  40. }
  41. static void deNoiseTemporal(unsigned char *FrameSrc,
  42. unsigned char *FrameDest,
  43. unsigned short *FrameAnt,
  44. int W, int H, int sStride, int dStride,
  45. int *Temporal)
  46. {
  47. long X, Y;
  48. unsigned int PixelDst;
  49. for (Y = 0; Y < H; Y++) {
  50. for (X = 0; X < W; X++) {
  51. PixelDst = LowPassMul(FrameAnt[X]<<8, FrameSrc[X]<<16, Temporal);
  52. FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
  53. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  54. }
  55. FrameSrc += sStride;
  56. FrameDest += dStride;
  57. FrameAnt += W;
  58. }
  59. }
  60. static void deNoiseSpacial(unsigned char *Frame,
  61. unsigned char *FrameDest,
  62. unsigned int *LineAnt,
  63. int W, int H, int sStride, int dStride,
  64. int *Horizontal, int *Vertical)
  65. {
  66. long X, Y;
  67. long sLineOffs = 0, dLineOffs = 0;
  68. unsigned int PixelAnt;
  69. unsigned int PixelDst;
  70. /* First pixel has no left nor top neighbor. */
  71. PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
  72. FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
  73. /* First line has no top neighbor, only left. */
  74. for (X = 1; X < W; X++) {
  75. PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
  76. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  77. }
  78. for (Y = 1; Y < H; Y++) {
  79. unsigned int PixelAnt;
  80. sLineOffs += sStride, dLineOffs += dStride;
  81. /* First pixel on each line doesn't have previous pixel */
  82. PixelAnt = Frame[sLineOffs]<<16;
  83. PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
  84. FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
  85. for (X = 1; X < W; X++) {
  86. unsigned int PixelDst;
  87. /* The rest are normal */
  88. PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
  89. PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
  90. FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
  91. }
  92. }
  93. }
  94. static void deNoise(unsigned char *Frame,
  95. unsigned char *FrameDest,
  96. unsigned int *LineAnt,
  97. unsigned short **FrameAntPtr,
  98. int W, int H, int sStride, int dStride,
  99. int *Horizontal, int *Vertical, int *Temporal)
  100. {
  101. long X, Y;
  102. long sLineOffs = 0, dLineOffs = 0;
  103. unsigned int PixelAnt;
  104. unsigned int PixelDst;
  105. unsigned short* FrameAnt=(*FrameAntPtr);
  106. if (!FrameAnt) {
  107. (*FrameAntPtr) = FrameAnt = av_malloc(W*H*sizeof(unsigned short));
  108. for (Y = 0; Y < H; Y++) {
  109. unsigned short* dst=&FrameAnt[Y*W];
  110. unsigned char* src=Frame+Y*sStride;
  111. for (X = 0; X < W; X++) dst[X]=src[X]<<8;
  112. }
  113. }
  114. if (!Horizontal[0] && !Vertical[0]) {
  115. deNoiseTemporal(Frame, FrameDest, FrameAnt,
  116. W, H, sStride, dStride, Temporal);
  117. return;
  118. }
  119. if (!Temporal[0]) {
  120. deNoiseSpacial(Frame, FrameDest, LineAnt,
  121. W, H, sStride, dStride, Horizontal, Vertical);
  122. return;
  123. }
  124. /* First pixel has no left nor top neighbor. Only previous frame */
  125. LineAnt[0] = PixelAnt = Frame[0]<<16;
  126. PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
  127. FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
  128. FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
  129. /* First line has no top neighbor. Only left one for each pixel and
  130. * last frame */
  131. for (X = 1; X < W; X++) {
  132. LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
  133. PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
  134. FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
  135. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  136. }
  137. for (Y = 1; Y < H; Y++) {
  138. unsigned int PixelAnt;
  139. unsigned short* LinePrev=&FrameAnt[Y*W];
  140. sLineOffs += sStride, dLineOffs += dStride;
  141. /* First pixel on each line doesn't have previous pixel */
  142. PixelAnt = Frame[sLineOffs]<<16;
  143. LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
  144. PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
  145. LinePrev[0] = ((PixelDst+0x1000007F)>>8);
  146. FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
  147. for (X = 1; X < W; X++) {
  148. unsigned int PixelDst;
  149. /* The rest are normal */
  150. PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
  151. LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
  152. PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
  153. LinePrev[X] = ((PixelDst+0x1000007F)>>8);
  154. FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
  155. }
  156. }
  157. }
  158. static void PrecalcCoefs(int *Ct, double Dist25)
  159. {
  160. int i;
  161. double Gamma, Simil, C;
  162. Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
  163. for (i = -255*16; i <= 255*16; i++) {
  164. Simil = 1.0 - FFABS(i) / (16*255.0);
  165. C = pow(Simil, Gamma) * 65536.0 * i / 16.0;
  166. Ct[16*256+i] = lrint(C);
  167. }
  168. Ct[0] = !!Dist25;
  169. }
  170. #define PARAM1_DEFAULT 4.0
  171. #define PARAM2_DEFAULT 3.0
  172. #define PARAM3_DEFAULT 6.0
  173. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  174. {
  175. HQDN3DContext *hqdn3d = ctx->priv;
  176. double LumSpac, LumTmp, ChromSpac, ChromTmp;
  177. double Param1, Param2, Param3, Param4;
  178. LumSpac = PARAM1_DEFAULT;
  179. ChromSpac = PARAM2_DEFAULT;
  180. LumTmp = PARAM3_DEFAULT;
  181. ChromTmp = LumTmp * ChromSpac / LumSpac;
  182. if (args) {
  183. switch (sscanf(args, "%lf:%lf:%lf:%lf",
  184. &Param1, &Param2, &Param3, &Param4)) {
  185. case 1:
  186. LumSpac = Param1;
  187. ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
  188. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  189. ChromTmp = LumTmp * ChromSpac / LumSpac;
  190. break;
  191. case 2:
  192. LumSpac = Param1;
  193. ChromSpac = Param2;
  194. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  195. ChromTmp = LumTmp * ChromSpac / LumSpac;
  196. break;
  197. case 3:
  198. LumSpac = Param1;
  199. ChromSpac = Param2;
  200. LumTmp = Param3;
  201. ChromTmp = LumTmp * ChromSpac / LumSpac;
  202. break;
  203. case 4:
  204. LumSpac = Param1;
  205. ChromSpac = Param2;
  206. LumTmp = Param3;
  207. ChromTmp = Param4;
  208. break;
  209. }
  210. }
  211. av_log(ctx, AV_LOG_INFO, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
  212. LumSpac, ChromSpac, LumTmp, ChromTmp);
  213. if (LumSpac < 0 || ChromSpac < 0 || isnan(ChromTmp)) {
  214. av_log(ctx, AV_LOG_ERROR,
  215. "Invalid negative value for luma or chroma spatial strength, "
  216. "or resulting value for chroma temporal strength is nan.\n");
  217. return AVERROR(EINVAL);
  218. }
  219. PrecalcCoefs(hqdn3d->Coefs[0], LumSpac);
  220. PrecalcCoefs(hqdn3d->Coefs[1], LumTmp);
  221. PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac);
  222. PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp);
  223. return 0;
  224. }
  225. static void uninit(AVFilterContext *ctx)
  226. {
  227. HQDN3DContext *hqdn3d = ctx->priv;
  228. av_freep(&hqdn3d->Line);
  229. av_freep(&hqdn3d->Frame[0]);
  230. av_freep(&hqdn3d->Frame[1]);
  231. av_freep(&hqdn3d->Frame[2]);
  232. }
  233. static int query_formats(AVFilterContext *ctx)
  234. {
  235. static const enum PixelFormat pix_fmts[] = {
  236. PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
  237. };
  238. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  239. return 0;
  240. }
  241. static int config_input(AVFilterLink *inlink)
  242. {
  243. HQDN3DContext *hqdn3d = inlink->dst->priv;
  244. hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  245. hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  246. hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line));
  247. if (!hqdn3d->Line)
  248. return AVERROR(ENOMEM);
  249. return 0;
  250. }
  251. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  252. static void end_frame(AVFilterLink *inlink)
  253. {
  254. HQDN3DContext *hqdn3d = inlink->dst->priv;
  255. AVFilterLink *outlink = inlink->dst->outputs[0];
  256. AVFilterBufferRef *inpic = inlink ->cur_buf;
  257. AVFilterBufferRef *outpic = outlink->out_buf;
  258. int cw = inpic->video->w >> hqdn3d->hsub;
  259. int ch = inpic->video->h >> hqdn3d->vsub;
  260. deNoise(inpic->data[0], outpic->data[0],
  261. hqdn3d->Line, &hqdn3d->Frame[0], inpic->video->w, inpic->video->h,
  262. inpic->linesize[0], outpic->linesize[0],
  263. hqdn3d->Coefs[0],
  264. hqdn3d->Coefs[0],
  265. hqdn3d->Coefs[1]);
  266. deNoise(inpic->data[1], outpic->data[1],
  267. hqdn3d->Line, &hqdn3d->Frame[1], cw, ch,
  268. inpic->linesize[1], outpic->linesize[1],
  269. hqdn3d->Coefs[2],
  270. hqdn3d->Coefs[2],
  271. hqdn3d->Coefs[3]);
  272. deNoise(inpic->data[2], outpic->data[2],
  273. hqdn3d->Line, &hqdn3d->Frame[2], cw, ch,
  274. inpic->linesize[2], outpic->linesize[2],
  275. hqdn3d->Coefs[2],
  276. hqdn3d->Coefs[2],
  277. hqdn3d->Coefs[3]);
  278. avfilter_draw_slice(outlink, 0, inpic->video->h, 1);
  279. avfilter_end_frame(outlink);
  280. avfilter_unref_buffer(inpic);
  281. avfilter_unref_buffer(outpic);
  282. }
  283. AVFilter avfilter_vf_hqdn3d = {
  284. .name = "hqdn3d",
  285. .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
  286. .priv_size = sizeof(HQDN3DContext),
  287. .init = init,
  288. .uninit = uninit,
  289. .query_formats = query_formats,
  290. .inputs = (const AVFilterPad[]) {{ .name = "default",
  291. .type = AVMEDIA_TYPE_VIDEO,
  292. .draw_slice = null_draw_slice,
  293. .config_props = config_input,
  294. .end_frame = end_frame },
  295. { .name = NULL}},
  296. .outputs = (const AVFilterPad[]) {{ .name = "default",
  297. .type = AVMEDIA_TYPE_VIDEO },
  298. { .name = NULL}},
  299. };