gstcaps.override 12.1 KB
Newer Older
1 2 3
/* -*- Mode: C; c-basic-offset: 4 -*- */
/* gst-python
 * Copyright (C) 2005 Johan Dahlin
4
 *		 2005 Benjamin Otte <otte@gnome.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 * 
 * Author: Johan Dahlin <johan@gnome.org>
22
 *	   Benjamin Otte <otte@gnome.org>
23 24
 */
%%
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
headers
/* This is a (hopefully) smart hack to allow access to a caps' 
 * structures without falling into traps when the caps get destroyed
 * before the structures get.
 * This Hash Table uses the structure PyObjects as keys and the caps 
 * PyObjects as values. No clue if this is a fast data structure but it
 * probably doesn't matter anyway.
 */
static GHashTable *structure_caps_map = NULL;

static void
pygst_caps_map_add (PyObject *structure, PyObject *caps)
{
  /* we can't have free_on_dealloc stuff in here */
  g_assert (((PyGBoxed *)structure)->free_on_dealloc == FALSE);
  g_hash_table_insert (structure_caps_map, structure, caps);
}

43 44 45 46 47
static void
pygst_caps_map_remove_structure (PyObject *structure)
{
  g_hash_table_remove (structure_caps_map, structure);
}
48

49 50 51 52
static gboolean
pygst_caps_map_foreach (gpointer structure, gpointer caps, gpointer match)
{
  PyGBoxed *boxed = structure;
53
  
54 55
  if (match != caps)
    return FALSE;
56

57 58 59 60 61 62
  /* we can't have free_on_dealloc stuff in here */
  g_assert (boxed->free_on_dealloc == FALSE);
  boxed->boxed = gst_structure_copy (boxed->boxed);
  boxed->free_on_dealloc = TRUE;
  return TRUE;
}
63

64 65 66 67 68
static void
pygst_caps_map_modified (PyObject *caps)
{
  g_hash_table_foreach_remove (structure_caps_map, pygst_caps_map_foreach, caps);
}
69 70 71 72 73

%%
init
  structure_caps_map = g_hash_table_new (g_direct_hash, g_direct_equal);
%%
74 75 76 77 78
ignore
  gst_caps_new_simple
  gst_caps_new_full
  gst_caps_set_simple
%%
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
override gst_caps_get_structure kwargs
static PyObject *pygst_caps_sq_item(PyObject *self, int i);
static PyObject *
_wrap_gst_caps_get_structure(PyObject *self, PyObject *args, PyObject *kwargs)
{
	static char *kwlist[] = { "index", NULL };
	int index;

	if (PyErr_Warn(PyExc_DeprecationWarning, "caps.get_structure(i) is deprecated, use caps[i]") < 0)
		return NULL;
	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:GstCaps.get_structure", kwlist, &index))
		return NULL;
	return pygst_caps_sq_item (self, index);
}
%%
94 95 96 97 98 99 100 101 102 103
override gst_caps_new_empty kwargs
static int
_wrap_gst_caps_new_empty(PyGBoxed *self, PyObject *args, PyObject *kwargs)
{
	PyObject* item;
	int len, i;

        /* we wrap caps_new, caps_from_string and caps_new_full */
        len = PyTuple_Size(args);
        self->gtype = GST_TYPE_CAPS;
104
	self->free_on_dealloc = TRUE;
105 106 107 108 109 110
	
        if (len == 0) {
		/* 0 length creates a new empty caps */
		self->boxed = gst_caps_new_empty();
	} else if (len == 1) {
		item = PyTuple_GetItem(args, 0);
111 112 113 114 115 116 117 118 119 120 121
		/* 1 length is either a string or a structure */
		self->boxed = pygst_caps_from_pyobject (item, NULL);
	} else {
		/* it's multiple arguments that can all be made to caps */
		GstCaps *append;
		self->boxed = gst_caps_new_empty();
		for (i = 0; i < len; i++)
		{
			item = PyTuple_GetItem(args, i);
			append = pygst_caps_from_pyobject (item, NULL);
			if (!append) {
122
			      gst_caps_unref (self->boxed);
123 124 125 126
			      self->boxed = NULL;
			      break;
			}
			gst_caps_append (self->boxed, append);
127 128 129
		}
	}
	if (!self->boxed) {
130
		PyErr_SetString(PyExc_TypeError, "wrong arguemntes when creating GstCaps object");
131 132 133 134
		return -1;
	}
	return 0;
}
135

