gstgamma.c 11.7 KB
Newer Older
David Schleef's avatar
David Schleef committed
1 2 3 4
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) <2003> David Schleef <ds@schleef.org>
 * Copyright (C) 2003 Arwed v. Merkatz <v.merkatz@gmx.net>
5
 * Copyright (C) 2006 Mark Nauwelaerts <manauw@skynet.be>
Sebastian Dröge's avatar
Sebastian Dröge committed
6
 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
David Schleef's avatar
David Schleef committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
David Schleef's avatar
David Schleef committed
22 23 24 25 26 27 28 29 30
 */

/*
 * This file was (probably) generated from
 * gstvideotemplate.c,v 1.12 2004/01/07 21:07:12 ds Exp 
 * and
 * make_filter,v 1.6 2004/01/07 21:33:01 ds Exp 
 */

31 32 33 34
/**
 * SECTION:element-gamma
 *
 * Performs gamma correction on a video stream.
35 36
 *
 * <refsect2>
37
 * <title>Example launch line</title>
38
 * |[
39
 * gst-launch-1.0 videotestsrc ! gamma gamma=2.0 ! videoconvert ! ximagesink
40
 * ]| This pipeline will make the image "brighter".
Sebastian Dröge's avatar
Sebastian Dröge committed
41
 * |[
42
 * gst-launch-1.0 videotestsrc ! gamma gamma=0.5 ! videoconvert ! ximagesink
Sebastian Dröge's avatar
Sebastian Dröge committed
43
 * ]| This pipeline will make the image "darker".
44 45 46
 * </refsect2>
 */

David Schleef's avatar
David Schleef committed
47 48 49 50
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

51
#include "gstgamma.h"
David Schleef's avatar
David Schleef committed
52 53 54
#include <string.h>
#include <math.h>

55
#include <gst/video/video.h>
David Schleef's avatar
David Schleef committed
56

57 58
GST_DEBUG_CATEGORY_STATIC (gamma_debug);
#define GST_CAT_DEFAULT gamma_debug
David Schleef's avatar
David Schleef committed
59

60
/* GstGamma properties */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61 62
enum
{
63 64
  PROP_0,
  PROP_GAMMA
65
      /* FILL ME */
David Schleef's avatar
David Schleef committed
66 67
};

68 69 70
#define DEFAULT_PROP_GAMMA  1

static GstStaticPadTemplate gst_gamma_src_template =
Wim Taymans's avatar
Wim Taymans committed
71
GST_STATIC_PAD_TEMPLATE ("src",
72 73
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Wim Taymans's avatar
Wim Taymans committed
74
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
75
            "ARGB, BGRA, ABGR, RGBA, Y444, "
Wim Taymans's avatar
Wim Taymans committed
76 77
            "xRGB, RGBx, xBGR, BGRx, RGB, BGR, Y42B, NV12, "
            "NV21, YUY2, UYVY, YVYU, I420, YV12, IYUV, Y41B }"))
78 79 80
    );

static GstStaticPadTemplate gst_gamma_sink_template =
Wim Taymans's avatar
Wim Taymans committed
81
GST_STATIC_PAD_TEMPLATE ("sink",
82 83
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Wim Taymans's avatar
Wim Taymans committed
84
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
85
            "ARGB, BGRA, ABGR, RGBA, Y444, "
Wim Taymans's avatar
Wim Taymans committed
86 87
            "xRGB, RGBx, xBGR, BGRx, RGB, BGR, Y42B, NV12, "
            "NV21, YUY2, UYVY, YVYU, I420, YV12, IYUV, Y41B }"))
88
    );
David Schleef's avatar
David Schleef committed
89

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
90 91 92 93
static void gst_gamma_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_gamma_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
David Schleef's avatar
David Schleef committed
94

95 96 97 98
static gboolean gst_gamma_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
    GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info);
static GstFlowReturn gst_gamma_transform_frame_ip (GstVideoFilter * vfilter,
    GstVideoFrame * frame);
99 100
static void gst_gamma_before_transform (GstBaseTransform * transform,
    GstBuffer * buf);
David Schleef's avatar
David Schleef committed
101

