vf_sab.c 9.1 KB

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