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 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30 #include <inttypes.h>
31 
32 #include "config.h"
33 #include "mp_msg.h"
34 #include "cpudetect.h"
35 
36 #include "img_format.h"
37 #include "mp_image.h"
38 #include "vf.h"
39 
40 #define LUT16
41 
42 /* Per channel parameters */
43 typedef struct eq2_param_t {
44   unsigned char lut[256];
45 #ifdef LUT16
46   uint16_t lut16[256*256];
47 #endif
48   int           lut_clean;
49 
50   void (*adjust) (struct eq2_param_t *par, unsigned char *dst, unsigned char *src,
51     unsigned w, unsigned h, unsigned dstride, unsigned sstride);
52 
53   double        c;
54   double        b;
55   double        g;
56   double        w;
57 } eq2_param_t;
58 
59 typedef struct vf_priv_s {
60   eq2_param_t param[3];
61 
62   double        contrast;
63   double        brightness;
64   double        saturation;
65 
66   double        gamma;
67   double        gamma_weight;
68   double        rgamma;
69   double        ggamma;
70   double        bgamma;
71 
72   unsigned      buf_w[3];
73   unsigned      buf_h[3];
74   unsigned char *buf[3];
75 } vf_eq2_t;
76 
77 
78 static
create_lut(eq2_param_t * par)79 void create_lut (eq2_param_t *par)
80 {
81   unsigned i;
82   double   g, v;
83   double   lw, gw;
84 
85   g = par->g;
86   gw = par->w;
87   lw = 1.0 - gw;
88 
89   if ((g < 0.001) || (g > 1000.0)) {
90     g = 1.0;
91   }
92 
93   g = 1.0 / g;
94 
95   for (i = 0; i < 256; i++) {
96     v = (double) i / 255.0;
97     v = par->c * (v - 0.5) + 0.5 + par->b;
98 
99     if (v <= 0.0) {
100       par->lut[i] = 0;
101     }
102     else {
103       v = v*lw + pow(v, g)*gw;
104 
105       if (v >= 1.0) {
106         par->lut[i] = 255;
107       }
108       else {
109         par->lut[i] = (unsigned char) (256.0 * v);
110       }
111     }
112   }
113 
114 #ifdef LUT16
115   for(i=0; i<256*256; i++){
116     par->lut16[i]= par->lut[i&0xFF] + (par->lut[i>>8]<<8);
117   }
118 #endif
119 
120   par->lut_clean = 1;
121 }
122 
123 #if HAVE_MMX_INLINE
124 static
affine_1d_MMX(eq2_param_t * par,unsigned char * dst,unsigned char * src,unsigned w,unsigned h,unsigned dstride,unsigned sstride)125 void affine_1d_MMX (eq2_param_t *par, unsigned char *dst, unsigned char *src,
126   unsigned w, unsigned h, unsigned dstride, unsigned sstride)
127 {
128   unsigned i;
129   int      contrast, brightness;
130   unsigned dstep, sstep;
131   int      pel;
132   short    brvec[4];
133   short    contvec[4];
134 
135 //  printf("\nmmx: src=%p dst=%p w=%d h=%d ds=%d ss=%d\n",src,dst,w,h,dstride,sstride);
136 
137   contrast = (int) (par->c * 256 * 16);
138   brightness = ((int) (100.0 * par->b + 100.0) * 511) / 200 - 128 - contrast / 32;
139 
140   brvec[0] = brvec[1] = brvec[2] = brvec[3] = brightness;
141   contvec[0] = contvec[1] = contvec[2] = contvec[3] = contrast;
142 
143   sstep = sstride - w;
144   dstep = dstride - w;
145 
146   while (h-- > 0) {
147     __asm__ volatile (
148       "movq (%5), %%mm3 \n\t"
149       "movq (%6), %%mm4 \n\t"
150       "pxor %%mm0, %%mm0 \n\t"
151       "movl %4, %%eax\n\t"
152       ASMALIGN(4)
153       "1: \n\t"
154       "movq (%0), %%mm1 \n\t"
155       "movq (%0), %%mm2 \n\t"
156       "punpcklbw %%mm0, %%mm1 \n\t"
157       "punpckhbw %%mm0, %%mm2 \n\t"
158       "psllw $4, %%mm1 \n\t"
159       "psllw $4, %%mm2 \n\t"
160       "pmulhw %%mm4, %%mm1 \n\t"
161       "pmulhw %%mm4, %%mm2 \n\t"
162       "paddw %%mm3, %%mm1 \n\t"
163       "paddw %%mm3, %%mm2 \n\t"
164       "packuswb %%mm2, %%mm1 \n\t"
165       "add $8, %0 \n\t"
166       "movq %%mm1, (%1) \n\t"
167       "add $8, %1 \n\t"
168       "decl %%eax \n\t"
169       "jnz 1b \n\t"
170       : "=r" (src), "=r" (dst)
171       : "0" (src), "1" (dst), "r" (w >> 3), "r" (brvec), "r" (contvec)
172       : "%eax"
173     );
174 
175     for (i = w & 7; i > 0; i--) {
176       pel = ((*src++ * contrast) >> 12) + brightness;
177       if (pel & 768) {
178         pel = (-pel) >> 31;
179       }
180       *dst++ = pel;
181     }
182 
183     src += sstep;
184     dst += dstep;
185   }
186 
187   __asm__ volatile ( "emms \n\t" ::: "memory" );
188 }
189 #endif
190 
191 #if HAVE_EMMINTRIN_H
192 #include <emmintrin.h>
193 
194 ATTR_TARGET_SSE2
affine_1d_SSE2(eq2_param_t * par,unsigned char * dst,unsigned char * src,unsigned w,unsigned h,unsigned dstride,unsigned sstride)195 static void affine_1d_SSE2(eq2_param_t *par, unsigned char *dst, unsigned char *src,
196   unsigned w, unsigned h, unsigned dstride, unsigned sstride)
197 {
198     int scaled_contrast = par->c * 256 * 16;
199     int scaled_brightness = ((par->b+1.0)*511)/2-128 - scaled_contrast/32;
200     __m128i mmcontrast = _mm_set1_epi16(scaled_contrast);
201     __m128i mmbrightness = _mm_set1_epi16(scaled_brightness);
202     __m128i zero = _mm_setzero_si128();
203     while (h--) {
204         int i;
205         for (i = 0; i < w - 15; i += 16)
206         {
207             __m128i mmsrc = _mm_loadu_si128((const __m128i *)(src + i));
208             __m128i srclo = _mm_unpacklo_epi8(mmsrc, zero);
209             __m128i srchi = _mm_unpackhi_epi8(mmsrc, zero);
210             srclo = _mm_slli_epi16(srclo, 4);
211             srchi = _mm_slli_epi16(srchi, 4);
212             srclo = _mm_mulhi_epu16(srclo, mmcontrast);
213             srchi = _mm_mulhi_epu16(srchi, mmcontrast);
214             srclo = _mm_add_epi16(srclo, mmbrightness);
215             srchi = _mm_add_epi16(srchi, mmbrightness);
216             _mm_storeu_si128((__m128i *)(dst + i), _mm_packus_epi16(srclo, srchi));
217         }
218         for (; i < w; i++)
219         {
220             int pel = ((src[i] * scaled_contrast)>>12) + scaled_brightness;
221             if(pel&768) pel = (-pel)>>31;
222             dst[i] = pel;
223         }
224         src += sstride;
225         dst += dstride;
226     }
227 }
228 #endif
229 
230 static
apply_lut(eq2_param_t * par,unsigned char * dst,unsigned char * src,unsigned w,unsigned h,unsigned dstride,unsigned sstride)231 void apply_lut (eq2_param_t *par, unsigned char *dst, unsigned char *src,
232   unsigned w, unsigned h, unsigned dstride, unsigned sstride)
233 {
234   unsigned      i, j, w2;
235   unsigned char *lut;
236   uint16_t *lut16;
237 
238   if (!par->lut_clean) {
239     create_lut (par);
240   }
241 
242   lut = par->lut;
243 #ifdef LUT16
244   lut16 = par->lut16;
245   w2= (w>>3)<<2;
246   for (j = 0; j < h; j++) {
247     uint16_t *src16= (uint16_t*)src;
248     uint16_t *dst16= (uint16_t*)dst;
249     for (i = 0; i < w2; i+=4) {
250       dst16[i+0] = lut16[src16[i+0]];
251       dst16[i+1] = lut16[src16[i+1]];
252       dst16[i+2] = lut16[src16[i+2]];
253       dst16[i+3] = lut16[src16[i+3]];
254     }
255     i <<= 1;
256 #else
257   w2= (w>>3)<<3;
258   for (j = 0; j < h; j++) {
259     for (i = 0; i < w2; i+=8) {
260       dst[i+0] = lut[src[i+0]];
261       dst[i+1] = lut[src[i+1]];
262       dst[i+2] = lut[src[i+2]];
263       dst[i+3] = lut[src[i+3]];
264       dst[i+4] = lut[src[i+4]];
265       dst[i+5] = lut[src[i+5]];
266       dst[i+6] = lut[src[i+6]];
267       dst[i+7] = lut[src[i+7]];
268     }
269 #endif
270     for (; i < w; i++) {
271       dst[i] = lut[src[i]];
272     }
273 
274     src += sstride;
275     dst += dstride;
276   }
277 }
278 
279 static
280 int put_image (vf_instance_t *vf, mp_image_t *src, double pts, double endpts)
281 {
282   unsigned      i;
283   vf_eq2_t      *eq2;
284   mp_image_t    *dst;
285   unsigned long img_n,img_c;
286 
287   eq2 = vf->priv;
288 
289   if ((eq2->buf_w[0] != src->w) || (eq2->buf_h[0] != src->h)) {
290     eq2->buf_w[0] = src->w;
291     eq2->buf_h[0] = src->h;
292       eq2->buf_w[1] = eq2->buf_w[2] = src->w >> src->chroma_x_shift;
293       eq2->buf_h[1] = eq2->buf_h[2] = src->h >> src->chroma_y_shift;
294     img_n = eq2->buf_w[0]*eq2->buf_h[0];
295     if(src->num_planes>1){
296       img_c = eq2->buf_w[1]*eq2->buf_h[1];
297       eq2->buf[0] = realloc (eq2->buf[0], img_n + 2*img_c);
298       eq2->buf[1] = eq2->buf[0] + img_n;
299       eq2->buf[2] = eq2->buf[1] + img_c;
300     } else
301       eq2->buf[0] = realloc (eq2->buf[0], img_n);
302   }
303 
304   dst = vf_get_image (vf->next, src->imgfmt, MP_IMGTYPE_EXPORT, 0, src->w, src->h);
305 
306   for (i = 0; i < ((src->num_planes>1)?3:1); i++) {
307     if (eq2->param[i].adjust != NULL) {
308       dst->planes[i] = eq2->buf[i];
309       dst->stride[i] = eq2->buf_w[i];
310 
311       eq2->param[i].adjust (&eq2->param[i], dst->planes[i], src->planes[i],
312         eq2->buf_w[i], eq2->buf_h[i], dst->stride[i], src->stride[i]);
313     }
314     else {
315       dst->planes[i] = src->planes[i];
316       dst->stride[i] = src->stride[i];
317     }
318   }
319 
320   return vf_next_put_image(vf, dst, pts, endpts);
321 }
322 
323 static
324 void check_values (eq2_param_t *par)
325 {
326   /* yuck! floating point comparisons... */
327 
328   if ((par->c == 1.0) && (par->b == 0.0) && (par->g == 1.0)) {
329     par->adjust = NULL;
330   }
331 #if HAVE_EMMINTRIN_H
332   else if (par->g == 1.0 && gCpuCaps.hasSSE2) {
333     par->adjust = &affine_1d_SSE2;
334   }
335 #endif
336 #if HAVE_MMX_INLINE
337   else if (par->g == 1.0 && gCpuCaps.hasMMX) {
338     par->adjust = &affine_1d_MMX;
339   }
340 #endif
341   else {
342     par->adjust = &apply_lut;
343   }
344 }
345 
346 static
347 void print_values (vf_eq2_t *eq2)
348 {
349   mp_msg (MSGT_VFILTER, MSGL_V, "vf_eq2: c=%.2f b=%.2f g=%.4f s=%.2f \n",
350     eq2->contrast, eq2->brightness, eq2->gamma, eq2->saturation
351   );
352 }
353 
354 static
355 void set_contrast (vf_eq2_t *eq2, double c)
356 {
357   eq2->contrast = c;
358   eq2->param[0].c = c;
359   eq2->param[0].lut_clean = 0;
360   check_values (&eq2->param[0]);
361   print_values (eq2);
362 }
363 
364 static
365 void set_brightness (vf_eq2_t *eq2, double b)
366 {
367   eq2->brightness = b;
368   eq2->param[0].b = b;
369   eq2->param[0].lut_clean = 0;
370   check_values (&eq2->param[0]);
371   print_values (eq2);
372 }
373 
374 static
375 void set_gamma (vf_eq2_t *eq2, double g)
376 {
377   eq2->gamma = g;
378 
379   eq2->param[0].g = eq2->gamma * eq2->ggamma;
380   eq2->param[1].g = sqrt (eq2->bgamma / eq2->ggamma);
381   eq2->param[2].g = sqrt (eq2->rgamma / eq2->ggamma);
382   eq2->param[0].w = eq2->param[1].w = eq2->param[2].w = eq2->gamma_weight;
383 
384   eq2->param[0].lut_clean = 0;
385   eq2->param[1].lut_clean = 0;
386   eq2->param[2].lut_clean = 0;
387 
388   check_values (&eq2->param[0]);
389   check_values (&eq2->param[1]);
390   check_values (&eq2->param[2]);
391 
392   print_values (eq2);
393 }
394 
395 static
396 void set_saturation (vf_eq2_t *eq2, double s)
397 {
398   eq2->saturation = s;
399 
400   eq2->param[1].c = s;
401   eq2->param[2].c = s;
402 
403   eq2->param[1].lut_clean = 0;
404   eq2->param[2].lut_clean = 0;
405 
406   check_values (&eq2->param[1]);
407   check_values (&eq2->param[2]);
408 
409   print_values (eq2);
410 }
411 
412 static
413 int control (vf_instance_t *vf, int request, void *data)
414 {
415   vf_equalizer_t *eq;
416 
417   switch (request) {
418     case VFCTRL_SET_EQUALIZER:
419       eq = (vf_equalizer_t *) data;
420 
421       if (strcmp (eq->item, "gamma") == 0) {
422         set_gamma (vf->priv, exp (log (8.0) * eq->value / 100.0));
423         return CONTROL_TRUE;
424       }
425       else if (strcmp (eq->item, "contrast") == 0) {
426         set_contrast (vf->priv, (1.0 / 100.0) * (eq->value + 100));
427         return CONTROL_TRUE;
428       }
429       else if (strcmp (eq->item, "brightness") == 0) {
430         set_brightness (vf->priv, (1.0 / 100.0) * eq->value);
431         return CONTROL_TRUE;
432       }
433       else if (strcmp (eq->item, "saturation") == 0) {
434         set_saturation (vf->priv, (double) (eq->value + 100) / 100.0);
435         return CONTROL_TRUE;
436       }
437       break;
438 
439     case VFCTRL_GET_EQUALIZER:
440       eq = (vf_equalizer_t *) data;
441       if (strcmp (eq->item, "gamma") == 0) {
442         eq->value = (int) (100.0 * log (vf->priv->gamma) / log (8.0));
443         return CONTROL_TRUE;
444       }
445       else if (strcmp (eq->item, "contrast") == 0) {
446         eq->value = (int) (100.0 * vf->priv->contrast) - 100;
447         return CONTROL_TRUE;
448       }
449       else if (strcmp (eq->item, "brightness") == 0) {
450         eq->value = (int) (100.0 * vf->priv->brightness);
451         return CONTROL_TRUE;
452       }
453       else if (strcmp (eq->item, "saturation") == 0) {
454         eq->value = (int) (100.0 * vf->priv->saturation) - 100;
455         return CONTROL_TRUE;
456       }
457       break;
458   }
459 
460   return vf_next_control (vf, request, data);
461 }
462 
463 static
464 int query_format (vf_instance_t *vf, unsigned fmt)
465 {
466   switch (fmt) {
467     case IMGFMT_YVU9:
468     case IMGFMT_IF09:
469     case IMGFMT_YV12:
470     case IMGFMT_I420:
471     case IMGFMT_IYUV:
472     case IMGFMT_Y800:
473     case IMGFMT_Y8:
474     case IMGFMT_444P:
475     case IMGFMT_422P:
476     case IMGFMT_411P:
477       return vf_next_query_format (vf, fmt);
478   }
479 
480   return 0;
481 }
482 
483 static
484 void uninit (vf_instance_t *vf)
485 {
486   if (vf->priv != NULL) {
487     free (vf->priv->buf[0]);
488     free (vf->priv);
489   }
490 }
491 
492 static
493 int vf_open(vf_instance_t *vf, char *args)
494 {
495   unsigned i;
496   vf_eq2_t *eq2;
497   double   par[8];
498 
499   vf->control = control;
500   vf->query_format = query_format;
501   vf->put_image = put_image;
502   vf->uninit = uninit;
503 
504   vf->priv = malloc (sizeof (vf_eq2_t));
505   eq2 = vf->priv;
506 
507   for (i = 0; i < 3; i++) {
508     eq2->buf[i] = NULL;
509     eq2->buf_w[i] = 0;
510     eq2->buf_h[i] = 0;
511 
512     eq2->param[i].adjust = NULL;
513     eq2->param[i].c = 1.0;
514     eq2->param[i].b = 0.0;
515     eq2->param[i].g = 1.0;
516     eq2->param[i].lut_clean = 0;
517   }
518 
519   eq2->contrast = 1.0;
520   eq2->brightness = 0.0;
521   eq2->saturation = 1.0;
522 
523   eq2->gamma = 1.0;
524   eq2->gamma_weight = 1.0;
525   eq2->rgamma = 1.0;
526   eq2->ggamma = 1.0;
527   eq2->bgamma = 1.0;
528 
529   if (args != NULL) {
530     par[0] = 1.0;
531     par[1] = 1.0;
532     par[2] = 0.0;
533     par[3] = 1.0;
534     par[4] = 1.0;
535     par[5] = 1.0;
536     par[6] = 1.0;
537     par[7] = 1.0;
538     sscanf (args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf",
539       par, par + 1, par + 2, par + 3, par + 4, par + 5, par + 6, par + 7
540     );
541 
542     eq2->rgamma = par[4];
543     eq2->ggamma = par[5];
544     eq2->bgamma = par[6];
545     eq2->gamma_weight = par[7];
546 
547     set_gamma (eq2, par[0]);
548     set_contrast (eq2, par[1]);
549     set_brightness (eq2, par[2]);
550     set_saturation (eq2, par[3]);
551   }
552 
553   return 1;
554 }
555 
556 const vf_info_t vf_info_eq2 = {
557   "Software equalizer",
558   "eq2",
559   "Hampa Hug, Daniel Moreno, Richard Felker",
560   "",
561   &vf_open,
562   NULL
563 };
564