102
static void gst_gamma_calculate_tables (GstGamma * gamma);
David Schleef's avatar
David Schleef committed
103

104
G_DEFINE_TYPE (GstGamma, gst_gamma, GST_TYPE_VIDEO_FILTER);
David Schleef's avatar
David Schleef committed
105 106

static void
107
gst_gamma_class_init (GstGammaClass * g_class)
David Schleef's avatar
David Schleef committed
108
{
109
  GObjectClass *gobject_class = (GObjectClass *) g_class;
110
  GstElementClass *gstelement_class = (GstElementClass *) g_class;
111
  GstBaseTransformClass *trans_class = (GstBaseTransformClass *) g_class;
112
  GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) g_class;
David Schleef's avatar
David Schleef committed
113

114 115
  GST_DEBUG_CATEGORY_INIT (gamma_debug, "gamma", 0, "gamma");

116 117 118
  gobject_class->set_property = gst_gamma_set_property;
  gobject_class->get_property = gst_gamma_get_property;

119
  g_object_class_install_property (gobject_class, PROP_GAMMA,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
120
      g_param_spec_double ("gamma", "Gamma", "gamma",
121
          0.01, 10, DEFAULT_PROP_GAMMA,
122
          GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
123

124
  gst_element_class_set_static_metadata (gstelement_class,
125 126 127 128 129 130 131 132
      "Video gamma correction", "Filter/Effect/Video",
      "Adjusts gamma on a video stream", "Arwed v. Merkatz <v.merkatz@gmx.net");

  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_gamma_sink_template));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_gamma_src_template));

133 134
  trans_class->before_transform =
      GST_DEBUG_FUNCPTR (gst_gamma_before_transform);
135
  trans_class->transform_ip_on_passthrough = FALSE;
136 137 138 139

  vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_gamma_set_info);
  vfilter_class->transform_frame_ip =
      GST_DEBUG_FUNCPTR (gst_gamma_transform_frame_ip);
David Schleef's avatar
David Schleef committed
140 141 142
}

