~ubuntu-branches/ubuntu/quantal/gst-plugins-bad0.10/quantal-proposed

« back to all changes in this revision

Viewing changes to gst/videofilters/gstzebrastripe.c

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2011-07-19 14:32:43 UTC
  • mfrom: (18.4.21 sid)
  • Revision ID: james.westby@ubuntu.com-20110719143243-p7pnkh45akfp0ihk
Tags: 0.10.22-2ubuntu1
* Rebased on debian unstable, remaining changes:
  - debian/gstreamer-plugins-bad.install
    * don't include dtmf, liveadder, rtpmux, autoconvert and shm, we include 
      them in -good

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GStreamer
 
2
 * Copyright (C) 2011 David Schleef <ds@entropywave.com>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Library General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
 
17
 * Boston, MA 02110-1335, USA.
 
18
 */
 
19
/**
 
20
 * SECTION:element-gstzebrastripe
 
21
 *
 
22
 * The zebrastripe element marks areas of images in a video stream
 
23
 * that are brighter than a threshold with a diagonal zebra stripe
 
24
 * pattern.  Typically, this is used to aid in adjusting the exposure
 
25
 * setting on the camera.  Setting the threshold to 95 or 100 will
 
26
 * show areas that are completely overexposed and clipping.  A
 
27
 * threshold setting of 70 is often used to properly adjust skin
 
28
 * tones.
 
29
 *
 
30
 * <refsect2>
 
31
 * <title>Example launch line</title>
 
32
 * |[
 
33
 * gst-launch -v videotestsrc ! zebrastripe ! xvimagesink
 
34
 * ]|
 
35
 * Marks overexposed areas of the video with zebra stripes.
 
36
 *
 
37
 * The threshold property is expressed as percentage of full scale,
 
38
 * whereas common usage expresses thresholds in terms of IRE.  The
 
39
 * property setting can be calculated from IRE by using the formula
 
40
 * percent = (IRE * 1.075) - 7.5.  Note that 100 IRE corresponds to
 
41
 * 100 %, and 70 IRE corresponds to 68 %.
 
42
 * </refsect2>
 
43
 */
 
44
 
 
45
#ifdef HAVE_CONFIG_H
 
46
#include "config.h"
 
47
#endif
 
48
 
 
49
#include <gst/gst.h>
 
50
#include <gst/base/gstbasetransform.h>
 
51
#include <gst/video/video.h>
 
52
#include <math.h>
 
53
#include "gstzebrastripe.h"
 
54
 
 
55
GST_DEBUG_CATEGORY_STATIC (gst_zebra_stripe_debug_category);
 
56
#define GST_CAT_DEFAULT gst_zebra_stripe_debug_category
 
57
 
 
58
/* prototypes */
 
59
 
 
60
 
 
61
static void gst_zebra_stripe_set_property (GObject * object,
 
62
    guint property_id, const GValue * value, GParamSpec * pspec);
 
63
static void gst_zebra_stripe_get_property (GObject * object,
 
64
    guint property_id, GValue * value, GParamSpec * pspec);
 
65
static void gst_zebra_stripe_finalize (GObject * object);
 
66
 
 
67
static gboolean gst_zebra_stripe_start (GstBaseTransform * trans);
 
68
static gboolean gst_zebra_stripe_stop (GstBaseTransform * trans);
 
69
 
 
70
static GstFlowReturn
 
71
gst_zebra_stripe_prefilter (GstVideoFilter2 * videofilter2, GstBuffer * buf);
 
72
 
 
73
static GstVideoFilter2Functions gst_zebra_stripe_filter_functions[];
 
74
 
 
75
enum
 
76
{
 
77
  PROP_0,
 
78
  PROP_THRESHOLD
 
79
};
 
80
 
 
81
#define DEFAULT_THRESHOLD 90
 
82
 
 
83
/* class initialization */
 
84
 
 
85
#define DEBUG_INIT(bla) \
 
86
  GST_DEBUG_CATEGORY_INIT (gst_zebra_stripe_debug_category, "zebrastripe", 0, \
 
87
      "debug category for zebrastripe element");
 
88
 
 
89
GST_BOILERPLATE_FULL (GstZebraStripe, gst_zebra_stripe, GstVideoFilter2,
 
90
    GST_TYPE_VIDEO_FILTER2, DEBUG_INIT);
 
91
 
 
92
static void
 
93
gst_zebra_stripe_base_init (gpointer g_class)
 
94
{
 
95
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
 
96
 
 
97
  gst_element_class_set_details_simple (element_class, "Zebra stripe overlay",
 
98
      "Filter/Analysis",
 
99
      "Overlays zebra striping on overexposed areas of video",
 
100
      "David Schleef <ds@entropywave.com>");
 
101
}
 
102
 
 
103
static void
 
104
gst_zebra_stripe_class_init (GstZebraStripeClass * klass)
 
