~ubuntu-branches/ubuntu/quantal/gst-plugins-bad-multiverse0.10/quantal

« back to all changes in this revision

Viewing changes to ext/cog/gstcogdownsample.c

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2009-12-07 08:54:28 UTC
  • mfrom: (1.1.15 upstream)
  • Revision ID: james.westby@ubuntu.com-20091207085428-ml6aaukf0p2ph34d
Tags: 0.10.17-0ubuntu1
* New upstream release.
* Add myself to maintainer.
* Fix misc lintian warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GStreamer
 
2
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 
3
 * Copyright (C) <2003> David Schleef <ds@schleef.org>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Library General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Library General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public
 
16
 * License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 */
 
20
 
 
21
/*
 
22
 * This file was (probably) generated from
 
23
 * gstvideotemplate.c,v 1.18 2005/11/14 02:13:34 thomasvs Exp 
 
24
 * and
 
25
 * $Id: make_filter,v 1.8 2004/04/19 22:51:57 ds Exp $
 
26
 */
 
27
 
 
28
#ifdef HAVE_CONFIG_H
 
29
#include "config.h"
 
30
#endif
 
31
 
 
32
#include <gst/gst.h>
 
33
#include <gst/base/gstbasetransform.h>
 
34
#include <gst/video/video.h>
 
35
#include <string.h>
 
36
#include <cog/cog.h>
 
37
#include <math.h>
 
38
#include <cog/cogvirtframe.h>
 
39
 
 
40
#define GST_TYPE_COGDOWNSAMPLE \
 
41
  (gst_cogdownsample_get_type())
 
42
#define GST_COGDOWNSAMPLE(obj) \
 
43
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_COGDOWNSAMPLE,GstCogdownsample))
 
44
#define GST_COGDOWNSAMPLE_CLASS(klass) \
 
45
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_COGDOWNSAMPLE,GstCogdownsampleClass))
 
46
#define GST_IS_COGDOWNSAMPLE(obj) \
 
47
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_COGDOWNSAMPLE))
 
48
#define GST_IS_COGDOWNSAMPLE_CLASS(obj) \
 
49
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_COGDOWNSAMPLE))
 
50
 
 
51
typedef struct _GstCogdownsample GstCogdownsample;
 
52
typedef struct _GstCogdownsampleClass GstCogdownsampleClass;
 
53
 
 
54
struct _GstCogdownsample
 
55
{
 
56
  GstBaseTransform base_transform;
 
57
 
 
58
};
 
59
 
 
60
struct _GstCogdownsampleClass
 
61
{
 
62
  GstBaseTransformClass parent_class;
 
63
 
 
64
};
 
65
 
 
66
GType gst_cogdownsample_get_type (void);
 
67
 
 
68
/* GstCogdownsample signals and args */
 
69
enum
 
70
{
 
71
  /* FILL ME */
 
72
  LAST_SIGNAL
 
73
};
 
74
 
 
75
enum
 
76
{
 
77
  ARG_0,
 
78
  ARG_WAVELET_TYPE,
 
79
  ARG_LEVEL
 
80
      /* FILL ME */
 
81
};
 
82
 
 
83
static void gst_cogdownsample_base_init (gpointer g_class);
 
84
static void gst_cogdownsample_class_init (gpointer g_class,
 
85
    gpointer class_data);
 
86
static void gst_cogdownsample_init (GTypeInstance * instance, gpointer g_class);
 
87
 
 
88
static void gst_cogdownsample_set_property (GObject * object, guint prop_id,
 
89
    const GValue * value, GParamSpec * pspec);
 
90
static void gst_cogdownsample_get_property (GObject * object, guint prop_id,
 
91
    GValue * value, GParamSpec * pspec);
 
92
 
 
93
static GstCaps *gst_cogdownsample_transform_caps (GstBaseTransform *
 
94
    base_transform, GstPadDirection direction, GstCaps * caps);
 
95
static GstFlowReturn gst_cogdownsample_transform (GstBaseTransform *
 
96
    base_transform, GstBuffer * inbuf, GstBuffer * outbuf);
 
97
static gboolean gst_cogdownsample_get_unit_size (GstBaseTransform *
 
98
    base_transform, GstCaps * caps, guint * size);
 
99
 
 
100
static GstStaticPadTemplate gst_cogdownsample_sink_template =
 