static void
143
gst_gamma_init (GstGamma * gamma)
David Schleef's avatar
David Schleef committed
144
{
145 146
  /* properties */
  gamma->gamma = DEFAULT_PROP_GAMMA;
147
  gst_gamma_calculate_tables (gamma);
David Schleef's avatar
David Schleef committed
148 149 150
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
151 152
gst_gamma_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
David Schleef's avatar
David Schleef committed
153
{
154
  GstGamma *gamma = GST_GAMMA (object);
David Schleef's avatar
David Schleef committed
155 156

  switch (prop_id) {
157 158 159 160 161
    case PROP_GAMMA:{
      gdouble val = g_value_get_double (value);

      GST_DEBUG_OBJECT (gamma, "Changing gamma from %lf to %lf", gamma->gamma,
          val);
162
      GST_OBJECT_LOCK (gamma);
163
      gamma->gamma = val;
164
      GST_OBJECT_UNLOCK (gamma);
165
      gst_gamma_calculate_tables (gamma);
166
      break;
167
    }
David Schleef's avatar
David Schleef committed
168
    default:
169
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
David Schleef's avatar
David Schleef committed
170 171 172 173 174
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
175 176
gst_gamma_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
David Schleef's avatar
David Schleef committed
177
{
178
  GstGamma *gamma = GST_GAMMA (object);
David Schleef's avatar
David Schleef committed
179 180

  switch (prop_id) {
181
    case PROP_GAMMA:
David Schleef's avatar
David Schleef committed
182
      g_value_set_double (value, gamma->gamma);
183
      break;
David Schleef's avatar
David Schleef committed
184 185 186 187 188 189 190
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
191
gst_gamma_calculate_tables (GstGamma * gamma)
David Schleef's avatar
David Schleef committed
192
{
193 194 195
  gint n;
  gdouble val;
  gdouble exp;
196
  gboolean passthrough = FALSE;
David Schleef's avatar
David Schleef committed
197

198
  GST_OBJECT_LOCK (gamma);
199
  if (gamma->gamma == 1.0) {
200 201 202 203 204 205 206 207 208
    passthrough = TRUE;
  } else {
    exp = 1.0 / gamma->gamma;
    for (n = 0; n < 256; n++) {
      val = n / 255.0;
      val = pow (val, exp);
      val = 255.0 * val;
      gamma->gamma_table[n] = (guint8) floor (val + 0.5);
    }
David Schleef's avatar
David Schleef committed
209
  }
210 211 212
  GST_OBJECT_UNLOCK (gamma);

  gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (gamma), passthrough);
David Schleef's avatar
David Schleef committed
213 214
}

215
static void
Wim Taymans's avatar
Wim Taymans committed
216
gst_gamma_planar_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
217
{
Sebastian Dröge's avatar
Sebastian Dröge committed
218
  gint i, j, height;
Wim Taymans's avatar
Wim Taymans committed
219
  gint width, stride, row_wrap;
Sebastian Dröge's avatar
Sebastian Dröge committed
220
  const guint8 *table = gamma->gamma_table;
Wim Taymans's avatar
Wim Taymans committed
221
  guint8 *data;
222

Wim Taymans's avatar
Wim Taymans committed
223 224 225 226 227
  data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
  stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
  width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
  height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
  row_wrap = stride - width;
228

Sebastian Dröge's avatar
Sebastian Dröge committed
229 230 231 232 233 234 235
  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
      *data = table[*data];
      data++;
    }
    data += row_wrap;
  }
236
}
237

238
static void
Wim Taymans's avatar
Wim Taymans committed
239
gst_gamma_packed_yuv_ip (GstGamma * gamma, GstVideoFrame * frame)
240 241
{
  gint i, j, height;
Wim Taymans's avatar
Wim Taymans committed
242
  gint width, stride, row_wrap;
243 244
  gint pixel_stride;
  const guint8 *table = gamma->gamma_table;
Wim Taymans's avatar
Wim Taymans committed
245
  guint8 *data;
246

Wim Taymans's avatar
Wim Taymans committed
247 248 249 250 251 252
  data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
  stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
  width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
  height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
  pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
  row_wrap = stride - pixel_stride * width;
253 254 255 256 257 258 259 260 261 262

  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
      *data = table[*data];
      data += pixel_stride;
    }
    data += row_wrap;
  }
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
  298, 0, 409, -57068,
  298, -100, -208, 34707,
  298, 516, 0, -70870,
};

static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
  66, 129, 25, 4096,
  -38, -74, 112, 32768,
  112, -94, -18, 32768,
};

#define APPLY_MATRIX(m,o,v1,v2,v3) ((m[o*4] * v1 + m[o*4+1] * v2 + m[o*4+2] * v3 + m[o*4+3]) >> 8)

static void
Wim Taymans's avatar
Wim Taymans committed
278
gst_gamma_packed_rgb_ip (GstGamma * gamma, GstVideoFrame * frame)
279 280
{
  gint i, j, height;
Wim Taymans's avatar
Wim Taymans committed
281
  gint width, stride, row_wrap;
282 283 284 285 286
  gint pixel_stride;
  const guint8 *table = gamma->gamma_table;
  gint offsets[3];
  gint r, g, b;
  gint y, u, v;
Wim Taymans's avatar
Wim Taymans committed
287
  guint8 *data;
288

Wim Taymans's avatar
Wim Taymans committed
289 290 291 292
  data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
  stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
  width = GST_VIDEO_FRAME_COMP_WIDTH (frame, 0);
  height = GST_VIDEO_FRAME_COMP_HEIGHT (frame, 0);
293

Wim Taymans's avatar
Wim Taymans committed
294 295 296 297 298 299
  offsets[0] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 0);
  offsets[1] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 1);
  offsets[2] = GST_VIDEO_FRAME_COMP_OFFSET (frame, 2);

  pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (frame, 0);
  row_wrap = stride - pixel_stride * width;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
      r = data[offsets[0]];
      g = data[offsets[1]];
      b = data[offsets[2]];

      y = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 0, r, g, b);
      u = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 1, r, g, b);
      v = APPLY_MATRIX (cog_rgb_to_ycbcr_matrix_8bit_sdtv, 2, r, g, b);

      y = table[CLAMP (y, 0, 255)];
      r = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 0, y, u, v);
      g = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 1, y, u, v);
      b = APPLY_MATRIX (cog_ycbcr_to_rgb_matrix_8bit_sdtv, 2, y, u, v);