136
%%
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
override-slot GstCaps.tp_richcompare

static gboolean
pygst_caps_is_true_subset (GstCaps *caps1, GstCaps *caps2)
{
  GstCaps *tmp;
  gboolean ret;
  
  /* order is important here */
  if (gst_caps_is_any (caps1))
    return FALSE;
  if (gst_caps_is_any (caps2))
    return TRUE;
  if (gst_caps_is_empty (caps2))
    return FALSE;
  if (gst_caps_is_empty (caps1))
    return TRUE;

  tmp = gst_caps_subtract (caps1, caps2);
  ret = gst_caps_is_empty (tmp);
157
  gst_caps_unref (tmp);
158 159 160 161
  if (!ret)
    return FALSE;
  tmp = gst_caps_subtract (caps2, caps1);
  ret = gst_caps_is_empty (tmp);
162
  gst_caps_unref (tmp);
163 164 165
  return !ret;
}

166
static PyObject *
167
_wrap_gst_caps_tp_richcompare (PyObject *py_caps1, PyObject *py_caps2, int comparison)
168
{
169 170 171
  GstCaps *caps1, *caps2; 
  gboolean caps2_is_copy;
  PyObject *ret;
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  caps1 = pyg_boxed_get (py_caps1, GstCaps);
  caps2 = pygst_caps_from_pyobject (py_caps2, &caps2_is_copy);
  if (PyErr_Occurred()) {
    /* the second arg is not a caps */
    switch (comparison) {
      case Py_EQ:
        PyErr_Clear();
        ret = Py_False;
        Py_INCREF (ret);
        return ret;
      case Py_NE:
        PyErr_Clear();
        ret = Py_True;
        Py_INCREF (ret);
        return ret;
      default:
189
        return NULL;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
  }

  switch (comparison) {
    case Py_LT:
      ret = pygst_caps_is_true_subset (caps1, caps2) ? Py_True : Py_False;
      break;
    case Py_LE:
      ret = gst_caps_is_subset (caps1, caps2) ? Py_True : Py_False;
      break;
    case Py_NE:
      ret = gst_caps_is_equal (caps1, caps2) ? Py_False : Py_True;
      break;
    case Py_EQ:
      ret = gst_caps_is_equal (caps1, caps2) ? Py_True : Py_False;
      break;
    case Py_GE:
      ret = gst_caps_is_subset (caps2, caps1) ? Py_True : Py_False;
      break;
    case Py_GT:
      ret = pygst_caps_is_true_subset (caps2, caps1) ? Py_True : Py_False;
      break;
    default:
      PyErr_SetString (PyExc_RuntimeError, "invalid comparison operation");
      if (caps2 && caps2_is_copy)
215
	gst_caps_unref (caps2);
216 217 218
      return NULL;
  }
  if (caps2 && caps2_is_copy)
219
    gst_caps_unref (caps2);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  
  Py_INCREF (ret);
  return ret;
}

%%
override-slot GstCaps.tp_as_number

/* In this number code, we mimic the Python set.Set datatype.
 * The same operations are supported. If you want to have an operation
 * supported for caps, add it to Python's Set type first.
 */
#define BINARY_FUNC(name,func) \
static PyObject * \
name (PyObject *py_caps1, PyObject *py_caps2) \
{ \
  GstCaps *caps1, *caps2, *ret; \
  gboolean caps2_is_copy; \
\
  caps1 = pyg_boxed_get (py_caps1, GstCaps); \
  caps2 = pygst_caps_from_pyobject (py_caps2, &caps2_is_copy); \
  if (PyErr_Occurred()) \
    return NULL; \
  ret = func (caps1, caps2); \
  if (caps2 && caps2_is_copy) \
245
    gst_caps_unref (caps2); \
246 247 248 249 250 251 252 253 254 255 256 257 258
  return pyg_boxed_new (GST_TYPE_CAPS, ret, FALSE, TRUE); \
}

BINARY_FUNC (pygst_caps_nb_subtract, gst_caps_subtract)
BINARY_FUNC (pygst_caps_nb_and, gst_caps_intersect)
BINARY_FUNC (pygst_caps_nb_or, gst_caps_union)
static GstCaps *
pygst_caps_xor (const GstCaps *caps1, const GstCaps *caps2)
{
  GstCaps *intersect, *_union, *ret;
  intersect = gst_caps_intersect (caps1, caps2);
  _union = gst_caps_union (caps1, caps2);
  ret = gst_caps_subtract (_union, intersect);
259 260
  gst_caps_unref (_union);
  gst_caps_unref (intersect);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  gst_caps_do_simplify (ret);
  return ret;
}
BINARY_FUNC (pygst_caps_nb_xor, pygst_caps_xor)

static int
pygst_caps_nb_nonzero (PyObject *py_caps)
{
  GstCaps *caps = pyg_boxed_get (py_caps, GstCaps);

  if (gst_caps_is_empty (caps))
    return 0;
  else
    return 1;
}

static int
pygst_caps_nb_coerce (PyObject **py_caps1, PyObject **py_caps2)
{
  GstCaps *caps1, *caps2 = NULL;
  gboolean caps1_is_copy, caps2_is_copy;
  
  caps1 = pygst_caps_from_pyobject (*py_caps1, &caps1_is_copy);
  if (!caps1)
    goto error;
  caps2 = pygst_caps_from_pyobject (*py_caps2, &caps2_is_copy);
  if (!caps2)
    goto error;
  /* if they're not copies, they're caps already */
  if (caps1_is_copy)
    *py_caps1 = pyg_boxed_new (GST_TYPE_CAPS, caps1, FALSE, TRUE);
  else
    Py_INCREF (*py_caps1);
  if (caps2_is_copy)
    *py_caps2 = pyg_boxed_new (GST_TYPE_CAPS, caps2, FALSE, TRUE);
  else
    Py_INCREF (*py_caps2);
  return 0;

error:
  g_assert (PyErr_Occurred ());
  PyErr_Clear ();
  if (caps1 && !caps1_is_copy)
304
    gst_caps_unref (caps1);
305
  if (caps2 && !caps2_is_copy)
306
    gst_caps_unref (caps2);
307
  return 1;
308
}
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

static PyNumberMethods _wrap_gst_caps_tp_as_number = {
	0,			      		/* nb_add */
	pygst_caps_nb_subtract,			/* nb_subtract */
	0,					/* nb_multiply */
	0,	  				/* nb_divide */
	0,					/* nb_remainder */
	0,					/* nb_divmod */
	0,			  		/* nb_power */
	0,					/* nb_negative */
	0,					/* nb_positive */
	0,					/* nb_absolute */
	pygst_caps_nb_nonzero,	      		/* nb_nonzero */
	0,					/* nb_invert */
	0,					/* nb_lshift */
	0,					/* nb_rshift */
	pygst_caps_nb_and,			/* nb_and */
	pygst_caps_nb_xor,			/* nb_xor */
	pygst_caps_nb_or,			/* nb_or */
	pygst_caps_nb_coerce,	      		/* nb_coerce */
	0,					/* nb_int */
	0,					/* nb_long */
	0,					/* nb_float */
	0,					/* nb_oct */
	0,		 			/* nb_hex */
	0,					/* nb_inplace_add */
	0,					/* nb_inplace_subtract */
	0,					/* nb_inplace_multiply */
	0,					/* nb_inplace_divide */
	0,					/* nb_inplace_remainder */
	0,					/* nb_inplace_power */
	0,					/* nb_inplace_lshift */
	0,					/* nb_inplace_rshift */
	0,					/* nb_inplace_and */
	0,					/* nb_inplace_xor */
	0,					/* nb_inplace_or */
	0,					/* nb_floor_divide */
	0,					/* nb_true_divide */
	0,					/* nb_inplace_floor_divide */
	0,					/* nb_inplace_true_divide */
};
350 351 352
%%
override-slot GstCaps.tp_as_sequence
static int
353
pygst_caps_sq_length(PyObject *self)
354
{
355 356
	GstCaps *caps = pyg_boxed_get (self, GstCaps);
	return gst_caps_get_size(caps);
357 358 359
}

static PyObject *
360
pygst_caps_sq_item(PyObject *self, int i)
361
{
362
	GstCaps *caps = pyg_boxed_get (self, GstCaps);
363
	GstStructure *structure;
364
	PyObject *ret;
365
	
366
	if (i < 0 || i >= gst_caps_get_size(caps)) {
367 368 369 370
		PyErr_SetString(PyExc_IndexError, "list index out of range");
		return NULL;
	}
	
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	structure = gst_caps_get_structure(caps, i);
				    
	/* pyg_boxed_new handles NULL checking */
	ret = pyg_boxed_new(GST_TYPE_STRUCTURE, 
	                    gst_caps_get_structure(pyg_boxed_get(self, GstCaps), i), 
			    FALSE, FALSE);
	if (ret)
	  pygst_caps_map_add (ret, self);
	return ret;
}

/* FIXME: This syntax sucks */
static PyObject *
pygst_caps_sq_slice(PyObject *self, int start, int end)
{
	GstCaps *caps = pyg_boxed_get (self, GstCaps);
	GstCaps *ret = gst_caps_new_empty ();
	int i;

	if (start < 0)
	  start = 0;
	if (end > gst_caps_get_size (caps))
	  end = gst_caps_get_size (caps);

	for (i = start; i < end; i++)
	  gst_caps_append_structure (ret, gst_structure_copy (gst_caps_get_structure (caps, i)));
	
	return pyg_boxed_new(GST_TYPE_CAPS, ret, FALSE, TRUE);
399 400 401
}

static PySequenceMethods _wrap_gst_caps_tp_as_sequence = {
402 403 404 405 406 407 408 409 410 411
	pygst_caps_sq_length,
        NULL, /* not allowed for sets, use | instead of + */
        NULL, /* doesn't make sense, because it'd still be the same */
	pygst_caps_sq_item,
        pygst_caps_sq_slice, 
	NULL, /* doesn't make sense, you can only append */
	NULL, /* doesn't make sense, you can only append */
        NULL, /* doesn't make sense really, unless you use is_subset */
        NULL, /* not allowed for sets, use | instead of + */
	NULL /* doesn't make sense, because it'd still be the same */
412 413
};
%%
414 415 416 417 418 419 420
override-slot GstCaps.tp_dealloc
static void
_wrap_gst_caps_tp_dealloc (PyObject *self)
{
  PyGBoxed *boxed = (PyGBoxed *) self;

  if (boxed->free_on_dealloc && boxed->boxed) {
421
      pygst_caps_map_modified (self);
422 423
    GST_DEBUG ("unreffing caps %" GST_PTR_FORMAT " at %p with refcount %d", 
      boxed->boxed, boxed->boxed, GST_CAPS_REFCOUNT (boxed->boxed));
424
    gst_caps_unref (boxed->boxed);
425 426 427 428
  }

  self->ob_type->tp_free((PyObject *)self);
}
429

430
%%
431 432 433 434 435 436 437 438 439 440 441 442 443
override-slot GstCaps.tp_str
static PyObject *
_wrap_gst_caps_tp_str(PyGObject *self)
{
    gchar *tmp;
    PyObject *retval;
    
    tmp = gst_caps_to_string((GstCaps*)self->obj);
    retval = PyString_FromString(tmp);
    g_free(tmp);

    return retval;
}
444 445 446 447 448 449 450 451 452 453

%%
override-attr GstCaps.__refcount__

static PyObject *
_wrap_gst_caps__get___refcount__(PyGObject *self, void *closure)
{
    return PyInt_FromLong(GST_CAPS_REFCOUNT(self->obj));
}