101
GST_STATIC_PAD_TEMPLATE ("sink",
 
102
    GST_PAD_SINK,
 
103
    GST_PAD_ALWAYS,
 
104
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YV12, YUY2, UYVY, AYUV }"))
 
105
    );
 
106
 
 
107
static GstStaticPadTemplate gst_cogdownsample_src_template =
 
108
GST_STATIC_PAD_TEMPLATE ("src",
 
109
    GST_PAD_SRC,
 
110
    GST_PAD_ALWAYS,
 
111
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YV12, YUY2, UYVY, AYUV }"))
 
112
    );
 
113
 
 
114
GType
 
115
gst_cogdownsample_get_type (void)
 
116
{
 
117
  static GType compress_type = 0;
 
118
 
 
119
  if (!compress_type) {
 
120
    static const GTypeInfo compress_info = {
 
121
      sizeof (GstCogdownsampleClass),
 
122
      gst_cogdownsample_base_init,
 
123
      NULL,
 
124
      gst_cogdownsample_class_init,
 
125
      NULL,
 
126
      NULL,
 
127
      sizeof (GstCogdownsample),
 
128
      0,
 
129
      gst_cogdownsample_init,
 
130
    };
 
131
 
 
132
    compress_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
 
133
        "GstCogdownsample", &compress_info, 0);
 
134
  }
 
135
  return compress_type;
 
136
}
 
137
 
 
138
 
 
139
static void
 
140
gst_cogdownsample_base_init (gpointer g_class)
 
141
{
 
142
  static GstElementDetails compress_details =
 
143
      GST_ELEMENT_DETAILS ("Downsample video",
 
144
      "Filter/Effect/Video",
 
145
      "Decreases size of video by a factor of 2",
 
146
      "David Schleef <ds@schleef.org>");
 
147
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
 
148
 
 
149
  gst_element_class_add_pad_template (element_class,
 
150
      gst_static_pad_template_get (&gst_cogdownsample_src_template));
 
151
  gst_element_class_add_pad_template (element_class,
 
152
      gst_static_pad_template_get (&gst_cogdownsample_sink_template));
 
153
 
 
154
  gst_element_class_set_details (element_class, &compress_details);
 
155
}
 
156
 
 
157
static void
 
158
gst_cogdownsample_class_init (gpointer g_class, gpointer class_data)
 
159
{
 
160
  GObjectClass *gobject_class;
 
161
  GstBaseTransformClass *base_transform_class;
 
162
  GstCogdownsampleClass *downsample_class;
 
163
 
 
164
  gobject_class = G_OBJECT_CLASS (g_class);
 
165
  base_transform_class = GST_BASE_TRANSFORM_CLASS (g_class);
 
166
  downsample_class = GST_COGDOWNSAMPLE_CLASS (g_class);
 
167
 
 
168
  gobject_class->set_property = gst_cogdownsample_set_property;
 
169
  gobject_class->get_property = gst_cogdownsample_get_property;
 
170
 
 
171
#if 0
 
172
  g_object_class_install_property (gobject_class, ARG_WAVELET_TYPE,
 
173
      g_param_spec_int ("wavelet-type", "wavelet type", "wavelet type",
 
174
          0, 4, 0, G_PARAM_READWRITE));
 
175
  g_object_class_install_property (gobject_class, ARG_LEVEL,
 
176
      g_param_spec_int ("level", "level", "level",
 
177
          0, 100, 0, G_PARAM_READWRITE));
 
178
#endif
 
179
 
 
180
  base_transform_class->transform = gst_cogdownsample_transform;
 
181
  base_transform_class->transform_caps = gst_cogdownsample_transform_caps;
 
182
  base_transform_class->get_unit_size = gst_cogdownsample_get_unit_size;
 
183
}
 
184
 
 
185
static void
 
186
gst_cogdownsample_init (GTypeInstance * instance, gpointer g_class)
 
187
{
 
188
 
 
189
  GST_DEBUG ("gst_cogdownsample_init");
 
190
}
 
191
 
 
192
static void
 
193
gst_cogdownsample_set_property (GObject * object, guint prop_id,
 
194
    const GValue * value, GParamSpec * pspec)
 