105
{
 
106
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
107
  GstVideoFilter2Class *video_filter2_class = GST_VIDEO_FILTER2_CLASS (klass);
 
108
  GstBaseTransformClass *base_transform_class =
 
109
      GST_BASE_TRANSFORM_CLASS (klass);
 
110
 
 
111
  gobject_class->set_property = gst_zebra_stripe_set_property;
 
112
  gobject_class->get_property = gst_zebra_stripe_get_property;
 
113
  gobject_class->finalize = gst_zebra_stripe_finalize;
 
114
  base_transform_class->start = GST_DEBUG_FUNCPTR (gst_zebra_stripe_start);
 
115
  base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_zebra_stripe_stop);
 
116
 
 
117
  video_filter2_class->prefilter =
 
118
      GST_DEBUG_FUNCPTR (gst_zebra_stripe_prefilter);
 
119
 
 
120
  g_object_class_install_property (gobject_class, PROP_THRESHOLD,
 
121
      g_param_spec_int ("threshold", "Threshold",
 
122
          "Threshold above which the video is striped", 0, 100,
 
123
          DEFAULT_THRESHOLD,
 
124
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
 
125
 
 
126
  gst_video_filter2_class_add_functions (video_filter2_class,
 
127
      gst_zebra_stripe_filter_functions);
 
128
}
 
129
 
 
130
static void
 
131
gst_zebra_stripe_init (GstZebraStripe * zebrastripe,
 
132
    GstZebraStripeClass * zebrastripe_class)
 
133
{
 
134
 
 
135
}
 
136
 
 
137
void
 
138
gst_zebra_stripe_set_property (GObject * object, guint property_id,
 
139
    const GValue * value, GParamSpec * pspec)
 
140
{
 
141
  GstZebraStripe *zebrastripe;
 
142
 
 
143
  g_return_if_fail (GST_IS_ZEBRA_STRIPE (object));
 
144
  zebrastripe = GST_ZEBRA_STRIPE (object);
 
145
 
 
146
  switch (property_id) {
 
147
    case PROP_THRESHOLD:
 
148
      zebrastripe->threshold = g_value_get_int (value);
 
149
      zebrastripe->y_threshold =
 
150
          16 + floor (0.5 + 2.19 * zebrastripe->threshold);
 
151
      break;
 
152
    default:
 
153
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
154
      break;
 
155
  }
 
156
}
 
157
 
 
158
void
 
159
gst_zebra_stripe_get_property (GObject * object, guint property_id,
 
160
    GValue * value, GParamSpec * pspec)
 
161
{
 
162
  GstZebraStripe *zebrastripe;
 
163
 
 
164
  g_return_if_fail (GST_IS_ZEBRA_STRIPE (object));
 
165
  zebrastripe = GST_ZEBRA_STRIPE (object);
 
166
 
 
167
  switch (property_id) {
 
168
    case PROP_THRESHOLD:
 
169
      g_value_set_int (value, zebrastripe->threshold);
 
170
      break;
 
171
    default:
 
172
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
173
      break;
 
174
  }
 
175
}
 
176
 
 
177
void
 
178
gst_zebra_stripe_finalize (GObject * object)
 
179
{
 
180
  GstZebraStripe *zebrastripe;
 
181
 
 
182
  g_return_if_fail (GST_IS_ZEBRA_STRIPE (object));
 
183
  zebrastripe = GST_ZEBRA_STRIPE (object);
 
184
 
 
185
  /* clean up object here */
 
186
 
 
187
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
188
}
 
189
 
 
190
 
 
191
static gboolean
 
192
gst_zebra_stripe_start (GstBaseTransform * trans)
 
193
{
 
194
 
 
195
  return TRUE;
 
196
}
 
197
 
 
198
static gboolean
 
199
gst_zebra_stripe_stop (GstBaseTransform * trans)
 
200
{
 
201
 
 
202
  return TRUE;
 
203
}
 
204
 
 
205
static GstFlowReturn
 
206
gst_zebra_stripe_prefilter (GstVideoFilter2 * videofilter2, GstBuffer * buf)
 
207
{
 
208
  GstZebraStripe *zebrastripe = GST_ZEBRA_STRIPE (videofilter2);
 
209
 
 
210
  zebrastripe->t++;
 
211
 
 
212
  return GST_FLOW_OK;
 
213
}
 
214
 
 
215
static GstFlowReturn
 
216
gst_zebra_stripe_filter_ip_planarY (GstVideoFilter2 * videofilter2,
 
217
    GstBuffer * buf, int start, int end)
 
218
{
 
219
  GstZebraStripe *zebrastripe = GST_ZEBRA_STRIPE (videofilter2);
 
220
  int width = GST_VIDEO_FILTER2_WIDTH (zebrastripe);
 
221
  int i, j;
 
222
  int threshold = zebrastripe->y_threshold;
 
223
  int t = zebrastripe->t;
 
224
  guint8 *ydata;
 
225
  int ystride;
 
226
 
 
227
  ydata = GST_BUFFER_DATA (buf);
 
228
  ystride =
 
229
      gst_video_format_get_row_stride (GST_VIDEO_FILTER2_FORMAT (videofilter2),
 
230
      0, width);
 
231
 
 
232
  for (j = start; j < end; j++) {
 
233
    guint8 *data = ydata + ystride * j;
 
234
    for (i = 0; i < width; i++) {
 
235
      if (data[i] >= threshold) {
 
236
        if ((i + j + t) & 0x4)
 
237
          data[i] = 16;
 
238
      }
 
239
    }
 
240
  }
 
241
  return GST_FLOW_OK;
 
242
}
 
243
 
 
244
static GstFlowReturn
 
245
gst_zebra_stripe_filter_ip_YxYy (GstVideoFilter2 * videofilter2,
 
246
    GstBuffer * buf, int start, int end)
 
247
{
 
248
  GstZebraStripe *zebrastripe = GST_ZEBRA_STRIPE (videofilter2);
 
249
  GstVideoFormat format = GST_VIDEO_FILTER2_FORMAT (zebrastripe);
 
250
  int width = GST_VIDEO_FILTER2_WIDTH (zebrastripe);
 
251
  int i, j;
 
252
  int threshold = zebrastripe->y_threshold;
 
253
  int t = zebrastripe->t;
 
254
  guint8 *ydata;
 
255
  int ystride;
 
256
 
 
257
  ydata = GST_BUFFER_DATA (buf);
 
258
  ystride = gst_video_format_get_row_stride (format, 0, width);
 
259
 
 
260
  if (format == GST_VIDEO_FORMAT_UYVY) {
 
261
    ydata++;
 
262
  }
 
263
 
 
264
  for (j = start; j < end; j++) {
 
265
    guint8 *data = ydata + ystride * j;
 
266
    for (i = 0; i < width; i++) {
 
267
      if (data[2 * i] >= threshold) {
 
268
        if ((i + j + t) & 0x4)
 
269
          data[2 * i] = 16;
 
270
      }
 
271
    }
 
272
  }
 
273
  return GST_FLOW_OK;
 
274
}
 
275
 
 
276
static GstFlowReturn
 
277
gst_zebra_stripe_filter_ip_AYUV (GstVideoFilter2 * videofilter2,
 
278
    GstBuffer * buf, int start, int end)
 
279
{
 
280
  GstZebraStripe *zebrastripe = GST_ZEBRA_STRIPE (videofilter2);
 
281
  int width = GST_VIDEO_FILTER2_WIDTH (zebrastripe);
 
282
  int i, j;
 
283
  int threshold = zebrastripe->y_threshold;
 
284
  int t = zebrastripe->t;
 
285
  guint8 *ydata;
 
286
  int ystride;
 
287
 
 
288
  ydata = GST_BUFFER_DATA (buf);
 
289
  ystride =
 
290
      gst_video_format_get_row_stride (GST_VIDEO_FILTER2_FORMAT (videofilter2),
 
291
      0, width);
 
292
 
 
293
  ydata++;
 
294
  for (j = start; j < end; j++) {
 
295
    guint8 *data = ydata + ystride * j;
 
296
    for (i = 0; i < width; i++) {
 
297
      if (data[4 * i] >= threshold) {
 
298
        if ((i + j + t) & 0x4)
 
299
          data[4 * i] = 16;
 
300
      }
 
301
    }
 
302
  }
 
303
 
 
304
  return GST_FLOW_OK;
 
305
}
 
306
 
 
307
static GstVideoFilter2Functions gst_zebra_stripe_filter_functions[] = {
 
308
  {GST_VIDEO_FORMAT_I420, NULL, gst_zebra_stripe_filter_ip_planarY},
 
309
  {GST_VIDEO_FORMAT_YV12, NULL, gst_zebra_stripe_filter_ip_planarY},
 
310
  {GST_VIDEO_FORMAT_Y41B, NULL, gst_zebra_stripe_filter_ip_planarY},
 
311
  {GST_VIDEO_FORMAT_Y42B, NULL, gst_zebra_stripe_filter_ip_planarY},
 
312
  {GST_VIDEO_FORMAT_NV12, NULL, gst_zebra_stripe_filter_ip_planarY},
 
313
  {GST_VIDEO_FORMAT_NV21, NULL, gst_zebra_stripe_filter_ip_planarY},
 
314
  {GST_VIDEO_FORMAT_YUV9, NULL, gst_zebra_stripe_filter_ip_planarY},
 
315
  {GST_VIDEO_FORMAT_YVU9, NULL, gst_zebra_stripe_filter_ip_planarY},
 
316
  {GST_VIDEO_FORMAT_Y444, NULL, gst_zebra_stripe_filter_ip_planarY},
 
317
  {GST_VIDEO_FORMAT_UYVY, NULL, gst_zebra_stripe_filter_ip_YxYy},
 
318
  {GST_VIDEO_FORMAT_YUY2, NULL, gst_zebra_stripe_filter_ip_YxYy},
 
319
  {GST_VIDEO_FORMAT_YVYU, NULL, gst_zebra_stripe_filter_ip_YxYy},
 
320
  {GST_VIDEO_FORMAT_AYUV, NULL, gst_zebra_stripe_filter_ip_AYUV},
 
321
  {GST_VIDEO_FORMAT_UNKNOWN}
 
322
};