vf_ow.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at>
  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. /**
  21. * @todo try to change to int
  22. * @todo try lifting based implementation
  23. * @todo optimize optimize optimize
  24. * @todo hard tresholding
  25. * @todo use QP to decide filter strength
  26. * @todo wavelet normalization / least squares optimal signal vs. noise thresholds
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <inttypes.h>
  31. #include <math.h>
  32. #include "mp_msg.h"
  33. #include "img_format.h"
  34. #include "mp_image.h"
  35. #include "vf.h"
  36. //===========================================================================//
  37. static const uint8_t __attribute__((aligned(8))) dither[8][8]={
  38. { 0, 48, 12, 60, 3, 51, 15, 63, },
  39. { 32, 16, 44, 28, 35, 19, 47, 31, },
  40. { 8, 56, 4, 52, 11, 59, 7, 55, },
  41. { 40, 24, 36, 20, 43, 27, 39, 23, },
  42. { 2, 50, 14, 62, 1, 49, 13, 61, },
  43. { 34, 18, 46, 30, 33, 17, 45, 29, },
  44. { 10, 58, 6, 54, 9, 57, 5, 53, },
  45. { 42, 26, 38, 22, 41, 25, 37, 21, },
  46. };
  47. //FIXME the above is duplicated in many filters
  48. struct vf_priv_s {
  49. float strength[2];
  50. float delta;
  51. int mode;
  52. int depth;
  53. float *plane[16][4];
  54. int stride;
  55. };
  56. #define S 1.41421356237 //sqrt(2)
  57. static const double coeff[2][5]={
  58. {
  59. 0.6029490182363579 *S,
  60. 0.2668641184428723 *S,
  61. -0.07822326652898785 *S,
  62. -0.01686411844287495 *S,
  63. 0.02674875741080976 *S
  64. },{
  65. 1.115087052456994 /S,
  66. -0.5912717631142470 /S,
  67. -0.05754352622849957 /S,
  68. 0.09127176311424948 /S
  69. }
  70. };
  71. static const double icoeff[2][5]={
  72. {
  73. 1.115087052456994 /S,
  74. 0.5912717631142470 /S,
  75. -0.05754352622849957 /S,
  76. -0.09127176311424948 /S
  77. },{
  78. 0.6029490182363579 *S,
  79. -0.2668641184428723 *S,
  80. -0.07822326652898785 *S,
  81. 0.01686411844287495 *S,
  82. 0.02674875741080976 *S
  83. }
  84. };
  85. #undef S
  86. static inline int mirror(int x, int w){
  87. while((unsigned)x > (unsigned)w){
  88. x=-x;
  89. if(x<0) x+= 2*w;
  90. }
  91. return x;
  92. }
  93. static inline void decompose(float *dstL, float *dstH, float *src, int stride, int w){
  94. int x, i;
  95. for(x=0; x<w; x++){
  96. double sumL= src[x*stride] * coeff[0][0];
  97. double sumH= src[x*stride] * coeff[1][0];
  98. for(i=1; i<=4; i++){
  99. double s= (src[mirror(x-i, w-1)*stride] + src[mirror(x+i, w-1)*stride]);
  100. sumL+= coeff[0][i]*s;
  101. sumH+= coeff[1][i]*s;
  102. }
  103. dstL[x*stride]= sumL;
  104. dstH[x*stride]= sumH;
  105. }
  106. }
  107. static inline void compose(float *dst, float *srcL, float *srcH, int stride, int w){
  108. int x, i;
  109. for(x=0; x<w; x++){
  110. double sumL= srcL[x*stride] * icoeff[0][0];
  111. double sumH= srcH[x*stride] * icoeff[1][0];
  112. for(i=1; i<=4; i++){
  113. int x0= mirror(x-i, w-1)*stride;
  114. int x1= mirror(x+i, w-1)*stride;
  115. sumL+= icoeff[0][i]*(srcL[x0] + srcL[x1]);
  116. sumH+= icoeff[1][i]*(srcH[x0] + srcH[x1]);
  117. }
  118. dst[x*stride]= (sumL + sumH)*0.5;
  119. }
  120. }
  121. static inline void decompose2D(float *dstL, float *dstH, float *src, int xstride, int ystride, int step, int w, int h){
  122. int y, x;
  123. for(y=0; y<h; y++)
  124. for(x=0; x<step; x++)
  125. decompose(dstL + ystride*y + xstride*x, dstH + ystride*y + xstride*x, src + ystride*y + xstride*x, step*xstride, (w-x+step-1)/step);
  126. }
  127. static inline void compose2D(float *dst, float *srcL, float *srcH, int xstride, int ystride, int step, int w, int h){
  128. int y, x;
  129. for(y=0; y<h; y++)
  130. for(x=0; x<step; x++)
  131. compose(dst + ystride*y + xstride*x, srcL + ystride*y + xstride*x, srcH + ystride*y + xstride*x, step*xstride, (w-x+step-1)/step);
  132. }
  133. static void decompose2D2(float *dst[4], float *src, float *temp[2], int stride, int step, int w, int h){
  134. decompose2D(temp[0], temp[1], src , 1, stride, step , w, h);
  135. decompose2D( dst[0], dst[1], temp[0], stride, 1, step , h, w);
  136. decompose2D( dst[2], dst[3], temp[1], stride, 1, step , h, w);
  137. }
  138. static void compose2D2(float *dst, float *src[4], float *temp[2], int stride, int step, int w, int h){
  139. compose2D(temp[0], src[0], src[1], stride, 1, step , h, w);
  140. compose2D(temp[1], src[2], src[3], stride, 1, step , h, w);
  141. compose2D(dst , temp[0], temp[1], 1, stride, step , w, h);
  142. }
  143. static void filter(struct vf_priv_s *p, uint8_t *dst, uint8_t *src, int dst_stride, int src_stride, int width, int height, int is_luma){
  144. int x,y, i, j;
  145. // double sum=0;
  146. double s= p->strength[!is_luma];
  147. int depth= p->depth;
  148. while(1<<depth > width || 1<<depth > height)
  149. depth--;
  150. for(y=0; y<height; y++)
  151. for(x=0; x<width; x++)
  152. p->plane[0][0][x + y*p->stride]= src[x + y*src_stride];
  153. for(i=0; i<depth; i++){
  154. decompose2D2(p->plane[i+1], p->plane[i][0], p->plane[0]+1,p->stride, 1<<i, width, height);
  155. }
  156. for(i=0; i<depth; i++){
  157. for(j=1; j<4; j++){
  158. for(y=0; y<height; y++){
  159. for(x=0; x<width; x++){
  160. double v= p->plane[i+1][j][x + y*p->stride];
  161. if (v> s) v-=s;
  162. else if(v<-s) v+=s;
  163. else v =0;
  164. p->plane[i+1][j][x + y*p->stride]= v;
  165. }
  166. }
  167. }
  168. }
  169. for(i=depth-1; i>=0; i--){
  170. compose2D2(p->plane[i][0], p->plane[i+1], p->plane[0]+1, p->stride, 1<<i, width, height);
  171. }
  172. for(y=0; y<height; y++)
  173. for(x=0; x<width; x++){
  174. i= p->plane[0][0][x + y*p->stride] + dither[x&7][y&7]*(1.0/64) + 1.0/128; //yes the rounding is insane but optimal :)
  175. // double e= i - src[x + y*src_stride];
  176. // sum += e*e;
  177. if((unsigned)i > 255U) i= ~(i>>31);
  178. dst[x + y*dst_stride]= i;
  179. }
  180. // printf("%f\n", sum/height/width);
  181. }
  182. static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt){
  183. int h= (height+15)&(~15);
  184. int i,j;
  185. vf->priv->stride= (width+15)&(~15);
  186. for(j=0; j<4; j++){
  187. for(i=0; i<=vf->priv->depth; i++)
  188. vf->priv->plane[i][j]= malloc(vf->priv->stride*h*sizeof(vf->priv->plane[0][0][0]));
  189. }
  190. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  191. }
  192. static void get_image(struct vf_instance *vf, mp_image_t *mpi){
  193. if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
  194. // ok, we can do pp in-place (or pp disabled):
  195. vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
  196. mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
  197. mpi->planes[0]=vf->dmpi->planes[0];
  198. mpi->stride[0]=vf->dmpi->stride[0];
  199. mpi->width=vf->dmpi->width;
  200. if(mpi->flags&MP_IMGFLAG_PLANAR){
  201. mpi->planes[1]=vf->dmpi->planes[1];
  202. mpi->planes[2]=vf->dmpi->planes[2];
  203. mpi->stride[1]=vf->dmpi->stride[1];
  204. mpi->stride[2]=vf->dmpi->stride[2];
  205. }
  206. mpi->flags|=MP_IMGFLAG_DIRECT;
  207. }
  208. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  209. mp_image_t *dmpi;
  210. if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
  211. // no DR, so get a new image! hope we'll get DR buffer:
  212. dmpi=vf_get_image(vf->next,mpi->imgfmt,
  213. MP_IMGTYPE_TEMP,
  214. MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
  215. mpi->width,mpi->height);
  216. vf_clone_mpi_attributes(dmpi, mpi);
  217. }else{
  218. dmpi=vf->dmpi;
  219. }
  220. filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, 1);
  221. filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, 0);
  222. filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, 0);
  223. return vf_next_put_image(vf,dmpi, pts);
  224. }
  225. static void uninit(struct vf_instance *vf){
  226. int i,j;
  227. if(!vf->priv) return;
  228. for(j=0; j<4; j++){
  229. for(i=0; i<16; i++){
  230. free(vf->priv->plane[i][j]);
  231. vf->priv->plane[i][j]= NULL;
  232. }
  233. }
  234. free(vf->priv);
  235. vf->priv=NULL;
  236. }
  237. //===========================================================================//
  238. static int query_format(struct vf_instance *vf, unsigned int fmt){
  239. switch(fmt){
  240. case IMGFMT_YVU9:
  241. case IMGFMT_IF09:
  242. case IMGFMT_YV12:
  243. case IMGFMT_I420:
  244. case IMGFMT_IYUV:
  245. case IMGFMT_CLPL:
  246. case IMGFMT_Y800:
  247. case IMGFMT_Y8:
  248. case IMGFMT_444P:
  249. case IMGFMT_422P:
  250. case IMGFMT_411P:
  251. return vf_next_query_format(vf,fmt);
  252. }
  253. return 0;
  254. }
  255. static int vf_open(vf_instance_t *vf, char *args){
  256. vf->config=config;
  257. vf->put_image=put_image;
  258. vf->get_image=get_image;
  259. vf->query_format=query_format;
  260. vf->uninit=uninit;
  261. vf->priv=malloc(sizeof(struct vf_priv_s));
  262. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  263. vf->priv->depth= 8;
  264. vf->priv->strength[0]= 1.0;
  265. vf->priv->strength[1]= 1.0;
  266. vf->priv->delta= 1.0;
  267. if (args) sscanf(args, "%d:%f:%f:%d:%f", &vf->priv->depth,
  268. &vf->priv->strength[0],
  269. &vf->priv->strength[1],
  270. &vf->priv->mode,
  271. &vf->priv->delta);
  272. return 1;
  273. }
  274. const vf_info_t vf_info_ow = {
  275. "overcomplete wavelet denoiser",
  276. "ow",
  277. "Michael Niedermayer",
  278. "",
  279. vf_open,
  280. NULL
  281. };