vf_smartblur.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "mp_msg.h"
  26. #include "libavutil/avutil.h"
  27. #include "img_format.h"
  28. #include "mp_image.h"
  29. #include "vf.h"
  30. #include "libswscale/swscale.h"
  31. #include "vf_scale.h"
  32. //===========================================================================//
  33. typedef struct FilterParam{
  34. float radius;
  35. float strength;
  36. int threshold;
  37. float quality;
  38. struct SwsContext *filterContext;
  39. }FilterParam;
  40. struct vf_priv_s {
  41. FilterParam luma;
  42. FilterParam chroma;
  43. };
  44. /***************************************************************************/
  45. //FIXME stupid code duplication
  46. static void getSubSampleFactors(int *h, int *v, int format){
  47. switch(format){
  48. case IMGFMT_YV12:
  49. case IMGFMT_I420:
  50. *h=1;
  51. *v=1;
  52. break;
  53. case IMGFMT_YVU9:
  54. *h=2;
  55. *v=2;
  56. break;
  57. case IMGFMT_444P:
  58. *h=0;
  59. *v=0;
  60. break;
  61. case IMGFMT_422P:
  62. *h=1;
  63. *v=0;
  64. break;
  65. case IMGFMT_411P:
  66. *h=2;
  67. *v=0;
  68. break;
  69. }
  70. }
  71. static int allocStuff(FilterParam *f, int width, int height){
  72. SwsVector *vec;
  73. SwsFilter swsF;
  74. vec = sws_getGaussianVec(f->radius, f->quality);
  75. sws_scaleVec(vec, f->strength);
  76. vec->coeff[vec->length/2]+= 1.0 - f->strength;
  77. swsF.lumH= swsF.lumV= vec;
  78. swsF.chrH= swsF.chrV= NULL;
  79. f->filterContext= sws_getContext(
  80. width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, SWS_BICUBIC, &swsF, NULL, NULL);
  81. sws_freeVec(vec);
  82. return 0;
  83. }
  84. static int config(struct vf_instance *vf,
  85. int width, int height, int d_width, int d_height,
  86. unsigned int flags, unsigned int outfmt){
  87. int sw, sh;
  88. allocStuff(&vf->priv->luma, width, height);
  89. getSubSampleFactors(&sw, &sh, outfmt);
  90. allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
  91. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  92. }
  93. static void freeBuffers(FilterParam *f){
  94. if(f->filterContext) sws_freeContext(f->filterContext);
  95. f->filterContext=NULL;
  96. }
  97. static void uninit(struct vf_instance *vf){
  98. if(!vf->priv) return;
  99. freeBuffers(&vf->priv->luma);
  100. freeBuffers(&vf->priv->chroma);
  101. free(vf->priv);
  102. vf->priv=NULL;
  103. }
  104. static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
  105. int x, y;
  106. FilterParam f= *fp;
  107. const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
  108. uint8_t *dstArray[MP_MAX_PLANES]= {dst};
  109. int srcStrideArray[MP_MAX_PLANES]= {srcStride};
  110. int dstStrideArray[MP_MAX_PLANES]= {dstStride};
  111. sws_scale(f.filterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
  112. if(f.threshold > 0){
  113. for(y=0; y<h; y++){
  114. for(x=0; x<w; x++){
  115. const int orig= src[x + y*srcStride];
  116. const int filtered= dst[x + y*dstStride];
  117. const int diff= orig - filtered;
  118. if(diff > 0){
  119. if(diff > 2*f.threshold){
  120. dst[x + y*dstStride]= orig;
  121. }else if(diff > f.threshold){
  122. dst[x + y*dstStride]= filtered + diff - f.threshold;
  123. }
  124. }else{
  125. if(-diff > 2*f.threshold){
  126. dst[x + y*dstStride]= orig;
  127. }else if(-diff > f.threshold){
  128. dst[x + y*dstStride]= filtered + diff + f.threshold;
  129. }
  130. }
  131. }
  132. }
  133. }else if(f.threshold < 0){
  134. for(y=0; y<h; y++){
  135. for(x=0; x<w; x++){
  136. const int orig= src[x + y*srcStride];
  137. const int filtered= dst[x + y*dstStride];
  138. const int diff= orig - filtered;
  139. if(diff > 0){
  140. if(diff > -2*f.threshold){
  141. }else if(diff > -f.threshold){
  142. dst[x + y*dstStride]= orig - diff - f.threshold;
  143. }else
  144. dst[x + y*dstStride]= orig;
  145. }else{
  146. if(diff < 2*f.threshold){
  147. }else if(diff < f.threshold){
  148. dst[x + y*dstStride]= orig - diff + f.threshold;
  149. }else
  150. dst[x + y*dstStride]= orig;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  157. int cw= mpi->w >> mpi->chroma_x_shift;
  158. int ch= mpi->h >> mpi->chroma_y_shift;
  159. int threshold = vf->priv->luma.threshold || vf->priv->chroma.threshold;
  160. mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
  161. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|
  162. (threshold ? MP_IMGFLAG_READABLE : 0),
  163. mpi->w,mpi->h);
  164. assert(mpi->flags&MP_IMGFLAG_PLANAR);
  165. blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
  166. blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
  167. blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
  168. return vf_next_put_image(vf,dmpi, pts);
  169. }
  170. //===========================================================================//
  171. static int query_format(struct vf_instance *vf, unsigned int fmt){
  172. switch(fmt)
  173. {
  174. case IMGFMT_YV12:
  175. case IMGFMT_I420:
  176. case IMGFMT_IYUV:
  177. case IMGFMT_YVU9:
  178. case IMGFMT_444P:
  179. case IMGFMT_422P:
  180. case IMGFMT_411P:
  181. return vf_next_query_format(vf, fmt);
  182. }
  183. return 0;
  184. }
  185. static int vf_open(vf_instance_t *vf, char *args){
  186. int e;
  187. vf->config=config;
  188. vf->put_image=put_image;
  189. // vf->get_image=get_image;
  190. vf->query_format=query_format;
  191. vf->uninit=uninit;
  192. vf->priv=malloc(sizeof(struct vf_priv_s));
  193. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  194. if(args==NULL) return 0;
  195. e=sscanf(args, "%f:%f:%d:%f:%f:%d",
  196. &vf->priv->luma.radius,
  197. &vf->priv->luma.strength,
  198. &vf->priv->luma.threshold,
  199. &vf->priv->chroma.radius,
  200. &vf->priv->chroma.strength,
  201. &vf->priv->chroma.threshold
  202. );
  203. vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
  204. if(e==3){
  205. vf->priv->chroma.radius= vf->priv->luma.radius;
  206. vf->priv->chroma.strength= vf->priv->luma.strength;
  207. vf->priv->chroma.threshold = vf->priv->luma.threshold;
  208. }else if(e!=6)
  209. return 0;
  210. return 1;
  211. }
  212. const vf_info_t vf_info_smartblur = {
  213. "smart blur",
  214. "smartblur",
  215. "Michael Niedermayer",
  216. "",
  217. vf_open,
  218. NULL
  219. };
  220. //===========================================================================//