docs: remove old 0.10 Since markers
[platform/upstream/gstreamer.git] / ext / pango / gstbasetextoverlay.c
index caffd25..be1cd6e 100644 (file)
@@ -18,8 +18,8 @@
  *
  * 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., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -76,6 +76,7 @@
 #endif
 
 #include <gst/video/video.h>
+#include <gst/video/gstvideometa.h>
 
 #include "gstbasetextoverlay.h"
 #include "gsttextoverlay.h"
@@ -98,8 +99,6 @@ GST_DEBUG_CATEGORY (pango_debug);
 #define DEFAULT_PROP_SHADING   FALSE
 #define DEFAULT_PROP_VALIGNMENT        GST_BASE_TEXT_OVERLAY_VALIGN_BASELINE
 #define DEFAULT_PROP_HALIGNMENT        GST_BASE_TEXT_OVERLAY_HALIGN_CENTER
-#define DEFAULT_PROP_VALIGN    "baseline"
-#define DEFAULT_PROP_HALIGN    "center"
 #define DEFAULT_PROP_XPAD      25
 #define DEFAULT_PROP_YPAD      25
 #define DEFAULT_PROP_DELTAX    0
@@ -122,55 +121,11 @@ GST_DEBUG_CATEGORY (pango_debug);
 #define MINIMUM_OUTLINE_OFFSET 1.0
 #define DEFAULT_SCALE_BASIS    640
 
-#define COMP_Y(ret, r, g, b) \
-{ \
-   ret = (int) (((19595 * r) >> 16) + ((38470 * g) >> 16) + ((7471 * b) >> 16)); \
-   ret = CLAMP (ret, 0, 255); \
-}
-
-#define COMP_U(ret, r, g, b) \
-{ \
-   ret = (int) (-((11059 * r) >> 16) - ((21709 * g) >> 16) + ((32768 * b) >> 16) + 128); \
-   ret = CLAMP (ret, 0, 255); \
-}
-
-#define COMP_V(ret, r, g, b) \
-{ \
-   ret = (int) (((32768 * r) >> 16) - ((27439 * g) >> 16) - ((5329 * b) >> 16) + 128); \
-   ret = CLAMP (ret, 0, 255); \
-}
-
-#define BLEND(ret, alpha, v0, v1) \
-{ \
-       ret = (v0 * alpha + v1 * (255 - alpha)) / 255; \
-}
-
-#define OVER(ret, alphaA, Ca, alphaB, Cb, alphaNew)    \
-{ \
-    gint _tmp; \
-    _tmp = (Ca * alphaA + Cb * alphaB * (255 - alphaA) / 255) / alphaNew; \
-    ret = CLAMP (_tmp, 0, 255); \
-}
-
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
-# define CAIRO_ARGB_A 3
-# define CAIRO_ARGB_R 2
-# define CAIRO_ARGB_G 1
-# define CAIRO_ARGB_B 0
-#else
-# define CAIRO_ARGB_A 0
-# define CAIRO_ARGB_R 1
-# define CAIRO_ARGB_G 2
-# define CAIRO_ARGB_B 3
-#endif
-
 enum
 {
   PROP_0,
   PROP_TEXT,
   PROP_SHADING,
-  PROP_VALIGN,                  /* deprecated */
-  PROP_HALIGN,                  /* deprecated */
   PROP_HALIGNMENT,
   PROP_VALIGNMENT,
   PROP_XPAD,
@@ -192,7 +147,7 @@ enum
   PROP_LAST
 };
 
-#define VIDEO_FORMATS "{ BGRx, RGBx, xRGB, xBGR, RGBA, BGRA, ARGB, ABGR, AYUV, I420, UYVY, NV12, NV21 } "
+#define VIDEO_FORMATS GST_VIDEO_OVERLAY_COMPOSITION_BLEND_FORMATS
 
 static GstStaticPadTemplate src_template_factory =
 GST_STATIC_PAD_TEMPLATE ("src",
@@ -293,8 +248,11 @@ gst_base_text_overlay_line_align_get_type (void)
   return base_text_overlay_line_align_type;
 }
 
-#define GST_BASE_TEXT_OVERLAY_GET_COND(ov) (((GstBaseTextOverlay *)ov)->cond)
-#define GST_BASE_TEXT_OVERLAY_WAIT(ov)     (g_cond_wait (GST_BASE_TEXT_OVERLAY_GET_COND (ov), GST_OBJECT_GET_LOCK (ov)))
+#define GST_BASE_TEXT_OVERLAY_GET_LOCK(ov) (&GST_BASE_TEXT_OVERLAY (ov)->lock)
+#define GST_BASE_TEXT_OVERLAY_GET_COND(ov) (&GST_BASE_TEXT_OVERLAY (ov)->cond)
+#define GST_BASE_TEXT_OVERLAY_LOCK(ov)     (g_mutex_lock (GST_BASE_TEXT_OVERLAY_GET_LOCK (ov)))
+#define GST_BASE_TEXT_OVERLAY_UNLOCK(ov)   (g_mutex_unlock (GST_BASE_TEXT_OVERLAY_GET_LOCK (ov)))
+#define GST_BASE_TEXT_OVERLAY_WAIT(ov)     (g_cond_wait (GST_BASE_TEXT_OVERLAY_GET_COND (ov), GST_BASE_TEXT_OVERLAY_GET_LOCK (ov)))
 #define GST_BASE_TEXT_OVERLAY_SIGNAL(ov)   (g_cond_signal (GST_BASE_TEXT_OVERLAY_GET_COND (ov)))
 #define GST_BASE_TEXT_OVERLAY_BROADCAST(ov)(g_cond_broadcast (GST_BASE_TEXT_OVERLAY_GET_COND (ov)))
 
@@ -307,28 +265,32 @@ static void gst_base_text_overlay_init (GstBaseTextOverlay * overlay,
 static GstStateChangeReturn gst_base_text_overlay_change_state (GstElement *
     element, GstStateChange transition);
 
-static GstCaps *gst_base_text_overlay_getcaps (GstPad * pad, GstCaps * filter);
+static GstCaps *gst_base_text_overlay_getcaps (GstPad * pad,
+    GstBaseTextOverlay * overlay, GstCaps * filter);
 static gboolean gst_base_text_overlay_setcaps (GstBaseTextOverlay * overlay,
     GstCaps * caps);
 static gboolean gst_base_text_overlay_setcaps_txt (GstBaseTextOverlay * overlay,
     GstCaps * caps);
 static gboolean gst_base_text_overlay_src_event (GstPad * pad,
-    GstEvent * event);
+    GstObject * parent, GstEvent * event);
 static gboolean gst_base_text_overlay_src_query (GstPad * pad,
-    GstQuery * query);
+    GstObject * parent, GstQuery * query);
 
 static gboolean gst_base_text_overlay_video_event (GstPad * pad,
-    GstEvent * event);
+    GstObject * parent, GstEvent * event);
+static gboolean gst_base_text_overlay_video_query (GstPad * pad,
+    GstObject * parent, GstQuery * query);
 static GstFlowReturn gst_base_text_overlay_video_chain (GstPad * pad,
-    GstBuffer * buffer);
+    GstObject * parent, GstBuffer * buffer);
 
 static gboolean gst_base_text_overlay_text_event (GstPad * pad,
-    GstEvent * event);
+    GstObject * parent, GstEvent * event);
 static GstFlowReturn gst_base_text_overlay_text_chain (GstPad * pad,
-    GstBuffer * buffer);
+    GstObject * parent, GstBuffer * buffer);
 static GstPadLinkReturn gst_base_text_overlay_text_pad_link (GstPad * pad,
-    GstPad * peer);
-static void gst_base_text_overlay_text_pad_unlink (GstPad * pad);
+    GstObject * parent, GstPad * peer);
+static void gst_base_text_overlay_text_pad_unlink (GstPad * pad,
+    GstObject * parent);
 static void gst_base_text_overlay_pop_text (GstBaseTextOverlay * overlay);
 static void gst_base_text_overlay_update_render_mode (GstBaseTextOverlay *
     overlay);
@@ -388,7 +350,7 @@ gst_base_text_overlay_base_init (gpointer g_class)
     g_mutex_lock (klass->pango_lock);
   fontmap = pango_cairo_font_map_get_default ();
   klass->pango_context =
-      pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
+      pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
   if (klass->pango_lock)
     g_mutex_unlock (klass->pango_lock);
 }
@@ -416,7 +378,8 @@ gst_base_text_overlay_class_init (GstBaseTextOverlayClass * klass)
   gstelement_class->change_state =
       GST_DEBUG_FUNCPTR (gst_base_text_overlay_change_state);
 
-  klass->pango_lock = g_mutex_new ();
+  klass->pango_lock = g_slice_new (GMutex);
+  g_mutex_init (klass->pango_lock);
 
   klass->get_text = gst_base_text_overlay_get_text;
 
