vf_unsharp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2002 Remi Guyomarch <rguyom@pobox.com>
  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 <math.h>
  25. #include "config.h"
  26. #include "mp_msg.h"
  27. #include "cpudetect.h"
  28. #if HAVE_MALLOC_H
  29. #include <malloc.h>
  30. #endif
  31. #include "img_format.h"
  32. #include "mp_image.h"
  33. #include "vf.h"
  34. #include "libvo/fastmemcpy.h"
  35. #include "libavutil/common.h"
  36. //===========================================================================//
  37. #define MIN_MATRIX_SIZE 3
  38. #define MAX_MATRIX_SIZE 63
  39. typedef struct FilterParam {
  40. int msizeX, msizeY;
  41. double amount;
  42. uint32_t *SC[MAX_MATRIX_SIZE-1];
  43. } FilterParam;
  44. struct vf_priv_s {
  45. FilterParam lumaParam;
  46. FilterParam chromaParam;
  47. unsigned int outfmt;
  48. };
  49. //===========================================================================//
  50. /* This code is based on :
  51. An Efficient algorithm for Gaussian blur using finite-state machines
  52. Frederick M. Waltz and John W. V. Miller
  53. SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII
  54. Originally published Boston, Nov 98
  55. */
  56. static void unsharp( uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp ) {
  57. uint32_t **SC = fp->SC;
  58. uint32_t SR[MAX_MATRIX_SIZE-1], Tmp1, Tmp2;
  59. uint8_t* src2 = src; // avoid gcc warning
  60. int32_t res;
  61. int x, y, z;
  62. int amount = fp->amount * 65536.0;
  63. int stepsX = fp->msizeX/2;
  64. int stepsY = fp->msizeY/2;
  65. int scalebits = (stepsX+stepsY)*2;
  66. int32_t halfscale = 1 << ((stepsX+stepsY)*2-1);
  67. if( !fp->amount ) {
  68. if( src == dst )
  69. return;
  70. if( dstStride == srcStride )
  71. fast_memcpy( dst, src, srcStride*height );
  72. else
  73. for( y=0; y<height; y++, dst+=dstStride, src+=srcStride )
  74. fast_memcpy( dst, src, width );
  75. return;
  76. }
  77. for( y=0; y<2*stepsY; y++ )
  78. memset( SC[y], 0, sizeof(SC[y][0]) * (width+2*stepsX) );
  79. for( y=-stepsY; y<height+stepsY; y++ ) {
  80. if( y < height ) src2 = src;
  81. memset( SR, 0, sizeof(SR[0]) * (2*stepsX-1) );
  82. for( x=-stepsX; x<width+stepsX; x++ ) {
  83. Tmp1 = x<=0 ? src2[0] : x>=width ? src2[width-1] : src2[x];
  84. for( z=0; z<stepsX*2; z+=2 ) {
  85. Tmp2 = SR[z+0] + Tmp1; SR[z+0] = Tmp1;
  86. Tmp1 = SR[z+1] + Tmp2; SR[z+1] = Tmp2;
  87. }
  88. for( z=0; z<stepsY*2; z+=2 ) {
  89. Tmp2 = SC[z+0][x+stepsX] + Tmp1; SC[z+0][x+stepsX] = Tmp1;
  90. Tmp1 = SC[z+1][x+stepsX] + Tmp2; SC[z+1][x+stepsX] = Tmp2;
  91. }
  92. if( x>=stepsX && y>=stepsY ) {
  93. uint8_t* srx = src - stepsY*srcStride + x - stepsX;
  94. uint8_t* dsx = dst - stepsY*dstStride + x - stepsX;
  95. res = (int32_t)*srx + ( ( ( (int32_t)*srx - (int32_t)((Tmp1+halfscale) >> scalebits) ) * amount ) >> 16 );
  96. *dsx = res>255 ? 255 : res<0 ? 0 : (uint8_t)res;
  97. }
  98. }
  99. if( y >= 0 ) {
  100. dst += dstStride;
  101. src += srcStride;
  102. }
  103. }
  104. }
  105. //===========================================================================//
  106. static int config( struct vf_instance *vf,
  107. int width, int height, int d_width, int d_height,
  108. unsigned int flags, unsigned int outfmt ) {
  109. int z, stepsX, stepsY;
  110. FilterParam *fp;
  111. const char *effect;
  112. // allocate buffers
  113. fp = &vf->priv->lumaParam;
  114. effect = fp->amount == 0 ? "don't touch" : fp->amount < 0 ? "blur" : "sharpen";
  115. mp_msg( MSGT_VFILTER, MSGL_INFO, "unsharp: %dx%d:%0.2f (%s luma) \n", fp->msizeX, fp->msizeY, fp->amount, effect );
  116. memset( fp->SC, 0, sizeof( fp->SC ) );
  117. stepsX = fp->msizeX/2;
  118. stepsY = fp->msizeY/2;
  119. for( z=0; z<2*stepsY; z++ )
  120. fp->SC[z] = av_malloc(sizeof(*(fp->SC[z])) * (width+2*stepsX));
  121. fp = &vf->priv->chromaParam;
  122. effect = fp->amount == 0 ? "don't touch" : fp->amount < 0 ? "blur" : "sharpen";
  123. mp_msg( MSGT_VFILTER, MSGL_INFO, "unsharp: %dx%d:%0.2f (%s chroma)\n", fp->msizeX, fp->msizeY, fp->amount, effect );
  124. memset( fp->SC, 0, sizeof( fp->SC ) );
  125. stepsX = fp->msizeX/2;
  126. stepsY = fp->msizeY/2;
  127. for( z=0; z<2*stepsY; z++ )
  128. fp->SC[z] = av_malloc(sizeof(*(fp->SC[z])) * (width+2*stepsX));
  129. return vf_next_config( vf, width, height, d_width, d_height, flags, outfmt );
  130. }
  131. //===========================================================================//
  132. static void get_image( struct vf_instance *vf, mp_image_t *mpi ) {
  133. if( mpi->flags & MP_IMGFLAG_PRESERVE )
  134. return; // don't change
  135. if( mpi->imgfmt!=vf->priv->outfmt )
  136. return; // colorspace differ
  137. vf->dmpi = vf_get_image( vf->next, mpi->imgfmt, mpi->type, mpi->flags, mpi->w, mpi->h );
  138. mpi->planes[0] = vf->dmpi->planes[0];
  139. mpi->stride[0] = vf->dmpi->stride[0];
  140. mpi->width = vf->dmpi->width;
  141. if( mpi->flags & MP_IMGFLAG_PLANAR ) {
  142. mpi->planes[1] = vf->dmpi->planes[1];
  143. mpi->planes[2] = vf->dmpi->planes[2];
  144. mpi->stride[1] = vf->dmpi->stride[1];
  145. mpi->stride[2] = vf->dmpi->stride[2];
  146. }
  147. mpi->flags |= MP_IMGFLAG_DIRECT;
  148. }
  149. static int put_image( struct vf_instance *vf, mp_image_t *mpi, double pts) {
  150. mp_image_t *dmpi;
  151. if( !(mpi->flags & MP_IMGFLAG_DIRECT) )
  152. // no DR, so get a new image! hope we'll get DR buffer:
  153. vf->dmpi = vf_get_image( vf->next,vf->priv->outfmt, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, mpi->w, mpi->h);
  154. dmpi= vf->dmpi;
  155. unsharp( dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, &vf->priv->lumaParam );
  156. unsharp( dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, &vf->priv->chromaParam );
  157. unsharp( dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, &vf->priv->chromaParam );
  158. vf_clone_mpi_attributes(dmpi, mpi);
  159. #if HAVE_MMX
  160. if(gCpuCaps.hasMMX)
  161. __asm__ volatile ("emms\n\t");
  162. #endif
  163. #if HAVE_MMX2
  164. if(gCpuCaps.hasMMX2)
  165. __asm__ volatile ("sfence\n\t");
  166. #endif
  167. return vf_next_put_image( vf, dmpi, pts);
  168. }
  169. static void uninit( struct vf_instance *vf ) {
  170. unsigned int z;
  171. FilterParam *fp;
  172. if( !vf->priv ) return;
  173. fp = &vf->priv->lumaParam;
  174. for( z=0; z<sizeof(fp->SC)/sizeof(fp->SC[0]); z++ ) {
  175. av_free( fp->SC[z] );
  176. fp->SC[z] = NULL;
  177. }
  178. fp = &vf->priv->chromaParam;
  179. for( z=0; z<sizeof(fp->SC)/sizeof(fp->SC[0]); z++ ) {
  180. av_free( fp->SC[z] );
  181. fp->SC[z] = NULL;
  182. }
  183. free( vf->priv );
  184. vf->priv = NULL;
  185. }
  186. //===========================================================================//
  187. static int query_format( struct vf_instance *vf, unsigned int fmt ) {
  188. switch(fmt) {
  189. case IMGFMT_YV12:
  190. case IMGFMT_I420:
  191. case IMGFMT_IYUV:
  192. return vf_next_query_format( vf, vf->priv->outfmt );
  193. }
  194. return 0;
  195. }
  196. //===========================================================================//
  197. static void parse( FilterParam *fp, char* args ) {
  198. // l7x5:0.8:c3x3:-0.2
  199. char *z;
  200. char *pos = args;
  201. char *max = args + strlen(args);
  202. // parse matrix sizes
  203. fp->msizeX = ( pos && pos+1<max ) ? atoi( pos+1 ) : 0;
  204. z = strchr( pos+1, 'x' );
  205. fp->msizeY = ( z && z+1<max ) ? atoi( pos=z+1 ) : fp->msizeX;
  206. // min/max & odd
  207. fp->msizeX = 1 | av_clip(fp->msizeX, MIN_MATRIX_SIZE, MAX_MATRIX_SIZE);
  208. fp->msizeY = 1 | av_clip(fp->msizeY, MIN_MATRIX_SIZE, MAX_MATRIX_SIZE);
  209. // parse amount
  210. pos = strchr( pos+1, ':' );
  211. fp->amount = ( pos && pos+1<max ) ? atof( pos+1 ) : 0;
  212. }
  213. //===========================================================================//
  214. static const unsigned int fmt_list[] = {
  215. IMGFMT_YV12,
  216. IMGFMT_I420,
  217. IMGFMT_IYUV,
  218. 0
  219. };
  220. static int vf_open( vf_instance_t *vf, char *args ) {
  221. vf->config = config;
  222. vf->put_image = put_image;
  223. vf->get_image = get_image;
  224. vf->query_format = query_format;
  225. vf->uninit = uninit;
  226. vf->priv = malloc( sizeof(struct vf_priv_s) );
  227. memset( vf->priv, 0, sizeof(struct vf_priv_s) );
  228. if( args ) {
  229. char *args2 = strchr( args, 'l' );
  230. if( args2 )
  231. parse( &vf->priv->lumaParam, args2 );
  232. else {
  233. vf->priv->lumaParam.amount =
  234. vf->priv->lumaParam.msizeX =
  235. vf->priv->lumaParam.msizeY = 0;
  236. }
  237. args2 = strchr( args, 'c' );
  238. if( args2 )
  239. parse( &vf->priv->chromaParam, args2 );
  240. else {
  241. vf->priv->chromaParam.amount =
  242. vf->priv->chromaParam.msizeX =
  243. vf->priv->chromaParam.msizeY = 0;
  244. }
  245. if( !vf->priv->lumaParam.msizeX && !vf->priv->chromaParam.msizeX )
  246. return 0; // nothing to do
  247. }
  248. // check csp:
  249. vf->priv->outfmt = vf_match_csp( &vf->next, fmt_list, IMGFMT_YV12 );
  250. if( !vf->priv->outfmt ) {
  251. uninit( vf );
  252. return 0; // no csp match :(
  253. }
  254. return 1;
  255. }
  256. const vf_info_t vf_info_unsharp = {
  257. "unsharp mask & gaussian blur",
  258. "unsharp",
  259. "Remi Guyomarch",
  260. "",
  261. vf_open,
  262. NULL
  263. };
  264. //===========================================================================//