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"
35
39
41
42 /* Per channel parameters */
44 unsigned char lut[256];
45 #ifdef LUT16
47 #endif
49
51 unsigned w,
unsigned h,
unsigned dstride,
unsigned sstride);
52
58
61
65
71
74 unsigned char *
buf[3];
76
77
78 static
80 {
81 unsigned i;
83 double lw, gw;
84
87 lw = 1.0 - gw;
88
89 if ((g < 0.001) || (g > 1000.0)) {
90 g = 1.0;
91 }
92
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) {
101 }
102 else {
103 v = v*lw + pow(v, g)*gw;
104
105 if (v >= 1.0) {
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
121 }
122
123 #if HAVE_MMX
124 static
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"
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 static
193 unsigned w, unsigned h, unsigned dstride, unsigned sstride)
194 {
195 unsigned i, j, w2;
196 unsigned char *lut;
197 uint16_t *lut16;
198
201 }
202
204 #ifdef LUT16
206 w2= (w>>3)<<2;
207 for (j = 0; j < h; j++) {
208 uint16_t *src16= (uint16_t*)src;
209 uint16_t *dst16= (uint16_t*)dst;
210 for (i = 0; i < w2; i+=4) {
211 dst16[i+0] = lut16[src16[i+0]];
212 dst16[i+1] = lut16[src16[i+1]];
213 dst16[i+2] = lut16[src16[i+2]];
214 dst16[i+3] = lut16[src16[i+3]];
215 }
216 i <<= 1;
217 #else
218 w2= (w>>3)<<3;
219 for (j = 0; j < h; j++) {
220 for (i = 0; i < w2; i+=8) {
221 dst[i+0] = lut[src[i+0]];
222 dst[i+1] = lut[src[i+1]];
223 dst[i+2] = lut[src[i+2]];
224 dst[i+3] = lut[src[i+3]];
225 dst[i+4] = lut[src[i+4]];
226 dst[i+5] = lut[src[i+5]];
227 dst[i+6] = lut[src[i+6]];
228 dst[i+7] = lut[src[i+7]];
229 }
230 #endif
231 for (; i < w; i++) {
232 dst[i] = lut[src[i]];
233 }
234
235 src += sstride;
236 dst += dstride;
237 }
238 }
239
240 static
242 {
243 unsigned i;
246 unsigned long img_n,img_c;
247
249
250 if ((eq2->
buf_w[0] != src->
w) || (eq2->
buf_h[0] != src->
h)) {
258 eq2->
buf[0] = realloc (eq2->
buf[0], img_n + 2*img_c);
259 eq2->
buf[1] = eq2->
buf[0] + img_n;
260 eq2->
buf[2] = eq2->
buf[1] + img_c;
261 } else
262 eq2->
buf[0] = realloc (eq2->
buf[0], img_n);
263 }
264
266
267 for (i = 0; i < ((src->
num_planes>1)?3:1); i++) {
271
274 }
275 else {
278 }
279 }
280
282 }
283
284 static
286 {
287 /* yuck! floating point comparisons... */
288
289 if ((par->
c == 1.0) && (par->
b == 0.0) && (par->
g == 1.0)) {
291 }
292 #if HAVE_MMX
295 }
296 #endif
297 else {
299 }
300 }
301
302 static
304 {
307 );
308 }
309
310 static
312 {
318 }
319
320 static
322 {
328 }
329
330 static
332 {
334
339
343
347
349 }
350
351 static
353 {
355
358
361
364
366 }
367
368 static
370 {
372
373 switch (request) {
376
377 if (strcmp (eq->
item,
"gamma") == 0) {
380 }
381 else if (strcmp (eq->
item,
"contrast") == 0) {
384 }
385 else if (strcmp (eq->
item,
"brightness") == 0) {
388 }
389 else if (strcmp (eq->
item,
"saturation") == 0) {
392 }
393 break;
394
397 if (strcmp (eq->
item,
"gamma") == 0) {
398 eq->
value = (int) (100.0 * log (vf->
priv->gamma) / log (8.0));
400 }
401 else if (strcmp (eq->
item,
"contrast") == 0) {
404 }
405 else if (strcmp (eq->
item,
"brightness") == 0) {
408 }
409 else if (strcmp (eq->
item,
"saturation") == 0) {
410 eq->
value = (int) (100.0 * vf->
priv->saturation) - 100;
412 }
413 break;
414 }
415
417 }
418
419 static
421 {
422 switch (fmt) {
434 }
435
436 return 0;
437 }
438
439 static
441 {
442 if (vf->
priv != NULL) {
445 }
446 }
447
448 static
450 {
451 unsigned i;
453 double par[8];
454
459
462
463 for (i = 0; i < 3; i++) {
467
473 }
474
478
484
485 if (args != NULL) {
486 par[0] = 1.0;
487 par[1] = 1.0;
488 par[2] = 0.0;
489 par[3] = 1.0;
490 par[4] = 1.0;
491 par[5] = 1.0;
492 par[6] = 1.0;
493 par[7] = 1.0;
494 sscanf (args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf",
495 par, par + 1, par + 2, par + 3, par + 4, par + 5, par + 6, par + 7
496 );
497
502
507 }
508
509 return 1;
510 }
511
513 "Software equalizer",
514 "eq2",
515 "Hampa Hug, Daniel Moreno, Richard Felker",
516 "",
518 NULL
519 };