+2007-03-01 Edward Hervey <edward@fluendo.com>
+
+ * gst/__init__.py:
+ Added __eq__ method to fractions so we can check if two fractions are
+ equal.
+ * gst/pygstvalue.c: (my_gcd), (pygst_value_from_pyobject):
+ Attempt to simplify gst.Fraction before filling in a GValue.
+ Fixes #381243
+ * testsuite/test_caps.py:
+ * testsuite/test_struct.py:
+ Minor beauty fixes. framerates are fractions, not floats.
+
2007-03-01 Jan Schmidt <thaytan@mad.scientist.com>
reviewed by: Edward Hervey <edward@fluendo.com>
Value.__init__(self, 'fraction')
self.num = num
self.denom = denom
+ def __eq__(self, other):
+ if isinstance(other, Fraction):
+ return self.num * other.denom == other.num * self.denom
+ return False
+
def __repr__(self):
return '<gst.Fraction %d/%d>' % (self.num, self.denom)
static PyObject *gstfraction_class = NULL;
static PyObject *gstfractionrange_class = NULL;
+/* helper function */
+
+/* Finds the greatest common divisor.
+ * Returns 1 if none other found.
+ * This is Euclid's algorithm. */
+static long
+my_gcd(long num, long denom)
+{
+ while (denom != 0) {
+ long temp = num;
+
+ num = denom;
+ denom = temp % denom;
+ }
+
+ return ABS (num);
+}
+
/**
* pygst_value_as_pyobject:
* @value: the GValue object.
} else if (PyObject_IsInstance (obj, gstfraction_class)) {
PyObject *pyval;
long num, denom;
+ long gcd = 0;
VALUE_TYPE_CHECK (value, GST_TYPE_FRACTION);
if (!(pyval = PyObject_GetAttrString (obj, "num")))
return -1;
num = PyInt_AsLong (pyval);
+ if ((num == -1) && PyErr_Occurred())
+ return -1;
g_assert (G_MININT <= num && num <= G_MAXINT);
if (!(pyval = PyObject_GetAttrString (obj, "denom")))
return -1;
denom = PyInt_AsLong (pyval);
+ if ((denom == -1) && PyErr_Occurred())
+ return -1;
+ /* we need to reduce the values to be smaller than MAXINT */
+ if ((gcd = my_gcd(num, denom))) {
+ num /= gcd;
+ denom /= gcd;
+ }
g_assert (G_MININT <= denom && denom <= G_MAXINT);
gst_value_set_fraction (value, (int)num, (int)denom);
} else if (PyObject_IsInstance (obj, gstfractionrange_class)) {
class CapsTest(TestCase):
def setUp(self):
TestCase.setUp(self)
- self.caps = gst.caps_from_string('video/x-raw-yuv,width=10,framerate=5.0;video/x-raw-rgb,width=15,framerate=10.0')
+ self.caps = gst.caps_from_string('video/x-raw-yuv,width=10,framerate=5/1;video/x-raw-rgb,width=15,framerate=10/1')
self.assertEquals(self.caps.__refcount__, 1)
self.structure = self.caps[0]
self.any = gst.Caps("ANY")
class StructureTest(TestCase):
def setUp(self):
TestCase.setUp(self)
- self.struct = gst.structure_from_string('video/x-raw-yuv,width=10,foo="bar",pixel-aspect-ratio=1/2,framerate=5.0,boolean=(boolean)true')
+ self.struct = gst.structure_from_string('video/x-raw-yuv,width=10,foo="bar",pixel-aspect-ratio=1/2,framerate=5/1,boolean=(boolean)true')
def testName(self):
assert self.struct.get_name() == 'video/x-raw-yuv'
assert s['rlist'] == [([(['a', 'b'], ['c', 'd']),'e'], ['f', 'g']), 'h']
def testStructureChange(self):
- assert self.struct['framerate'] == 5.0
- self.struct['framerate'] = 10.0
- assert self.struct['framerate'] == 10.0
+ assert self.struct['framerate'] == gst.Fraction(5, 1)
+ self.struct['framerate'] = gst.Fraction(10, 1)
+ assert self.struct['framerate'] == gst.Fraction(10, 1)
self.struct['pixel-aspect-ratio'] = gst.Fraction(4, 2)
assert self.struct['pixel-aspect-ratio'].num == 2
assert self.struct['pixel-aspect-ratio'].denom == 1