vf_geq.c 5.3 KB

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