195
{
 
196
  GstCogdownsample *src;
 
197
 
 
198
  g_return_if_fail (GST_IS_COGDOWNSAMPLE (object));
 
199
  src = GST_COGDOWNSAMPLE (object);
 
200
 
 
201
  GST_DEBUG ("gst_cogdownsample_set_property");
 
202
  switch (prop_id) {
 
203
    default:
 
204
      break;
 
205
  }
 
206
}
 
207
 
 
208
static void
 
209
gst_cogdownsample_get_property (GObject * object, guint prop_id, GValue * value,
 
210
    GParamSpec * pspec)
 
211
{
 
212
  GstCogdownsample *src;
 
213
 
 
214
  g_return_if_fail (GST_IS_COGDOWNSAMPLE (object));
 
215
  src = GST_COGDOWNSAMPLE (object);
 
216
 
 
217
  switch (prop_id) {
 
218
    default:
 
219
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
220
      break;
 
221
  }
 
222
}
 
223
 
 
224
static void
 
225
transform_value (GValue * dest, const GValue * src, GstPadDirection dir)
 
226
{
 
227
  g_value_init (dest, G_VALUE_TYPE (src));
 
228
 
 
229
  if (G_VALUE_HOLDS_INT (src)) {
 
230
    int x;
 
231
 
 
232
    x = g_value_get_int (src);
 
233
    if (dir == GST_PAD_SINK) {
 
234
      g_value_set_int (dest, x / 2);
 
235
    } else {
 
236
      g_value_set_int (dest, x * 2);
 
237
    }
 
238
  } else if (GST_VALUE_HOLDS_INT_RANGE (src)) {
 
239
    int min, max;
 
240
 
 
241
    min = gst_value_get_int_range_min (src);
 
242
    max = gst_value_get_int_range_max (src);
 
243
 
 
244
    if (dir == GST_PAD_SINK) {
 
245
      min = (min + 1) / 2;
 
246
      if (max == G_MAXINT) {
 
247
        max = G_MAXINT / 2;
 
248
      } else {
 
249
        max = (max + 1) / 2;
 
250
      }
 
251
    } else {
 
252
      if (max > G_MAXINT / 2) {
 
253
        max = G_MAXINT;
 
254
      } else {
 
255
        max = max * 2;
 
256
      }
 
257
      if (min > G_MAXINT / 2) {
 
258
        min = G_MAXINT;
 
259
      } else {
 
260
        min = min * 2;
 
261
      }
 
262
    }
 
263
    gst_value_set_int_range (dest, min, max);
 
264
  } else {
 
265
    /* FIXME */
 
266
    g_warning ("case not handled");
 
267
    g_value_set_int (dest, 100);
 
268
  }
 
269
}
 
270
 
 
271
static GstCaps *
 
272
gst_cogdownsample_transform_caps (GstBaseTransform * base_transform,
 
273
    GstPadDirection direction, GstCaps * caps)
 
274
{
 
275
  int i;
 
276
  GstStructure *structure;
 
277
  GValue new_value = { 0 };
 
278
  const GValue *value;
 
279
 
 
280
  caps = gst_caps_copy (caps);
 
281
 
 
282
  for (i = 0; i < gst_caps_get_size (caps); i++) {
 
283
    structure = gst_caps_get_structure (caps, i);
 
284
 
 
285
    value = gst_structure_get_value (structure, "width");
 
286
    transform_value (&new_value, value, direction);
 
287
    gst_structure_set_value (structure, "width", &new_value);
 
288
    g_value_unset (&new_value);
 
289
 
 
290
    value = gst_structure_get_value (structure, "height");
 
291
    transform_value (&new_value, value, direction);
 
292
    gst_structure_set_value (structure, "height", &new_value);
 
293
    g_value_unset (&new_value);
 
294
  }
 
295
 
 
296
  return caps;
 
297
}
 
298
 
 
299
static gboolean
 
300
gst_cogdownsample_get_unit_size (GstBaseTransform * base_transform,
 
301
    GstCaps * caps, guint * size)
 
