vf_denoise3d.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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];
  35. unsigned char *Line;
  36. mp_image_t *pmpi;
  37. };
  38. /***************************************************************************/
  39. static int config(struct vf_instance *vf,
  40. int width, int height, int d_width, int d_height,
  41. unsigned int flags, unsigned int outfmt){
  42. free(vf->priv->Line);
  43. vf->priv->Line = malloc(width);
  44. vf->priv->pmpi=NULL;
  45. // vf->default_caps &= !VFCAP_ACCEPT_STRIDE;
  46. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  47. }
  48. static void uninit(struct vf_instance *vf)
  49. {
  50. free(vf->priv->Line);
  51. }
  52. #define LowPass(Prev, Curr, Coef) (Curr + Coef[Prev - Curr])
  53. static void deNoise(unsigned char *Frame, // mpi->planes[x]
  54. unsigned char *FramePrev, // pmpi->planes[x]
  55. unsigned char *FrameDest, // dmpi->planes[x]
  56. unsigned char *LineAnt, // vf->priv->Line (width bytes)
  57. int W, int H, int sStride, int pStride, int dStride,
  58. int *Horizontal, int *Vertical, int *Temporal)
  59. {
  60. int X, Y;
  61. int sLineOffs = 0, pLineOffs = 0, dLineOffs = 0;
  62. unsigned char PixelAnt;
  63. /* First pixel has no left nor top neighbor. Only previous frame */
  64. LineAnt[0] = PixelAnt = Frame[0];
  65. FrameDest[0] = LowPass(FramePrev[0], LineAnt[0], Temporal);
  66. /* Fist line has no top neighbor. Only left one for each pixel and
  67. * last frame */
  68. for (X = 1; X < W; X++)
  69. {
  70. PixelAnt = LowPass(PixelAnt, Frame[X], Horizontal);
  71. LineAnt[X] = PixelAnt;
  72. FrameDest[X] = LowPass(FramePrev[X], LineAnt[X], Temporal);
  73. }
  74. for (Y = 1; Y < H; Y++)
  75. {
  76. sLineOffs += sStride, pLineOffs += pStride, dLineOffs += dStride;
  77. /* First pixel on each line doesn't have previous pixel */
  78. PixelAnt = Frame[sLineOffs];
  79. LineAnt[0] = LowPass(LineAnt[0], PixelAnt, Vertical);
  80. FrameDest[dLineOffs] = LowPass(FramePrev[pLineOffs], LineAnt[0], Temporal);
  81. for (X = 1; X < W; X++)
  82. {
  83. /* The rest are normal */
  84. PixelAnt = LowPass(PixelAnt, Frame[sLineOffs+X], Horizontal);
  85. LineAnt[X] = LowPass(LineAnt[X], PixelAnt, Vertical);
  86. FrameDest[dLineOffs+X] = LowPass(FramePrev[pLineOffs+X], LineAnt[X], Temporal);
  87. }
  88. }
  89. }
  90. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  91. int cw= mpi->w >> mpi->chroma_x_shift;
  92. int ch= mpi->h >> mpi->chroma_y_shift;
  93. int W = mpi->w, H = mpi->h;
  94. mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
  95. MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE |
  96. MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
  97. mpi->w,mpi->h);
  98. if(!dmpi) return 0;
  99. if (!vf->priv->pmpi) vf->priv->pmpi=mpi;
  100. deNoise(mpi->planes[0], vf->priv->pmpi->planes[0], dmpi->planes[0],
  101. vf->priv->Line, W, H,
  102. mpi->stride[0], vf->priv->pmpi->stride[0], dmpi->stride[0],
  103. vf->priv->Coefs[0] + 256,
  104. vf->priv->Coefs[0] + 256,
  105. vf->priv->Coefs[1] + 256);
  106. deNoise(mpi->planes[1], vf->priv->pmpi->planes[1], dmpi->planes[1],
  107. vf->priv->Line, cw, ch,
  108. mpi->stride[1], vf->priv->pmpi->stride[1], dmpi->stride[1],
  109. vf->priv->Coefs[2] + 256,
  110. vf->priv->Coefs[2] + 256,
  111. vf->priv->Coefs[3] + 256);
  112. deNoise(mpi->planes[2], vf->priv->pmpi->planes[2], dmpi->planes[2],
  113. vf->priv->Line, cw, ch,
  114. mpi->stride[2], vf->priv->pmpi->stride[2], dmpi->stride[2],
  115. vf->priv->Coefs[2] + 256,
  116. vf->priv->Coefs[2] + 256,
  117. vf->priv->Coefs[3] + 256);
  118. vf->priv->pmpi=dmpi; // save reference image
  119. return vf_next_put_image(vf,dmpi, pts);
  120. }
  121. //===========================================================================//
  122. static int query_format(struct vf_instance *vf, unsigned int fmt){
  123. switch(fmt)
  124. {
  125. case IMGFMT_YV12:
  126. case IMGFMT_I420:
  127. case IMGFMT_IYUV:
  128. case IMGFMT_YVU9:
  129. case IMGFMT_444P:
  130. case IMGFMT_422P:
  131. case IMGFMT_411P:
  132. return vf_next_query_format(vf, fmt);
  133. }
  134. return 0;
  135. }
  136. #define ABS(A) ( (A) > 0 ? (A) : -(A) )
  137. static void PrecalcCoefs(int *Ct, double Dist25)
  138. {
  139. int i;
  140. double Gamma, Simil, C;
  141. Gamma = log(0.25) / log(1.0 - Dist25/255.0);
  142. for (i = -256; i <= 255; i++)
  143. {
  144. Simil = 1.0 - ABS(i) / 255.0;
  145. // Ct[256+i] = lround(pow(Simil, Gamma) * (double)i);
  146. C = pow(Simil, Gamma) * (double)i;
  147. Ct[256+i] = (C<0) ? (C-0.5) : (C+0.5);
  148. }
  149. }
  150. static int vf_open(vf_instance_t *vf, char *args){
  151. double LumSpac, LumTmp, ChromSpac, ChromTmp;
  152. double Param1, Param2, Param3;
  153. vf->config=config;
  154. vf->put_image=put_image;
  155. vf->query_format=query_format;
  156. vf->uninit=uninit;
  157. vf->priv=malloc(sizeof(struct vf_priv_s));
  158. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  159. if (args)
  160. {
  161. switch(sscanf(args, "%lf:%lf:%lf",
  162. &Param1, &Param2, &Param3
  163. ))
  164. {
  165. case 0:
  166. LumSpac = PARAM1_DEFAULT;
  167. LumTmp = PARAM3_DEFAULT;
  168. ChromSpac = PARAM2_DEFAULT;
  169. ChromTmp = LumTmp * ChromSpac / LumSpac;
  170. break;
  171. case 1:
  172. LumSpac = Param1;
  173. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  174. ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
  175. ChromTmp = LumTmp * ChromSpac / LumSpac;
  176. break;
  177. case 2:
  178. LumSpac = Param1;
  179. LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
  180. ChromSpac = Param2;
  181. ChromTmp = LumTmp * ChromSpac / LumSpac;
  182. break;
  183. case 3:
  184. LumSpac = Param1;
  185. LumTmp = Param3;
  186. ChromSpac = Param2;
  187. ChromTmp = LumTmp * ChromSpac / LumSpac;
  188. break;
  189. default:
  190. LumSpac = PARAM1_DEFAULT;
  191. LumTmp = PARAM3_DEFAULT;
  192. ChromSpac = PARAM2_DEFAULT;
  193. ChromTmp = LumTmp * ChromSpac / LumSpac;
  194. }
  195. }
  196. else
  197. {
  198. LumSpac = PARAM1_DEFAULT;
  199. LumTmp = PARAM3_DEFAULT;
  200. ChromSpac = PARAM2_DEFAULT;
  201. ChromTmp = LumTmp * ChromSpac / LumSpac;
  202. }
  203. PrecalcCoefs(vf->priv->Coefs[0], LumSpac);
  204. PrecalcCoefs(vf->priv->Coefs[1], LumTmp);
  205. PrecalcCoefs(vf->priv->Coefs[2], ChromSpac);
  206. PrecalcCoefs(vf->priv->Coefs[3], ChromTmp);
  207. return 1;
  208. }
  209. const vf_info_t vf_info_denoise3d = {
  210. "3D Denoiser (variable lowpass filter)",
  211. "denoise3d",
  212. "Daniel Moreno",
  213. "",
  214. vf_open,
  215. NULL
  216. };
  217. //===========================================================================//