summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDaniel Klamt <graphics@pengutronix.de>2019-04-05 15:58:38 +0200
committerPhilipp Zabel <philipp.zabel@gmail.com>2019-12-09 09:23:55 +0100
commitfecfe451a7566740960d87da8a177f8a776f6137 (patch)
treee34a6eb39e3c931853d6d32fdbd81b27fdf4a986 /examples
parent91d05de9b93c38621bd70240b154dfdccf3bb0e1 (diff)
Changes the mapinfo so that the mapped data is writable
The Problem is, that in the current state it is not easily possible to edit the buffer data in a gstreamer python element since you get a copy of the real buffer. This patch overrides the mapinfo and the function generating it in a way so that mapinfo.data is now a memoryview pointing to the real buffer. Depending on the flags given for this buffer the memoryview is r/w.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/plugins/python/exampleTransform.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/plugins/python/exampleTransform.py b/examples/plugins/python/exampleTransform.py
new file mode 100755
index 0000000..3d9bdb3
--- /dev/null
+++ b/examples/plugins/python/exampleTransform.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python3
+# exampleTransform.py
+# 2019 Daniel Klamt <graphics@pengutronix.de>
+
+# Inverts a grayscale image in place, requires numpy.
+#
+# gst-launch-1.0 videotestsrc ! ExampleTransform ! videoconvert ! xvimagesink
+
+import gi
+gi.require_version('Gst', '1.0')
+gi.require_version('GstBase', '1.0')
+gi.require_version('GstVideo', '1.0')
+
+from gi.repository import Gst, GObject, GstBase, GstVideo
+
+import numpy as np
+
+Gst.init(None)
+FIXED_CAPS = Gst.Caps.from_string('video/x-raw,format=GRAY8,width=[1,2147483647],height=[1,2147483647]')
+
+class ExampleTransform(GstBase.BaseTransform):
+ __gstmetadata__ = ('ExampleTransform Python','Transform',
+ 'example gst-python element that can modify the buffer gst-launch-1.0 videotestsrc ! ExampleTransform ! videoconvert ! xvimagesink', 'dkl')
+
+ __gsttemplates__ = (Gst.PadTemplate.new("src",
+ Gst.PadDirection.SRC,
+ Gst.PadPresence.ALWAYS,
+ FIXED_CAPS),
+ Gst.PadTemplate.new("sink",
+ Gst.PadDirection.SINK,
+ Gst.PadPresence.ALWAYS,
+ FIXED_CAPS))
+
+ def do_set_caps(self, incaps, outcaps):
+ struct = incaps.get_structure(0)
+ self.width = struct.get_int("width").value
+ self.height = struct.get_int("height").value
+ return True
+
+ def do_transform_ip(self, buf):
+ with buf.map(Gst.MapFlags.READ | Gst.MapFlags.WRITE) as info:
+ # Create a NumPy ndarray from the memoryview and modify it in place:
+ A = np.ndarray(shape = (self.height, self.width), dtype = np.uint8, buffer = info.data)
+ A[:] = np.invert(A)
+
+ return Gst.FlowReturn.OK
+
+GObject.type_register(ExampleTransform)
+__gstelementfactory__ = ("ExampleTransform", Gst.Rank.NONE, ExampleTransform)