From ba4053a2b942f893dc6b5e0bbcd7a6f8f607811e Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Thu, 22 Apr 2021 16:50:17 -0400 Subject: [PATCH] alphadecodebin: Add wrappers to decode VP8/VP9 alpha This includes base class with wrappers bin that will create a static pipeline capable of handling the VP8/VP9 alpha channel decoding using two instances of vp8/vp9dec element each. Part-of: --- gst/codecalpha/gstalphadecodebin.c | 219 ++++++++++++++++++++++++++++++++++ gst/codecalpha/gstalphadecodebin.h | 48 ++++++++ gst/codecalpha/gstplugin.c | 12 ++ gst/codecalpha/gstvp8alphadecodebin.c | 73 ++++++++++++ gst/codecalpha/gstvp8alphadecodebin.h | 35 ++++++ gst/codecalpha/gstvp9alphadecodebin.c | 73 ++++++++++++ gst/codecalpha/gstvp9alphadecodebin.h | 35 ++++++ gst/codecalpha/meson.build | 5 +- 8 files changed, 499 insertions(+), 1 deletion(-) create mode 100644 gst/codecalpha/gstalphadecodebin.c create mode 100644 gst/codecalpha/gstalphadecodebin.h create mode 100644 gst/codecalpha/gstvp8alphadecodebin.c create mode 100644 gst/codecalpha/gstvp8alphadecodebin.h create mode 100644 gst/codecalpha/gstvp9alphadecodebin.c create mode 100644 gst/codecalpha/gstvp9alphadecodebin.h diff --git a/gst/codecalpha/gstalphadecodebin.c b/gst/codecalpha/gstalphadecodebin.c new file mode 100644 index 0000000..aead165 --- /dev/null +++ b/gst/codecalpha/gstalphadecodebin.c @@ -0,0 +1,219 @@ +/* GStreamer + * Copyright (C) <2021> Collabora Ltd. + * Author: Nicolas Dufresne + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "gstalphadecodebin.h" + +GST_DEBUG_CATEGORY_STATIC (alphadecodebin_debug); +#define GST_CAT_DEFAULT (alphadecodebin_debug) + +typedef struct +{ + GstBin parent; + + gboolean constructed; + const gchar *missing_element; +} GstAlphaDecodeBinPrivate; + +#define gst_alpha_decode_bin_parent_class parent_class +G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstAlphaDecodeBin, gst_alpha_decode_bin, + GST_TYPE_BIN, + G_ADD_PRIVATE (GstAlphaDecodeBin); + GST_DEBUG_CATEGORY_INIT (alphadecodebin_debug, "alphadecodebin", 0, + "alphadecodebin")); + +static GstStaticPadTemplate gst_alpha_decode_bin_src_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("ANY") + ); + +static gboolean +gst_alpha_decode_bin_open (GstAlphaDecodeBin * self) +{ + GstAlphaDecodeBinPrivate *priv = + gst_alpha_decode_bin_get_instance_private (self); + + if (priv->missing_element) { + gst_element_post_message (GST_ELEMENT (self), + gst_missing_element_message_new (GST_ELEMENT (self), + priv->missing_element)); + } else if (!priv->constructed) { + GST_ELEMENT_ERROR (self, CORE, FAILED, + ("Failed to construct alpha decoder pipeline."), (NULL)); + } + + return priv->constructed; +} + +static GstStateChangeReturn +gst_alpha_decode_bin_change_state (GstElement * element, + GstStateChange transition) +{ + GstAlphaDecodeBin *self = GST_ALPHA_DECODE_BIN (element); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + if (!gst_alpha_decode_bin_open (self)) + return GST_STATE_CHANGE_FAILURE; + break; + default: + break; + } + + return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); +} + +static void +gst_alpha_decode_bin_constructed (GObject * obj) +{ + GstAlphaDecodeBin *self = GST_ALPHA_DECODE_BIN (obj); + GstAlphaDecodeBinPrivate *priv = + gst_alpha_decode_bin_get_instance_private (self); + GstAlphaDecodeBinClass *klass = GST_ALPHA_DECODE_BIN_GET_CLASS (self); + GstPad *src_gpad, *sink_gpad; + GstPad *src_pad = NULL, *sink_pad = NULL; + GstElement *alphademux = NULL; + GstElement *multiqueue = NULL; + GstElement *decoder = NULL; + GstElement *alpha_decoder = NULL; + GstElement *alphacombine = NULL; + + /* setup ghost pads */ + sink_gpad = gst_ghost_pad_new_no_target_from_template ("sink", + gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "sink")); + gst_element_add_pad (GST_ELEMENT (self), sink_gpad); + + src_gpad = gst_ghost_pad_new_no_target_from_template ("src", + gst_element_class_get_pad_template (GST_ELEMENT_CLASS (klass), "src")); + gst_element_add_pad (GST_ELEMENT (self), src_gpad); + + /* create elements */ + alphademux = gst_element_factory_make ("codecalphademux", NULL); + if (!alphademux) { + priv->missing_element = "codecalphademux"; + goto cleanup; + } + + multiqueue = gst_element_factory_make ("multiqueue", NULL); + if (!multiqueue) { + priv->missing_element = "multiqueue"; + goto cleanup; + } + + decoder = gst_element_factory_make (klass->decoder_name, NULL); + if (!decoder) { + priv->missing_element = klass->decoder_name; + goto cleanup; + } + + alpha_decoder = gst_element_factory_make (klass->decoder_name, NULL); + if (!alpha_decoder) { + priv->missing_element = klass->decoder_name; + goto cleanup; + } + + alphacombine = gst_element_factory_make ("alphacombine", NULL); + if (!alphacombine) { + priv->missing_element = "alphacombine"; + goto cleanup; + } + + gst_bin_add_many (GST_BIN (self), alphademux, multiqueue, decoder, + alpha_decoder, alphacombine, NULL); + + /* link elements */ + sink_pad = gst_element_get_static_pad (alphademux, "sink"); + gst_ghost_pad_set_target (GST_GHOST_PAD (sink_gpad), sink_pad); + gst_clear_object (&sink_pad); + + sink_pad = gst_element_request_pad_simple (multiqueue, "sink_0"); + src_pad = gst_element_get_static_pad (multiqueue, "src_0"); + g_object_set (sink_pad, "group_id", 0, NULL); + g_object_set (src_pad, "group_id", 0, NULL); + gst_clear_object (&sink_pad); + gst_clear_object (&src_pad); + gst_element_link_pads (alphademux, "src", multiqueue, "sink_0"); + gst_element_link_pads (multiqueue, "src_0", decoder, "sink"); + gst_element_link_pads (decoder, "src", alphacombine, "sink"); + + sink_pad = gst_element_request_pad_simple (multiqueue, "sink_1"); + src_pad = gst_element_get_static_pad (multiqueue, "src_1"); + g_object_set (sink_pad, "group_id", 1, NULL); + g_object_set (src_pad, "group_id", 1, NULL); + gst_clear_object (&sink_pad); + gst_clear_object (&src_pad); + gst_element_link_pads (alphademux, "alpha", multiqueue, "sink_1"); + gst_element_link_pads (multiqueue, "src_1", alpha_decoder, "sink"); + gst_element_link_pads (alpha_decoder, "src", alphacombine, "alpha"); + + src_pad = gst_element_get_static_pad (alphacombine, "src"); + gst_ghost_pad_set_target (GST_GHOST_PAD (src_gpad), src_pad); + gst_object_unref (src_pad); + + gst_element_link_pads (alphademux, "alpha", multiqueue, "sink_0"); + + /* configure the elements */ + g_object_set (multiqueue, "max-size-bytes", 0, "max-size-time", 0, + "max-size-buffers", 1, NULL); + + /* signal success, we will handle this in NULL->READY transition */ + priv->constructed = TRUE; + return; + +cleanup: + gst_clear_object (&alphademux); + gst_clear_object (&multiqueue); + gst_clear_object (&decoder); + gst_clear_object (&alpha_decoder); + gst_clear_object (&alphacombine); + + G_OBJECT_CLASS (parent_class)->constructed (obj); +} + +static void +gst_alpha_decode_bin_class_init (GstAlphaDecodeBinClass * klass) +{ + GstElementClass *element_class = (GstElementClass *) klass; + GObjectClass *obj_class = (GObjectClass *) klass; + + /* This is needed to access the subclass class instance, otherwise we cannot + * read the class parameters */ + obj_class->constructed = gst_alpha_decode_bin_constructed; + + gst_element_class_add_static_pad_template (element_class, + &gst_alpha_decode_bin_src_template); + element_class->change_state = + GST_DEBUG_FUNCPTR (gst_alpha_decode_bin_change_state); + + /* let's make the doc generator happy */ + gst_type_mark_as_plugin_api (GST_TYPE_ALPHA_DECODE_BIN, 0); +} + +static void +gst_alpha_decode_bin_init (GstAlphaDecodeBin * self) +{ +} diff --git a/gst/codecalpha/gstalphadecodebin.h b/gst/codecalpha/gstalphadecodebin.h new file mode 100644 index 0000000..a2ecb57 --- /dev/null +++ b/gst/codecalpha/gstalphadecodebin.h @@ -0,0 +1,48 @@ +/* GStreamer + * Copyright (C) <2021> Collabora Ltd. + * Author: Nicolas Dufresne + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_ALPHA_DECODE_BIN_H__ +#define __GST_ALPHA_DECODE_BIN_H__ + +#include + +/* When wrapping, use the original rank plus this offset. The ad-hoc rules is + * that hardware implementation will use PRIMARY+1 or +2 to override the + * software decoder, so the offset must be large enough to jump over those. + * This should also be small enough so that a marginal (64) or secondary + * wrapper does not cross the PRIMARY line. + */ +#define GST_ALPHA_DECODE_BIN_RANK_OFFSET 10 + +G_BEGIN_DECLS + +#define GST_TYPE_ALPHA_DECODE_BIN (gst_alpha_decode_bin_get_type()) +G_DECLARE_DERIVABLE_TYPE (GstAlphaDecodeBin, + gst_alpha_decode_bin, GST, ALPHA_DECODE_BIN, GstBin); + +struct _GstAlphaDecodeBinClass +{ + GstBinClass parent_class; + + const gchar *decoder_name; +}; + +G_END_DECLS +#endif diff --git a/gst/codecalpha/gstplugin.c b/gst/codecalpha/gstplugin.c index f886932..46a2a16 100644 --- a/gst/codecalpha/gstplugin.c +++ b/gst/codecalpha/gstplugin.c @@ -26,6 +26,16 @@ #include "gstcodecalphademux.h" #include "gstalphacombine.h" +#include "gstvp8alphadecodebin.h" +#include "gstvp9alphadecodebin.h" + +/* When wrapping, use the original rank plus this offset. The ad-hoc rules is + * that hardware implementation will use PRIMARY+1 or +2 to override the + * software decoder, so the offset must be large enough to jump over those. + * This should also be small enough so that a marginal (64) or secondary + * wrapper does not cross the PRIMARY line. + */ +#define RANK_OFFSET 10 static gboolean plugin_init (GstPlugin * plugin) @@ -34,6 +44,8 @@ plugin_init (GstPlugin * plugin) ret |= GST_ELEMENT_REGISTER (codec_alpha_demux, plugin); ret |= GST_ELEMENT_REGISTER (alpha_combine, plugin); + ret |= GST_ELEMENT_REGISTER (vp8_alpha_decode_bin, plugin); + ret |= GST_ELEMENT_REGISTER (vp9_alpha_decode_bin, plugin); return ret; } diff --git a/gst/codecalpha/gstvp8alphadecodebin.c b/gst/codecalpha/gstvp8alphadecodebin.c new file mode 100644 index 0000000..1605b7e --- /dev/null +++ b/gst/codecalpha/gstvp8alphadecodebin.c @@ -0,0 +1,73 @@ +/* GStreamer + * Copyright (C) <2021> Collabora Ltd. + * Author: Nicolas Dufresne + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +/** + * SECTION:element-vp8alphadecodebin + * @title: Wrapper to decode VP8 alpha using vp8dec + * + * Use two `vp8dec` instance in order to decode VP8 alpha channel. + * + * Since: 1.20 + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstvp8alphadecodebin.h" + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-vp8, codec-alpha = (boolean) true") + ); + +struct _GstVp8AlphaDecodeBin +{ + GstAlphaDecodeBin parent; +}; + +#define gst_vp8_alpha_decode_bin_parent_class parent_class +G_DEFINE_TYPE (GstVp8AlphaDecodeBin, gst_vp8_alpha_decode_bin, + GST_TYPE_ALPHA_DECODE_BIN); + +GST_ELEMENT_REGISTER_DEFINE (vp8_alpha_decode_bin, "vp8alphadecodebin", + GST_RANK_PRIMARY + GST_ALPHA_DECODE_BIN_RANK_OFFSET, + GST_TYPE_VP8_ALPHA_DECODE_BIN); + +static void +gst_vp8_alpha_decode_bin_class_init (GstVp8AlphaDecodeBinClass * klass) +{ + GstAlphaDecodeBinClass *adbin_class = (GstAlphaDecodeBinClass *) klass; + GstElementClass *element_class = (GstElementClass *) klass; + + adbin_class->decoder_name = "vp8dec"; + gst_element_class_add_static_pad_template (element_class, &sink_template); + + gst_element_class_set_static_metadata (element_class, + "VP8 Alpha Decoder", "Codec/Decoder/Video", + "Wrapper bin to decode VP8 with alpha stream.", + "Nicolas Dufresne "); +} + +static void +gst_vp8_alpha_decode_bin_init (GstVp8AlphaDecodeBin * self) +{ +} diff --git a/gst/codecalpha/gstvp8alphadecodebin.h b/gst/codecalpha/gstvp8alphadecodebin.h new file mode 100644 index 0000000..1eeaf26 --- /dev/null +++ b/gst/codecalpha/gstvp8alphadecodebin.h @@ -0,0 +1,35 @@ +/* GStreamer + * Copyright (C) <2021> Collabora Ltd. + * Author: Nicolas Dufresne + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_VP8_ALPHA_DECODE_BIN_H__ +#define __GST_VP8_ALPHA_DECODE_BIN_H__ + +#include "gstalphadecodebin.h" + +G_BEGIN_DECLS + +#define GST_TYPE_VP8_ALPHA_DECODE_BIN (gst_vp8_alpha_decode_bin_get_type()) +G_DECLARE_FINAL_TYPE (GstVp8AlphaDecodeBin, gst_vp8_alpha_decode_bin, + GST, VP8_ALPHA_DECODE_BIN, GstAlphaDecodeBin); + +GST_ELEMENT_REGISTER_DECLARE (vp8_alpha_decode_bin); + +G_END_DECLS +#endif diff --git a/gst/codecalpha/gstvp9alphadecodebin.c b/gst/codecalpha/gstvp9alphadecodebin.c new file mode 100644 index 0000000..6d89765 --- /dev/null +++ b/gst/codecalpha/gstvp9alphadecodebin.c @@ -0,0 +1,73 @@ +/* GStreamer + * Copyright (C) <2021> Collabora Ltd. + * Author: Nicolas Dufresne + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +/** + * SECTION:element-vp9alphadecodebin + * @title: Wrapper to decode VP9 alpha using vp9dec + * + * Use two `vp9dec` instance in order to decode VP9 alpha channel. + * + * Since: 1.20 + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstvp9alphadecodebin.h" + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-vp9, codec-alpha = (boolean) true") + ); + +struct _GstVp9AlphaDecodeBin +{ + GstAlphaDecodeBin parent; +}; + +#define gst_vp9_alpha_decode_bin_parent_class parent_class +G_DEFINE_TYPE (GstVp9AlphaDecodeBin, gst_vp9_alpha_decode_bin, + GST_TYPE_ALPHA_DECODE_BIN); + +GST_ELEMENT_REGISTER_DEFINE (vp9_alpha_decode_bin, "vp9alphadecodebin", + GST_RANK_PRIMARY + GST_ALPHA_DECODE_BIN_RANK_OFFSET, + GST_TYPE_VP9_ALPHA_DECODE_BIN); + +static void +gst_vp9_alpha_decode_bin_class_init (GstVp9AlphaDecodeBinClass * klass) +{ + GstAlphaDecodeBinClass *adbin_class = (GstAlphaDecodeBinClass *) klass; + GstElementClass *element_class = (GstElementClass *) klass; + + adbin_class->decoder_name = "vp9dec"; + gst_element_class_add_static_pad_template (element_class, &sink_template); + + gst_element_class_set_static_metadata (element_class, + "VP9 Alpha Decoder", "Codec/Decoder/Video", + "Wrapper bin to decode VP9 with alpha stream.", + "Nicolas Dufresne "); +} + +static void +gst_vp9_alpha_decode_bin_init (GstVp9AlphaDecodeBin * self) +{ +} diff --git a/gst/codecalpha/gstvp9alphadecodebin.h b/gst/codecalpha/gstvp9alphadecodebin.h new file mode 100644 index 0000000..29e1761 --- /dev/null +++ b/gst/codecalpha/gstvp9alphadecodebin.h @@ -0,0 +1,35 @@ +/* GStreamer + * Copyright (C) <2021> Collabora Ltd. + * Author: Nicolas Dufresne + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_VP9_ALPHA_DECODE_BIN_H__ +#define __GST_VP9_ALPHA_DECODE_BIN_H__ + +#include "gstalphadecodebin.h" + +G_BEGIN_DECLS + +#define GST_TYPE_VP9_ALPHA_DECODE_BIN (gst_vp9_alpha_decode_bin_get_type()) +G_DECLARE_FINAL_TYPE (GstVp9AlphaDecodeBin, + gst_vp9_alpha_decode_bin, GST, VP9_ALPHA_DECODE_BIN, GstAlphaDecodeBin); + +GST_ELEMENT_REGISTER_DECLARE (vp9_alpha_decode_bin); + +G_END_DECLS +#endif diff --git a/gst/codecalpha/meson.build b/gst/codecalpha/meson.build index 9638f3b..fbde4cb 100644 --- a/gst/codecalpha/meson.build +++ b/gst/codecalpha/meson.build @@ -1,6 +1,9 @@ codecalpha_sources = [ 'gstplugin.c', 'gstalphacombine.c', + 'gstalphadecodebin.c', + 'gstvp8alphadecodebin.c', + 'gstvp9alphadecodebin.c', 'gstcodecalphademux.c', ] @@ -8,7 +11,7 @@ gstcodecalpha = library('gstcodecalpha', codecalpha_sources, c_args : gst_plugins_bad_args, include_directories : [configinc], - dependencies : [gstvideo_dep], + dependencies : [gstvideo_dep, gstpbutils_dep], install : true, install_dir : plugins_install_dir, ) -- 2.7.4