2 * Copyright (C) <2021> Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
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.
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.
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-codecalphademux
23 * @title: CODEC Alpha Demuxer
25 * Extracts the CODEC (typically VP8/VP9) alpha stream stored as meta and
26 * exposes it as a stream. This element allow using single stream VP8/9
27 * decoders in order to decode both streams.
29 * ## Example launch line
31 * gst-launch-1.0 -v filesrc location=transparency.webm ! matroskademux !
32 * codecalphademux name=d
33 * d.video ! queue ! vp9dec ! autovideosink
34 * d.alpha ! queue ! vp9dec ! autovideosink
35 * ]| This pipeline splits and decode the video and the alpha stream, showing
36 * the result on seperate windows.
45 #include <gst/video/video.h>
46 #include <gst/base/gstflowcombiner.h>
48 #include "gstcodecalphademux.h"
50 GST_DEBUG_CATEGORY_STATIC (codecalphademux_debug);
51 #define GST_CAT_DEFAULT (codecalphademux_debug)
53 struct _GstCodecAlphaDemux
61 GstFlowCombiner *flow_combiner;
64 #define gst_codec_alpha_demux_parent_class parent_class
65 G_DEFINE_TYPE_WITH_CODE (GstCodecAlphaDemux, gst_codec_alpha_demux,
67 GST_DEBUG_CATEGORY_INIT (codecalphademux_debug, "codecalphademux", 0,
70 GST_ELEMENT_REGISTER_DEFINE (codec_alpha_demux, "codecalphademux",
71 GST_RANK_NONE, GST_TYPE_CODEC_ALPHA_DEMUX);
73 static GstStaticPadTemplate gst_codec_alpha_demux_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS ("ANY")
80 static GstStaticPadTemplate gst_codec_alpha_demux_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
84 GST_STATIC_CAPS ("ANY")
87 static GstStaticPadTemplate gst_codec_alpha_demux_alpha_template =
88 GST_STATIC_PAD_TEMPLATE ("alpha",
91 GST_STATIC_CAPS ("ANY")
95 gst_codec_alpha_demux_chain (GstPad * pad, GstObject * object,
98 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (object);
99 GstVideoCodecAlphaMeta *alpha_meta =
100 gst_buffer_get_video_codec_alpha_meta (buffer);
101 GstBuffer *alpha_buffer = NULL;
102 GstClockTime pts = GST_BUFFER_PTS (buffer);
103 GstClockTime duration = GST_BUFFER_DURATION (buffer);
104 GstFlowReturn ret = GST_FLOW_EOS;
107 alpha_buffer = gst_buffer_ref (alpha_meta->buffer);
109 ret = gst_pad_push (self->src_pad, buffer);
110 ret = gst_flow_combiner_update_flow (self->flow_combiner, ret);
112 /* we lost ownership here */
117 ret = gst_pad_push (self->alpha_pad, alpha_buffer);
119 gst_pad_push_event (self->alpha_pad, gst_event_new_gap (pts, duration));
120 ret = GST_PAD_LAST_FLOW_RETURN (self->alpha_pad);
122 ret = gst_flow_combiner_update_flow (self->flow_combiner, ret);
128 gst_codec_alpha_demux_transform_caps (GstCaps * caps, gboolean codec_alpha)
133 caps = gst_caps_copy (caps);
134 gst_caps_set_simple (caps, "codec-alpha", G_TYPE_BOOLEAN, codec_alpha, NULL);
140 gst_codec_alpha_demux_transform_caps_event (GstEvent * src_event)
145 gst_event_parse_caps (src_event, &caps);
147 caps = gst_codec_alpha_demux_transform_caps (caps, FALSE);
148 dst_event = gst_event_new_caps (caps);
149 gst_event_set_seqnum (dst_event, gst_event_get_seqnum (src_event));
151 gst_caps_unref (caps);
152 gst_event_unref (src_event);
157 gst_codec_alpha_demux_sink_event (GstPad * sink_pad, GstObject * parent,
160 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (parent);
162 switch (event->type) {
163 case GST_EVENT_FLUSH_STOP:
164 gst_flow_combiner_reset (self->flow_combiner);
167 event = gst_codec_alpha_demux_transform_caps_event (event);
173 return gst_pad_event_default (sink_pad, parent, event);
177 gst_codec_alpha_demux_sink_query (GstPad * sink_pad, GstObject * parent,
180 GstQuery *peer_query;
184 switch (query->type) {
186 gst_query_parse_caps (query, &caps);
187 caps = gst_codec_alpha_demux_transform_caps (caps, FALSE);
188 peer_query = gst_query_new_caps (caps);
189 gst_clear_caps (&caps);
191 case GST_QUERY_ACCEPT_CAPS:
192 gst_query_parse_accept_caps (query, &caps);
193 caps = gst_codec_alpha_demux_transform_caps (caps, FALSE);
194 peer_query = gst_query_new_accept_caps (caps);
195 gst_clear_caps (&caps);
202 ret = gst_pad_query_default (sink_pad, parent, peer_query);
204 if (peer_query != query)
205 gst_query_unref (peer_query);
209 switch (query->type) {
211 gst_query_parse_caps_result (peer_query, &caps);
212 caps = gst_caps_copy (caps);
213 caps = gst_codec_alpha_demux_transform_caps (caps, TRUE);
214 gst_query_set_caps_result (query, caps);
215 gst_caps_unref (caps);
216 gst_query_unref (peer_query);
218 case GST_QUERY_ACCEPT_CAPS:
221 gst_query_parse_accept_caps_result (peer_query, &result);
222 gst_query_set_accept_caps_result (query, result);
223 gst_query_unref (peer_query);
235 gst_codec_alpha_demux_start (GstCodecAlphaDemux * self)
237 gst_flow_combiner_reset (self->flow_combiner);
240 static GstStateChangeReturn
241 gst_codec_alpha_demux_change_state (GstElement * element,
242 GstStateChange transition)
244 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (element);
246 switch (transition) {
247 case GST_STATE_CHANGE_READY_TO_PAUSED:
248 gst_codec_alpha_demux_start (self);
253 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
257 gst_codec_alpha_demux_dispose (GObject * object)
259 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (object);
261 g_clear_object (&self->sink_pad);
262 g_clear_object (&self->src_pad);
263 g_clear_object (&self->alpha_pad);
264 g_clear_pointer (&self->flow_combiner, gst_flow_combiner_unref);
266 G_OBJECT_CLASS (parent_class)->dispose (object);
270 gst_codec_alpha_demux_class_init (GstCodecAlphaDemuxClass * klass)
272 GstElementClass *element_class = (GstElementClass *) klass;
273 GObjectClass *object_class = (GObjectClass *) klass;
275 gst_element_class_set_static_metadata (element_class,
276 "CODEC Alpha Demuxer", "Codec/Demuxer",
277 "Extract and expose as a stream the CODEC alpha.",
278 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
280 gst_element_class_add_static_pad_template (element_class,
281 &gst_codec_alpha_demux_sink_template);
282 gst_element_class_add_static_pad_template (element_class,
283 &gst_codec_alpha_demux_src_template);
284 gst_element_class_add_static_pad_template (element_class,
285 &gst_codec_alpha_demux_alpha_template);
287 element_class->change_state =
288 GST_DEBUG_FUNCPTR (gst_codec_alpha_demux_change_state);
290 object_class->dispose = GST_DEBUG_FUNCPTR (gst_codec_alpha_demux_dispose);
294 gst_codec_alpha_demux_init (GstCodecAlphaDemux * self)
296 gst_element_create_all_pads (GST_ELEMENT (self));
297 self->sink_pad = gst_element_get_static_pad (GST_ELEMENT (self), "sink");
298 self->src_pad = gst_element_get_static_pad (GST_ELEMENT (self), "src");
299 self->alpha_pad = gst_element_get_static_pad (GST_ELEMENT (self), "alpha");
301 self->flow_combiner = gst_flow_combiner_new ();
302 gst_flow_combiner_add_pad (self->flow_combiner, self->src_pad);
303 gst_flow_combiner_add_pad (self->flow_combiner, self->alpha_pad);
305 GST_PAD_SET_PROXY_CAPS (self->sink_pad);
306 GST_PAD_SET_PROXY_CAPS (self->src_pad);
307 GST_PAD_SET_PROXY_CAPS (self->alpha_pad);
309 GST_PAD_SET_PROXY_SCHEDULING (self->sink_pad);
310 GST_PAD_SET_PROXY_SCHEDULING (self->src_pad);
311 GST_PAD_SET_PROXY_SCHEDULING (self->alpha_pad);
313 gst_pad_set_chain_function (self->sink_pad, gst_codec_alpha_demux_chain);
314 gst_pad_set_event_function (self->sink_pad, gst_codec_alpha_demux_sink_event);
315 gst_pad_set_query_function (self->sink_pad, gst_codec_alpha_demux_sink_query);