vf_sab.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (C) 2002 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. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <inttypes.h>
  24. #include <assert.h>
  25. #include "config.h"
  26. #include "mp_msg.h"
  27. #if HAVE_MALLOC_H
  28. #include <malloc.h>
  29. #endif
  30. #include "libavutil/avutil.h"
  31. #include "img_format.h"
  32. #include "mp_image.h"
  33. #include "vf.h"
  34. #include "libswscale/swscale.h"
  35. #include "vf_scale.h"
  36. //===========================================================================//
  37. typedef struct FilterParam{
  38. float radius;
  39. float preFilterRadius;
  40. float strength;
  41. float quality;
  42. struct SwsContext *preFilterContext;
  43. uint8_t *preFilterBuf;
  44. int preFilterStride;
  45. int distWidth;
  46. int distStride;
  47. int *distCoeff;
  48. int colorDiffCoeff[512];
  49. }FilterParam;
  50. struct vf_priv_s {
  51. FilterParam luma;
  52. FilterParam chroma;
  53. };
  54. /***************************************************************************/
  55. //FIXME stupid code duplication
  56. static void getSubSampleFactors(int *h, int *v, int format){
  57. switch(format){
  58. case IMGFMT_YV12:
  59. case IMGFMT_I420:
  60. *h=1;
  61. *v=1;
  62. break;
  63. case IMGFMT_YVU9:
  64. *h=2;
  65. *v=2;
  66. break;
  67. case IMGFMT_444P:
  68. *h=0;
  69. *v=0;
  70. break;
  71. case IMGFMT_422P:
  72. *h=1;
  73. *v=0;
  74. break;
  75. case IMGFMT_411P:
  76. *h=2;
  77. *v=0;
  78. break;
  79. }
  80. }
  81. static int allocStuff(FilterParam *f, int width, int height){
  82. int stride= (width+7)&~7;
  83. SwsVector *vec;
  84. SwsFilter swsF;
  85. int i,x,y;
  86. f->preFilterBuf= av_malloc(stride*height);
  87. f->preFilterStride= stride;
  88. vec = sws_getGaussianVec(f->preFilterRadius, f->quality);
  89. swsF.lumH= swsF.lumV= vec;
  90. swsF.chrH= swsF.chrV= NULL;
  91. f->preFilterContext= sws_getContext(
  92. width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, SWS_POINT, &swsF, NULL, NULL);
  93. sws_freeVec(vec);
  94. vec = sws_getGaussianVec(f->strength, 5.0);
  95. for(i=0; i<512; i++){
  96. double d;
  97. int index= i-256 + vec->length/2;
  98. if(index<0 || index>=vec->length) d= 0.0;
  99. else d= vec->coeff[index];
  100. f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
  101. }
  102. sws_freeVec(vec);
  103. vec = sws_getGaussianVec(f->radius, f->quality);
  104. f->distWidth= vec->length;
  105. f->distStride= (vec->length+7)&~7;
  106. f->distCoeff= av_malloc(f->distWidth*f->distStride*sizeof(int32_t));
  107. for(y=0; y<vec->length; y++){
  108. for(x=0; x<vec->length; x++){
  109. double d= vec->coeff[x] * vec->coeff[y];
  110. f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5);
  111. // if(y==vec->length/2)
  112. // printf("%6d ", f->distCoeff[x + y*f->distStride]);
  113. }
  114. }
  115. sws_freeVec(vec);
  116. return 0;
  117. }
  118. static int config(struct vf_instance *vf,
  119. int width, int height, int d_width, int d_height,
  120. unsigned int flags, unsigned int outfmt){
  121. int sw, sh;
  122. //__asm__ volatile("emms\n\t");
  123. allocStuff(&vf->priv->luma, width, height);
  124. getSubSampleFactors(&sw, &sh, outfmt);
  125. allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
  126. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  127. }
  128. static void freeBuffers(FilterParam *f){
  129. if(f->preFilterContext) sws_freeContext(f->preFilterContext);
  130. f->preFilterContext=NULL;
  131. av_free(f->preFilterBuf);
  132. f->preFilterBuf=NULL;
  133. av_free(f->distCoeff);
  134. f->distCoeff=NULL;
  135. }
  136. static void uninit(struct vf_instance *vf){
  137. if(!vf->priv) return;
  138. freeBuffers(&vf->priv->luma);
  139. freeBuffers(&vf->priv->chroma);
  140. free(vf->priv);
  141. vf->priv=NULL;
  142. }
  143. static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
  144. int x, y;
  145. FilterParam f= *fp;
  146. const int radius= f.distWidth/2;
  147. const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
  148. uint8_t *dstArray[MP_MAX_PLANES]= {f.preFilterBuf};
  149. int srcStrideArray[MP_MAX_PLANES]= {srcStride};
  150. int dstStrideArray[MP_MAX_PLANES]= {f.preFilterStride};
  151. // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
  152. sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
  153. for(y=0; y<h; y++){
  154. for(x=0; x<w; x++){
  155. int sum=0;
  156. int div=0;
  157. int dy;
  158. const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
  159. #if 0
  160. const int srcVal= src[x + y*srcStride];
  161. if((x/32)&1){
  162. dst[x + y*dstStride]= srcVal;
  163. if(y%32==0) dst[x + y*dstStride]= 0;
  164. continue;
  165. }
  166. #endif
  167. if(x >= radius && x < w - radius){
  168. for(dy=0; dy<radius*2+1; dy++){
  169. int dx;
  170. int iy= y+dy - radius;
  171. if (iy<0) iy= -iy;
  172. else if(iy>=h) iy= h+h-iy-1;
  173. for(dx=0; dx<radius*2+1; dx++){
  174. const int ix= x+dx - radius;
  175. int factor;
  176. factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
  177. *f.distCoeff[dx + dy*f.distStride];
  178. sum+= src[ix + iy*srcStride] *factor;
  179. div+= factor;
  180. }
  181. }
  182. }else{
  183. for(dy=0; dy<radius*2+1; dy++){
  184. int dx;
  185. int iy= y+dy - radius;
  186. if (iy<0) iy= -iy;
  187. else if(iy>=h) iy= h+h-iy-1;
  188. for(dx=0; dx<radius*2+1; dx++){
  189. int ix= x+dx - radius;
  190. int factor;
  191. if (ix<0) ix= -ix;
  192. else if(ix>=w) ix= w+w-ix-1;
  193. factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
  194. *f.distCoeff[dx + dy*f.distStride];
  195. sum+= src[ix + iy*srcStride] *factor;
  196. div+= factor;
  197. }
  198. }
  199. }
  200. dst[x + y*dstStride]= (sum + div/2)/div;
  201. }
  202. }
  203. }
  204. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  205. int cw= mpi->w >> mpi->chroma_x_shift;
  206. int ch= mpi->h >> mpi->chroma_y_shift;
  207. mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
  208. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  209. mpi->w,mpi->h);
  210. assert(mpi->flags&MP_IMGFLAG_PLANAR);
  211. blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
  212. blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
  213. blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
  214. return vf_next_put_image(vf,dmpi, pts);
  215. }
  216. //===========================================================================//
  217. static int query_format(struct vf_instance *vf, unsigned int fmt){
  218. switch(fmt)
  219. {
  220. case IMGFMT_YV12:
  221. case IMGFMT_I420:
  222. case IMGFMT_IYUV:
  223. case IMGFMT_YVU9:
  224. case IMGFMT_444P:
  225. case IMGFMT_422P:
  226. case IMGFMT_411P:
  227. return vf_next_query_format(vf, fmt);
  228. }
  229. return 0;
  230. }
  231. static int vf_open(vf_instance_t *vf, char *args){
  232. int e;
  233. vf->config=config;
  234. vf->put_image=put_image;
  235. // vf->get_image=get_image;
  236. vf->query_format=query_format;
  237. vf->uninit=uninit;
  238. vf->priv=malloc(sizeof(struct vf_priv_s));
  239. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  240. if(args==NULL) return 0;
  241. e=sscanf(args, "%f:%f:%f:%f:%f:%f",
  242. &vf->priv->luma.radius,
  243. &vf->priv->luma.preFilterRadius,
  244. &vf->priv->luma.strength,
  245. &vf->priv->chroma.radius,
  246. &vf->priv->chroma.preFilterRadius,
  247. &vf->priv->chroma.strength
  248. );
  249. vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
  250. if(e==3){
  251. vf->priv->chroma.radius= vf->priv->luma.radius;
  252. vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius;
  253. vf->priv->chroma.strength= vf->priv->luma.strength;
  254. }else if(e!=6)
  255. return 0;
  256. // if(vf->priv->luma.radius < 0) return 0;
  257. // if(vf->priv->chroma.radius < 0) return 0;
  258. return 1;
  259. }
  260. const vf_info_t vf_info_sab = {
  261. "shape adaptive blur",
  262. "sab",
  263. "Michael Niedermayer",
  264. "",
  265. vf_open,
  266. NULL
  267. };
  268. //===========================================================================//