vf_smartblur.c 7.5 KB

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