vf_hqdn3d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (C) 2003 Daniel Moreno <comac@comac.darktech.org>
  3. *
  4. * This file is part of MPlayer.
  5. *
  6. * MPlayer is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * MPlayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <inttypes.h>
  24. #include <math.h>
  25. #include "mp_msg.h"
  26. #include "img_format.h"
  27. #include "mp_image.h"
  28. #include "vf.h"
  29. #define PARAM1_DEFAULT 4.0
  30. #define PARAM2_DEFAULT 3.0
  31. #define PARAM3_DEFAULT 6.0
  32. //===========================================================================//
  33. struct vf_priv_s {
  34. int Coefs[4][512*16];
  35. unsigned int *Line;
  36. unsigned short *Frame[3];
  37. };
  38. /***************************************************************************/
  39. static void uninit(struct vf_instance *vf)
  40. {
  41. free(vf->priv->Line);
  42. free(vf->priv->Frame[0]);
  43. free(vf->priv->Frame[1]);
  44. free(vf->priv->Frame[2]);
  45. vf->priv->Line = NULL;
  46. vf->priv->Frame[0] = NULL;
  47. vf->priv->Frame[1] = NULL;
  48. vf->priv->Frame[2] = NULL;
  49. }
  50. static int config(struct vf_instance *vf,
  51. int width, int height, int d_width, int d_height,
  52. unsigned int flags, unsigned int outfmt){
  53. uninit(vf);
  54. vf->priv->Line = malloc(width*sizeof(int));
  55. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  56. }
  57. static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int* Coef){
  58. // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
  59. int dMul= PrevMul-CurrMul;
  60. unsigned int d=((dMul+0x10007FF)>>12);
  61. return CurrMul + Coef[d];
  62. }
  63. static void deNoiseTemporal(
  64. unsigned char *Frame, // mpi->planes[x]
  65. unsigned char *FrameDest, // dmpi->planes[x]
  66. unsigned short *FrameAnt,
  67. int W, int H, int sStride, int dStride,
  68. int *Temporal)
  69. {
  70. long X, Y;
  71. unsigned int PixelDst;
  72. for (Y = 0; Y < H; Y++){
  73. for (X = 0; X < W; X++){
  74. PixelDst = LowPassMul(FrameAnt[X]<<8, Frame[X]<<16, Temporal);
  75. FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
  76. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  77. }
  78. Frame += sStride;
  79. FrameDest += dStride;
  80. FrameAnt += W;
  81. }
  82. }
  83. static void deNoiseSpacial(
  84. unsigned char *Frame, // mpi->planes[x]
  85. unsigned char *FrameDest, // dmpi->planes[x]
  86. unsigned int *LineAnt, // vf->priv->Line (width bytes)
  87. int W, int H, int sStride, int dStride,
  88. int *Horizontal, int *Vertical)
  89. {
  90. long X, Y;
  91. long sLineOffs = 0, dLineOffs = 0;
  92. unsigned int PixelAnt;
  93. unsigned int PixelDst;
  94. /* First pixel has no left nor top neighbor. */
  95. PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
  96. FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
  97. /* First line has no top neighbor, only left. */
  98. for (X = 1; X < W; X++){
  99. PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
  100. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  101. }
  102. for (Y = 1; Y < H; Y++){
  103. unsigned int PixelAnt;
  104. sLineOffs += sStride, dLineOffs += dStride;
  105. /* First pixel on each line doesn't have previous pixel */
  106. PixelAnt = Frame[sLineOffs]<<16;
  107. PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
  108. FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
  109. for (X = 1; X < W; X++){
  110. unsigned int PixelDst;
  111. /* The rest are normal */
  112. PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
  113. PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
  114. FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
  115. }
  116. }
  117. }
  118. static void deNoise(unsigned char *Frame, // mpi->planes[x]
  119. unsigned char *FrameDest, // dmpi->planes[x]
  120. unsigned int *LineAnt, // vf->priv->Line (width bytes)
  121. unsigned short **FrameAntPtr,
  122. int W, int H, int sStride, int dStride,
  123. int *Horizontal, int *Vertical, int *Temporal)
  124. {
  125. long X, Y;
  126. long sLineOffs = 0, dLineOffs = 0;
  127. unsigned int PixelAnt;
  128. unsigned int PixelDst;
  129. unsigned short* FrameAnt=(*FrameAntPtr);
  130. if(!FrameAnt){
  131. (*FrameAntPtr)=FrameAnt=malloc(W*H*sizeof(unsigned short));
  132. for (Y = 0; Y < H; Y++){
  133. unsigned short* dst=&FrameAnt[Y*W];
  134. unsigned char* src=Frame+Y*sStride;
  135. for (X = 0; X < W; X++) dst[X]=src[X]<<8;
  136. }
  137. }
  138. if(!Horizontal[0] && !Vertical[0]){
  139. deNoiseTemporal(Frame, FrameDest, FrameAnt,
  140. W, H, sStride, dStride, Temporal);
  141. return;
  142. }
  143. if(!Temporal[0]){
  144. deNoiseSpacial(Frame, FrameDest, LineAnt,
  145. W, H, sStride, dStride, Horizontal, Vertical);
  146. return;
  147. }
  148. /* First pixel has no left nor top neighbor. Only previous frame */
  149. LineAnt[0] = PixelAnt = Frame[0]<<16;
  150. PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
  151. FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
  152. FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
  153. /* First line has no top neighbor. Only left one for each pixel and
  154. * last frame */
  155. for (X = 1; X < W; X++){
  156. LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
  157. PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
  158. FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
  159. FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
  160. }
  161. for (Y = 1; Y < H; Y++){
  162. unsigned int PixelAnt;
  163. unsigned short* LinePrev=&FrameAnt[Y*W];
  164. sLineOffs += sStride, dLineOffs += dStride;
  165. /* First pixel on each line doesn't have previous pixel */
  166. PixelAnt = Frame[sLineOffs]<<16;
  167. LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
  168. PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
  169. LinePrev[0] = ((PixelDst+0x1000007F)>>8);
  170. FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
  171. for (X = 1; X < W; X++){
  172. unsigned int PixelDst;
  173. /* The rest are normal */
  174. PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
  175. LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
  176. PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
  177. LinePrev[X] = ((PixelDst+0x1000007F)>>8);
  178. FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
  179. }
  180. }
  181. }
  182. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  183. int cw= mpi->w >> mpi->chroma_x_shift;
  184. int ch= mpi->h >> mpi->chroma_y_shift;
  185. int W = mpi->w, H = mpi->h;
  186. mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
  187. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  188. mpi->w,mpi->h);
  189. if(!dmpi) return 0;
  190. deNoise(mpi->planes[0], dmpi->planes[0],
  191. vf->priv->Line, &vf->priv->Frame[0], W, H,
  192. mpi->stride[0], dmpi->stride[0],
  193. vf->priv->Coefs[0],
  194. vf->priv->Coefs[0],
  195. vf->priv->Coefs[1]);
  196. deNoise(mpi->planes[1], dmpi->planes[1],
  197. vf->priv->Line, &vf->priv->Frame[1], cw, ch,
  198. mpi->stride[1], dmpi->stride[1],
  199. vf->priv->Coefs[2],
  200. vf->priv->Coefs[2],
  201. vf->priv->Coefs[3]);
  202. deNoise(mpi->planes[2], dmpi->planes[2],
  203. vf->priv->Line, &vf->priv->Frame[2], cw, ch,
  204. mpi->stride[2], dmpi->stride[2],
  205. vf->priv->Coefs[2],
  206. vf->priv->Coefs[2],
  207. vf->priv->Coefs[3]);
  208. return vf_next_put_image(vf,dmpi, pts);
  209. }
  210. //===========================================================================//
  211. static int query_format(struct vf_instance *vf, unsigned int fmt){
  212. switch(fmt)
  213. {
  214. case IMGFMT_YV12:
  215. case IMGFMT_I420:
  216. case IMGFMT_IYUV:
  217. case IMGFMT_YVU9:
  218. case IMGFMT_444P:
  219. case IMGFMT_422P:
  220. case IMGFMT_411P:
  221. return vf_next_query_format(vf, fmt);
  222. }
  223. return 0;
  224. }
  225. #define ABS(A) ( (A) > 0 ? (A) : -(A) )
  226. static void PrecalcCoefs(int *Ct, double Dist25)
  227. {
  228. int i;
  229. double Gamma, Simil, C;
  230. Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
  231. for (i = -255*16; i <= 255*16; i++)
  232. {
  233. Simil = 1.0 - ABS(i) / (16*255.0);
  234. C = pow(Simil, Gamma) * 65536.0 * (double)i / 16.0;
  235. Ct[16*256+i] = (C<0) ? (C-0.5) : (C+0.5);
  236. }
  237. Ct[0] = (Dist25 != 0);
  238. }
  239. static int vf_open(vf_instance_t *vf, char *args){
  240. double LumSpac, LumTmp, ChromSpac, ChromTmp;
  241. double Param1, Param2, Param3, Param4;
  242. vf->config=config;
  243. vf->put_image=put_image;
  244. vf->query_format=query_format;
  245. vf->uninit=uninit;
  246. vf->priv=malloc(sizeof(struct vf_priv_s));
  247. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  248. if (args)
  249. {
  250. switch(sscanf(args, "%lf:%lf:%lf:%lf",
  251. &Param1, &Param2, &Param3, &Param4
  252. ))
  253. {
  254. case 0:
  255. LumSpac = PARAM1_DEFAULT;
  256. LumTmp = PARAM3_DEFAULT;
  257. ChromSpac = PARAM2_DEFAULT;
  258. ChromTmp = LumTmp * ChromSpac / LumSpac;
  259. break;
  260. case 1:
  261. LumSpac = Param1;
  262. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  263. ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
  264. ChromTmp = LumTmp * ChromSpac / LumSpac;
  265. break;
  266. case 2:
  267. LumSpac = Param1;
  268. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  269. ChromSpac = Param2;
  270. ChromTmp = LumTmp * ChromSpac / LumSpac;
  271. break;
  272. case 3:
  273. LumSpac = Param1;
  274. LumTmp = Param3;
  275. ChromSpac = Param2;
  276. ChromTmp = LumTmp * ChromSpac / LumSpac;
  277. break;
  278. case 4:
  279. LumSpac = Param1;
  280. LumTmp = Param3;
  281. ChromSpac = Param2;
  282. ChromTmp = Param4;
  283. break;
  284. default:
  285. LumSpac = PARAM1_DEFAULT;
  286. LumTmp = PARAM3_DEFAULT;
  287. ChromSpac = PARAM2_DEFAULT;
  288. ChromTmp = LumTmp * ChromSpac / LumSpac;
  289. }
  290. }
  291. else
  292. {
  293. LumSpac = PARAM1_DEFAULT;
  294. LumTmp = PARAM3_DEFAULT;
  295. ChromSpac = PARAM2_DEFAULT;
  296. ChromTmp = LumTmp * ChromSpac / LumSpac;
  297. }
  298. PrecalcCoefs(vf->priv->Coefs[0], LumSpac);
  299. PrecalcCoefs(vf->priv->Coefs[1], LumTmp);
  300. PrecalcCoefs(vf->priv->Coefs[2], ChromSpac);
  301. PrecalcCoefs(vf->priv->Coefs[3], ChromTmp);
  302. return 1;
  303. }
  304. const vf_info_t vf_info_hqdn3d = {
  305. "High Quality 3D Denoiser",
  306. "hqdn3d",
  307. "Daniel Moreno & A'rpi",
  308. "",
  309. vf_open,
  310. NULL
  311. };
  312. //===========================================================================//