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.

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