vf_eq2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Software equalizer (brightness, contrast, gamma, saturation)
  3. *
  4. * Hampa Hug <hampa@hampa.ch> (original LUT gamma/contrast/brightness filter)
  5. * Daniel Moreno <comac@comac.darktech.org> (saturation, R/G/B gamma support)
  6. * Richard Felker (original MMX contrast/brightness code (vf_eq.c))
  7. * Michael Niedermayer <michalni@gmx.at> (LUT16)
  8. *
  9. * This file is part of MPlayer.
  10. *
  11. * MPlayer is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * MPlayer is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include <inttypes.h>
  30. #include "config.h"
  31. #include "mp_msg.h"
  32. #include "cpudetect.h"
  33. #include "img_format.h"
  34. #include "mp_image.h"
  35. #include "vf.h"
  36. #define LUT16
  37. /* Per channel parameters */
  38. typedef struct eq2_param_t {
  39. unsigned char lut[256];
  40. #ifdef LUT16
  41. uint16_t lut16[256*256];
  42. #endif
  43. int lut_clean;
  44. void (*adjust) (struct eq2_param_t *par, unsigned char *dst, unsigned char *src,
  45. unsigned w, unsigned h, unsigned dstride, unsigned sstride);
  46. double c;
  47. double b;
  48. double g;
  49. double w;
  50. } eq2_param_t;
  51. typedef struct vf_priv_s {
  52. eq2_param_t param[3];
  53. double contrast;
  54. double brightness;
  55. double saturation;
  56. double gamma;
  57. double gamma_weight;
  58. double rgamma;
  59. double ggamma;
  60. double bgamma;
  61. unsigned buf_w[3];
  62. unsigned buf_h[3];
  63. unsigned char *buf[3];
  64. } vf_eq2_t;
  65. static
  66. void create_lut (eq2_param_t *par)
  67. {
  68. unsigned i;
  69. double g, v;
  70. double lw, gw;
  71. g = par->g;
  72. gw = par->w;
  73. lw = 1.0 - gw;
  74. if ((g < 0.001) || (g > 1000.0)) {
  75. g = 1.0;
  76. }
  77. g = 1.0 / g;
  78. for (i = 0; i < 256; i++) {
  79. v = (double) i / 255.0;
  80. v = par->c * (v - 0.5) + 0.5 + par->b;
  81. if (v <= 0.0) {
  82. par->lut[i] = 0;
  83. }
  84. else {
  85. v = v*lw + pow(v, g)*gw;
  86. if (v >= 1.0) {
  87. par->lut[i] = 255;
  88. }
  89. else {
  90. par->lut[i] = (unsigned char) (256.0 * v);
  91. }
  92. }
  93. }
  94. #ifdef LUT16
  95. for(i=0; i<256*256; i++){
  96. par->lut16[i]= par->lut[i&0xFF] + (par->lut[i>>8]<<8);
  97. }
  98. #endif
  99. par->lut_clean = 1;
  100. }
  101. #if HAVE_MMX
  102. static
  103. void affine_1d_MMX (eq2_param_t *par, unsigned char *dst, unsigned char *src,
  104. unsigned w, unsigned h, unsigned dstride, unsigned sstride)
  105. {
  106. unsigned i;
  107. int contrast, brightness;
  108. unsigned dstep, sstep;
  109. int pel;
  110. short brvec[4];
  111. short contvec[4];
  112. // printf("\nmmx: src=%p dst=%p w=%d h=%d ds=%d ss=%d\n",src,dst,w,h,dstride,sstride);
  113. contrast = (int) (par->c * 256 * 16);
  114. brightness = ((int) (100.0 * par->b + 100.0) * 511) / 200 - 128 - contrast / 32;
  115. brvec[0] = brvec[1] = brvec[2] = brvec[3] = brightness;
  116. contvec[0] = contvec[1] = contvec[2] = contvec[3] = contrast;
  117. sstep = sstride - w;
  118. dstep = dstride - w;
  119. while (h-- > 0) {
  120. __asm__ volatile (
  121. "movq (%5), %%mm3 \n\t"
  122. "movq (%6), %%mm4 \n\t"
  123. "pxor %%mm0, %%mm0 \n\t"
  124. "movl %4, %%eax\n\t"
  125. ASMALIGN(4)
  126. "1: \n\t"
  127. "movq (%0), %%mm1 \n\t"
  128. "movq (%0), %%mm2 \n\t"
  129. "punpcklbw %%mm0, %%mm1 \n\t"
  130. "punpckhbw %%mm0, %%mm2 \n\t"
  131. "psllw $4, %%mm1 \n\t"
  132. "psllw $4, %%mm2 \n\t"
  133. "pmulhw %%mm4, %%mm1 \n\t"
  134. "pmulhw %%mm4, %%mm2 \n\t"
  135. "paddw %%mm3, %%mm1 \n\t"
  136. "paddw %%mm3, %%mm2 \n\t"
  137. "packuswb %%mm2, %%mm1 \n\t"
  138. "add $8, %0 \n\t"
  139. "movq %%mm1, (%1) \n\t"
  140. "add $8, %1 \n\t"
  141. "decl %%eax \n\t"
  142. "jnz 1b \n\t"
  143. : "=r" (src), "=r" (dst)
  144. : "0" (src), "1" (dst), "r" (w >> 3), "r" (brvec), "r" (contvec)
  145. : "%eax"
  146. );
  147. for (i = w & 7; i > 0; i--) {
  148. pel = ((*src++ * contrast) >> 12) + brightness;
  149. if (pel & 768) {
  150. pel = (-pel) >> 31;
  151. }
  152. *dst++ = pel;
  153. }
  154. src += sstep;
  155. dst += dstep;
  156. }
  157. __asm__ volatile ( "emms \n\t" ::: "memory" );
  158. }
  159. #endif
  160. static
  161. void apply_lut (eq2_param_t *par, unsigned char *dst, unsigned char *src,
  162. unsigned w, unsigned h, unsigned dstride, unsigned sstride)
  163. {
  164. unsigned i, j, w2;
  165. unsigned char *lut;
  166. uint16_t *lut16;
  167. if (!par->lut_clean) {
  168. create_lut (par);
  169. }
  170. lut = par->lut;
  171. #ifdef LUT16
  172. lut16 = par->lut16;
  173. w2= (w>>3)<<2;
  174. for (j = 0; j < h; j++) {
  175. uint16_t *src16= (uint16_t*)src;
  176. uint16_t *dst16= (uint16_t*)dst;
  177. for (i = 0; i < w2; i+=4) {
  178. dst16[i+0] = lut16[src16[i+0]];
  179. dst16[i+1] = lut16[src16[i+1]];
  180. dst16[i+2] = lut16[src16[i+2]];
  181. dst16[i+3] = lut16[src16[i+3]];
  182. }
  183. i <<= 1;
  184. #else
  185. w2= (w>>3)<<3;
  186. for (j = 0; j < h; j++) {
  187. for (i = 0; i < w2; i+=8) {
  188. dst[i+0] = lut[src[i+0]];
  189. dst[i+1] = lut[src[i+1]];
  190. dst[i+2] = lut[src[i+2]];
  191. dst[i+3] = lut[src[i+3]];
  192. dst[i+4] = lut[src[i+4]];
  193. dst[i+5] = lut[src[i+5]];
  194. dst[i+6] = lut[src[i+6]];
  195. dst[i+7] = lut[src[i+7]];
  196. }
  197. #endif
  198. for (; i < w; i++) {
  199. dst[i] = lut[src[i]];
  200. }
  201. src += sstride;
  202. dst += dstride;
  203. }
  204. }
  205. static
  206. int put_image (vf_instance_t *vf, mp_image_t *src, double pts)
  207. {
  208. unsigned i;
  209. vf_eq2_t *eq2;
  210. mp_image_t *dst;
  211. unsigned long img_n,img_c;
  212. eq2 = vf->priv;
  213. if ((eq2->buf_w[0] != src->w) || (eq2->buf_h[0] != src->h)) {
  214. eq2->buf_w[0] = src->w;
  215. eq2->buf_h[0] = src->h;
  216. eq2->buf_w[1] = eq2->buf_w[2] = src->w >> src->chroma_x_shift;
  217. eq2->buf_h[1] = eq2->buf_h[2] = src->h >> src->chroma_y_shift;
  218. img_n = eq2->buf_w[0]*eq2->buf_h[0];
  219. if(src->num_planes>1){
  220. img_c = eq2->buf_w[1]*eq2->buf_h[1];
  221. eq2->buf[0] = realloc (eq2->buf[0], img_n + 2*img_c);
  222. eq2->buf[1] = eq2->buf[0] + img_n;
  223. eq2->buf[2] = eq2->buf[1] + img_c;
  224. } else
  225. eq2->buf[0] = realloc (eq2->buf[0], img_n);
  226. }
  227. dst = vf_get_image (vf->next, src->imgfmt, MP_IMGTYPE_EXPORT, 0, src->w, src->h);
  228. for (i = 0; i < ((src->num_planes>1)?3:1); i++) {
  229. if (eq2->param[i].adjust != NULL) {
  230. dst->planes[i] = eq2->buf[i];
  231. dst->stride[i] = eq2->buf_w[i];
  232. eq2->param[i].adjust (&eq2->param[i], dst->planes[i], src->planes[i],
  233. eq2->buf_w[i], eq2->buf_h[i], dst->stride[i], src->stride[i]);
  234. }
  235. else {
  236. dst->planes[i] = src->planes[i];
  237. dst->stride[i] = src->stride[i];
  238. }
  239. }
  240. return vf_next_put_image (vf, dst, pts);
  241. }
  242. static
  243. void check_values (eq2_param_t *par)
  244. {
  245. /* yuck! floating point comparisons... */
  246. if ((par->c == 1.0) && (par->b == 0.0) && (par->g == 1.0)) {
  247. par->adjust = NULL;
  248. }
  249. #if HAVE_MMX
  250. else if (par->g == 1.0 && gCpuCaps.hasMMX) {
  251. par->adjust = &affine_1d_MMX;
  252. }
  253. #endif
  254. else {
  255. par->adjust = &apply_lut;
  256. }
  257. }
  258. static
  259. void print_values (vf_eq2_t *eq2)
  260. {
  261. mp_msg (MSGT_VFILTER, MSGL_V, "vf_eq2: c=%.2f b=%.2f g=%.4f s=%.2f \n",
  262. eq2->contrast, eq2->brightness, eq2->gamma, eq2->saturation
  263. );
  264. }
  265. static
  266. void set_contrast (vf_eq2_t *eq2, double c)
  267. {
  268. eq2->contrast = c;
  269. eq2->param[0].c = c;
  270. eq2->param[0].lut_clean = 0;
  271. check_values (&eq2->param[0]);
  272. print_values (eq2);
  273. }
  274. static
  275. void set_brightness (vf_eq2_t *eq2, double b)
  276. {
  277. eq2->brightness = b;
  278. eq2->param[0].b = b;
  279. eq2->param[0].lut_clean = 0;
  280. check_values (&eq2->param[0]);
  281. print_values (eq2);
  282. }
  283. static
  284. void set_gamma (vf_eq2_t *eq2, double g)
  285. {
  286. eq2->gamma = g;
  287. eq2->param[0].g = eq2->gamma * eq2->ggamma;
  288. eq2->param[1].g = sqrt (eq2->bgamma / eq2->ggamma);
  289. eq2->param[2].g = sqrt (eq2->rgamma / eq2->ggamma);
  290. eq2->param[0].w = eq2->param[1].w = eq2->param[2].w = eq2->gamma_weight;
  291. eq2->param[0].lut_clean = 0;
  292. eq2->param[1].lut_clean = 0;
  293. eq2->param[2].lut_clean = 0;
  294. check_values (&eq2->param[0]);
  295. check_values (&eq2->param[1]);
  296. check_values (&eq2->param[2]);
  297. print_values (eq2);
  298. }
  299. static
  300. void set_saturation (vf_eq2_t *eq2, double s)
  301. {
  302. eq2->saturation = s;
  303. eq2->param[1].c = s;
  304. eq2->param[2].c = s;
  305. eq2->param[1].lut_clean = 0;
  306. eq2->param[2].lut_clean = 0;
  307. check_values (&eq2->param[1]);
  308. check_values (&eq2->param[2]);
  309. print_values (eq2);
  310. }
  311. static
  312. int control (vf_instance_t *vf, int request, void *data)
  313. {
  314. vf_equalizer_t *eq;
  315. switch (request) {
  316. case VFCTRL_SET_EQUALIZER:
  317. eq = (vf_equalizer_t *) data;
  318. if (strcmp (eq->item, "gamma") == 0) {
  319. set_gamma (vf->priv, exp (log (8.0) * eq->value / 100.0));
  320. return CONTROL_TRUE;
  321. }
  322. else if (strcmp (eq->item, "contrast") == 0) {
  323. set_contrast (vf->priv, (1.0 / 100.0) * (eq->value + 100));
  324. return CONTROL_TRUE;
  325. }
  326. else if (strcmp (eq->item, "brightness") == 0) {
  327. set_brightness (vf->priv, (1.0 / 100.0) * eq->value);
  328. return CONTROL_TRUE;
  329. }
  330. else if (strcmp (eq->item, "saturation") == 0) {
  331. set_saturation (vf->priv, (double) (eq->value + 100) / 100.0);
  332. return CONTROL_TRUE;
  333. }
  334. break;
  335. case VFCTRL_GET_EQUALIZER:
  336. eq = (vf_equalizer_t *) data;
  337. if (strcmp (eq->item, "gamma") == 0) {
  338. eq->value = (int) (100.0 * log (vf->priv->gamma) / log (8.0));
  339. return CONTROL_TRUE;
  340. }
  341. else if (strcmp (eq->item, "contrast") == 0) {
  342. eq->value = (int) (100.0 * vf->priv->contrast) - 100;
  343. return CONTROL_TRUE;
  344. }
  345. else if (strcmp (eq->item, "brightness") == 0) {
  346. eq->value = (int) (100.0 * vf->priv->brightness);
  347. return CONTROL_TRUE;
  348. }
  349. else if (strcmp (eq->item, "saturation") == 0) {
  350. eq->value = (int) (100.0 * vf->priv->saturation) - 100;
  351. return CONTROL_TRUE;
  352. }
  353. break;
  354. }
  355. return vf_next_control (vf, request, data);
  356. }
  357. static
  358. int query_format (vf_instance_t *vf, unsigned fmt)
  359. {
  360. switch (fmt) {
  361. case IMGFMT_YVU9:
  362. case IMGFMT_IF09:
  363. case IMGFMT_YV12:
  364. case IMGFMT_I420:
  365. case IMGFMT_IYUV:
  366. case IMGFMT_Y800:
  367. case IMGFMT_Y8:
  368. case IMGFMT_444P:
  369. case IMGFMT_422P:
  370. case IMGFMT_411P:
  371. return vf_next_query_format (vf, fmt);
  372. }
  373. return 0;
  374. }
  375. static
  376. void uninit (vf_instance_t *vf)
  377. {
  378. if (vf->priv != NULL) {
  379. free (vf->priv->buf[0]);
  380. free (vf->priv);
  381. }
  382. }
  383. static
  384. int vf_open(vf_instance_t *vf, char *args)
  385. {
  386. unsigned i;
  387. vf_eq2_t *eq2;
  388. double par[8];
  389. vf->control = control;
  390. vf->query_format = query_format;
  391. vf->put_image = put_image;
  392. vf->uninit = uninit;
  393. vf->priv = malloc (sizeof (vf_eq2_t));
  394. eq2 = vf->priv;
  395. for (i = 0; i < 3; i++) {
  396. eq2->buf[i] = NULL;
  397. eq2->buf_w[i] = 0;
  398. eq2->buf_h[i] = 0;
  399. eq2->param[i].adjust = NULL;
  400. eq2->param[i].c = 1.0;
  401. eq2->param[i].b = 0.0;
  402. eq2->param[i].g = 1.0;
  403. eq2->param[i].lut_clean = 0;
  404. }
  405. eq2->contrast = 1.0;
  406. eq2->brightness = 0.0;
  407. eq2->saturation = 1.0;
  408. eq2->gamma = 1.0;
  409. eq2->gamma_weight = 1.0;
  410. eq2->rgamma = 1.0;
  411. eq2->ggamma = 1.0;
  412. eq2->bgamma = 1.0;
  413. if (args != NULL) {
  414. par[0] = 1.0;
  415. par[1] = 1.0;
  416. par[2] = 0.0;
  417. par[3] = 1.0;
  418. par[4] = 1.0;
  419. par[5] = 1.0;
  420. par[6] = 1.0;
  421. par[7] = 1.0;
  422. sscanf (args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf",
  423. par, par + 1, par + 2, par + 3, par + 4, par + 5, par + 6, par + 7
  424. );
  425. eq2->rgamma = par[4];
  426. eq2->ggamma = par[5];
  427. eq2->bgamma = par[6];
  428. eq2->gamma_weight = par[7];
  429. set_gamma (eq2, par[0]);
  430. set_contrast (eq2, par[1]);
  431. set_brightness (eq2, par[2]);
  432. set_saturation (eq2, par[3]);
  433. }
  434. return 1;
  435. }
  436. const vf_info_t vf_info_eq2 = {
  437. "Software equalizer",
  438. "eq2",
  439. "Hampa Hug, Daniel Moreno, Richard Felker",
  440. "",
  441. &vf_open,
  442. NULL
  443. };