00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <math.h>
00030 #include <inttypes.h>
00031
00032 #include "config.h"
00033 #include "mp_msg.h"
00034 #include "cpudetect.h"
00035
00036 #include "img_format.h"
00037 #include "mp_image.h"
00038 #include "vf.h"
00039
00040 #define LUT16
00041
00042
00043 typedef struct eq2_param_t {
00044 unsigned char lut[256];
00045 #ifdef LUT16
00046 uint16_t lut16[256*256];
00047 #endif
00048 int lut_clean;
00049
00050 void (*adjust) (struct eq2_param_t *par, unsigned char *dst, unsigned char *src,
00051 unsigned w, unsigned h, unsigned dstride, unsigned sstride);
00052
00053 double c;
00054 double b;
00055 double g;
00056 double w;
00057 } eq2_param_t;
00058
00059 typedef struct vf_priv_s {
00060 eq2_param_t param[3];
00061
00062 double contrast;
00063 double brightness;
00064 double saturation;
00065
00066 double gamma;
00067 double gamma_weight;
00068 double rgamma;
00069 double ggamma;
00070 double bgamma;
00071
00072 unsigned buf_w[3];
00073 unsigned buf_h[3];
00074 unsigned char *buf[3];
00075 } vf_eq2_t;
00076
00077
00078 static
00079 void create_lut (eq2_param_t *par)
00080 {
00081 unsigned i;
00082 double g, v;
00083 double lw, gw;
00084
00085 g = par->g;
00086 gw = par->w;
00087 lw = 1.0 - gw;
00088
00089 if ((g < 0.001) || (g > 1000.0)) {
00090 g = 1.0;
00091 }
00092
00093 g = 1.0 / g;
00094
00095 for (i = 0; i < 256; i++) {
00096 v = (double) i / 255.0;
00097 v = par->c * (v - 0.5) + 0.5 + par->b;
00098
00099 if (v <= 0.0) {
00100 par->lut[i] = 0;
00101 }
00102 else {
00103 v = v*lw + pow(v, g)*gw;
00104
00105 if (v >= 1.0) {
00106 par->lut[i] = 255;
00107 }
00108 else {
00109 par->lut[i] = (unsigned char) (256.0 * v);
00110 }
00111 }
00112 }
00113
00114 #ifdef LUT16
00115 for(i=0; i<256*256; i++){
00116 par->lut16[i]= par->lut[i&0xFF] + (par->lut[i>>8]<<8);
00117 }
00118 #endif
00119
00120 par->lut_clean = 1;
00121 }
00122
00123 #if HAVE_MMX
00124 static
00125 void affine_1d_MMX (eq2_param_t *par, unsigned char *dst, unsigned char *src,
00126 unsigned w, unsigned h, unsigned dstride, unsigned sstride)
00127 {
00128 unsigned i;
00129 int contrast, brightness;
00130 unsigned dstep, sstep;
00131 int pel;
00132 short brvec[4];
00133 short contvec[4];
00134
00135
00136
00137 contrast = (int) (par->c * 256 * 16);
00138 brightness = ((int) (100.0 * par->b + 100.0) * 511) / 200 - 128 - contrast / 32;
00139
00140 brvec[0] = brvec[1] = brvec[2] = brvec[3] = brightness;
00141 contvec[0] = contvec[1] = contvec[2] = contvec[3] = contrast;
00142
00143 sstep = sstride - w;
00144 dstep = dstride - w;
00145
00146 while (h-- > 0) {
00147 __asm__ volatile (
00148 "movq (%5), %%mm3 \n\t"
00149 "movq (%6), %%mm4 \n\t"
00150 "pxor %%mm0, %%mm0 \n\t"
00151 "movl %4, %%eax\n\t"
00152 ASMALIGN(4)
00153 "1: \n\t"
00154 "movq (%0), %%mm1 \n\t"
00155 "movq (%0), %%mm2 \n\t"
00156 "punpcklbw %%mm0, %%mm1 \n\t"
00157 "punpckhbw %%mm0, %%mm2 \n\t"
00158 "psllw $4, %%mm1 \n\t"
00159 "psllw $4, %%mm2 \n\t"
00160 "pmulhw %%mm4, %%mm1 \n\t"
00161 "pmulhw %%mm4, %%mm2 \n\t"
00162 "paddw %%mm3, %%mm1 \n\t"
00163 "paddw %%mm3, %%mm2 \n\t"
00164 "packuswb %%mm2, %%mm1 \n\t"
00165 "add $8, %0 \n\t"
00166 "movq %%mm1, (%1) \n\t"
00167 "add $8, %1 \n\t"
00168 "decl %%eax \n\t"
00169 "jnz 1b \n\t"
00170 : "=r" (src), "=r" (dst)
00171 : "0" (src), "1" (dst), "r" (w >> 3), "r" (brvec), "r" (contvec)
00172 : "%eax"
00173 );
00174
00175 for (i = w & 7; i > 0; i--) {
00176 pel = ((*src++ * contrast) >> 12) + brightness;
00177 if (pel & 768) {
00178 pel = (-pel) >> 31;
00179 }
00180 *dst++ = pel;
00181 }
00182
00183 src += sstep;
00184 dst += dstep;
00185 }
00186
00187 __asm__ volatile ( "emms \n\t" ::: "memory" );
00188 }
00189 #endif
00190
00191 static
00192 void apply_lut (eq2_param_t *par, unsigned char *dst, unsigned char *src,
00193 unsigned w, unsigned h, unsigned dstride, unsigned sstride)
00194 {
00195 unsigned i, j, w2;
00196 unsigned char *lut;
00197 uint16_t *lut16;
00198
00199 if (!par->lut_clean) {
00200 create_lut (par);
00201 }
00202
00203 lut = par->lut;
00204 #ifdef LUT16
00205 lut16 = par->lut16;
00206 w2= (w>>3)<<2;
00207 for (j = 0; j < h; j++) {
00208 uint16_t *src16= (uint16_t*)src;
00209 uint16_t *dst16= (uint16_t*)dst;
00210 for (i = 0; i < w2; i+=4) {
00211 dst16[i+0] = lut16[src16[i+0]];
00212 dst16[i+1] = lut16[src16[i+1]];
00213 dst16[i+2] = lut16[src16[i+2]];
00214 dst16[i+3] = lut16[src16[i+3]];
00215 }
00216 i <<= 1;
00217 #else
00218 w2= (w>>3)<<3;
00219 for (j = 0; j < h; j++) {
00220 for (i = 0; i < w2; i+=8) {
00221 dst[i+0] = lut[src[i+0]];
00222 dst[i+1] = lut[src[i+1]];
00223 dst[i+2] = lut[src[i+2]];
00224 dst[i+3] = lut[src[i+3]];
00225 dst[i+4] = lut[src[i+4]];
00226 dst[i+5] = lut[src[i+5]];
00227 dst[i+6] = lut[src[i+6]];
00228 dst[i+7] = lut[src[i+7]];
00229 }
00230 #endif
00231 for (; i < w; i++) {
00232 dst[i] = lut[src[i]];
00233 }
00234
00235 src += sstride;
00236 dst += dstride;
00237 }
00238 }
00239
00240 static
00241 int put_image (vf_instance_t *vf, mp_image_t *src, double pts)
00242 {
00243 unsigned i;
00244 vf_eq2_t *eq2;
00245 mp_image_t *dst;
00246 unsigned long img_n,img_c;
00247
00248 eq2 = vf->priv;
00249
00250 if ((eq2->buf_w[0] != src->w) || (eq2->buf_h[0] != src->h)) {
00251 eq2->buf_w[0] = src->w;
00252 eq2->buf_h[0] = src->h;
00253 eq2->buf_w[1] = eq2->buf_w[2] = src->w >> src->chroma_x_shift;
00254 eq2->buf_h[1] = eq2->buf_h[2] = src->h >> src->chroma_y_shift;
00255 img_n = eq2->buf_w[0]*eq2->buf_h[0];
00256 if(src->num_planes>1){
00257 img_c = eq2->buf_w[1]*eq2->buf_h[1];
00258 eq2->buf[0] = realloc (eq2->buf[0], img_n + 2*img_c);
00259 eq2->buf[1] = eq2->buf[0] + img_n;
00260 eq2->buf[2] = eq2->buf[1] + img_c;
00261 } else
00262 eq2->buf[0] = realloc (eq2->buf[0], img_n);
00263 }
00264
00265 dst = vf_get_image (vf->next, src->imgfmt, MP_IMGTYPE_EXPORT, 0, src->w, src->h);
00266
00267 for (i = 0; i < ((src->num_planes>1)?3:1); i++) {
00268 if (eq2->param[i].adjust != NULL) {
00269 dst->planes[i] = eq2->buf[i];
00270 dst->stride[i] = eq2->buf_w[i];
00271
00272 eq2->param[i].adjust (&eq2->param[i], dst->planes[i], src->planes[i],
00273 eq2->buf_w[i], eq2->buf_h[i], dst->stride[i], src->stride[i]);
00274 }
00275 else {
00276 dst->planes[i] = src->planes[i];
00277 dst->stride[i] = src->stride[i];
00278 }
00279 }
00280
00281 return vf_next_put_image (vf, dst, pts);
00282 }
00283
00284 static
00285 void check_values (eq2_param_t *par)
00286 {
00287
00288
00289 if ((par->c == 1.0) && (par->b == 0.0) && (par->g == 1.0)) {
00290 par->adjust = NULL;
00291 }
00292 #if HAVE_MMX
00293 else if (par->g == 1.0 && gCpuCaps.hasMMX) {
00294 par->adjust = &affine_1d_MMX;
00295 }
00296 #endif
00297 else {
00298 par->adjust = &apply_lut;
00299 }
00300 }
00301
00302 static
00303 void print_values (vf_eq2_t *eq2)
00304 {
00305 mp_msg (MSGT_VFILTER, MSGL_V, "vf_eq2: c=%.2f b=%.2f g=%.4f s=%.2f \n",
00306 eq2->contrast, eq2->brightness, eq2->gamma, eq2->saturation
00307 );
00308 }
00309
00310 static
00311 void set_contrast (vf_eq2_t *eq2, double c)
00312 {
00313 eq2->contrast = c;
00314 eq2->param[0].c = c;
00315 eq2->param[0].lut_clean = 0;
00316 check_values (&eq2->param[0]);
00317 print_values (eq2);
00318 }
00319
00320 static
00321 void set_brightness (vf_eq2_t *eq2, double b)
00322 {
00323 eq2->brightness = b;
00324 eq2->param[0].b = b;
00325 eq2->param[0].lut_clean = 0;
00326 check_values (&eq2->param[0]);
00327 print_values (eq2);
00328 }
00329
00330 static
00331 void set_gamma (vf_eq2_t *eq2, double g)
00332 {
00333 eq2->gamma = g;
00334
00335 eq2->param[0].g = eq2->gamma * eq2->ggamma;
00336 eq2->param[1].g = sqrt (eq2->bgamma / eq2->ggamma);
00337 eq2->param[2].g = sqrt (eq2->rgamma / eq2->ggamma);
00338 eq2->param[0].w = eq2->param[1].w = eq2->param[2].w = eq2->gamma_weight;
00339
00340 eq2->param[0].lut_clean = 0;
00341 eq2->param[1].lut_clean = 0;
00342 eq2->param[2].lut_clean = 0;
00343
00344 check_values (&eq2->param[0]);
00345 check_values (&eq2->param[1]);
00346 check_values (&eq2->param[2]);
00347
00348 print_values (eq2);
00349 }
00350
00351 static
00352 void set_saturation (vf_eq2_t *eq2, double s)
00353 {
00354 eq2->saturation = s;
00355
00356 eq2->param[1].c = s;
00357 eq2->param[2].c = s;
00358
00359 eq2->param[1].lut_clean = 0;
00360 eq2->param[2].lut_clean = 0;
00361
00362 check_values (&eq2->param[1]);
00363 check_values (&eq2->param[2]);
00364
00365 print_values (eq2);
00366 }
00367
00368 static
00369 int control (vf_instance_t *vf, int request, void *data)
00370 {
00371 vf_equalizer_t *eq;
00372
00373 switch (request) {
00374 case VFCTRL_SET_EQUALIZER:
00375 eq = (vf_equalizer_t *) data;
00376
00377 if (strcmp (eq->item, "gamma") == 0) {
00378 set_gamma (vf->priv, exp (log (8.0) * eq->value / 100.0));
00379 return CONTROL_TRUE;
00380 }
00381 else if (strcmp (eq->item, "contrast") == 0) {
00382 set_contrast (vf->priv, (1.0 / 100.0) * (eq->value + 100));
00383 return CONTROL_TRUE;
00384 }
00385 else if (strcmp (eq->item, "brightness") == 0) {
00386 set_brightness (vf->priv, (1.0 / 100.0) * eq->value);
00387 return CONTROL_TRUE;
00388 }
00389 else if (strcmp (eq->item, "saturation") == 0) {
00390 set_saturation (vf->priv, (double) (eq->value + 100) / 100.0);
00391 return CONTROL_TRUE;
00392 }
00393 break;
00394
00395 case VFCTRL_GET_EQUALIZER:
00396 eq = (vf_equalizer_t *) data;
00397 if (strcmp (eq->item, "gamma") == 0) {
00398 eq->value = (int) (100.0 * log (vf->priv->gamma) / log (8.0));
00399 return CONTROL_TRUE;
00400 }
00401 else if (strcmp (eq->item, "contrast") == 0) {
00402 eq->value = (int) (100.0 * vf->priv->contrast) - 100;
00403 return CONTROL_TRUE;
00404 }
00405 else if (strcmp (eq->item, "brightness") == 0) {
00406 eq->value = (int) (100.0 * vf->priv->brightness);
00407 return CONTROL_TRUE;
00408 }
00409 else if (strcmp (eq->item, "saturation") == 0) {
00410 eq->value = (int) (100.0 * vf->priv->saturation) - 100;
00411 return CONTROL_TRUE;
00412 }
00413 break;
00414 }
00415
00416 return vf_next_control (vf, request, data);
00417 }
00418
00419 static
00420 int query_format (vf_instance_t *vf, unsigned fmt)
00421 {
00422 switch (fmt) {
00423 case IMGFMT_YVU9:
00424 case IMGFMT_IF09:
00425 case IMGFMT_YV12:
00426 case IMGFMT_I420:
00427 case IMGFMT_IYUV:
00428 case IMGFMT_Y800:
00429 case IMGFMT_Y8:
00430 case IMGFMT_444P:
00431 case IMGFMT_422P:
00432 case IMGFMT_411P:
00433 return vf_next_query_format (vf, fmt);
00434 }
00435
00436 return 0;
00437 }
00438
00439 static
00440 void uninit (vf_instance_t *vf)
00441 {
00442 if (vf->priv != NULL) {
00443 free (vf->priv->buf[0]);
00444 free (vf->priv);
00445 }
00446 }
00447
00448 static
00449 int vf_open(vf_instance_t *vf, char *args)
00450 {
00451 unsigned i;
00452 vf_eq2_t *eq2;
00453 double par[8];
00454
00455 vf->control = control;
00456 vf->query_format = query_format;
00457 vf->put_image = put_image;
00458 vf->uninit = uninit;
00459
00460 vf->priv = malloc (sizeof (vf_eq2_t));
00461 eq2 = vf->priv;
00462
00463 for (i = 0; i < 3; i++) {
00464 eq2->buf[i] = NULL;
00465 eq2->buf_w[i] = 0;
00466 eq2->buf_h[i] = 0;
00467
00468 eq2->param[i].adjust = NULL;
00469 eq2->param[i].c = 1.0;
00470 eq2->param[i].b = 0.0;
00471 eq2->param[i].g = 1.0;
00472 eq2->param[i].lut_clean = 0;
00473 }
00474
00475 eq2->contrast = 1.0;
00476 eq2->brightness = 0.0;
00477 eq2->saturation = 1.0;
00478
00479 eq2->gamma = 1.0;
00480 eq2->gamma_weight = 1.0;
00481 eq2->rgamma = 1.0;
00482 eq2->ggamma = 1.0;
00483 eq2->bgamma = 1.0;
00484
00485 if (args != NULL) {
00486 par[0] = 1.0;
00487 par[1] = 1.0;
00488 par[2] = 0.0;
00489 par[3] = 1.0;
00490 par[4] = 1.0;
00491 par[5] = 1.0;
00492 par[6] = 1.0;
00493 par[7] = 1.0;
00494 sscanf (args, "%lf:%lf:%lf:%lf:%lf:%lf:%lf:%lf",
00495 par, par + 1, par + 2, par + 3, par + 4, par + 5, par + 6, par + 7
00496 );
00497
00498 eq2->rgamma = par[4];
00499 eq2->ggamma = par[5];
00500 eq2->bgamma = par[6];
00501 eq2->gamma_weight = par[7];
00502
00503 set_gamma (eq2, par[0]);
00504 set_contrast (eq2, par[1]);
00505 set_brightness (eq2, par[2]);
00506 set_saturation (eq2, par[3]);
00507 }
00508
00509 return 1;
00510 }
00511
00512 const vf_info_t vf_info_eq2 = {
00513 "Software equalizer",
00514 "eq2",
00515 "Hampa Hug, Daniel Moreno, Richard Felker",
00516 "",
00517 &vf_open,
00518 NULL
00519 };