Sebastian Dröge's avatar
Sebastian Dröge committed
315

316 317 318 319 320 321 322 323 324
      data[offsets[0]] = CLAMP (r, 0, 255);
      data[offsets[1]] = CLAMP (g, 0, 255);
      data[offsets[2]] = CLAMP (b, 0, 255);
      data += pixel_stride;
    }
    data += row_wrap;
  }
}

325
static gboolean
326 327
gst_gamma_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
    GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
328
{
329
  GstGamma *gamma = GST_GAMMA (vfilter);
330

331 332 333
  GST_DEBUG_OBJECT (gamma,
      "setting caps: in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT, incaps,
      outcaps);
334

335
  switch (GST_VIDEO_INFO_FORMAT (in_info)) {
336 337
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
338 339 340 341 342
    case GST_VIDEO_FORMAT_Y41B:
    case GST_VIDEO_FORMAT_Y42B:
    case GST_VIDEO_FORMAT_Y444:
    case GST_VIDEO_FORMAT_NV12:
    case GST_VIDEO_FORMAT_NV21:
343 344 345 346 347 348 349
      gamma->process = gst_gamma_planar_yuv_ip;
      break;
    case GST_VIDEO_FORMAT_YUY2:
    case GST_VIDEO_FORMAT_UYVY:
    case GST_VIDEO_FORMAT_AYUV:
    case GST_VIDEO_FORMAT_YVYU:
      gamma->process = gst_gamma_packed_yuv_ip;
350
      break;
351 352 353 354 355 356 357 358 359 360 361 362
    case GST_VIDEO_FORMAT_ARGB:
    case GST_VIDEO_FORMAT_ABGR:
    case GST_VIDEO_FORMAT_RGBA:
    case GST_VIDEO_FORMAT_BGRA:
    case GST_VIDEO_FORMAT_xRGB:
    case GST_VIDEO_FORMAT_xBGR:
    case GST_VIDEO_FORMAT_RGBx:
    case GST_VIDEO_FORMAT_BGRx:
    case GST_VIDEO_FORMAT_RGB:
    case GST_VIDEO_FORMAT_BGR:
      gamma->process = gst_gamma_packed_rgb_ip;
      break;
363 364 365 366 367
    default:
      goto invalid_caps;
      break;
  }
  return TRUE;
368

369
  /* ERRORS */
370
invalid_caps:
Wim Taymans's avatar
Wim Taymans committed
371 372 373 374
  {
    GST_ERROR_OBJECT (gamma, "Invalid caps: %" GST_PTR_FORMAT, incaps);
    return FALSE;
  }
375 376
}

377 378
static void
gst_gamma_before_transform (GstBaseTransform * base, GstBuffer * outbuf)
379
{
380
  GstGamma *gamma = GST_GAMMA (base);
381 382 383 384 385 386 387 388 389 390
  GstClockTime timestamp, stream_time;

  timestamp = GST_BUFFER_TIMESTAMP (outbuf);
  stream_time =
      gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);

  GST_DEBUG_OBJECT (gamma, "sync to %" GST_TIME_FORMAT,
      GST_TIME_ARGS (timestamp));

  if (GST_CLOCK_TIME_IS_VALID (stream_time))
391
    gst_object_sync_values (GST_OBJECT (gamma), stream_time);
392 393 394
}

static GstFlowReturn
395
gst_gamma_transform_frame_ip (GstVideoFilter * vfilter, GstVideoFrame * frame)
396
{
397
  GstGamma *gamma = GST_GAMMA (vfilter);
398 399 400

  if (!gamma->process)
    goto not_negotiated;
401

402
  GST_OBJECT_LOCK (gamma);
403
  gamma->process (gamma, frame);
404
  GST_OBJECT_UNLOCK (gamma);
405 406 407 408

  return GST_FLOW_OK;

  /* ERRORS */
409 410 411 412 413
not_negotiated:
  {
    GST_ERROR_OBJECT (gamma, "Not negotiated yet");
    return GST_FLOW_NOT_NEGOTIATED;
  }
414
}