1 /* GStreamer
2 * Copyright (C) <2021> Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:element-codecalphademux
23 * @title: CODEC Alpha Demuxer
24 *
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.
28 *
29 * ## Example launch line
30 * |[
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.
37 *
38 * Since: 1.20
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <gst/video/video.h>
46 #include <gst/base/gstflowcombiner.h>
47
48 #include "gstcodecalphademux.h"
49
50 GST_DEBUG_CATEGORY_STATIC (codecalphademux_debug);
51 #define GST_CAT_DEFAULT (codecalphademux_debug)
52
53 struct _GstCodecAlphaDemux
54 {
55 GstElement parent;
56
57 GstPad *sink_pad;
58 GstPad *src_pad;
59 GstPad *alpha_pad;
60
61 GstFlowCombiner *flow_combiner;
62 };
63
64 #define gst_codec_alpha_demux_parent_class parent_class
65 G_DEFINE_TYPE_WITH_CODE (GstCodecAlphaDemux, gst_codec_alpha_demux,
66 GST_TYPE_ELEMENT,
67 GST_DEBUG_CATEGORY_INIT (codecalphademux_debug, "codecalphademux", 0,
68 "codecalphademux"));
69
70 GST_ELEMENT_REGISTER_DEFINE (codec_alpha_demux, "codecalphademux",
71 GST_RANK_NONE, GST_TYPE_CODEC_ALPHA_DEMUX);
72
73 static GstStaticPadTemplate gst_codec_alpha_demux_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
75 GST_PAD_SINK,
76 GST_PAD_ALWAYS,
77 GST_STATIC_CAPS ("ANY")
78 );
79
80 static GstStaticPadTemplate gst_codec_alpha_demux_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
82 GST_PAD_SRC,
83 GST_PAD_ALWAYS,
84 GST_STATIC_CAPS ("ANY")
85 );
86
87 static GstStaticPadTemplate gst_codec_alpha_demux_alpha_template =
88 GST_STATIC_PAD_TEMPLATE ("alpha",
89 GST_PAD_SRC,
90 GST_PAD_ALWAYS,
91 GST_STATIC_CAPS ("ANY")
92 );
93
94 static GstFlowReturn
gst_codec_alpha_demux_chain(GstPad * pad,GstObject * object,GstBuffer * buffer)95 gst_codec_alpha_demux_chain (GstPad * pad, GstObject * object,
96 GstBuffer * buffer)
97 {
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;
105
106 if (alpha_meta)
107 alpha_buffer = gst_buffer_ref (alpha_meta->buffer);
108
109 ret = gst_pad_push (self->src_pad, buffer);
110 ret = gst_flow_combiner_update_flow (self->flow_combiner, ret);
111
112 /* we lost ownership here */
113 buffer = NULL;
114 alpha_meta = NULL;
115
116 if (alpha_buffer) {
117 ret = gst_pad_push (self->alpha_pad, alpha_buffer);
118 } else {
119 gst_pad_push_event (self->alpha_pad, gst_event_new_gap (pts, duration));
120 ret = GST_PAD_LAST_FLOW_RETURN (self->alpha_pad);
121 }
122 ret = gst_flow_combiner_update_flow (self->flow_combiner, ret);
123
124 return ret;
125 }
126
127 static GstCaps *
gst_codec_alpha_demux_transform_caps(GstCaps * caps,gboolean codec_alpha)128 gst_codec_alpha_demux_transform_caps (GstCaps * caps, gboolean codec_alpha)
129 {
130 if (!caps)
131 return NULL;
132
133 caps = gst_caps_copy (caps);
134 gst_caps_set_simple (caps, "codec-alpha", G_TYPE_BOOLEAN, codec_alpha, NULL);
135
136 return caps;
137 }
138
139 static GstEvent *
gst_codec_alpha_demux_transform_caps_event(GstEvent * src_event)140 gst_codec_alpha_demux_transform_caps_event (GstEvent * src_event)
141 {
142 GstEvent *dst_event;
143 GstCaps *caps;
144
145 gst_event_parse_caps (src_event, &caps);
146
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));
150
151 gst_caps_unref (caps);
152 gst_event_unref (src_event);
153 return dst_event;
154 }
155
156 static gboolean
gst_codec_alpha_demux_sink_event(GstPad * sink_pad,GstObject * parent,GstEvent * event)157 gst_codec_alpha_demux_sink_event (GstPad * sink_pad, GstObject * parent,
158 GstEvent * event)
159 {
160 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (parent);
161
162 switch (event->type) {
163 case GST_EVENT_FLUSH_STOP:
164 gst_flow_combiner_reset (self->flow_combiner);
165 break;
166 case GST_EVENT_CAPS:
167 event = gst_codec_alpha_demux_transform_caps_event (event);
168 break;
169 default:
170 break;
171 }
172
173 return gst_pad_event_default (sink_pad, parent, event);
174 }
175
176 static gboolean
gst_codec_alpha_demux_sink_query(GstPad * sink_pad,GstObject * parent,GstQuery * query)177 gst_codec_alpha_demux_sink_query (GstPad * sink_pad, GstObject * parent,
178 GstQuery * query)
179 {
180 GstQuery *peer_query;
181 GstCaps *caps;
182 gboolean ret;
183
184 switch (query->type) {
185 case GST_QUERY_CAPS:
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);
190 break;
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);
196 break;
197 default:
198 peer_query = query;
199 break;
200 }
201
202 ret = gst_pad_query_default (sink_pad, parent, peer_query);
203 if (!ret) {
204 if (peer_query != query)
205 gst_query_unref (peer_query);
206 return FALSE;
207 }
208
209 switch (query->type) {
210 case GST_QUERY_CAPS:
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);
217 break;
218 case GST_QUERY_ACCEPT_CAPS:
219 {
220 gboolean result;
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);
224 break;
225 }
226 default:
227 break;
228 }
229
230
231 return ret;
232 }
233
234 static void
gst_codec_alpha_demux_start(GstCodecAlphaDemux * self)235 gst_codec_alpha_demux_start (GstCodecAlphaDemux * self)
236 {
237 gst_flow_combiner_reset (self->flow_combiner);
238 }
239
240 static GstStateChangeReturn
gst_codec_alpha_demux_change_state(GstElement * element,GstStateChange transition)241 gst_codec_alpha_demux_change_state (GstElement * element,
242 GstStateChange transition)
243 {
244 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (element);
245
246 switch (transition) {
247 case GST_STATE_CHANGE_READY_TO_PAUSED:
248 gst_codec_alpha_demux_start (self);
249 default:
250 break;
251 }
252
253 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
254 }
255
256 static void
gst_codec_alpha_demux_dispose(GObject * object)257 gst_codec_alpha_demux_dispose (GObject * object)
258 {
259 GstCodecAlphaDemux *self = GST_CODEC_ALPHA_DEMUX (object);
260
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);
265
266 G_OBJECT_CLASS (parent_class)->dispose (object);
267 }
268
269 static void
gst_codec_alpha_demux_class_init(GstCodecAlphaDemuxClass * klass)270 gst_codec_alpha_demux_class_init (GstCodecAlphaDemuxClass * klass)
271 {
272 GstElementClass *element_class = (GstElementClass *) klass;
273 GObjectClass *object_class = (GObjectClass *) klass;
274
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>");
279
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);
286
287 element_class->change_state =
288 GST_DEBUG_FUNCPTR (gst_codec_alpha_demux_change_state);
289
290 object_class->dispose = GST_DEBUG_FUNCPTR (gst_codec_alpha_demux_dispose);
291 }
292
293 static void
gst_codec_alpha_demux_init(GstCodecAlphaDemux * self)294 gst_codec_alpha_demux_init (GstCodecAlphaDemux * self)
295 {
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");
300
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);
304
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);
308
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);
312
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);
316 }
317