vf_hue.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This file is part of MPlayer.
  3. *
  4. * MPlayer is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * MPlayer is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <inttypes.h>
  22. #include <math.h>
  23. #include "config.h"
  24. #include "mp_msg.h"
  25. #include "cpudetect.h"
  26. #include "img_format.h"
  27. #include "mp_image.h"
  28. #include "vf.h"
  29. #include "libvo/video_out.h"
  30. struct vf_priv_s {
  31. uint8_t *buf[2];
  32. float hue;
  33. float saturation;
  34. };
  35. static void process_C(uint8_t *udst, uint8_t *vdst, uint8_t *usrc, uint8_t *vsrc, int dststride, int srcstride,
  36. int w, int h, float hue, float sat)
  37. {
  38. int i;
  39. const int s= rint(sin(hue) * (1<<16) * sat);
  40. const int c= rint(cos(hue) * (1<<16) * sat);
  41. while (h--) {
  42. for (i = 0; i<w; i++)
  43. {
  44. const int u= usrc[i] - 128;
  45. const int v= vsrc[i] - 128;
  46. int new_u= (c*u - s*v + (1<<15) + (128<<16))>>16;
  47. int new_v= (s*u + c*v + (1<<15) + (128<<16))>>16;
  48. if(new_u & 768) new_u= (-new_u)>>31;
  49. if(new_v & 768) new_v= (-new_v)>>31;
  50. udst[i]= new_u;
  51. vdst[i]= new_v;
  52. }
  53. usrc += srcstride;
  54. vsrc += srcstride;
  55. udst += dststride;
  56. vdst += dststride;
  57. }
  58. }
  59. static void (*process)(uint8_t *udst, uint8_t *vdst, uint8_t *usrc, uint8_t *vsrc, int dststride, int srcstride,
  60. int w, int h, float hue, float sat);
  61. /* FIXME: add packed yuv version of process */
  62. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
  63. {
  64. mp_image_t *dmpi;
  65. dmpi=vf_get_image(vf->next, mpi->imgfmt,
  66. MP_IMGTYPE_EXPORT, 0,
  67. mpi->w, mpi->h);
  68. dmpi->planes[0] = mpi->planes[0];
  69. dmpi->stride[0] = mpi->stride[0];
  70. dmpi->stride[1] = mpi->stride[1];
  71. dmpi->stride[2] = mpi->stride[2];
  72. if (!vf->priv->buf[0]){
  73. vf->priv->buf[0] = malloc(mpi->stride[1]*mpi->h >> mpi->chroma_y_shift);
  74. vf->priv->buf[1] = malloc(mpi->stride[2]*mpi->h >> mpi->chroma_y_shift);
  75. }
  76. if (vf->priv->hue == 0 && vf->priv->saturation == 1){
  77. dmpi->planes[1] = mpi->planes[1];
  78. dmpi->planes[2] = mpi->planes[2];
  79. }else {
  80. dmpi->planes[1] = vf->priv->buf[0];
  81. dmpi->planes[2] = vf->priv->buf[1];
  82. process(dmpi->planes[1], dmpi->planes[2],
  83. mpi->planes[1], mpi->planes[2],
  84. dmpi->stride[1],mpi->stride[1],
  85. mpi->w>> mpi->chroma_x_shift, mpi->h>> mpi->chroma_y_shift,
  86. vf->priv->hue, vf->priv->saturation);
  87. }
  88. return vf_next_put_image(vf,dmpi, pts);
  89. }
  90. static int control(struct vf_instance *vf, int request, void* data)
  91. {
  92. vf_equalizer_t *eq;
  93. switch (request) {
  94. case VFCTRL_SET_EQUALIZER:
  95. eq = data;
  96. if (!strcmp(eq->item,"hue")) {
  97. vf->priv->hue = eq->value * M_PI / 100;
  98. return CONTROL_TRUE;
  99. } else if (!strcmp(eq->item,"saturation")) {
  100. vf->priv->saturation = (eq->value + 100)/100.0;
  101. return CONTROL_TRUE;
  102. }
  103. break;
  104. case VFCTRL_GET_EQUALIZER:
  105. eq = data;
  106. if (!strcmp(eq->item,"hue")) {
  107. eq->value = rint(vf->priv->hue *100 / M_PI);
  108. return CONTROL_TRUE;
  109. }else if (!strcmp(eq->item,"saturation")) {
  110. eq->value = rint(vf->priv->saturation*100 - 100);
  111. return CONTROL_TRUE;
  112. }
  113. break;
  114. }
  115. return vf_next_control(vf, request, data);
  116. }
  117. static int query_format(struct vf_instance *vf, unsigned int fmt)
  118. {
  119. switch (fmt) {
  120. case IMGFMT_YVU9:
  121. case IMGFMT_IF09:
  122. case IMGFMT_YV12:
  123. case IMGFMT_I420:
  124. case IMGFMT_IYUV:
  125. case IMGFMT_CLPL:
  126. case IMGFMT_444P:
  127. case IMGFMT_422P:
  128. case IMGFMT_411P:
  129. return vf_next_query_format(vf, fmt);
  130. }
  131. return 0;
  132. }
  133. static void uninit(struct vf_instance *vf)
  134. {
  135. free(vf->priv->buf[0]);
  136. free(vf->priv->buf[1]);
  137. free(vf->priv);
  138. }
  139. static int vf_open(vf_instance_t *vf, char *args)
  140. {
  141. vf->control=control;
  142. vf->query_format=query_format;
  143. vf->put_image=put_image;
  144. vf->uninit=uninit;
  145. vf->priv = malloc(sizeof(struct vf_priv_s));
  146. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  147. sscanf(args, "%f:%f", &vf->priv->hue, &vf->priv->saturation);
  148. vf->priv->hue *= M_PI / 180.0;
  149. process = process_C;
  150. return 1;
  151. }
  152. const vf_info_t vf_info_hue = {
  153. "hue changer",
  154. "hue",
  155. "Michael Niedermayer",
  156. "",
  157. vf_open,
  158. };