changeset 7517:9d433771b6d0

-vf eq2, LUT-based brightness/contrast/gamma correction (Y-only) by Hampa Hug <hhug@student.ethz.ch>
author arpi
date Fri, 27 Sep 2002 21:08:36 +0000
parents 657041b7236b
children efa56e59a01c
files input/input.c input/input.h libmpcodecs/Makefile libmpcodecs/vf.c libmpcodecs/vf_eq2.c mplayer.c
diffstat 6 files changed, 275 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/input/input.c	Fri Sep 27 20:57:00 2002 +0000
+++ b/input/input.c	Fri Sep 27 21:08:36 2002 +0000
@@ -53,6 +53,7 @@
   { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
   { MP_CMD_MUTE, "mute", 0, { {-1,{0}} } },
   { MP_CMD_CONTRAST, "contrast",1,  { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
+  { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} }  },
   { MP_CMD_BRIGHTNESS, "brightness",1,  { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} }  },
   { MP_CMD_HUE, "hue",1,  { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
   { MP_CMD_SATURATION, "saturation",1,  { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} }  },
--- a/input/input.h	Fri Sep 27 20:57:00 2002 +0000
+++ b/input/input.h	Fri Sep 27 21:08:36 2002 +0000
@@ -28,6 +28,7 @@
 #define MP_CMD_LOADFILE 26
 #define MP_CMD_LOADLIST 27
 #define MP_CMD_VF_CHANGE_RECTANGLE 28
+#define MP_CMD_GAMMA 29
 
 #define MP_CMD_GUI_EVENTS       5000
 #define MP_CMD_GUI_LOADFILE     5001
--- a/libmpcodecs/Makefile	Fri Sep 27 20:57:00 2002 +0000
+++ b/libmpcodecs/Makefile	Fri Sep 27 21:08:36 2002 +0000
@@ -6,7 +6,7 @@
 
 AUDIO_SRCS=dec_audio.c ad.c ad_liba52.c ad_acm.c ad_alaw.c ad_dk3adpcm.c ad_dshow.c ad_dvdpcm.c ad_ffmpeg.c ad_hwac3.c ad_imaadpcm.c ad_mp3lib.c ad_msadpcm.c ad_pcm.c ad_roqaudio.c ad_msgsm.c ad_faad.c ad_libvorbis.c ad_libmad.c ad_realaud.c ad_libdv.c
 VIDEO_SRCS=dec_video.c vd.c vd_null.c vd_realvid.c vd_cinepak.c vd_qtrpza.c vd_ffmpeg.c vd_dshow.c vd_vfw.c vd_vfwex.c vd_odivx.c vd_divx4.c vd_raw.c vd_xanim.c vd_msvidc.c vd_fli.c vd_qtrle.c vd_qtsmc.c vd_roqvideo.c vd_cyuv.c vd_nuv.c vd_libmpeg2.c vd_msrle.c vd_huffyuv.c vd_mpegpes.c vd_svq1.c vd_xvid.c vd_libdv.c vd_lcl.c vd_mtga.c
-VFILTER_SRCS=vf.c vf_vo.c vf_crop.c vf_expand.c vf_pp.c vf_scale.c vf_format.c vf_yuy2.c vf_flip.c vf_rgb2bgr.c vf_rotate.c vf_mirror.c vf_palette.c vf_lavc.c vf_dvbscale.c vf_cropdetect.c vf_test.c vf_noise.c vf_yvu9.c vf_rectangle.c vf_lavcdeint.c vf_eq.c vf_halfpack.c vf_dint.c
+VFILTER_SRCS=vf.c vf_vo.c vf_crop.c vf_expand.c vf_pp.c vf_scale.c vf_format.c vf_yuy2.c vf_flip.c vf_rgb2bgr.c vf_rotate.c vf_mirror.c vf_palette.c vf_lavc.c vf_dvbscale.c vf_cropdetect.c vf_test.c vf_noise.c vf_yvu9.c vf_rectangle.c vf_lavcdeint.c vf_eq.c vf_eq2.c vf_halfpack.c vf_dint.c
 ENCODER_SRCS=ve.c ve_divx4.c ve_lavc.c ve_vfw.c ve_rawrgb.c ve_libdv.c ve_xvid.c
 NATIVE_SRCS=native/RTjpegN.c native/cinepak.c native/cyuv.c native/fli.c native/minilzo.c native/msvidc.c native/nuppelvideo.c native/qtrle.c native/qtrpza.c native/qtsmc.c native/roqav.c native/xa_gsm.c native/svq1.c
 
--- a/libmpcodecs/vf.c	Fri Sep 27 20:57:00 2002 +0000
+++ b/libmpcodecs/vf.c	Fri Sep 27 21:08:36 2002 +0000
@@ -39,6 +39,7 @@
 extern vf_info_t vf_info_yvu9;
 extern vf_info_t vf_info_lavcdeint;
 extern vf_info_t vf_info_eq;
+extern vf_info_t vf_info_eq2;
 extern vf_info_t vf_info_halfpack;
 extern vf_info_t vf_info_dint;
 
@@ -73,6 +74,7 @@
     &vf_info_noise,
     &vf_info_yvu9,
     &vf_info_eq,
+    &vf_info_eq2,
     &vf_info_halfpack,
     &vf_info_dint,
     NULL
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmpcodecs/vf_eq2.c	Fri Sep 27 21:08:36 2002 +0000
@@ -0,0 +1,236 @@
+/*
+ * vf_eq2.c
+ *
+ * Extended software equalizer (brightness, contrast, gamma)
+ *
+ * Hampa Hug <hhug@student.ethz.ch>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include "../config.h"
+#include "../mp_msg.h"
+
+#include "img_format.h"
+#include "mp_image.h"
+#include "vf.h"
+
+
+typedef struct vf_priv_s {
+  unsigned char *buf;
+  int           buf_w;
+  int           buf_h;
+
+  double        contrast;
+  double        bright;
+  double        gamma;
+
+  unsigned char lut[256];
+} vf_eq2_t;
+
+
+static
+void create_lut (vf_eq2_t *eq2)
+{
+  unsigned i;
+  double   c, b, g;
+  double   v;
+
+  c = eq2->contrast;
+  b = eq2->bright;
+  g = eq2->gamma;
+
+  if ((g < 0.001) || (g > 1000.0)) {
+    g = 1.0;
+  }
+
+  fprintf (stderr, "vf_eq2: c=%.2f b=%.2f g=%.4f\n", c, b, g);
+
+  g = 1.0 / g;
+
+  for (i = 0; i < 256; i++) {
+    v = (double) i / 255.0;
+    v = c * (v - 0.5) + 0.5 + b;
+
+    if (v <= 0.0) {
+      eq2->lut[i] = 0;
+    }
+    else {
+      v = pow (v, g);
+
+      if (v >= 1.0) {
+        eq2->lut[i] = 255;
+      }
+      else {
+        eq2->lut[i] = (unsigned char) (256.0 * v);
+      }
+    }
+  }
+}
+
+/* could inline this */
+static
+void process (unsigned char *dst, int dstride, unsigned char *src, int sstride,
+  int w, int h, unsigned char lut[256])
+{
+  int i, j;
+
+  for (j = 0; j < h; j++) {
+    for (i = 0; i < w; i++) {
+      *(dst++) = lut[*(src++)];
+    }
+
+    src += sstride - w;
+    dst += dstride - w;
+  }
+}
+
+static
+int put_image (vf_instance_t *vf, mp_image_t *src)
+{
+  mp_image_t *dst;
+  vf_eq2_t   *eq2;
+
+  eq2 = vf->priv;
+
+  if ((eq2->buf == NULL) || (eq2->buf_w != src->stride[0]) || (eq2->buf_h != src->h)) {
+    eq2->buf = (unsigned char *) realloc (eq2->buf, src->stride[0] * src->h);
+    eq2->buf_w = src->stride[0];
+    eq2->buf_h = src->h;
+  }
+
+  dst = vf_get_image (vf->next, src->imgfmt, MP_IMGTYPE_EXPORT, 0, src->w, src->h);
+
+  dst->stride[0] = src->stride[0];
+  dst->stride[1] = src->stride[1];
+  dst->stride[2] = src->stride[2];
+  dst->planes[0] = vf->priv->buf;
+  dst->planes[1] = src->planes[1];
+  dst->planes[2] = src->planes[2];
+
+  process (
+    dst->planes[0], dst->stride[0], src->planes[0], src->stride[0],
+    src->w, src->h, eq2->lut
+  );
+
+  return vf_next_put_image (vf, dst);
+}
+
+static
+int control (vf_instance_t *vf, int request, void *data)
+{
+  vf_equalizer_t *eq;
+
+  switch (request) {
+    case VFCTRL_SET_EQUALIZER:
+      eq = (vf_equalizer_t *) data;
+
+      if (strcmp (eq->item, "gamma") == 0) {
+        vf->priv->gamma = exp (log (8.0) * eq->value / 100.0);
+        create_lut (vf->priv);
+        return CONTROL_TRUE;
+      }
+      else if (strcmp (eq->item, "contrast") == 0) {
+        vf->priv->contrast = (1.0 / 100.0) * (eq->value + 100);
+        create_lut (vf->priv);
+        return CONTROL_TRUE;
+      }
+      else if (strcmp (eq->item, "brightness") == 0) {
+        vf->priv->bright = (1.0 / 100.0) * eq->value;
+        create_lut (vf->priv);
+        return CONTROL_TRUE;
+      }
+      break;
+
+    case VFCTRL_GET_EQUALIZER:
+      eq = (vf_equalizer_t *) data;
+      if (strcmp (eq->item, "gamma") == 0) {
+        eq->value = (int) (100.0 * log (vf->priv->gamma) / log (8.0));
+        return CONTROL_TRUE;
+      }
+      else if (strcmp (eq->item, "contrast") == 0) {
+        eq->value = (int) (100.0 * vf->priv->contrast) - 100;
+        return CONTROL_TRUE;
+      }
+      else if (strcmp (eq->item, "brightness") == 0) {
+        eq->value = (int) (100.0 * vf->priv->bright);
+        return CONTROL_TRUE;
+      }
+      break;
+  }
+
+  return vf_next_control (vf, request, data);
+}
+
+static
+int query_format (vf_instance_t *vf, unsigned fmt)
+{
+  switch (fmt) {
+    case IMGFMT_YVU9:
+    case IMGFMT_IF09:
+    case IMGFMT_YV12:
+    case IMGFMT_I420:
+    case IMGFMT_IYUV:
+    case IMGFMT_CLPL:
+    case IMGFMT_Y800:
+    case IMGFMT_Y8:
+    case IMGFMT_NV12:
+    case IMGFMT_444P:
+    case IMGFMT_422P:
+    case IMGFMT_411P:
+      return vf_next_query_format (vf, fmt);
+  }
+
+  return 0;
+}
+
+static
+void uninit (vf_instance_t *vf)
+{
+  if (vf->priv != NULL) {
+    free (vf->priv->buf);
+    free (vf->priv);
+  }
+}
+
+static
+int open (vf_instance_t *vf, char *args)
+{
+  vf_eq2_t *eq2;
+
+  vf->control = control;
+  vf->query_format = query_format;
+  vf->put_image = put_image;
+  vf->uninit = uninit;
+
+  vf->priv = (vf_eq2_t *) malloc (sizeof (vf_eq2_t));
+  eq2 = vf->priv;
+
+  eq2->buf = NULL;
+  eq2->buf_w = 0;
+  eq2->buf_h = 0;
+
+  eq2->gamma = 1.0;
+  eq2->contrast = 1.0;
+  eq2->bright = 0.0;
+
+  if (args != NULL) {
+    sscanf (args, "%lf:%lf:%lf", &eq2->gamma, &eq2->contrast, &eq2->bright);
+  }
+
+  create_lut (eq2);
+
+  return 1;
+}
+
+vf_info_t vf_info_eq2 = {
+  "extended software equalizer",
+  "eq2",
+  "Hampa Hug",
+  "",
+  &open
+};
--- a/mplayer.c	Fri Sep 27 20:57:00 2002 +0000
+++ b/mplayer.c	Fri Sep 27 21:08:36 2002 +0000
@@ -251,6 +251,7 @@
 
 char* current_module=NULL; // for debugging
 
+int vo_gamma_gamma = 1000;
 int vo_gamma_brightness = 1000;
 int vo_gamma_contrast = 1000;
 int vo_gamma_saturation = 1000;
@@ -1268,6 +1269,8 @@
 current_module="init_vo";
     if (sh_video)
     {
+        if (vo_gamma_gamma != 1000)
+          set_video_colors (sh_video, "gamma", vo_gamma_gamma);
 	if (vo_gamma_brightness != 1000)
 	    set_video_colors(sh_video, "brightness", vo_gamma_brightness);
 	if (vo_gamma_contrast != 1000)
@@ -1985,6 +1988,37 @@
 	eof = PT_NEXT_SRC;	
       }
     } break;
+    case MP_CMD_GAMMA :  {
+      int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;
+
+      if (!sh_video)
+	break;
+
+      if (vo_gamma_gamma == 1000)
+      {
+	vo_gamma_gamma = 0;
+	get_video_colors (sh_video, "gamma", &vo_gamma_gamma);
+      }
+
+      if (abs)
+        vo_gamma_gamma = v;
+      else
+        vo_gamma_gamma += v;
+
+      if (vo_gamma_gamma > 100)
+        vo_gamma_gamma = 100;
+      else if (vo_gamma_gamma < -100)
+        vo_gamma_gamma = -100;
+      set_video_colors(sh_video, "gamma", vo_gamma_gamma);
+#ifdef USE_OSD
+       if(osd_level){
+	 osd_visible=sh_video->fps; // 1 sec
+	 vo_osd_progbar_type=OSD_BRIGHTNESS;
+	 vo_osd_progbar_value=(vo_gamma_gamma<<7)/100 + 128;
+	 vo_osd_changed(OSDTYPE_PROGBAR);
+       }
+#endif // USE_OSD
+    } break;
     case MP_CMD_BRIGHTNESS :  {
       int v = cmd->args[0].v.i, abs = cmd->args[1].v.i;