vf_geq.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2006 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 <math.h>
  24. #include <inttypes.h>
  25. #include "config.h"
  26. #include "mp_msg.h"
  27. #include "cpudetect.h"
  28. #include "img_format.h"
  29. #include "mp_image.h"
  30. #include "vf.h"
  31. #include "libavcodec/avcodec.h"
  32. #include "libavutil/eval.h"
  33. struct vf_priv_s {
  34. AVExpr * e[3];
  35. int framenum;
  36. mp_image_t *mpi;
  37. };
  38. static int config(struct vf_instance *vf,
  39. int width, int height, int d_width, int d_height,
  40. unsigned int flags, unsigned int outfmt){
  41. return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  42. }
  43. static inline double getpix(struct vf_instance *vf, double x, double y, int plane){
  44. int xi, yi;
  45. mp_image_t *mpi= vf->priv->mpi;
  46. int stride= mpi->stride[plane];
  47. uint8_t *src= mpi->planes[plane];
  48. xi=x= FFMIN(FFMAX(x, 0), (mpi->w >> (plane ? mpi->chroma_x_shift : 0))-1);
  49. yi=y= FFMIN(FFMAX(y, 0), (mpi->h >> (plane ? mpi->chroma_y_shift : 0))-1);
  50. x-=xi;
  51. y-=yi;
  52. return
  53. (1-y)*((1-x)*src[xi + yi * stride] + x*src[xi + 1 + yi * stride])
  54. + y *((1-x)*src[xi + (yi+1) * stride] + x*src[xi + 1 + (yi+1) * stride]);
  55. }
  56. //FIXME cubic interpolate
  57. //FIXME keep the last few frames
  58. static double lum(void *vf, double x, double y){
  59. return getpix(vf, x, y, 0);
  60. }
  61. static double cb(void *vf, double x, double y){
  62. return getpix(vf, x, y, 1);
  63. }
  64. static double cr(void *vf, double x, double y){
  65. return getpix(vf, x, y, 2);
  66. }
  67. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  68. mp_image_t *dmpi;
  69. int x,y, plane;
  70. if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
  71. // no DR, so get a new image! hope we'll get DR buffer:
  72. vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, MP_IMGTYPE_TEMP,
  73. MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
  74. mpi->w,mpi->h);
  75. }
  76. dmpi= vf->dmpi;
  77. vf->priv->mpi= mpi;
  78. vf_clone_mpi_attributes(dmpi, mpi);
  79. for(plane=0; plane<3; plane++){
  80. int w= mpi->w >> (plane ? mpi->chroma_x_shift : 0);
  81. int h= mpi->h >> (plane ? mpi->chroma_y_shift : 0);
  82. uint8_t *dst = dmpi->planes[plane];
  83. int dst_stride= dmpi->stride[plane];
  84. double const_values[]={
  85. M_PI,
  86. M_E,
  87. 0,
  88. 0,
  89. w,
  90. h,
  91. vf->priv->framenum,
  92. w/(double)mpi->w,
  93. h/(double)mpi->h,
  94. 0
  95. };
  96. if (!vf->priv->e[plane]) continue;
  97. for(y=0; y<h; y++){
  98. const_values[3]=y;
  99. for(x=0; x<w; x++){
  100. const_values[2]=x;
  101. dst[x + y * dst_stride] = av_expr_eval(vf->priv->e[plane],
  102. const_values, vf);
  103. }
  104. }
  105. }
  106. vf->priv->framenum++;
  107. return vf_next_put_image(vf,dmpi, pts);
  108. }
  109. static void uninit(struct vf_instance *vf){
  110. av_free(vf->priv);
  111. vf->priv=NULL;
  112. }
  113. //===========================================================================//
  114. static int vf_open(vf_instance_t *vf, char *args){
  115. char eq[3][2000] = { { 0 }, { 0 }, { 0 } };
  116. int plane, res;
  117. vf->config=config;
  118. vf->put_image=put_image;
  119. // vf->get_image=get_image;
  120. vf->uninit=uninit;
  121. vf->priv=av_malloc(sizeof(struct vf_priv_s));
  122. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  123. if (args) sscanf(args, "%1999[^:]:%1999[^:]:%1999[^:]", eq[0], eq[1], eq[2]);
  124. if (!eq[1][0]) strncpy(eq[1], eq[0], sizeof(eq[0])-1);
  125. if (!eq[2][0]) strncpy(eq[2], eq[1], sizeof(eq[0])-1);
  126. for(plane=0; plane<3; plane++){
  127. static const char *const_names[]={
  128. "PI",
  129. "E",
  130. "X",
  131. "Y",
  132. "W",
  133. "H",
  134. "N",
  135. "SW",
  136. "SH",
  137. NULL
  138. };
  139. static const char *func2_names[]={
  140. "lum",
  141. "cb",
  142. "cr",
  143. "p",
  144. NULL
  145. };
  146. double (*func2[])(void *, double, double)={
  147. lum,
  148. cb,
  149. cr,
  150. plane==0 ? lum : (plane==1 ? cb : cr),
  151. NULL
  152. };
  153. res = av_expr_parse(&vf->priv->e[plane], eq[plane], const_names, NULL, NULL, func2_names, func2, 0, NULL);
  154. if (res < 0) {
  155. mp_msg(MSGT_VFILTER, MSGL_ERR, "geq: error loading equation `%s'\n", eq[plane]);
  156. return 0;
  157. }
  158. }
  159. return 1;
  160. }
  161. const vf_info_t vf_info_geq = {
  162. "generic equation filter",
  163. "geq",
  164. "Michael Niedermayer",
  165. "",
  166. vf_open,
  167. NULL
  168. };