@@ -436,14 +399,6 @@ gst_base_text_overlay_class_init (GstBaseTextOverlayClass * klass)
       g_param_spec_enum ("halignment", "horizontal alignment",
           "Horizontal alignment of the text", GST_TYPE_BASE_TEXT_OVERLAY_HALIGN,
           DEFAULT_PROP_HALIGNMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VALIGN,
-      g_param_spec_string ("valign", "vertical alignment",
-          "Vertical alignment of the text (deprecated; use valignment)",
-          DEFAULT_PROP_VALIGN, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
-  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HALIGN,
-      g_param_spec_string ("halign", "horizontal alignment",
-          "Horizontal alignment of the text (deprecated; use halignment)",
-          DEFAULT_PROP_HALIGN, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_XPAD,
       g_param_spec_int ("xpad", "horizontal paddding",
           "Horizontal paddding when using left/right alignment", 0, G_MAXINT,
@@ -462,24 +417,20 @@ gst_base_text_overlay_class_init (GstBaseTextOverlayClass * klass)
           "Shift Y position up or down. Unit is pixels.", G_MININT, G_MAXINT,
           DEFAULT_PROP_DELTAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   /**
-   * GstBaseTextOverlay:xpos
+   * GstBaseTextOverlay:xpos:
    *
    * Horizontal position of the rendered text when using positioned alignment.
-   *
-   * Since: 0.10.31
-   **/
+   */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_XPOS,
       g_param_spec_double ("xpos", "horizontal position",
           "Horizontal position when using position alignment", 0, 1.0,
           DEFAULT_PROP_XPOS,
           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
   /**
-   * GstBaseTextOverlay:ypos
+   * GstBaseTextOverlay:ypos:
    *
    * Vertical position of the rendered text when using positioned alignment.
-   *
-   * Since: 0.10.31
-   **/
+   */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_YPOS,
       g_param_spec_double ("ypos", "vertical position",
           "Vertical position when using position alignment", 0, 1.0,
@@ -497,24 +448,20 @@ gst_base_text_overlay_class_init (GstBaseTextOverlayClass * klass)
           "for syntax.", DEFAULT_PROP_FONT_DESC,
           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
   /**
-   * GstBaseTextOverlay:color
+   * GstBaseTextOverlay:color:
    *
    * Color of the rendered text.
-   *
-   * Since: 0.10.31
-   **/
+   */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_COLOR,
       g_param_spec_uint ("color", "Color",
           "Color to use for text (big-endian ARGB).", 0, G_MAXUINT32,
           DEFAULT_PROP_COLOR,
           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
   /**
-   * GstTextOverlay:outline-color
+   * GstTextOverlay:outline-color:
    *
    * Color of the outline of the rendered text.
-   *
-   * Since: 0.10.35
-   **/
+   */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_OUTLINE_COLOR,
       g_param_spec_uint ("outline-color", "Text Outline Color",
           "Color to use for outline the text (big-endian ARGB).", 0,
@@ -522,25 +469,21 @@ gst_base_text_overlay_class_init (GstBaseTextOverlayClass * klass)
           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
 
   /**
-   * GstBaseTextOverlay:line-alignment
+   * GstBaseTextOverlay:line-alignment:
    *
    * Alignment of text lines relative to each other (for multi-line text)
-   *
-   * Since: 0.10.15
-   **/
+   */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LINE_ALIGNMENT,
       g_param_spec_enum ("line-alignment", "line alignment",
           "Alignment of text lines relative to each other.",
           GST_TYPE_BASE_TEXT_OVERLAY_LINE_ALIGN, DEFAULT_PROP_LINE_ALIGNMENT,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   /**
-   * GstBaseTextOverlay:silent
+   * GstBaseTextOverlay:silent:
    *
    * If set, no text is rendered. Useful to switch off text rendering
    * temporarily without removing the textoverlay element from the pipeline.
-   *
-   * Since: 0.10.15
-   **/
+   */
   /* FIXME 0.11: rename to "visible" or "text-visible" or "render-text" */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SILENT,
       g_param_spec_boolean ("silent", "silent",
@@ -548,14 +491,12 @@ gst_base_text_overlay_class_init (GstBaseTextOverlayClass * klass)
           DEFAULT_PROP_SILENT,
           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
   /**
-   * GstBaseTextOverlay:wait-text
+   * GstBaseTextOverlay:wait-text:
    *
    * If set, the video will block until a subtitle is received on the text pad.
    * If video and subtitles are sent in sync, like from the same demuxer, this
    * property should be set.
-   *
-   * Since: 0.10.20
-   **/
+   */
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_WAIT_TEXT,
       g_param_spec_boolean ("wait-text", "Wait Text",
           "Whether to wait for subtitles",
@@ -580,8 +521,13 @@ gst_base_text_overlay_finalize (GObject * object)
 
   g_free (overlay->default_text);
 
+  if (overlay->composition) {
+    gst_video_overlay_composition_unref (overlay->composition);
+    overlay->composition = NULL;
+  }
+
   if (overlay->text_image) {
-    g_free (overlay->text_image);
+    gst_buffer_unref (overlay->text_image);
     overlay->text_image = NULL;
   }
 
@@ -595,10 +541,8 @@ gst_base_text_overlay_finalize (GObject * object)
     overlay->text_buffer = NULL;
   }
 
-  if (overlay->cond) {
-    g_cond_free (overlay->cond);
-    overlay->cond = NULL;
-  }
+  g_mutex_clear (&overlay->lock);
+  g_cond_clear (&overlay->cond);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -614,12 +558,13 @@ gst_base_text_overlay_init (GstBaseTextOverlay * overlay,
   template = gst_static_pad_template_get (&video_sink_template_factory);
   overlay->video_sinkpad = gst_pad_new_from_template (template, "video_sink");
   gst_object_unref (template);
-  gst_pad_set_getcaps_function (overlay->video_sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_text_overlay_getcaps));
   gst_pad_set_event_function (overlay->video_sinkpad,
       GST_DEBUG_FUNCPTR (gst_base_text_overlay_video_event));
   gst_pad_set_chain_function (overlay->video_sinkpad,
       GST_DEBUG_FUNCPTR (gst_base_text_overlay_video_chain));
+  gst_pad_set_query_function (overlay->video_sinkpad,
+      GST_DEBUG_FUNCPTR (gst_base_text_overlay_video_query));
+  GST_PAD_SET_PROXY_ALLOCATION (overlay->video_sinkpad);
   gst_element_add_pad (GST_ELEMENT (overlay), overlay->video_sinkpad);
 
   template =
@@ -628,7 +573,6 @@ gst_base_text_overlay_init (GstBaseTextOverlay * overlay,
   if (template) {
     /* text sink */
     overlay->text_sinkpad = gst_pad_new_from_template (template, "text_sink");
-    gst_object_unref (template);
 
     gst_pad_set_event_function (overlay->text_sinkpad,
         GST_DEBUG_FUNCPTR (gst_base_text_overlay_text_event));
@@ -645,8 +589,6 @@ gst_base_text_overlay_init (GstBaseTextOverlay * overlay,
   template = gst_static_pad_template_get (&src_template_factory);
   overlay->srcpad = gst_pad_new_from_template (template, "src");
   gst_object_unref (template);
-  gst_pad_set_getcaps_function (overlay->srcpad,
-      GST_DEBUG_FUNCPTR (gst_base_text_overlay_getcaps));
   gst_pad_set_event_function (overlay->srcpad,
       GST_DEBUG_FUNCPTR (gst_base_text_overlay_src_event));
   gst_pad_set_query_function (overlay->srcpad,
@@ -690,7 +632,8 @@ gst_base_text_overlay_init (GstBaseTextOverlay * overlay,
 
   overlay->text_buffer = NULL;
   overlay->text_linked = FALSE;
-  overlay->cond = g_cond_new ();
+  g_mutex_init (&overlay->lock);
+  g_cond_init (&overlay->cond);
   gst_segment_init (&overlay->segment, GST_FORMAT_TIME);
   g_mutex_unlock (GST_BASE_TEXT_OVERLAY_GET_CLASS (overlay)->pango_lock);
 }
@@ -736,7 +679,8 @@ gst_base_text_overlay_update_render_mode (GstBaseTextOverlay * overlay)
   } else {
     pango_context_set_base_gravity (context, PANGO_GRAVITY_SOUTH);
     pango_context_set_matrix (context, &matrix);
-    pango_layout_set_alignment (overlay->layout, overlay->line_align);
+    pango_layout_set_alignment (overlay->layout,
+        (PangoAlignment) overlay->line_align);
   }
 }
 
@@ -744,16 +688,59 @@ static gboolean
 gst_base_text_overlay_setcaps_txt (GstBaseTextOverlay * overlay, GstCaps * caps)
 {
   GstStructure *structure;
+  const gchar *format;
 
   structure = gst_caps_get_structure (caps, 0);
-  overlay->have_pango_markup =
-      gst_structure_has_name (structure, "text/x-pango-markup");
+  format = gst_structure_get_string (structure, "format");
+  overlay->have_pango_markup = (strcmp (format, "pango-markup") == 0);
 
   return TRUE;
 }
 
 /* FIXME: upstream nego (e.g. when the video window is resized) */
 
+/* only negotiate/query video overlay composition support for now */
+static gboolean
+gst_base_text_overlay_negotiate (GstBaseTextOverlay * overlay)
+{
+  GstCaps *target;
+  GstQuery *query;
+  gboolean attach = FALSE;
+
+  GST_DEBUG_OBJECT (overlay, "performing negotiation");
+
+  target = gst_pad_get_current_caps (overlay->srcpad);
+
+  if (!target || gst_caps_is_empty (target))
+    goto no_format;
+
+  /* find supported meta */
+  query = gst_query_new_allocation (target, TRUE);
+
+  if (!gst_pad_peer_query (overlay->srcpad, query)) {
+    /* no problem, we use the query defaults */
+    GST_DEBUG_OBJECT (overlay, "ALLOCATION query failed");
+  }
+
+  if (gst_query_find_allocation_meta (query,
+          GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL))
+    attach = TRUE;
+
+  overlay->attach_compo_to_buffer = attach;
+
+  gst_query_unref (query);
+  gst_caps_unref (target);
+
+  return TRUE;
+
+no_format:
+  {
+    if (target)
+      gst_caps_unref (target);
+    return FALSE;
+  }
+}
+
 static gboolean
 gst_base_text_overlay_setcaps (GstBaseTextOverlay * overlay, GstCaps * caps)
 {
@@ -768,14 +755,15 @@ gst_base_text_overlay_setcaps (GstBaseTextOverlay * overlay, GstCaps * caps)
   overlay->width = GST_VIDEO_INFO_WIDTH (&info);
   overlay->height = GST_VIDEO_INFO_HEIGHT (&info);
 
-  ret = gst_pad_push_event (overlay->srcpad, gst_event_new_caps (caps));
+  ret = gst_pad_set_caps (overlay->srcpad, caps);
 
   if (ret) {
-    GST_OBJECT_LOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_LOCK (overlay);
     g_mutex_lock (GST_BASE_TEXT_OVERLAY_GET_CLASS (overlay)->pango_lock);
+    gst_base_text_overlay_negotiate (overlay);
     gst_base_text_overlay_update_wrap_mode (overlay);
     g_mutex_unlock (GST_BASE_TEXT_OVERLAY_GET_CLASS (overlay)->pango_lock);
-    GST_OBJECT_UNLOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
   }
 
   return ret;
@@ -794,7 +782,7 @@ gst_base_text_overlay_set_property (GObject * object, guint prop_id,
 {
   GstBaseTextOverlay *overlay = GST_BASE_TEXT_OVERLAY (object);
 
-  GST_OBJECT_LOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_LOCK (overlay);
   switch (prop_id) {
     case PROP_TEXT:
       g_free (overlay->default_text);
@@ -822,34 +810,6 @@ gst_base_text_overlay_set_property (GObject * object, guint prop_id,
     case PROP_YPOS:
       overlay->ypos = g_value_get_double (value);
       break;
-    case PROP_HALIGN:{
-      const gchar *s = g_value_get_string (value);
-
-      if (s && g_ascii_strcasecmp (s, "left") == 0)
-        overlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_LEFT;
-      else if (s && g_ascii_strcasecmp (s, "center") == 0)
-        overlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_CENTER;
-      else if (s && g_ascii_strcasecmp (s, "right") == 0)
-        overlay->halign = GST_BASE_TEXT_OVERLAY_HALIGN_RIGHT;
-      else
-        g_warning ("Invalid value '%s' for textoverlay property 'halign'",
-            GST_STR_NULL (s));
-      break;
-    }
-    case PROP_VALIGN:{
-      const gchar *s = g_value_get_string (value);
-
-      if (s && g_ascii_strcasecmp (s, "baseline") == 0)
-        overlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_BASELINE;
-      else if (s && g_ascii_strcasecmp (s, "bottom") == 0)
-        overlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_BOTTOM;
-      else if (s && g_ascii_strcasecmp (s, "top") == 0)
-        overlay->valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
-      else
-        g_warning ("Invalid value '%s' for textoverlay property 'valign'",
-            GST_STR_NULL (s));
-      break;
-    }
     case PROP_VALIGNMENT:
       overlay->valign = g_value_get_enum (value);
       break;
@@ -918,7 +878,7 @@ gst_base_text_overlay_set_property (GObject * object, guint prop_id,
   }
 
   overlay->need_render = TRUE;
-  GST_OBJECT_UNLOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
 }
 
 static void
@@ -927,7 +887,7 @@ gst_base_text_overlay_get_property (GObject * object, guint prop_id,
 {
   GstBaseTextOverlay *overlay = GST_BASE_TEXT_OVERLAY (object);
 
-  GST_OBJECT_LOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_LOCK (overlay);
   switch (prop_id) {
     case PROP_TEXT:
       g_value_set_string (value, overlay->default_text);
@@ -989,37 +949,46 @@ gst_base_text_overlay_get_property (GObject * object, guint prop_id,
   }
 
   overlay->need_render = TRUE;
-  GST_OBJECT_UNLOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
 }
 
 static gboolean
-gst_base_text_overlay_src_query (GstPad * pad, GstQuery * query)
+gst_base_text_overlay_src_query (GstPad * pad, GstObject * parent,
+    GstQuery * query)
 {
   gboolean ret = FALSE;
-  GstBaseTextOverlay *overlay = NULL;
+  GstBaseTextOverlay *overlay;
 
-  overlay = GST_BASE_TEXT_OVERLAY (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (!overlay))
-    return FALSE;
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
 
-  ret = gst_pad_peer_query (overlay->video_sinkpad, query);
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_CAPS:
+    {
+      GstCaps *filter, *caps;
 
-  gst_object_unref (overlay);
+      gst_query_parse_caps (query, &filter);
+      caps = gst_base_text_overlay_getcaps (pad, overlay, filter);
+      gst_query_set_caps_result (query, caps);
+      gst_caps_unref (caps);
+      ret = TRUE;
+      break;
+    }
+    default:
+      ret = gst_pad_query_default (pad, parent, query);
+      break;
+  }
 
   return ret;
 }
 
 static gboolean
-gst_base_text_overlay_src_event (GstPad * pad, GstEvent * event)
+gst_base_text_overlay_src_event (GstPad * pad, GstObject * parent,
+    GstEvent * event)
 {
   gboolean ret = FALSE;
   GstBaseTextOverlay *overlay = NULL;
 
-  overlay = GST_BASE_TEXT_OVERLAY (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (!overlay)) {
-    gst_event_unref (event);
-    return FALSE;
-  }
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_SEEK:{
@@ -1041,11 +1010,11 @@ gst_base_text_overlay_src_event (GstPad * pad, GstEvent * event)
         gst_pad_push_event (overlay->srcpad, gst_event_new_flush_start ());
 
       /* Mark ourself as flushing, unblock chains */
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       overlay->video_flushing = TRUE;
       overlay->text_flushing = TRUE;
       gst_base_text_overlay_pop_text (overlay);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
 
       /* Seek on each sink pad */
       gst_event_ref (event);
@@ -1069,21 +1038,19 @@ gst_base_text_overlay_src_event (GstPad * pad, GstEvent * event)
   }
 
 beach:
-  gst_object_unref (overlay);
 
   return ret;
 }
 
 static GstCaps *
-gst_base_text_overlay_getcaps (GstPad * pad, GstCaps * filter)
+gst_base_text_overlay_getcaps (GstPad * pad, GstBaseTextOverlay * overlay,
+    GstCaps * filter)
 {
-  GstBaseTextOverlay *overlay;
   GstPad *otherpad;
   GstCaps *caps;
 
-  overlay = GST_BASE_TEXT_OVERLAY (gst_pad_get_parent (pad));
   if (G_UNLIKELY (!overlay))
-    return gst_caps_copy (gst_pad_get_pad_template_caps (pad));
+    return gst_pad_get_pad_template_caps (pad);
 
   if (pad == overlay->srcpad)
     otherpad = overlay->video_sinkpad;
@@ -1091,7 +1058,7 @@ gst_base_text_overlay_getcaps (GstPad * pad, GstCaps * filter)
     otherpad = overlay->srcpad;
 
   /* we can do what the peer can */
-  caps = gst_pad_peer_get_caps (otherpad, filter);
+  caps = gst_pad_peer_query_caps (otherpad, filter);
   if (caps) {
     GstCaps *temp, *templ;
 
@@ -1121,8 +1088,6 @@ gst_base_text_overlay_getcaps (GstPad * pad, GstCaps * filter)
 
   GST_DEBUG_OBJECT (overlay, "returning  %" GST_PTR_FORMAT, caps);
 
-  gst_object_unref (overlay);
-
   return caps;
 }
 
@@ -1137,159 +1102,106 @@ gst_base_text_overlay_adjust_values_with_fontdesc (GstBaseTextOverlay * overlay,
     overlay->outline_offset = MINIMUM_OUTLINE_OFFSET;
 }
 
-#define CAIRO_UNPREMULTIPLY(a,r,g,b) G_STMT_START { \
-  b = (a > 0) ? MIN ((b * 255 + a / 2) / a, 255) : 0; \
-  g = (a > 0) ? MIN ((g * 255 + a / 2) / a, 255) : 0; \
-  r = (a > 0) ? MIN ((r * 255 + a / 2) / a, 255) : 0; \
-} G_STMT_END
-
-static inline void
-gst_base_text_overlay_blit_1 (GstBaseTextOverlay * overlay, guchar * dest,
-    gint xpos, gint ypos, guchar * text_image, guint dest_stride)
+static void
+gst_base_text_overlay_get_pos (GstBaseTextOverlay * overlay,
+    gint * xpos, gint * ypos)
 {
-  gint i, j = 0;
-  gint x, y;
-  guchar r, g, b, a;
-  guchar *pimage;
-  guchar *py;
-  gint width = overlay->image_width;
-  gint height = overlay->image_height;
-
-  if (xpos < 0) {
-    xpos = 0;
-  }
+  gint width, height;
+  GstBaseTextOverlayVAlign valign;
+  GstBaseTextOverlayHAlign halign;
 
-  if (xpos + width > overlay->width) {
-    width = overlay->width - xpos;
-  }
+  width = overlay->image_width;
+  height = overlay->image_height;
+
+  if (overlay->use_vertical_render)
+    halign = GST_BASE_TEXT_OVERLAY_HALIGN_RIGHT;
+  else
+    halign = overlay->halign;
 
-  if (ypos + height > overlay->height) {
-    height = overlay->height - ypos;
+  switch (halign) {
+    case GST_BASE_TEXT_OVERLAY_HALIGN_LEFT:
+      *xpos = overlay->xpad;
+      break;
+    case GST_BASE_TEXT_OVERLAY_HALIGN_CENTER:
+      *xpos = (overlay->width - width) / 2;
+      break;
+    case GST_BASE_TEXT_OVERLAY_HALIGN_RIGHT:
+      *xpos = overlay->width - width - overlay->xpad;
+      break;
+    case GST_BASE_TEXT_OVERLAY_HALIGN_POS:
+      *xpos = (gint) (overlay->width * overlay->xpos) - width / 2;
+      *xpos = CLAMP (*xpos, 0, overlay->width - width);
+      if (*xpos < 0)
+        *xpos = 0;
+      break;
+    default:
+      *xpos = 0;
   }
+  *xpos += overlay->deltax;
 
-  dest += (ypos / 1) * dest_stride;
-
-  for (i = 0; i < height; i++) {
-    pimage = text_image + 4 * (i * overlay->image_width);
-    py = dest + i * dest_stride + xpos;
-    for (j = 0; j < width; j++) {
-      b = pimage[CAIRO_ARGB_B];
-      g = pimage[CAIRO_ARGB_G];
-      r = pimage[CAIRO_ARGB_R];
-      a = pimage[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a, r, g, b);
-
-      pimage += 4;
-      if (a == 0) {
-        py++;
-        continue;
-      }
-      COMP_Y (y, r, g, b);
-      x = *py;
-      BLEND (*py++, a, y, x);
-    }
+  if (overlay->use_vertical_render)
+    valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
+  else
+    valign = overlay->valign;
+
+  switch (valign) {
+    case GST_BASE_TEXT_OVERLAY_VALIGN_BOTTOM:
+      *ypos = overlay->height - height - overlay->ypad;
+      break;
+    case GST_BASE_TEXT_OVERLAY_VALIGN_BASELINE:
+      *ypos = overlay->height - (height + overlay->ypad);
+      break;
+    case GST_BASE_TEXT_OVERLAY_VALIGN_TOP:
+      *ypos = overlay->ypad;
+      break;
+    case GST_BASE_TEXT_OVERLAY_VALIGN_POS:
+      *ypos = (gint) (overlay->height * overlay->ypos) - height / 2;
+      *ypos = CLAMP (*ypos, 0, overlay->height - height);
+      break;
+    case GST_BASE_TEXT_OVERLAY_VALIGN_CENTER:
+      *ypos = (overlay->height - height) / 2;
+      break;
+    default:
+      *ypos = overlay->ypad;
+      break;
   }
+  *ypos += overlay->deltay;
 }
 
 static inline void
-gst_base_text_overlay_blit_sub2x2cbcr (GstBaseTextOverlay * overlay,
-    guchar * destcb, guchar * destcr, gint xpos, gint ypos, guchar * text_image,
-    guint destcb_stride, guint destcr_stride, guint pix_stride)
+gst_base_text_overlay_set_composition (GstBaseTextOverlay * overlay)
 {
-  gint i, j;
-  gint x, cb, cr;
-  gushort r, g, b, a;
-  gushort r1, g1, b1, a1;
-  guchar *pimage1, *pimage2;
-  guchar *pcb, *pcr;
-  gint width = overlay->image_width - 2;
-  gint height = overlay->image_height - 2;
-
-  xpos *= pix_stride;
-
-  if (xpos < 0) {
-    xpos = 0;
-  }
+  gint xpos, ypos;
+  GstVideoOverlayRectangle *rectangle;
 
-  if (xpos + width > overlay->width) {
-    width = overlay->width - xpos;
-  }
+  gst_base_text_overlay_get_pos (overlay, &xpos, &ypos);
 
-  if (ypos + height > overlay->height) {
-    height = overlay->height - ypos;
+  if (overlay->text_image) {
+    gst_buffer_add_video_meta (overlay->text_image, GST_VIDEO_FRAME_FLAG_NONE,
+        GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB,
+        overlay->image_width, overlay->image_height);
+    rectangle = gst_video_overlay_rectangle_new_raw (overlay->text_image,
+        xpos, ypos, overlay->image_width, overlay->image_height,
+        GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
+
+    if (overlay->composition)
+      gst_video_overlay_composition_unref (overlay->composition);
+    overlay->composition = gst_video_overlay_composition_new (rectangle);
+    gst_video_overlay_rectangle_unref (rectangle);
+
+  } else if (overlay->composition) {
+    gst_video_overlay_composition_unref (overlay->composition);
+    overlay->composition = NULL;
   }
+}
 
-  destcb += (ypos / 2) * destcb_stride;
-  destcr += (ypos / 2) * destcr_stride;
-
-  for (i = 0; i < height; i += 2) {
-    pimage1 = text_image + 4 * (i * overlay->image_width);
-    pimage2 = pimage1 + 4 * overlay->image_width;
-    pcb = destcb + (i / 2) * destcb_stride + xpos / 2;
-    pcr = destcr + (i / 2) * destcr_stride + xpos / 2;
-    for (j = 0; j < width; j += 2) {
-      b = pimage1[CAIRO_ARGB_B];
-      g = pimage1[CAIRO_ARGB_G];
-      r = pimage1[CAIRO_ARGB_R];
-      a = pimage1[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a, r, g, b);
-      pimage1 += 4;
-
-      b1 = pimage1[CAIRO_ARGB_B];
-      g1 = pimage1[CAIRO_ARGB_G];
-      r1 = pimage1[CAIRO_ARGB_R];
-      a1 = pimage1[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a1, r1, g1, b1);
-      b += b1;
-      g += g1;
-      r += r1;
-      a += a1;
-      pimage1 += 4;
-
-      b1 = pimage2[CAIRO_ARGB_B];
-      g1 = pimage2[CAIRO_ARGB_G];
-      r1 = pimage2[CAIRO_ARGB_R];
-      a1 = pimage2[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a1, r1, g1, b1);
-      b += b1;
-      g += g1;
-      r += r1;
-      a += a1;
-      pimage2 += 4;
-
-      /* + 2 for rounding */
-      b1 = pimage2[CAIRO_ARGB_B];
-      g1 = pimage2[CAIRO_ARGB_G];
-      r1 = pimage2[CAIRO_ARGB_R];
-      a1 = pimage2[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a1, r1, g1, b1);
-      b += b1 + 2;
-      g += g1 + 2;
-      r += r1 + 2;
-      a += a1 + 2;
-      pimage2 += 4;
-
-      b /= 4;
-      g /= 4;
-      r /= 4;
-      a /= 4;
-
-      if (a == 0) {
-        pcb += pix_stride;
-        pcr += pix_stride;
-        continue;
-      }
-      COMP_U (cb, r, g, b);
-      COMP_V (cr, r, g, b);
-
-      x = *pcb;
-      BLEND (*pcb, a, cb, x);
-      x = *pcr;
-      BLEND (*pcr, a, cr, x);
-
-      pcb += pix_stride;
-      pcr += pix_stride;
-    }
+static gboolean
+gst_text_overlay_filter_foreground_attr (PangoAttribute * attr, gpointer data)
+{
+  if (attr->klass->type == PANGO_ATTR_FOREGROUND) {
+    return FALSE;
+  } else {
+    return TRUE;
   }
 }
 
@@ -1304,6 +1216,8 @@ gst_base_text_overlay_render_pangocairo (GstBaseTextOverlay * overlay,
   int width, height;
   double scalef = 1.0;
   double a, r, g, b;
+  GstBuffer *buffer;
+  GstMapInfo map;
 
   g_mutex_lock (GST_BASE_TEXT_OVERLAY_GET_CLASS (overlay)->pango_lock);
 
@@ -1370,10 +1284,13 @@ gst_base_text_overlay_render_pangocairo (GstBaseTextOverlay * overlay,
     cairo_matrix_init_scale (&cairo_matrix, scalef, scalef);
   }
 
-  /* reallocate surface */
-  overlay->text_image = g_realloc (overlay->text_image, 4 * width * height);
+  /* reallocate overlay buffer */
+  buffer = gst_buffer_new_and_alloc (4 * width * height);
+  gst_buffer_replace (&overlay->text_image, buffer);
+  gst_buffer_unref (buffer);
 
-  surface = cairo_image_surface_create_for_data (overlay->text_image,
+  gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
+  surface = cairo_image_surface_create_for_data (map.data,
       CAIRO_FORMAT_ARGB32, width, height, width * 4);
   cr = cairo_create (surface);
 
@@ -1402,11 +1319,30 @@ gst_base_text_overlay_render_pangocairo (GstBaseTextOverlay * overlay,
    */
 
   /* draw shadow text */
-  cairo_save (cr);
-  cairo_translate (cr, overlay->shadow_offset, overlay->shadow_offset);
-  cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
-  pango_cairo_show_layout (cr, overlay->layout);
-  cairo_restore (cr);
+  {
+    PangoAttrList *origin_attr, *filtered_attr, *temp_attr;
+
+    /* Store a ref on the original attributes for later restoration */
+    origin_attr =
+        pango_attr_list_ref (pango_layout_get_attributes (overlay->layout));
+    /* Take a copy of the original attributes, because pango_attr_list_filter
+     * modifies the passed list */
+    temp_attr = pango_attr_list_copy (origin_attr);
+    filtered_attr =
+        pango_attr_list_filter (temp_attr,
+        gst_text_overlay_filter_foreground_attr, NULL);
+    pango_attr_list_unref (temp_attr);
+
+    cairo_save (cr);
+    cairo_translate (cr, overlay->shadow_offset, overlay->shadow_offset);
+    cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
+    pango_layout_set_attributes (overlay->layout, filtered_attr);
+    pango_cairo_show_layout (cr, overlay->layout);
+    pango_layout_set_attributes (overlay->layout, origin_attr);
+    pango_attr_list_unref (filtered_attr);
+    pango_attr_list_unref (origin_attr);
+    cairo_restore (cr);
+  }
 
   a = (overlay->outline_color >> 24) & 0xff;
   r = (overlay->outline_color >> 16) & 0xff;
@@ -1434,14 +1370,14 @@ gst_base_text_overlay_render_pangocairo (GstBaseTextOverlay * overlay,
 
   cairo_destroy (cr);
   cairo_surface_destroy (surface);
+  gst_buffer_unmap (buffer, &map);
   overlay->image_width = width;
   overlay->image_height = height;
   overlay->baseline_y = ink_rect.y;
   g_mutex_unlock (GST_BASE_TEXT_OVERLAY_GET_CLASS (overlay)->pango_lock);
-}
 
-#define BOX_XPAD         6
-#define BOX_YPAD         6
+  gst_base_text_overlay_set_composition (overlay);
+}
 
 static inline void
 gst_base_text_overlay_shade_planar_Y (GstBaseTextOverlay * overlay,
@@ -1453,12 +1389,6 @@ gst_base_text_overlay_shade_planar_Y (GstBaseTextOverlay * overlay,
   dest_stride = dest->info.stride[0];
   dest_ptr = dest->data[0];
 
-  x0 = CLAMP (x0 - BOX_XPAD, 0, overlay->width);
-  x1 = CLAMP (x1 + BOX_XPAD, 0, overlay->width);
-
-  y0 = CLAMP (y0 - BOX_YPAD, 0, overlay->height);
-  y1 = CLAMP (y1 + BOX_YPAD, 0, overlay->height);
-
   for (i = y0; i < y1; ++i) {
     for (j = x0; j < x1; ++j) {
       gint y = dest_ptr[(i * dest_stride) + j] + overlay->shading_value;
@@ -1480,12 +1410,6 @@ gst_base_text_overlay_shade_packed_Y (GstBaseTextOverlay * overlay,
   dest_ptr = GST_VIDEO_FRAME_COMP_DATA (dest, 0);
   pixel_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (dest, 0);
 
-  x0 = CLAMP (x0 - BOX_XPAD, 0, overlay->width);
-  x1 = CLAMP (x1 + BOX_XPAD, 0, overlay->width);
-
-  y0 = CLAMP (y0 - BOX_YPAD, 0, overlay->height);
-  y1 = CLAMP (y1 + BOX_YPAD, 0, overlay->height);
-
   if (x0 != 0)
     x0 = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (dest->info.finfo, 0, x0);
   if (x1 != 0)
@@ -1521,12 +1445,6 @@ gst_base_text_overlay_shade_xRGB (GstBaseTextOverlay * overlay,
 
   dest_ptr = dest->data[0];
 
-  x0 = CLAMP (x0 - BOX_XPAD, 0, overlay->width);
-  x1 = CLAMP (x1 + BOX_XPAD, 0, overlay->width);
-
-  y0 = CLAMP (y0 - BOX_YPAD, 0, overlay->height);
-  y1 = CLAMP (y1 + BOX_YPAD, 0, overlay->height);
-
   for (i = y0; i < y1; i++) {
     for (j = x0; j < x1; j++) {
       gint y, y_pos, k;
@@ -1540,6 +1458,60 @@ gst_base_text_overlay_shade_xRGB (GstBaseTextOverlay * overlay,
   }
 }
 
+/* FIXME: orcify */
+static void
+gst_base_text_overlay_shade_rgb24 (GstBaseTextOverlay * overlay,
+    GstVideoFrame * frame, gint x0, gint x1, gint y0, gint y1)
+{
+  const int pstride = 3;
+  gint y, x, stride, shading_val, tmp;
+  guint8 *p;
+
+  shading_val = overlay->shading_value;
+  stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
+
+  for (y = y0; y < y1; ++y) {
+    p = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
+    p += (y * stride) + (x0 * pstride);
+    for (x = x0; x < x1; ++x) {
+      tmp = *p + shading_val;
+      *p++ = CLAMP (tmp, 0, 255);
+      tmp = *p + shading_val;
+      *p++ = CLAMP (tmp, 0, 255);
+      tmp = *p + shading_val;
+      *p++ = CLAMP (tmp, 0, 255);
+    }
+  }
+}
+
+static void
+gst_base_text_overlay_shade_IYU1 (GstBaseTextOverlay * overlay,
+    GstVideoFrame * frame, gint x0, gint x1, gint y0, gint y1)
+{
+  gint y, x, stride, shading_val, tmp;
+  guint8 *p;
+
+  shading_val = overlay->shading_value;
+  stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0);
+
+  /* IYU1: packed 4:1:1 YUV (Cb-Y0-Y1-Cr-Y2-Y3 ...) */
+  for (y = y0; y < y1; ++y) {
+    p = GST_VIDEO_FRAME_PLANE_DATA (frame, 0);
+    /* move to Y0 or Y1 (we pretend the chroma is the last of the 3 bytes) */
+    /* FIXME: we're not pixel-exact here if x0 is an odd number, but it's
+     * unlikely anyone will notice.. */
+    p += (y * stride) + ((x0 / 2) * 3) + 1;
+    for (x = x0; x < x1; x += 2) {
+      tmp = *p + shading_val;
+      *p++ = CLAMP (tmp, 0, 255);
+      tmp = *p + shading_val;
+      *p++ = CLAMP (tmp, 0, 255);
+      /* skip chroma */
+      p++;
+    }
+  }
+}
+
 #define ARGB_SHADE_FUNCTION(name, OFFSET)      \
 static inline void \
 gst_base_text_overlay_shade_##name (GstBaseTextOverlay * overlay, GstVideoFrame * dest, \
@@ -1550,12 +1522,6 @@ gint x0, gint x1, gint y0, gint y1) \
   \
   dest_ptr = dest->data[0];\
   \
-  x0 = CLAMP (x0 - BOX_XPAD, 0, overlay->width);\
-  x1 = CLAMP (x1 + BOX_XPAD, 0, overlay->width);\
-  \
-  y0 = CLAMP (y0 - BOX_YPAD, 0, overlay->height);\
-  y1 = CLAMP (y1 + BOX_YPAD, 0, overlay->height);\
-  \
   for (i = y0; i < y1; i++) {\
     for (j = x0; j < x1; j++) {\
       gint y, y_pos, k;\
@@ -1572,313 +1538,6 @@ ARGB_SHADE_FUNCTION (ABGR, 1);
 ARGB_SHADE_FUNCTION (RGBA, 0);
 ARGB_SHADE_FUNCTION (BGRA, 0);
 
-
-/* FIXME:
- *  - use proper strides and offset for I420
- *  - don't draw over the edge of the picture (try a longer
- *    text with a huge font size)
- */
-
-static inline void
-gst_base_text_overlay_blit_NV12_NV21 (GstBaseTextOverlay * overlay,
-    GstVideoFrame * dest, gint xpos, gint ypos)
-{
-  int y_stride, u_stride, v_stride;
-  guint8 *y_pixels, *u_pixels, *v_pixels;
-
-  /* because U/V is 2x2 subsampled, we need to round, either up or down,
-   * to a boundary of integer number of U/V pixels:
-   */
-  xpos = GST_ROUND_UP_2 (xpos);
-  ypos = GST_ROUND_UP_2 (ypos);
-
-  y_pixels = dest->data[0];
-  u_pixels = dest->data[1];
-  v_pixels = dest->data[2];
-  y_stride = dest->info.stride[0];
-  u_stride = dest->info.stride[1];
-  v_stride = dest->info.stride[2];
-
-  gst_base_text_overlay_blit_1 (overlay, y_pixels, xpos, ypos,
-      overlay->text_image, y_stride);
-  gst_base_text_overlay_blit_sub2x2cbcr (overlay, u_pixels,
-      v_pixels, xpos, ypos, overlay->text_image, u_stride, v_stride, 2);
-}
-
-static inline void
-gst_base_text_overlay_blit_I420 (GstBaseTextOverlay * overlay,
-    GstVideoFrame * dest, gint xpos, gint ypos)
-{
-  int y_stride, u_stride, v_stride;
-  guint8 *y_pixels, *u_pixels, *v_pixels;
-
-  /* because U/V is 2x2 subsampled, we need to round, either up or down,
-   * to a boundary of integer number of U/V pixels:
-   */
-  xpos = GST_ROUND_UP_2 (xpos);
-  ypos = GST_ROUND_UP_2 (ypos);
-
-  y_pixels = dest->data[0];
-  u_pixels = dest->data[1];
-  v_pixels = dest->data[2];
-  y_stride = dest->info.stride[0];
-  u_stride = dest->info.stride[1];
-  v_stride = dest->info.stride[2];
-
-  gst_base_text_overlay_blit_1 (overlay, y_pixels, xpos, ypos,
-      overlay->text_image, y_stride);
-  gst_base_text_overlay_blit_sub2x2cbcr (overlay, u_pixels,
-      v_pixels, xpos, ypos, overlay->text_image, u_stride, v_stride, 1);
-}
-
-static inline void
-gst_base_text_overlay_blit_UYVY (GstBaseTextOverlay * overlay,
-    GstVideoFrame * dest, gint xpos, gint ypos)
-{
-  int a0, r0, g0, b0;
-  int a1, r1, g1, b1;
-  int y0, y1, u, v;
-  int i, j;
-  int h, w;
-  guchar *pimage, *dest_ptr;
-  guint8 *yuv_pixels;
-
-  yuv_pixels = dest->data[0];
-
-  /* because U/V is 2x horizontally subsampled, we need to round to a
-   * boundary of integer number of U/V pixels in x dimension:
-   */
-  xpos = GST_ROUND_UP_2 (xpos);
-
-  w = overlay->image_width - 2;
-  h = overlay->image_height - 2;
-
-  if (xpos < 0) {
-    xpos = 0;
-  }
-
-  if (xpos + w > overlay->width) {
-    w = overlay->width - xpos;
-  }
-
-  if (ypos + h > overlay->height) {
-    h = overlay->height - ypos;
-  }
-
-  for (i = 0; i < h; i++) {
-    pimage = overlay->text_image + i * overlay->image_width * 4;
-    dest_ptr = yuv_pixels + (i + ypos) * overlay->width * 2 + xpos * 2;
-    for (j = 0; j < w; j += 2) {
-      b0 = pimage[CAIRO_ARGB_B];
-      g0 = pimage[CAIRO_ARGB_G];
-      r0 = pimage[CAIRO_ARGB_R];
-      a0 = pimage[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a0, r0, g0, b0);
-      pimage += 4;
-
-      b1 = pimage[CAIRO_ARGB_B];
-      g1 = pimage[CAIRO_ARGB_G];
-      r1 = pimage[CAIRO_ARGB_R];
-      a1 = pimage[CAIRO_ARGB_A];
-      CAIRO_UNPREMULTIPLY (a1, r1, g1, b1);
-      pimage += 4;
-
-      a0 += a1 + 2;
-      a0 /= 2;
-      if (a0 == 0) {
-        dest_ptr += 4;
-        continue;
-      }
-
-      COMP_Y (y0, r0, g0, b0);
-      COMP_Y (y1, r1, g1, b1);
-
-      b0 += b1 + 2;
-      g0 += g1 + 2;
-      r0 += r1 + 2;
-
-      b0 /= 2;
-      g0 /= 2;
-      r0 /= 2;
-
-      COMP_U (u, r0, g0, b0);
-      COMP_V (v, r0, g0, b0);
-
-      BLEND (*dest_ptr, a0, u, *dest_ptr);
-      dest_ptr++;
-      BLEND (*dest_ptr, a0, y0, *dest_ptr);
-      dest_ptr++;
-      BLEND (*dest_ptr, a0, v, *dest_ptr);
-      dest_ptr++;
-      BLEND (*dest_ptr, a0, y1, *dest_ptr);
-      dest_ptr++;
-    }
-  }
-}
-
-static inline void
-gst_base_text_overlay_blit_AYUV (GstBaseTextOverlay * overlay,
-    GstVideoFrame * dest, gint xpos, gint ypos)
-{
-  int a, r, g, b, a1;
-  int y, u, v;
-  int i, j;
-  int h, w;
-  guchar *pimage, *dest_ptr;
-  guint8 *rgb_pixels;
-
-  rgb_pixels = dest->data[0];
-
-  w = overlay->image_width;
-  h = overlay->image_height;
-
-  if (xpos < 0) {
-    xpos = 0;
-  }
-
-  if (xpos + w > overlay->width) {
-    w = overlay->width - xpos;
-  }
-
-  if (ypos + h > overlay->height) {
-    h = overlay->height - ypos;
-  }
-
-  for (i = 0; i < h; i++) {
-    pimage = overlay->text_image + i * overlay->image_width * 4;
-    dest_ptr = rgb_pixels + (i + ypos) * 4 * overlay->width + xpos * 4;
-    for (j = 0; j < w; j++) {
-      a = pimage[CAIRO_ARGB_A];
-      b = pimage[CAIRO_ARGB_B];
-      g = pimage[CAIRO_ARGB_G];
-      r = pimage[CAIRO_ARGB_R];
-
-      CAIRO_UNPREMULTIPLY (a, r, g, b);
-
-      // convert background to yuv
-      COMP_Y (y, r, g, b);
-      COMP_U (u, r, g, b);
-      COMP_V (v, r, g, b);
-
-      // preform text "OVER" background alpha compositing
-      a1 = a + (dest_ptr[0] * (255 - a)) / 255 + 1;     // add 1 to prevent divide by 0
-      OVER (dest_ptr[1], a, y, dest_ptr[0], dest_ptr[1], a1);
-      OVER (dest_ptr[2], a, u, dest_ptr[0], dest_ptr[2], a1);
-      OVER (dest_ptr[3], a, v, dest_ptr[0], dest_ptr[3], a1);
-      dest_ptr[0] = a1 - 1;     // remove the temporary 1 we added
-
-      pimage += 4;
-      dest_ptr += 4;
-    }
-  }
-}
-
-#define xRGB_BLIT_FUNCTION(name, R, G, B) \
-static inline void \
-gst_base_text_overlay_blit_##name (GstBaseTextOverlay * overlay, \
-    GstVideoFrame * dest, gint xpos, gint ypos) \
-{ \
-  int a, r, g, b; \
-  int i, j; \
-  int h, w; \
-  guchar *pimage, *dest_ptr; \
-  guint8 *rgb_pixels;\
-  \
-  rgb_pixels = dest->data[0];\
-  \
-  w = overlay->image_width; \
-  h = overlay->image_height; \
-  \
-  if (xpos < 0) { \
-    xpos = 0; \
-  } \
-  \
-  if (xpos + w > overlay->width) { \
-    w = overlay->width - xpos; \
-  } \
-  \
-  if (ypos + h > overlay->height) { \
-    h = overlay->height - ypos; \
-  } \
-  \
-  for (i = 0; i < h; i++) { \
-    pimage = overlay->text_image + i * overlay->image_width * 4; \
-    dest_ptr = rgb_pixels + (i + ypos) * 4 * overlay->width + xpos * 4; \
-    for (j = 0; j < w; j++) { \
-      a = pimage[CAIRO_ARGB_A]; \
-      b = pimage[CAIRO_ARGB_B]; \
-      g = pimage[CAIRO_ARGB_G]; \
-      r = pimage[CAIRO_ARGB_R]; \
-      CAIRO_UNPREMULTIPLY (a, r, g, b); \
-      b = (b*a + dest_ptr[B] * (255-a)) / 255; \
-      g = (g*a + dest_ptr[G] * (255-a)) / 255; \
-      r = (r*a + dest_ptr[R] * (255-a)) / 255; \
-      \
-      dest_ptr[B] = b; \
-      dest_ptr[G] = g; \
-      dest_ptr[R] = r; \
-      pimage += 4; \
-      dest_ptr += 4; \
-    } \
-  } \
-}
-xRGB_BLIT_FUNCTION (xRGB, 1, 2, 3);
-xRGB_BLIT_FUNCTION (BGRx, 2, 1, 0);
-xRGB_BLIT_FUNCTION (xBGR, 3, 2, 1);
-xRGB_BLIT_FUNCTION (RGBx, 0, 1, 2);
-
-#define ARGB_BLIT_FUNCTION(name, A, R, G, B)   \
-static inline void \
-gst_base_text_overlay_blit_##name (GstBaseTextOverlay * overlay, \
-    GstVideoFrame * dest, gint xpos, gint ypos) \
-{ \
-  int a, r, g, b, a1;                          \
-  int i, j; \
-  int h, w; \
-  guchar *pimage, *dest_ptr; \
-  guint8 *rgb_pixels;\
-  \
-  rgb_pixels = dest->data[0];\
-  \
-  w = overlay->image_width; \
-  h = overlay->image_height; \
-  \
-  if (xpos < 0) { \
-    xpos = 0; \
-  } \
-  \
-  if (xpos + w > overlay->width) { \
-    w = overlay->width - xpos; \
-  } \
-  \
-  if (ypos + h > overlay->height) { \
-    h = overlay->height - ypos; \
-  } \
-  \
-  for (i = 0; i < h; i++) { \
-    pimage = overlay->text_image + i * overlay->image_width * 4; \
-    dest_ptr = rgb_pixels + (i + ypos) * 4 * overlay->width + xpos * 4; \
-    for (j = 0; j < w; j++) { \
-      a = pimage[CAIRO_ARGB_A]; \
-      b = pimage[CAIRO_ARGB_B]; \
-      g = pimage[CAIRO_ARGB_G]; \
-      r = pimage[CAIRO_ARGB_R]; \
-      CAIRO_UNPREMULTIPLY (a, r, g, b); \
-      a1 = a + (dest_ptr[A] * (255 - a)) / 255 + 1; \
-      OVER (dest_ptr[R], a, r, dest_ptr[0], dest_ptr[R], a1); \
-      OVER (dest_ptr[G], a, g, dest_ptr[0], dest_ptr[G], a1); \
-      OVER (dest_ptr[B], a, b, dest_ptr[0], dest_ptr[B], a1); \
-      dest_ptr[A] = a1 - 1; \
-      pimage += 4; \
-      dest_ptr += 4; \
-    } \
-  } \
-}
-ARGB_BLIT_FUNCTION (RGBA, 3, 0, 1, 2);
-ARGB_BLIT_FUNCTION (BGRA, 3, 2, 1, 0);
-ARGB_BLIT_FUNCTION (ARGB, 0, 1, 2, 3);
-ARGB_BLIT_FUNCTION (ABGR, 0, 3, 2, 1);
-
 static void
 gst_base_text_overlay_render_text (GstBaseTextOverlay * overlay,
     const gchar * text, gint textlen)
@@ -1913,203 +1572,138 @@ gst_base_text_overlay_render_text (GstBaseTextOverlay * overlay,
   overlay->need_render = FALSE;
 }
 
-static GstFlowReturn
-gst_base_text_overlay_push_frame (GstBaseTextOverlay * overlay,
-    GstBuffer * video_frame)
-{
-  gint xpos, ypos;
-  gint width, height;
-  GstBaseTextOverlayVAlign valign;
-  GstBaseTextOverlayHAlign halign;
-  GstVideoFrame frame;
-
-  width = overlay->image_width;
-  height = overlay->image_height;
-
-  video_frame = gst_buffer_make_writable (video_frame);
+/* FIXME: should probably be relative to width/height (adjusted for PAR) */
+#define BOX_XPAD  6
+#define BOX_YPAD  6
 
-  if (!gst_video_frame_map (&frame, &overlay->info, video_frame, GST_MAP_WRITE))
-    goto invalid_frame;
+static void
+gst_base_text_overlay_shade_background (GstBaseTextOverlay * overlay,
+    GstVideoFrame * frame, gint x0, gint x1, gint y0, gint y1)
+{
+  x0 = CLAMP (x0 - BOX_XPAD, 0, overlay->width);
+  x1 = CLAMP (x1 + BOX_XPAD, 0, overlay->width);
 
-  if (overlay->use_vertical_render)
-    halign = GST_BASE_TEXT_OVERLAY_HALIGN_RIGHT;
-  else
-    halign = overlay->halign;
+  y0 = CLAMP (y0 - BOX_YPAD, 0, overlay->height);
+  y1 = CLAMP (y1 + BOX_YPAD, 0, overlay->height);
 
-  switch (halign) {
-    case GST_BASE_TEXT_OVERLAY_HALIGN_LEFT:
-      xpos = overlay->xpad;
+  switch (overlay->format) {
+    case GST_VIDEO_FORMAT_I420:
+    case GST_VIDEO_FORMAT_YV12:
+    case GST_VIDEO_FORMAT_NV12:
+    case GST_VIDEO_FORMAT_NV21:
+    case GST_VIDEO_FORMAT_Y41B:
+    case GST_VIDEO_FORMAT_Y42B:
+    case GST_VIDEO_FORMAT_Y444:
+    case GST_VIDEO_FORMAT_YUV9:
+    case GST_VIDEO_FORMAT_YVU9:
+    case GST_VIDEO_FORMAT_GRAY8:
+    case GST_VIDEO_FORMAT_A420:
+      gst_base_text_overlay_shade_planar_Y (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_HALIGN_CENTER:
-      xpos = (overlay->width - width) / 2;
+    case GST_VIDEO_FORMAT_AYUV:
+    case GST_VIDEO_FORMAT_UYVY:
+    case GST_VIDEO_FORMAT_YUY2:
+    case GST_VIDEO_FORMAT_v308:
+      gst_base_text_overlay_shade_packed_Y (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_HALIGN_RIGHT:
-      xpos = overlay->width - width - overlay->xpad;
+    case GST_VIDEO_FORMAT_xRGB:
+      gst_base_text_overlay_shade_xRGB (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_HALIGN_POS:
-      xpos = (gint) (overlay->width * overlay->xpos) - width / 2;
-      xpos = CLAMP (xpos, 0, overlay->width - width);
-      if (xpos < 0)
-        xpos = 0;
+    case GST_VIDEO_FORMAT_xBGR:
+      gst_base_text_overlay_shade_xBGR (overlay, frame, x0, x1, y0, y1);
       break;
-    default:
-      xpos = 0;
-  }
-  xpos += overlay->deltax;
-
-  if (overlay->use_vertical_render)
-    valign = GST_BASE_TEXT_OVERLAY_VALIGN_TOP;
-  else
-    valign = overlay->valign;
-
-  switch (valign) {
-    case GST_BASE_TEXT_OVERLAY_VALIGN_BOTTOM:
-      ypos = overlay->height - height - overlay->ypad;
+    case GST_VIDEO_FORMAT_BGRx:
+      gst_base_text_overlay_shade_BGRx (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_VALIGN_BASELINE:
-      ypos = overlay->height - (height + overlay->ypad);
+    case GST_VIDEO_FORMAT_RGBx:
+      gst_base_text_overlay_shade_RGBx (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_VALIGN_TOP:
-      ypos = overlay->ypad;
+    case GST_VIDEO_FORMAT_ARGB:
+      gst_base_text_overlay_shade_ARGB (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_VALIGN_POS:
-      ypos = (gint) (overlay->height * overlay->ypos) - height / 2;
-      ypos = CLAMP (ypos, 0, overlay->height - height);
+    case GST_VIDEO_FORMAT_ABGR:
+      gst_base_text_overlay_shade_ABGR (overlay, frame, x0, x1, y0, y1);
       break;
-    case GST_BASE_TEXT_OVERLAY_VALIGN_CENTER:
-      ypos = (overlay->height - height) / 2;
+    case GST_VIDEO_FORMAT_RGBA:
+      gst_base_text_overlay_shade_RGBA (overlay, frame, x0, x1, y0, y1);
+      break;
+    case GST_VIDEO_FORMAT_BGRA:
+      gst_base_text_overlay_shade_BGRA (overlay, frame, x0, x1, y0, y1);
+      break;
+    case GST_VIDEO_FORMAT_BGR:
+    case GST_VIDEO_FORMAT_RGB:
+      gst_base_text_overlay_shade_rgb24 (overlay, frame, x0, x1, y0, y1);
+      break;
+    case GST_VIDEO_FORMAT_IYU1:
+      gst_base_text_overlay_shade_IYU1 (overlay, frame, x0, x1, y0, y1);
       break;
     default:
-      ypos = overlay->ypad;
+      GST_FIXME_OBJECT (overlay, "implement background shading for format %s",
+          gst_video_format_to_string (GST_VIDEO_FRAME_FORMAT (frame)));
       break;
   }
-  ypos += overlay->deltay;
+}
+
+static GstFlowReturn
+gst_base_text_overlay_push_frame (GstBaseTextOverlay * overlay,
+    GstBuffer * video_frame)
+{
+  GstVideoFrame frame;
+
+  if (overlay->composition == NULL)
+    goto done;
+
+  if (gst_pad_check_reconfigure (overlay->srcpad))
+    gst_base_text_overlay_negotiate (overlay);
+
+  video_frame = gst_buffer_make_writable (video_frame);
+
+  if (overlay->attach_compo_to_buffer) {
+    GST_DEBUG_OBJECT (overlay, "Attaching text overlay image to video buffer");
+    gst_buffer_add_video_overlay_composition_meta (video_frame,
+        overlay->composition);
+    /* FIXME: emulate shaded background box if want_shading=true */
+    goto done;
+  }
+
+  if (!gst_video_frame_map (&frame, &overlay->info, video_frame,
+          GST_MAP_READWRITE))
+    goto invalid_frame;
 
   /* shaded background box */
   if (overlay->want_shading) {
-    switch (overlay->format) {
-      case GST_VIDEO_FORMAT_I420:
-      case GST_VIDEO_FORMAT_NV12:
-      case GST_VIDEO_FORMAT_NV21:
-        gst_base_text_overlay_shade_planar_Y (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_AYUV:
-      case GST_VIDEO_FORMAT_UYVY:
-        gst_base_text_overlay_shade_packed_Y (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_xRGB:
-        gst_base_text_overlay_shade_xRGB (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_xBGR:
-        gst_base_text_overlay_shade_xBGR (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_BGRx:
-        gst_base_text_overlay_shade_BGRx (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_RGBx:
-        gst_base_text_overlay_shade_RGBx (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_ARGB:
-        gst_base_text_overlay_shade_ARGB (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_ABGR:
-        gst_base_text_overlay_shade_ABGR (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_RGBA:
-        gst_base_text_overlay_shade_RGBA (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      case GST_VIDEO_FORMAT_BGRA:
-        gst_base_text_overlay_shade_BGRA (overlay, &frame,
-            xpos, xpos + overlay->image_width,
-            ypos, ypos + overlay->image_height);
-        break;
-      default:
-        g_assert_not_reached ();
-    }
-  }
+    gint xpos, ypos;
 
-  if (ypos < 0)
-    ypos = 0;
+    gst_base_text_overlay_get_pos (overlay, &xpos, &ypos);
 
-  if (overlay->text_image) {
-    switch (overlay->format) {
-      case GST_VIDEO_FORMAT_I420:
-        gst_base_text_overlay_blit_I420 (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_NV12:
-      case GST_VIDEO_FORMAT_NV21:
-        gst_base_text_overlay_blit_NV12_NV21 (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_UYVY:
-        gst_base_text_overlay_blit_UYVY (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_AYUV:
-        gst_base_text_overlay_blit_AYUV (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_BGRx:
-        gst_base_text_overlay_blit_BGRx (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_xRGB:
-        gst_base_text_overlay_blit_xRGB (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_RGBx:
-        gst_base_text_overlay_blit_RGBx (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_xBGR:
-        gst_base_text_overlay_blit_xBGR (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_ARGB:
-        gst_base_text_overlay_blit_ARGB (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_ABGR:
-        gst_base_text_overlay_blit_ABGR (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_RGBA:
-        gst_base_text_overlay_blit_RGBA (overlay, &frame, xpos, ypos);
-        break;
-      case GST_VIDEO_FORMAT_BGRA:
-        gst_base_text_overlay_blit_BGRA (overlay, &frame, xpos, ypos);
-        break;
-      default:
-        g_assert_not_reached ();
-    }
+    gst_base_text_overlay_shade_background (overlay, &frame,
+        xpos, xpos + overlay->image_width, ypos, ypos + overlay->image_height);
   }
+
+  gst_video_overlay_composition_blend (overlay->composition, &frame);
+
   gst_video_frame_unmap (&frame);
 
+done:
+
   return gst_pad_push (overlay->srcpad, video_frame);
 
   /* ERRORS */
 invalid_frame:
   {
+    gst_buffer_unref (video_frame);
     GST_DEBUG_OBJECT (overlay, "received invalid buffer");
     return GST_FLOW_OK;
   }
 }
 
 static GstPadLinkReturn
-gst_base_text_overlay_text_pad_link (GstPad * pad, GstPad * peer)
+gst_base_text_overlay_text_pad_link (GstPad * pad, GstObject * parent,
+    GstPad * peer)
 {
   GstBaseTextOverlay *overlay;
 
-  overlay = GST_BASE_TEXT_OVERLAY (gst_pad_get_parent (pad));
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
   if (G_UNLIKELY (!overlay))
     return GST_PAD_LINK_REFUSED;
 
@@ -2117,18 +1711,16 @@ gst_base_text_overlay_text_pad_link (GstPad * pad, GstPad * peer)
 
   overlay->text_linked = TRUE;
 
-  gst_object_unref (overlay);
-
   return GST_PAD_LINK_OK;
 }
 
 static void
-gst_base_text_overlay_text_pad_unlink (GstPad * pad)
+gst_base_text_overlay_text_pad_unlink (GstPad * pad, GstObject * parent)
 {
   GstBaseTextOverlay *overlay;
 
   /* don't use gst_pad_get_parent() here, will deadlock */
-  overlay = GST_BASE_TEXT_OVERLAY (GST_PAD_PARENT (pad));
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
 
   GST_DEBUG_OBJECT (overlay, "Text pad unlinked");
 
@@ -2138,16 +1730,13 @@ gst_base_text_overlay_text_pad_unlink (GstPad * pad)
 }
 
 static gboolean
-gst_base_text_overlay_text_event (GstPad * pad, GstEvent * event)
+gst_base_text_overlay_text_event (GstPad * pad, GstObject * parent,
+    GstEvent * event)
 {
   gboolean ret = FALSE;
   GstBaseTextOverlay *overlay = NULL;
 
-  overlay = GST_BASE_TEXT_OVERLAY (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (!overlay)) {
-    gst_event_unref (event);
-    return FALSE;
-  }
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
 
   GST_LOG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
 
@@ -2170,11 +1759,11 @@ gst_base_text_overlay_text_event (GstPad * pad, GstEvent * event)
       gst_event_parse_segment (event, &segment);
 
       if (segment->format == GST_FORMAT_TIME) {
-        GST_OBJECT_LOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_LOCK (overlay);
         gst_segment_copy_into (segment, &overlay->text_segment);
         GST_DEBUG_OBJECT (overlay, "TEXT SEGMENT now: %" GST_SEGMENT_FORMAT,
             &overlay->text_segment);
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       } else {
         GST_ELEMENT_WARNING (overlay, STREAM, MUX, (NULL),
             ("received non-TIME newsegment event on text input"));
@@ -2185,63 +1774,76 @@ gst_base_text_overlay_text_event (GstPad * pad, GstEvent * event)
 
       /* wake up the video chain, it might be waiting for a text buffer or
        * a text segment update */
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_BROADCAST (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
+      break;
+    }
+    case GST_EVENT_GAP:
+    {
+      GstClockTime start, duration;
+
+      gst_event_parse_gap (event, &start, &duration);
+      if (GST_CLOCK_TIME_IS_VALID (duration))
+        start += duration;
+      /* we do not expect another buffer until after gap,
+       * so that is our position now */
+      overlay->text_segment.position = start;
+
+      /* wake up the video chain, it might be waiting for a text buffer or
+       * a text segment update */
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       GST_BASE_TEXT_OVERLAY_BROADCAST (overlay);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       break;
     }
     case GST_EVENT_FLUSH_STOP:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       GST_INFO_OBJECT (overlay, "text flush stop");
       overlay->text_flushing = FALSE;
       overlay->text_eos = FALSE;
       gst_base_text_overlay_pop_text (overlay);
       gst_segment_init (&overlay->text_segment, GST_FORMAT_TIME);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       gst_event_unref (event);
       ret = TRUE;
       break;
     case GST_EVENT_FLUSH_START:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       GST_INFO_OBJECT (overlay, "text flush start");
       overlay->text_flushing = TRUE;
       GST_BASE_TEXT_OVERLAY_BROADCAST (overlay);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       gst_event_unref (event);
       ret = TRUE;
       break;
     case GST_EVENT_EOS:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       overlay->text_eos = TRUE;
       GST_INFO_OBJECT (overlay, "text EOS");
       /* wake up the video chain, it might be waiting for a text buffer or
        * a text segment update */
       GST_BASE_TEXT_OVERLAY_BROADCAST (overlay);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       gst_event_unref (event);
       ret = TRUE;
       break;
     default:
-      ret = gst_pad_event_default (pad, event);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
   }
 
-  gst_object_unref (overlay);
-
   return ret;
 }
 
 static gboolean
-gst_base_text_overlay_video_event (GstPad * pad, GstEvent * event)
+gst_base_text_overlay_video_event (GstPad * pad, GstObject * parent,
+    GstEvent * event)
 {
   gboolean ret = FALSE;
   GstBaseTextOverlay *overlay = NULL;
 
-  overlay = GST_BASE_TEXT_OVERLAY (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (!overlay)) {
-    gst_event_unref (event);
-    return FALSE;
-  }
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
 
   GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
 
@@ -2273,39 +1875,66 @@ gst_base_text_overlay_video_event (GstPad * pad, GstEvent * event)
             ("received non-TIME newsegment event on video input"));
       }
 
-      ret = gst_pad_event_default (pad, event);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
     }
     case GST_EVENT_EOS:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       GST_INFO_OBJECT (overlay, "video EOS");
       overlay->video_eos = TRUE;
-      GST_OBJECT_UNLOCK (overlay);
-      ret = gst_pad_event_default (pad, event);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
     case GST_EVENT_FLUSH_START:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       GST_INFO_OBJECT (overlay, "video flush start");
       overlay->video_flushing = TRUE;
       GST_BASE_TEXT_OVERLAY_BROADCAST (overlay);
-      GST_OBJECT_UNLOCK (overlay);
-      ret = gst_pad_event_default (pad, event);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
     case GST_EVENT_FLUSH_STOP:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       GST_INFO_OBJECT (overlay, "video flush stop");
       overlay->video_flushing = FALSE;
       overlay->video_eos = FALSE;
       gst_segment_init (&overlay->segment, GST_FORMAT_TIME);
-      GST_OBJECT_UNLOCK (overlay);
-      ret = gst_pad_event_default (pad, event);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
     default:
-      ret = gst_pad_event_default (pad, event);
+      ret = gst_pad_event_default (pad, parent, event);
       break;
   }
 
-  gst_object_unref (overlay);
+  return ret;
+}
+
+static gboolean
+gst_base_text_overlay_video_query (GstPad * pad, GstObject * parent,
+    GstQuery * query)
+{
+  gboolean ret = FALSE;
+  GstBaseTextOverlay *overlay;
+
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
+
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_CAPS:
+    {
+      GstCaps *filter, *caps;
+
+      gst_query_parse_caps (query, &filter);
+      caps = gst_base_text_overlay_getcaps (pad, overlay, filter);
+      gst_query_set_caps_result (query, caps);
+      gst_caps_unref (caps);
+      ret = TRUE;
+      break;
+    }
+    default:
+      ret = gst_pad_query_default (pad, parent, query);
+      break;
+  }
 
   return ret;
 }
@@ -2331,26 +1960,27 @@ gst_base_text_overlay_pop_text (GstBaseTextOverlay * overlay)
    If the buffer is in our segment we keep it internally except if another one
    is already waiting here, in that case we wait that it gets kicked out */
 static GstFlowReturn
-gst_base_text_overlay_text_chain (GstPad * pad, GstBuffer * buffer)
+gst_base_text_overlay_text_chain (GstPad * pad, GstObject * parent,
+    GstBuffer * buffer)
 {
   GstFlowReturn ret = GST_FLOW_OK;
   GstBaseTextOverlay *overlay = NULL;
   gboolean in_seg = FALSE;
   guint64 clip_start = 0, clip_stop = 0;
 
-  overlay = GST_BASE_TEXT_OVERLAY (GST_PAD_PARENT (pad));
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
 
-  GST_OBJECT_LOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_LOCK (overlay);
 
   if (overlay->text_flushing) {
-    GST_OBJECT_UNLOCK (overlay);
-    ret = GST_FLOW_WRONG_STATE;
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
+    ret = GST_FLOW_FLUSHING;
     GST_LOG_OBJECT (overlay, "text flushing");
     goto beach;
   }
 
   if (overlay->text_eos) {
-    GST_OBJECT_UNLOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
     ret = GST_FLOW_EOS;
     GST_LOG_OBJECT (overlay, "text EOS");
     goto beach;
@@ -2389,8 +2019,8 @@ gst_base_text_overlay_text_chain (GstPad * pad, GstBuffer * buffer)
       GST_BASE_TEXT_OVERLAY_WAIT (overlay);
       GST_DEBUG ("Pad %s:%s resuming", GST_DEBUG_PAD_NAME (pad));
       if (overlay->text_flushing) {
-        GST_OBJECT_UNLOCK (overlay);
-        ret = GST_FLOW_WRONG_STATE;
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
+        ret = GST_FLOW_FLUSHING;
         goto beach;
       }
     }
@@ -2406,7 +2036,7 @@ gst_base_text_overlay_text_chain (GstPad * pad, GstBuffer * buffer)
     GST_BASE_TEXT_OVERLAY_BROADCAST (overlay);
   }
 
-  GST_OBJECT_UNLOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
 
 beach:
 
@@ -2414,7 +2044,8 @@ beach:
 }
 
 static GstFlowReturn
-gst_base_text_overlay_video_chain (GstPad * pad, GstBuffer * buffer)
+gst_base_text_overlay_video_chain (GstPad * pad, GstObject * parent,
+    GstBuffer * buffer)
 {
   GstBaseTextOverlayClass *klass;
   GstBaseTextOverlay *overlay;
@@ -2423,7 +2054,7 @@ gst_base_text_overlay_video_chain (GstPad * pad, GstBuffer * buffer)
   guint64 start, stop, clip_start = 0, clip_stop = 0;
   gchar *text = NULL;
 
-  overlay = GST_BASE_TEXT_OVERLAY (GST_PAD_PARENT (pad));
+  overlay = GST_BASE_TEXT_OVERLAY (parent);
   klass = GST_BASE_TEXT_OVERLAY_GET_CLASS (overlay);
 
   if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
@@ -2484,11 +2115,11 @@ gst_base_text_overlay_video_chain (GstPad * pad, GstBuffer * buffer)
     gst_caps_unref (caps);
   }
 
-  gst_object_sync_values (G_OBJECT (overlay), GST_BUFFER_TIMESTAMP (buffer));
+  gst_object_sync_values (GST_OBJECT (overlay), GST_BUFFER_TIMESTAMP (buffer));
 
 wait_for_text_buf:
 
-  GST_OBJECT_LOCK (overlay);
+  GST_BASE_TEXT_OVERLAY_LOCK (overlay);
 
   if (overlay->video_flushing)
     goto flushing;
@@ -2497,7 +2128,7 @@ wait_for_text_buf:
     goto have_eos;
 
   if (overlay->silent) {
-    GST_OBJECT_UNLOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
     ret = gst_pad_push (overlay->srcpad, buffer);
 
     /* Update position */
@@ -2517,7 +2148,7 @@ wait_for_text_buf:
     GST_LOG_OBJECT (overlay, "Text pad not linked, rendering default "
         "text: '%s'", GST_STR_NULL (text));
 
-    GST_OBJECT_UNLOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
 
     if (text != NULL && *text != '\0') {
       /* Render and push */
@@ -2580,60 +2211,65 @@ wait_for_text_buf:
         GST_LOG_OBJECT (overlay, "text buffer too old, popping");
         pop_text = FALSE;
         gst_base_text_overlay_pop_text (overlay);
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
         goto wait_for_text_buf;
       } else if (valid_text_time && vid_running_time_end <= text_running_time) {
         GST_LOG_OBJECT (overlay, "text in future, pushing video buf");
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
         /* Push the video frame */
         ret = gst_pad_push (overlay->srcpad, buffer);
       } else {
-        gchar *in_text, *otext;
-        gsize in_size, osize;
-
-        otext =
-            gst_buffer_map (overlay->text_buffer, &osize, NULL, GST_MAP_READ);
-        in_text = otext;
-        in_size = osize;
-
-        /* g_markup_escape_text() absolutely requires valid UTF8 input, it
-         * might crash otherwise. We don't fall back on GST_SUBTITLE_ENCODING
-         * here on purpose, this is something that needs fixing upstream */
-        if (!g_utf8_validate (in_text, in_size, NULL)) {
-          const gchar *end = NULL;
-
-          GST_WARNING_OBJECT (overlay, "received invalid UTF-8");
-          in_text = g_strndup (in_text, in_size);
-          while (!g_utf8_validate (in_text, in_size, &end) && end)
-            *((gchar *) end) = '*';
-        }
-
-        /* Get the string */
-        if (overlay->have_pango_markup) {
-          text = g_strndup (in_text, in_size);
-        } else {
-          text = g_markup_escape_text (in_text, in_size);
-        }
+        GstMapInfo map;
+        gchar *in_text;
+        gsize in_size;
+
+        gst_buffer_map (overlay->text_buffer, &map, GST_MAP_READ);
+        in_text = (gchar *) map.data;
+        in_size = map.size;
+
+        if (in_size > 0) {
+          /* g_markup_escape_text() absolutely requires valid UTF8 input, it
+           * might crash otherwise. We don't fall back on GST_SUBTITLE_ENCODING
+           * here on purpose, this is something that needs fixing upstream */
+          if (!g_utf8_validate (in_text, in_size, NULL)) {
+            const gchar *end = NULL;
+
+            GST_WARNING_OBJECT (overlay, "received invalid UTF-8");
+            in_text = g_strndup (in_text, in_size);
+            while (!g_utf8_validate (in_text, in_size, &end) && end)
+              *((gchar *) end) = '*';
+          }
 
-        if (text != NULL && *text != '\0') {
-          gint text_len = strlen (text);
+          /* Get the string */
+          if (overlay->have_pango_markup) {
+            text = g_strndup (in_text, in_size);
+          } else {
+            text = g_markup_escape_text (in_text, in_size);
+          }
 
-          while (text_len > 0 && (text[text_len - 1] == '\n' ||
-                  text[text_len - 1] == '\r')) {
-            --text_len;
+          if (text != NULL && *text != '\0') {
+            gint text_len = strlen (text);
+
+            while (text_len > 0 && (text[text_len - 1] == '\n' ||
+                    text[text_len - 1] == '\r')) {
+              --text_len;
+            }
+            GST_DEBUG_OBJECT (overlay, "Rendering text '%*s'", text_len, text);
+            gst_base_text_overlay_render_text (overlay, text, text_len);
+          } else {
+            GST_DEBUG_OBJECT (overlay, "No text to render (empty buffer)");
+            gst_base_text_overlay_render_text (overlay, " ", 1);
           }
-          GST_DEBUG_OBJECT (overlay, "Rendering text '%*s'", text_len, text);
-          gst_base_text_overlay_render_text (overlay, text, text_len);
+          if (in_text != (gchar *) map.data)
+            g_free (in_text);
         } else {
           GST_DEBUG_OBJECT (overlay, "No text to render (empty buffer)");
           gst_base_text_overlay_render_text (overlay, " ", 1);
         }
-        gst_buffer_unmap (overlay->text_buffer, otext, osize);
 
-        if (in_text != otext)
-          g_free (in_text);
+        gst_buffer_unmap (overlay->text_buffer, &map);
 
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
         ret = gst_base_text_overlay_push_frame (overlay, buffer);
 
         if (valid_text_time && text_running_time_end <= vid_running_time_end) {
@@ -2642,9 +2278,9 @@ wait_for_text_buf:
         }
       }
       if (pop_text) {
-        GST_OBJECT_LOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_LOCK (overlay);
         gst_base_text_overlay_pop_text (overlay);
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       }
     } else {
       gboolean wait_for_text_buf = TRUE;
@@ -2682,10 +2318,10 @@ wait_for_text_buf:
         GST_DEBUG_OBJECT (overlay, "no text buffer, need to wait for one");
         GST_BASE_TEXT_OVERLAY_WAIT (overlay);
         GST_DEBUG_OBJECT (overlay, "resuming");
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
         goto wait_for_text_buf;
       } else {
-        GST_OBJECT_UNLOCK (overlay);
+        GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
         GST_LOG_OBJECT (overlay, "no need to wait for a text buffer");
         ret = gst_pad_push (overlay->srcpad, buffer);
       }
@@ -2708,14 +2344,14 @@ missing_timestamp:
 
 flushing:
   {
-    GST_OBJECT_UNLOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
     GST_DEBUG_OBJECT (overlay, "flushing, discarding buffer");
     gst_buffer_unref (buffer);
-    return GST_FLOW_WRONG_STATE;
+    return GST_FLOW_FLUSHING;
   }
 have_eos:
   {
-    GST_OBJECT_UNLOCK (overlay);
+    GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
     GST_DEBUG_OBJECT (overlay, "eos, discarding buffer");
     gst_buffer_unref (buffer);
     return GST_FLOW_EOS;
@@ -2737,13 +2373,13 @@ gst_base_text_overlay_change_state (GstElement * element,
 
   switch (transition) {
     case GST_STATE_CHANGE_PAUSED_TO_READY:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       overlay->text_flushing = TRUE;
       overlay->video_flushing = TRUE;
       /* pop_text will broadcast on the GCond and thus also make the video
        * chain exit if it's waiting for a text buffer */
       gst_base_text_overlay_pop_text (overlay);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       break;
     default:
       break;
@@ -2755,14 +2391,14 @@ gst_base_text_overlay_change_state (GstElement * element,
 
   switch (transition) {
     case GST_STATE_CHANGE_READY_TO_PAUSED:
-      GST_OBJECT_LOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_LOCK (overlay);
       overlay->text_flushing = FALSE;
       overlay->video_flushing = FALSE;
       overlay->video_eos = FALSE;
       overlay->text_eos = FALSE;
       gst_segment_init (&overlay->segment, GST_FORMAT_TIME);
       gst_segment_init (&overlay->text_segment, GST_FORMAT_TIME);
-      GST_OBJECT_UNLOCK (overlay);
+      GST_BASE_TEXT_OVERLAY_UNLOCK (overlay);
       break;
     default:
       break;
@@ -2774,8 +2410,6 @@ gst_base_text_overlay_change_state (GstElement * element,
 static gboolean
 plugin_init (GstPlugin * plugin)
 {
-  gst_controller_init (NULL, NULL);
-
   if (!gst_element_register (plugin, "textoverlay", GST_RANK_NONE,
           GST_TYPE_TEXT_OVERLAY) ||
       !gst_element_register (plugin, "timeoverlay", GST_RANK_NONE,
@@ -2795,5 +2429,5 @@ plugin_init (GstPlugin * plugin)
 }
 
 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
-    "pango", "Pango-based text rendering and overlay", plugin_init,
+    pango, "Pango-based text rendering and overlay", plugin_init,
     VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)