302
{
 
303
  int width, height;
 
304
  uint32_t format;
 
305
 
 
306
  gst_structure_get_fourcc (gst_caps_get_structure (caps, 0),
 
307
      "format", &format);
 
308
  gst_structure_get_int (gst_caps_get_structure (caps, 0), "width", &width);
 
309
  gst_structure_get_int (gst_caps_get_structure (caps, 0), "height", &height);
 
310
 
 
311
  switch (format) {
 
312
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
 
313
    case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
 
314
      *size = width * height * 3 / 2;
 
315
      break;
 
316
    case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
 
317
    case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
 
318
      *size = width * height * 2;
 
319
      break;
 
320
    case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
 
321
      *size = width * height * 4;
 
322
      break;
 
323
    default:
 
324
      g_assert_not_reached ();
 
325
  }
 
326
 
 
327
  return TRUE;
 
328
}
 
329
 
 
330
static GstFlowReturn
 
331
gst_cogdownsample_transform (GstBaseTransform * base_transform,
 
332
    GstBuffer * inbuf, GstBuffer * outbuf)
 
333
{
 
334
  GstCogdownsample *compress;
 
335
  CogFrame *outframe;
 
336
  int width, height;
 
337
  uint32_t format;
 
338
  CogFrame *frame;
 
339
 
 
340
  g_return_val_if_fail (GST_IS_COGDOWNSAMPLE (base_transform), GST_FLOW_ERROR);
 
341
  compress = GST_COGDOWNSAMPLE (base_transform);
 
342
 
 
343
  gst_structure_get_fourcc (gst_caps_get_structure (inbuf->caps, 0),
 
344
      "format", &format);
 
345
  gst_structure_get_int (gst_caps_get_structure (inbuf->caps, 0),
 
346
      "width", &width);
 
347
  gst_structure_get_int (gst_caps_get_structure (inbuf->caps, 0),
 
348
      "height", &height);
 
349
 
 
350
  switch (format) {
 
351
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
 
352
      frame = cog_frame_new_from_data_I420 (GST_BUFFER_DATA (inbuf),
 
353
          width, height);
 
354
      outframe = cog_frame_new_from_data_I420 (GST_BUFFER_DATA (outbuf),
 
355
          width / 2, height / 2);
 
356
      break;
 
357
    case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
 
358
      frame = cog_frame_new_from_data_YV12 (GST_BUFFER_DATA (inbuf),
 
359
          width, height);
 
360
      outframe = cog_frame_new_from_data_YV12 (GST_BUFFER_DATA (outbuf),
 
361
          width / 2, height / 2);
 
362
      break;
 
363
    case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
 
364
      frame = cog_frame_new_from_data_YUY2 (GST_BUFFER_DATA (inbuf),
 
365
          width, height);
 
366
      outframe = cog_frame_new_from_data_YUY2 (GST_BUFFER_DATA (outbuf),
 
367
          width / 2, height / 2);
 
368
      break;
 
369
    case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
 
370
      frame = cog_frame_new_from_data_UYVY (GST_BUFFER_DATA (inbuf),
 
371
          width, height);
 
372
      outframe = cog_frame_new_from_data_UYVY (GST_BUFFER_DATA (outbuf),
 
373
          width / 2, height / 2);
 
374
      break;
 
375
    case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
 
376
      frame = cog_frame_new_from_data_AYUV (GST_BUFFER_DATA (inbuf),
 
377
          width, height);
 
378
      outframe = cog_frame_new_from_data_AYUV (GST_BUFFER_DATA (outbuf),
 
379
          width / 2, height / 2);
 
380
      break;
 
381
    default:
 
382
      g_assert_not_reached ();
 
383
  }
 
384
 
 
385
  frame = cog_virt_frame_new_unpack (frame);
 
386
  frame = cog_virt_frame_new_horiz_downsample (frame, 3);
 
387
  frame = cog_virt_frame_new_vert_downsample (frame, 2);
 
388
 
 
389
  switch (format) {
 
390
    case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
 
391
      frame = cog_virt_frame_new_pack_YUY2 (frame);
 
392
      break;
 
393
    case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
 
394
      frame = cog_virt_frame_new_pack_UYVY (frame);
 
395
      break;
 
396
    case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
 
397
      frame = cog_virt_frame_new_pack_AYUV (frame);
 
398
      break;
 
399
    default:
 
400
      break;
 
401
  }
 
402
 
 
403
  cog_virt_frame_render (frame, outframe);
 
404
  cog_frame_unref (frame);
 
405
  cog_frame_unref (outframe);
 
406
 
 
407
  return GST_FLOW_OK;
 
408
}