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.

349 lines
11KB

  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 Libav, ported from MPlayer.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct {
  32. int Coefs[4][512*16];
  33. unsigned int *Line;
  34. unsigned short *Frame[3];
  35. int hsub, vsub;
  36. } HQDN3DContext;
  37. static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef)
  38. {
  39. // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
  40. int dMul= PrevMul-CurrMul;
  41. unsigned int d=((dMul+0x10007FF)>>12);
  42. return CurrMul + Coef[d];
  43. }
  44. static void deNoiseTemporal(unsigned char *FrameSrc,
  45. unsigned char *FrameDest,
  46. unsigned short *FrameAnt,
  47. int W, int H, int sStride, int dStride,
  48. int *Temporal)
  49. {
  50. long X, Y;
  51. unsigned int PixelDst;
  52. for (Y = 0; Y < H; Y++) {
  53. for (X = 0; X < W; X++) {
  54. PixelDst = LowPassMul(FrameAnt[X]<<8, FrameSrc[X]<<16, Temporal);
  55. FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
  56. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  57. }
  58. FrameSrc += sStride;
  59. FrameDest += dStride;
  60. FrameAnt += W;
  61. }
  62. }
  63. static void deNoiseSpacial(unsigned char *Frame,
  64. unsigned char *FrameDest,
  65. unsigned int *LineAnt,
  66. int W, int H, int sStride, int dStride,
  67. int *Horizontal, int *Vertical)
  68. {
  69. long X, Y;
  70. long sLineOffs = 0, dLineOffs = 0;
  71. unsigned int PixelAnt;
  72. unsigned int PixelDst;
  73. /* First pixel has no left nor top neighbor. */
  74. PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
  75. FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
  76. /* First line has no top neighbor, only left. */
  77. for (X = 1; X < W; X++) {
  78. PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
  79. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  80. }
  81. for (Y = 1; Y < H; Y++) {
  82. unsigned int PixelAnt;
  83. sLineOffs += sStride, dLineOffs += dStride;
  84. /* First pixel on each line doesn't have previous pixel */
  85. PixelAnt = Frame[sLineOffs]<<16;
  86. PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
  87. FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
  88. for (X = 1; X < W; X++) {
  89. unsigned int PixelDst;
  90. /* The rest are normal */
  91. PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
  92. PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
  93. FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
  94. }
  95. }
  96. }
  97. static void deNoise(unsigned char *Frame,
  98. unsigned char *FrameDest,
  99. unsigned int *LineAnt,
  100. unsigned short **FrameAntPtr,
  101. int W, int H, int sStride, int dStride,
  102. int *Horizontal, int *Vertical, int *Temporal)
  103. {
  104. long X, Y;
  105. long sLineOffs = 0, dLineOffs = 0;
  106. unsigned int PixelAnt;
  107. unsigned int PixelDst;
  108. unsigned short* FrameAnt=(*FrameAntPtr);
  109. if (!FrameAnt) {
  110. (*FrameAntPtr) = FrameAnt = av_malloc(W*H*sizeof(unsigned short));
  111. for (Y = 0; Y < H; Y++) {
  112. unsigned short* dst=&FrameAnt[Y*W];
  113. unsigned char* src=Frame+Y*sStride;
  114. for (X = 0; X < W; X++) dst[X]=src[X]<<8;
  115. }
  116. }
  117. if (!Horizontal[0] && !Vertical[0]) {
  118. deNoiseTemporal(Frame, FrameDest, FrameAnt,
  119. W, H, sStride, dStride, Temporal);
  120. return;
  121. }
  122. if (!Temporal[0]) {
  123. deNoiseSpacial(Frame, FrameDest, LineAnt,
  124. W, H, sStride, dStride, Horizontal, Vertical);
  125. return;
  126. }
  127. /* First pixel has no left nor top neighbor. Only previous frame */
  128. LineAnt[0] = PixelAnt = Frame[0]<<16;
  129. PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
  130. FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
  131. FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
  132. /* First line has no top neighbor. Only left one for each pixel and
  133. * last frame */
  134. for (X = 1; X < W; X++) {
  135. LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
  136. PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
  137. FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
  138. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  139. }
  140. for (Y = 1; Y < H; Y++) {
  141. unsigned int PixelAnt;
  142. unsigned short* LinePrev=&FrameAnt[Y*W];
  143. sLineOffs += sStride, dLineOffs += dStride;
  144. /* First pixel on each line doesn't have previous pixel */
  145. PixelAnt = Frame[sLineOffs]<<16;
  146. LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
  147. PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
  148. LinePrev[0] = ((PixelDst+0x1000007F)>>8);
  149. FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
  150. for (X = 1; X < W; X++) {
  151. unsigned int PixelDst;
  152. /* The rest are normal */
  153. PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
  154. LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
  155. PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
  156. LinePrev[X] = ((PixelDst+0x1000007F)>>8);
  157. FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
  158. }
  159. }
  160. }
  161. static void PrecalcCoefs(int *Ct, double Dist25)
  162. {
  163. int i;
  164. double Gamma, Simil, C;
  165. Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
  166. for (i = -255*16; i <= 255*16; i++) {
  167. Simil = 1.0 - FFABS(i) / (16*255.0);
  168. C = pow(Simil, Gamma) * 65536.0 * i / 16.0;
  169. Ct[16*256+i] = lrint(C);
  170. }
  171. Ct[0] = !!Dist25;
  172. }
  173. #define PARAM1_DEFAULT 4.0
  174. #define PARAM2_DEFAULT 3.0
  175. #define PARAM3_DEFAULT 6.0
  176. static int init(AVFilterContext *ctx, const char *args)
  177. {
  178. HQDN3DContext *hqdn3d = ctx->priv;
  179. double LumSpac, LumTmp, ChromSpac, ChromTmp;
  180. double Param1, Param2, Param3, Param4;
  181. LumSpac = PARAM1_DEFAULT;
  182. ChromSpac = PARAM2_DEFAULT;
  183. LumTmp = PARAM3_DEFAULT;
  184. ChromTmp = LumTmp * ChromSpac / LumSpac;
  185. if (args) {
  186. switch (sscanf(args, "%lf:%lf:%lf:%lf",
  187. &Param1, &Param2, &Param3, &Param4)) {
  188. case 1:
  189. LumSpac = Param1;
  190. ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
  191. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  192. ChromTmp = LumTmp * ChromSpac / LumSpac;
  193. break;
  194. case 2:
  195. LumSpac = Param1;
  196. ChromSpac = Param2;
  197. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  198. ChromTmp = LumTmp * ChromSpac / LumSpac;
  199. break;
  200. case 3:
  201. LumSpac = Param1;
  202. ChromSpac = Param2;
  203. LumTmp = Param3;
  204. ChromTmp = LumTmp * ChromSpac / LumSpac;
  205. break;
  206. case 4:
  207. LumSpac = Param1;
  208. ChromSpac = Param2;
  209. LumTmp = Param3;
  210. ChromTmp = Param4;
  211. break;
  212. }
  213. }
  214. av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
  215. LumSpac, ChromSpac, LumTmp, ChromTmp);
  216. if (LumSpac < 0 || ChromSpac < 0 || isnan(ChromTmp)) {
  217. av_log(ctx, AV_LOG_ERROR,
  218. "Invalid negative value for luma or chroma spatial strength, "
  219. "or resulting value for chroma temporal strength is nan.\n");
  220. return AVERROR(EINVAL);
  221. }
  222. PrecalcCoefs(hqdn3d->Coefs[0], LumSpac);
  223. PrecalcCoefs(hqdn3d->Coefs[1], LumTmp);
  224. PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac);
  225. PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp);
  226. return 0;
  227. }
  228. static void uninit(AVFilterContext *ctx)
  229. {
  230. HQDN3DContext *hqdn3d = ctx->priv;
  231. av_freep(&hqdn3d->Line);
  232. av_freep(&hqdn3d->Frame[0]);
  233. av_freep(&hqdn3d->Frame[1]);
  234. av_freep(&hqdn3d->Frame[2]);
  235. }
  236. static int query_formats(AVFilterContext *ctx)
  237. {
  238. static const enum PixelFormat pix_fmts[] = {
  239. PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
  240. };
  241. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  242. return 0;
  243. }
  244. static int config_input(AVFilterLink *inlink)
  245. {
  246. HQDN3DContext *hqdn3d = inlink->dst->priv;
  247. hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
  248. hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
  249. hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line));
  250. if (!hqdn3d->Line)
  251. return AVERROR(ENOMEM);
  252. return 0;
  253. }
  254. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  255. static void end_frame(AVFilterLink *inlink)
  256. {
  257. HQDN3DContext *hqdn3d = inlink->dst->priv;
  258. AVFilterLink *outlink = inlink->dst->outputs[0];
  259. AVFilterBufferRef *inpic = inlink ->cur_buf;
  260. AVFilterBufferRef *outpic = outlink->out_buf;
  261. int cw = inpic->video->w >> hqdn3d->hsub;
  262. int ch = inpic->video->h >> hqdn3d->vsub;
  263. deNoise(inpic->data[0], outpic->data[0],
  264. hqdn3d->Line, &hqdn3d->Frame[0], inpic->video->w, inpic->video->h,
  265. inpic->linesize[0], outpic->linesize[0],
  266. hqdn3d->Coefs[0],
  267. hqdn3d->Coefs[0],
  268. hqdn3d->Coefs[1]);
  269. deNoise(inpic->data[1], outpic->data[1],
  270. hqdn3d->Line, &hqdn3d->Frame[1], cw, ch,
  271. inpic->linesize[1], outpic->linesize[1],
  272. hqdn3d->Coefs[2],
  273. hqdn3d->Coefs[2],
  274. hqdn3d->Coefs[3]);
  275. deNoise(inpic->data[2], outpic->data[2],
  276. hqdn3d->Line, &hqdn3d->Frame[2], cw, ch,
  277. inpic->linesize[2], outpic->linesize[2],
  278. hqdn3d->Coefs[2],
  279. hqdn3d->Coefs[2],
  280. hqdn3d->Coefs[3]);
  281. ff_draw_slice(outlink, 0, inpic->video->h, 1);
  282. ff_end_frame(outlink);
  283. avfilter_unref_buffer(inpic);
  284. avfilter_unref_buffer(outpic);
  285. }
  286. AVFilter avfilter_vf_hqdn3d = {
  287. .name = "hqdn3d",
  288. .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
  289. .priv_size = sizeof(HQDN3DContext),
  290. .init = init,
  291. .uninit = uninit,
  292. .query_formats = query_formats,
  293. .inputs = (AVFilterPad[]) {{ .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .draw_slice = null_draw_slice,
  296. .config_props = config_input,
  297. .end_frame = end_frame },
  298. { .name = NULL}},
  299. .outputs = (AVFilterPad[]) {{ .name = "default",
  300. .type = AVMEDIA_TYPE_VIDEO },
  301. { .name = NULL}},
  302. };