Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / subparse / gstsubparse.c
index d17be39..c910cb2 100644 (file)
@@ -1,6 +1,7 @@
 /* GStreamer
  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
- * Copyright (c) 2004 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
+ * Copyright (C) 2004 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
+ * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
 #include "config.h"
 #endif
 
-#include <string.h>
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
-#include <regex.h>
+#include <glib.h>
 
 #include "gstsubparse.h"
 #include "gstssaparse.h"
 #include "samiparse.h"
+#include "tmplayerparse.h"
+#include "mpl2parse.h"
+#include "qttextparse.h"
 
-GST_DEBUG_CATEGORY_STATIC (sub_parse_debug);
-#define GST_CAT_DEFAULT sub_parse_debug
+GST_DEBUG_CATEGORY (sub_parse_debug);
 
 #define DEFAULT_ENCODING   NULL
 
 enum
 {
   PROP_0,
-  PROP_ENCODING
+  PROP_ENCODING,
+  PROP_VIDEOFPS
 };
 
 static void
@@ -50,24 +55,21 @@ gst_sub_parse_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
 
 
-static const GstElementDetails sub_parse_details =
-GST_ELEMENT_DETAILS ("Subtitle parser",
-    "Codec/Parser/Subtitle",
-    "Parses subtitle (.sub) files into text streams",
-    "Gustavo J. A. M. Carneiro <gjc@inescporto.pt>\n"
-    "Ronald S. Bultje <rbultje@ronald.bitfreak.net>");
-
-#ifndef GST_DISABLE_LOADSAVE_REGISTRY
+#ifndef GST_DISABLE_XML
 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-sami")
+    GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-sami; "
+        "application/x-subtitle-tmplayer; application/x-subtitle-mpl2; "
+        "application/x-subtitle-dks; application/x-subtitle-qttext")
     );
 #else
 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("application/x-subtitle")
+    GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-dks; "
+        "application/x-subtitle-tmplayer; application/x-subtitle-mpl2; "
+        "application/x-subtitle-qttext")
     );
 #endif
 
@@ -77,11 +79,9 @@ static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
     GST_STATIC_CAPS ("text/plain; text/x-pango-markup")
     );
 
-static void gst_sub_parse_base_init (GstSubParseClass * klass);
-static void gst_sub_parse_class_init (GstSubParseClass * klass);
-static void gst_sub_parse_init (GstSubParse * subparse);
 
 static gboolean gst_sub_parse_src_event (GstPad * pad, GstEvent * event);
+static gboolean gst_sub_parse_src_query (GstPad * pad, GstQuery * query);
 static gboolean gst_sub_parse_sink_event (GstPad * pad, GstEvent * event);
 
 static GstStateChangeReturn gst_sub_parse_change_state (GstElement * element,
@@ -89,44 +89,8 @@ static GstStateChangeReturn gst_sub_parse_change_state (GstElement * element,
 
 static GstFlowReturn gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf);
 
-static GstElementClass *parent_class = NULL;
-
-GType
-gst_sub_parse_get_type (void)
-{
-  static GType sub_parse_type = 0;
-
-  if (!sub_parse_type) {
-    static const GTypeInfo sub_parse_info = {
-      sizeof (GstSubParseClass),
-      (GBaseInitFunc) gst_sub_parse_base_init,
-      NULL,
-      (GClassInitFunc) gst_sub_parse_class_init,
-      NULL,
-      NULL,
-      sizeof (GstSubParse),
-      0,
-      (GInstanceInitFunc) gst_sub_parse_init,
-    };
-
-    sub_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
-        "GstSubParse", &sub_parse_info, 0);
-  }
-
-  return sub_parse_type;
-}
-
-static void
-gst_sub_parse_base_init (GstSubParseClass * klass)
-{
-  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
-
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&sink_templ));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&src_templ));
-  gst_element_class_set_details (element_class, &sub_parse_details);
-}
+#define gst_sub_parse_parent_class parent_class
+G_DEFINE_TYPE (GstSubParse, gst_sub_parse, GST_TYPE_ELEMENT);
 
 static void
 gst_sub_parse_dispose (GObject * object)
@@ -135,19 +99,38 @@ gst_sub_parse_dispose (GObject * object)
 
   GST_DEBUG_OBJECT (subparse, "cleaning up subtitle parser");
 
-  if (subparse->segment) {
-    gst_segment_free (subparse->segment);
-    subparse->segment = NULL;
+  switch (subparse->parser_type) {
+    case GST_SUB_PARSE_FORMAT_QTTEXT:
+      qttext_context_deinit (&subparse->state);
+      break;
+#ifndef GST_DISABLE_XML
+    case GST_SUB_PARSE_FORMAT_SAMI:
+      sami_context_deinit (&subparse->state);
+      break;
+#endif
+    default:
+      break;
   }
+
   if (subparse->encoding) {
     g_free (subparse->encoding);
     subparse->encoding = NULL;
   }
+
+  if (subparse->detected_encoding) {
+    g_free (subparse->detected_encoding);
+    subparse->detected_encoding = NULL;
+  }
+
+  if (subparse->adapter) {
+    g_object_unref (subparse->adapter);
+    subparse->adapter = NULL;
+  }
+
   if (subparse->textbuf) {
     g_string_free (subparse->textbuf, TRUE);
     subparse->textbuf = NULL;
   }
-  sami_context_deinit (&subparse->state);
 
   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
 }
@@ -158,20 +141,37 @@ gst_sub_parse_class_init (GstSubParseClass * klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
 
-  parent_class = g_type_class_peek_parent (klass);
-
   object_class->dispose = gst_sub_parse_dispose;
   object_class->set_property = gst_sub_parse_set_property;
   object_class->get_property = gst_sub_parse_get_property;
 
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&sink_templ));
+  gst_element_class_add_pad_template (element_class,
+      gst_static_pad_template_get (&src_templ));
+  gst_element_class_set_details_simple (element_class,
+      "Subtitle parser", "Codec/Parser/Subtitle",
+      "Parses subtitle (.sub) files into text streams",
+      "Gustavo J. A. M. Carneiro <gjc@inescporto.pt>, "
+      "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
+
   element_class->change_state = gst_sub_parse_change_state;
 
   g_object_class_install_property (object_class, PROP_ENCODING,
       g_param_spec_string ("subtitle-encoding", "subtitle charset encoding",
-          "Encoding to assume if input subtitles are not in UTF-8 encoding. "
-          "If not set, the GST_SUBTITLE_ENCODING environment variable will "
-          "be checked for an encoding to use. If that is not set either, "
-          "ISO-8859-15 will be assumed.", DEFAULT_ENCODING, G_PARAM_READWRITE));
+          "Encoding to assume if input subtitles are not in UTF-8 or any other "
+          "Unicode encoding. If not set, the GST_SUBTITLE_ENCODING environment "
+          "variable will be checked for an encoding to use. If that is not set "
+          "either, ISO-8859-15 will be assumed.", DEFAULT_ENCODING,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (object_class, PROP_VIDEOFPS,
+      gst_param_spec_fraction ("video-fps", "Video framerate",
+          "Framerate of the video stream. This is needed by some subtitle "
+          "formats to synchronize subtitles and video properly. If not set "
+          "and the subtitle format requires it subtitles may be out of sync.",
+          0, 1, G_MAXINT, 1, 24000, 1001,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -187,20 +187,21 @@ gst_sub_parse_init (GstSubParse * subparse)
   subparse->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
   gst_pad_set_event_function (subparse->srcpad,
       GST_DEBUG_FUNCPTR (gst_sub_parse_src_event));
+  gst_pad_set_query_function (subparse->srcpad,
+      GST_DEBUG_FUNCPTR (gst_sub_parse_src_query));
   gst_element_add_pad (GST_ELEMENT (subparse), subparse->srcpad);
 
   subparse->textbuf = g_string_new (NULL);
   subparse->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
   subparse->flushing = FALSE;
-  subparse->segment = gst_segment_new ();
-  if (subparse->segment) {
-    gst_segment_init (subparse->segment, GST_FORMAT_TIME);
-    subparse->need_segment = TRUE;
-  } else {
-    GST_WARNING_OBJECT (subparse, "segment creation failed");
-    g_assert_not_reached ();
-  }
+  gst_segment_init (&subparse->segment, GST_FORMAT_TIME);
+  subparse->need_segment = TRUE;
   subparse->encoding = g_strdup (DEFAULT_ENCODING);
+  subparse->detected_encoding = NULL;
+  subparse->adapter = gst_adapter_new ();
+
+  subparse->fps_n = 24000;
+  subparse->fps_d = 1001;
 }
 
 /*
@@ -208,6 +209,57 @@ gst_sub_parse_init (GstSubParse * subparse)
  */
 
 static gboolean
+gst_sub_parse_src_query (GstPad * pad, GstQuery * query)
+{
+  GstSubParse *self = GST_SUBPARSE (gst_pad_get_parent (pad));
+  gboolean ret = FALSE;
+
+  GST_DEBUG ("Handling %s query", GST_QUERY_TYPE_NAME (query));
+
+  switch (GST_QUERY_TYPE (query)) {
+    case GST_QUERY_POSITION:{
+      GstFormat fmt;
+
+      gst_query_parse_position (query, &fmt, NULL);
+      if (fmt != GST_FORMAT_TIME) {
+        ret = gst_pad_peer_query (self->sinkpad, query);
+      } else {
+        ret = TRUE;
+        gst_query_set_position (query, GST_FORMAT_TIME, self->segment.position);
+      }
+      break;
+    }
+    case GST_QUERY_SEEKING:
+    {
+      GstFormat fmt;
+      gboolean seekable = FALSE;
+
+      ret = TRUE;
+
+      gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
+      if (fmt == GST_FORMAT_TIME) {
+        GstQuery *peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
+
+        seekable = gst_pad_peer_query (self->sinkpad, peerquery);
+        if (seekable)
+          gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
+        gst_query_unref (peerquery);
+      }
+
+      gst_query_set_seeking (query, fmt, seekable, seekable ? 0 : -1, -1);
+      break;
+    }
+    default:
+      ret = gst_pad_peer_query (self->sinkpad, query);
+      break;
+  }
+
+  gst_object_unref (self);
+
+  return ret;
+}
+
+static gboolean
 gst_sub_parse_src_event (GstPad * pad, GstEvent * event)
 {
   GstSubParse *self = GST_SUBPARSE (gst_pad_get_parent (pad));
@@ -219,12 +271,13 @@ gst_sub_parse_src_event (GstPad * pad, GstEvent * event)
     case GST_EVENT_SEEK:
     {
       GstFormat format;
+      GstSeekFlags flags;
       GstSeekType start_type, stop_type;
       gint64 start, stop;
       gdouble rate;
       gboolean update;
 
-      gst_event_parse_seek (event, &rate, &format, &self->segment_flags,
+      gst_event_parse_seek (event, &rate, &format, &flags,
           &start_type, &start, &stop_type, &stop);
 
       if (format != GST_FORMAT_TIME) {
@@ -236,19 +289,16 @@ gst_sub_parse_src_event (GstPad * pad, GstEvent * event)
       /* Convert that seek to a seeking in bytes at position 0,
          FIXME: could use an index */
       ret = gst_pad_push_event (self->sinkpad,
-          gst_event_new_seek (rate, GST_FORMAT_BYTES, self->segment_flags,
+          gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
               GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, 0));
 
       if (ret) {
         /* Apply the seek to our segment */
-        gst_segment_set_seek (self->segment, rate, format, self->segment_flags,
+        gst_segment_do_seek (&self->segment, rate, format, flags,
             start_type, start, stop_type, stop, &update);
 
-        GST_DEBUG_OBJECT (self, "segment configured from %" GST_TIME_FORMAT
-            " to %" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT,
-            GST_TIME_ARGS (self->segment->start),
-            GST_TIME_ARGS (self->segment->stop),
-            GST_TIME_ARGS (self->segment->last_stop));
+        GST_DEBUG_OBJECT (self, "segment after seek: %" GST_SEGMENT_FORMAT,
+            &self->segment);
 
         self->next_offset = 0;
 
@@ -285,6 +335,19 @@ gst_sub_parse_set_property (GObject * object, guint prop_id,
       GST_LOG_OBJECT (object, "subtitle encoding set to %s",
           GST_STR_NULL (subparse->encoding));
       break;
+    case PROP_VIDEOFPS:
+    {
+      subparse->fps_n = gst_value_get_fraction_numerator (value);
+      subparse->fps_d = gst_value_get_fraction_denominator (value);
+      GST_DEBUG_OBJECT (object, "video framerate set to %d/%d", subparse->fps_n,
+          subparse->fps_d);
+
+      if (!subparse->state.have_internal_fps) {
+        subparse->state.fps_n = subparse->fps_n;
+        subparse->state.fps_d = subparse->fps_d;
+      }
+      break;
+    }
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -303,6 +366,9 @@ gst_sub_parse_get_property (GObject * object, guint prop_id,
     case PROP_ENCODING:
       g_value_set_string (value, subparse->encoding);
       break;
+    case PROP_VIDEOFPS:
+      gst_value_set_fraction (value, subparse->fps_n, subparse->fps_d);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -310,22 +376,119 @@ gst_sub_parse_get_property (GObject * object, guint prop_id,
   GST_OBJECT_UNLOCK (subparse);
 }
 
+static const gchar *
+gst_sub_parse_get_format_description (GstSubParseFormat format)
+{
+  switch (format) {
+    case GST_SUB_PARSE_FORMAT_MDVDSUB:
+      return "MicroDVD";
+    case GST_SUB_PARSE_FORMAT_SUBRIP:
+      return "SubRip";
+    case GST_SUB_PARSE_FORMAT_MPSUB:
+      return "MPSub";
+    case GST_SUB_PARSE_FORMAT_SAMI:
+      return "SAMI";
+    case GST_SUB_PARSE_FORMAT_TMPLAYER:
+      return "TMPlayer";
+    case GST_SUB_PARSE_FORMAT_MPL2:
+      return "MPL2";
+    case GST_SUB_PARSE_FORMAT_SUBVIEWER:
+      return "SubViewer";
+    case GST_SUB_PARSE_FORMAT_DKS:
+      return "DKS";
+    case GST_SUB_PARSE_FORMAT_QTTEXT:
+      return "QTtext";
+    default:
+    case GST_SUB_PARSE_FORMAT_UNKNOWN:
+      break;
+  }
+  return NULL;
+}
+
+static gchar *
+gst_convert_to_utf8 (const gchar * str, gsize len, const gchar * encoding,
+    gsize * consumed, GError ** err)
+{
+  gchar *ret = NULL;
+
+  *consumed = 0;
+  /* The char cast is necessary in glib < 2.24 */
+  ret =
+      g_convert_with_fallback (str, len, "UTF-8", encoding, (char *) "*",
+      consumed, NULL, err);
+  if (ret == NULL)
+    return ret;
+
+  /* + 3 to skip UTF-8 BOM if it was added */
+  len = strlen (ret);
+  if (len >= 3 && (guint8) ret[0] == 0xEF && (guint8) ret[1] == 0xBB
+      && (guint8) ret[2] == 0xBF)
+    g_memmove (ret, ret + 3, len + 1 - 3);
+
+  return ret;
+}
+
+static gchar *
+detect_encoding (const gchar * str, gsize len)
+{
+  if (len >= 3 && (guint8) str[0] == 0xEF && (guint8) str[1] == 0xBB
+      && (guint8) str[2] == 0xBF)
+    return g_strdup ("UTF-8");
+
+  if (len >= 2 && (guint8) str[0] == 0xFE && (guint8) str[1] == 0xFF)
+    return g_strdup ("UTF-16BE");
+
+  if (len >= 2 && (guint8) str[0] == 0xFF && (guint8) str[1] == 0xFE)
+    return g_strdup ("UTF-16LE");
+
+  if (len >= 4 && (guint8) str[0] == 0x00 && (guint8) str[1] == 0x00
+      && (guint8) str[2] == 0xFE && (guint8) str[3] == 0xFF)
+    return g_strdup ("UTF-32BE");
+
+  if (len >= 4 && (guint8) str[0] == 0xFF && (guint8) str[1] == 0xFE
+      && (guint8) str[2] == 0x00 && (guint8) str[3] == 0x00)
+    return g_strdup ("UTF-32LE");
+
+  return NULL;
+}
+
 static gchar *
-convert_encoding (GstSubParse * self, const gchar * str, gsize len)
+convert_encoding (GstSubParse * self, const gchar * str, gsize len,
+    gsize * consumed)
 {
   const gchar *encoding;
   GError *err = NULL;
-  gchar *ret;
+  gchar *ret = NULL;
+
+  *consumed = 0;
+
+  /* First try any detected encoding */
+  if (self->detected_encoding) {
+    ret =
+        gst_convert_to_utf8 (str, len, self->detected_encoding, consumed, &err);
+
+    if (!err)
+      return ret;
 
+    GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
+        self->detected_encoding, err->message);
+    g_free (self->detected_encoding);
+    self->detected_encoding = NULL;
+    g_error_free (err);
+  }
+
+  /* Otherwise check if it's UTF8 */
   if (self->valid_utf8) {
     if (g_utf8_validate (str, len, NULL)) {
       GST_LOG_OBJECT (self, "valid UTF-8, no conversion needed");
+      *consumed = len;
       return g_strndup (str, len);
     }
     GST_INFO_OBJECT (self, "invalid UTF-8!");
     self->valid_utf8 = FALSE;
   }
 
+  /* Else try fallback */
   encoding = self->encoding;
   if (encoding == NULL || *encoding == '\0') {
     encoding = g_getenv ("GST_SUBTITLE_ENCODING");
@@ -338,8 +501,7 @@ convert_encoding (GstSubParse * self, const gchar * str, gsize len)
     }
   }
 
-  ret = g_convert_with_fallback (str, len, "UTF-8", encoding, "*", NULL,
-      NULL, &err);
+  ret = gst_convert_to_utf8 (str, len, encoding, consumed, &err);
 
   if (err) {
     GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
@@ -347,11 +509,11 @@ convert_encoding (GstSubParse * self, const gchar * str, gsize len)
     g_error_free (err);
 
     /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
-    ret = g_convert_with_fallback (str, len, "UTF-8", "ISO-8859-15", "*",
-        NULL, NULL, NULL);
+    ret = gst_convert_to_utf8 (str, len, "ISO-8859-15", consumed, NULL);
   }
 
-  GST_LOG_OBJECT (self, "successfully converted %d characters from %s to UTF-8"
+  GST_LOG_OBJECT (self,
+      "successfully converted %" G_GSIZE_FORMAT " characters from %s to UTF-8"
       "%s", len, encoding, (err) ? " , using ISO-8859-15 as fallback" : "");
 
   return ret;
@@ -379,7 +541,7 @@ get_next_line (GstSubParse * self)
   }
 
   line_len = line_end - self->textbuf->str;
-  line = convert_encoding (self, self->textbuf->str, line_len);
+  line = g_strndup (self->textbuf->str, line_len);
   self->textbuf = g_string_erase (self->textbuf, 0,
       line_len + (have_r ? 2 : 1));
   return line;
@@ -391,12 +553,8 @@ parse_mdvdsub (ParserState * state, const gchar * line)
   const gchar *line_split;
   gchar *line_chunk;
   guint start_frame, end_frame;
-  gint64 clip_start = 0, clip_stop = 0;
+  guint64 clip_start = 0, clip_stop = 0;
   gboolean in_seg = FALSE;
-
-  /* FIXME: hardcoded for now, but detecting the correct value is
-   * not going to be easy, I suspect... */
-  const double frames_per_sec = 24000 / 1001.;
   GString *markup;
   gchar *ret;
 
@@ -404,6 +562,7 @@ parse_mdvdsub (ParserState * state, const gchar * line)
   gboolean italic;
   gboolean bold;
   guint fontsize;
+  gdouble fps = 0.0;
 
   if (sscanf (line, "{%u}{%u}", &start_frame, &end_frame) != 2) {
     g_warning ("Parse of the following line, assumed to be in microdvd .sub"
@@ -411,8 +570,32 @@ parse_mdvdsub (ParserState * state, const gchar * line)
     return NULL;
   }
 
-  state->start_time = (start_frame - 1000) / frames_per_sec * GST_SECOND;
-  state->duration = (end_frame - start_frame) / frames_per_sec * GST_SECOND;
+  /* skip the {%u}{%u} part */
+  line = strchr (line, '}') + 1;
+  line = strchr (line, '}') + 1;
+
+  /* see if there's a first line with a framerate */
+  if (start_frame == 1 && end_frame == 1) {
+    gchar *rest, *end = NULL;
+
+    rest = g_strdup (line);
+    g_strdelimit (rest, ",", '.');
+    fps = g_ascii_strtod (rest, &end);
+    if (end != rest) {
+      gst_util_double_to_fraction (fps, &state->fps_n, &state->fps_d);
+      GST_INFO ("framerate from file: %d/%d ('%s')", state->fps_n,
+          state->fps_d, rest);
+    }
+    g_free (rest);
+    return NULL;
+  }
+
+  state->start_time =
+      gst_util_uint64_scale (start_frame, GST_SECOND * state->fps_d,
+      state->fps_n);
+  state->duration =
+      gst_util_uint64_scale (end_frame - start_frame, GST_SECOND * state->fps_d,
+      state->fps_n);
 
   /* Check our segment start/stop */
   in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
@@ -427,10 +610,6 @@ parse_mdvdsub (ParserState * state, const gchar * line)
     return NULL;
   }
 
-  /* skip the {%u}{%u} part */
-  line = strchr (line, '}') + 1;
-  line = strchr (line, '}') + 1;
-
   markup = g_string_new (NULL);
   while (1) {
     italic = FALSE;
@@ -448,10 +627,22 @@ parse_mdvdsub (ParserState * state, const gchar * line)
     if (sscanf (line, "{s:%u}", &fontsize) == 1) {
       line = strchr (line, '}') + 1;
     }
+    /* forward slashes at beginning/end signify italics too */
+    if (g_str_has_prefix (line, "/")) {
+      italic = TRUE;
+      ++line;
+    }
     if ((line_split = strchr (line, '|')))
       line_chunk = g_markup_escape_text (line, line_split - line);
     else
       line_chunk = g_markup_escape_text (line, strlen (line));
+
+    /* Remove italics markers at end of line/stanza (CHECKME: are end slashes
+     * always at the end of a line or can they span multiple lines?) */
+    if (g_str_has_suffix (line_chunk, "/")) {
+      line_chunk[strlen (line_chunk) - 1] = '\0';
+    }
+
     markup = g_string_append (markup, "<span");
     if (italic)
       g_string_append (markup, " style=\"italic\"");
@@ -528,11 +719,162 @@ subrip_unescape_formatting (gchar * txt)
   }
 }
 
+
+static gboolean
+subrip_remove_unhandled_tag (gchar * start, gchar * stop)
+{
+  gchar *tag, saved;
+
+  tag = start + strlen ("&lt;");
+  if (*tag == '/')
+    ++tag;
+
+  if (g_ascii_tolower (*tag) < 'a' || g_ascii_tolower (*tag) > 'z')
+    return FALSE;
+
+  saved = *stop;
+  *stop = '\0';
+  GST_LOG ("removing unhandled tag '%s'", start);
+  *stop = saved;
+  g_memmove (start, stop, strlen (stop) + 1);
+  return TRUE;
+}
+
+/* remove tags we haven't explicitly allowed earlier on, like font tags
+ * for example */
+static void
+subrip_remove_unhandled_tags (gchar * txt)
+{
+  gchar *pos, *gt;
+
+  for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
+    if (strncmp (pos, "&lt;", 4) == 0 && (gt = strstr (pos + 4, "&gt;"))) {
+      if (subrip_remove_unhandled_tag (pos, gt + strlen ("&gt;")))
+        --pos;
+    }
+  }
+}
+
+/* we only allow <i>, <u> and <b>, so let's take a simple approach. This code
+ * assumes the input has been escaped and subrip_unescape_formatting() has then
+ * been run over the input! This function adds missing closing markup tags and
+ * removes broken closing tags for tags that have never been opened. */
+static void
+subrip_fix_up_markup (gchar ** p_txt)
+{
+  gchar *cur, *next_tag;
+  gchar open_tags[32];
+  guint num_open_tags = 0;
+
+  g_assert (*p_txt != NULL);
+
+  cur = *p_txt;
+  while (*cur != '\0') {
+    next_tag = strchr (cur, '<');
+    if (next_tag == NULL)
+      break;
+    ++next_tag;
+    switch (*next_tag) {
+      case '/':{
+        ++next_tag;
+        if (num_open_tags == 0 || open_tags[num_open_tags - 1] != *next_tag) {
+          GST_LOG ("broken input, closing tag '%c' is not open", *next_tag);
+          g_memmove (next_tag - 2, next_tag + 2, strlen (next_tag + 2) + 1);
+          next_tag -= 2;
+        } else {
+          /* it's all good, closing tag which is open */
+          --num_open_tags;
+        }
+        break;
+      }
+      case 'i':
+      case 'b':
+      case 'u':
+        if (num_open_tags == G_N_ELEMENTS (open_tags))
+          return;               /* something dodgy is going on, stop parsing */
+        open_tags[num_open_tags] = *next_tag;
+        ++num_open_tags;
+        break;
+      default:
+        GST_ERROR ("unexpected tag '%c' (%s)", *next_tag, next_tag);
+        g_assert_not_reached ();
+        break;
+    }
+    cur = next_tag;
+  }
+
+  if (num_open_tags > 0) {
+    GString *s;
+
+    s = g_string_new (*p_txt);
+    while (num_open_tags > 0) {
+      GST_LOG ("adding missing closing tag '%c'", open_tags[num_open_tags - 1]);
+      g_string_append_c (s, '<');
+      g_string_append_c (s, '/');
+      g_string_append_c (s, open_tags[num_open_tags - 1]);
+      g_string_append_c (s, '>');
+      --num_open_tags;
+    }
+    g_free (*p_txt);
+    *p_txt = g_string_free (s, FALSE);
+  }
+}
+
+static gboolean
+parse_subrip_time (const gchar * ts_string, GstClockTime * t)
+{
+  gchar s[128] = { '\0', };
+  gchar *end, *p;
+  guint hour, min, sec, msec, len;
+
+  while (*ts_string == ' ')
+    ++ts_string;
+
+  g_strlcpy (s, ts_string, sizeof (s));
+  if ((end = strstr (s, "-->")))
+    *end = '\0';
+  g_strchomp (s);
+
+  /* ms may be in these formats:
+   * hh:mm:ss,500 = 500ms
+   * hh:mm:ss,  5 =   5ms
+   * hh:mm:ss, 5  =  50ms
+   * hh:mm:ss, 50 =  50ms
+   * hh:mm:ss,5   = 500ms
+   * and the same with . instead of ,.
+   * sscanf() doesn't differentiate between '  5' and '5' so munge
+   * the white spaces within the timestamp to '0' (I'm sure there's a
+   * way to make sscanf() do this for us, but how?)
+   */
+  g_strdelimit (s, " ", '0');
+  g_strdelimit (s, ".", ',');
+
+  /* make sure we have exactly three digits after he comma */
+  p = strchr (s, ',');
+  g_assert (p != NULL);
+  ++p;
+  len = strlen (p);
+  if (len > 3) {
+    p[3] = '\0';
+  } else
+    while (len < 3) {
+      g_strlcat (&p[len], "0", 2);
+      ++len;
+    }
+
+  GST_LOG ("parsing timestamp '%s'", s);
+  if (sscanf (s, "%u:%u:%u,%u", &hour, &min, &sec, &msec) != 4) {
+    GST_WARNING ("failed to parse subrip timestamp string '%s'", s);
+    return FALSE;
+  }
+
+  *t = ((hour * 3600) + (min * 60) + sec) * GST_SECOND + msec * GST_MSECOND;
+  return TRUE;
+}
+
 static gchar *
 parse_subrip (ParserState * state, const gchar * line)
 {
-  guint h1, m1, s1, ms1;
-  guint h2, m2, s2, ms2;
   int subnum;
   gchar *ret;
 
@@ -543,24 +885,28 @@ parse_subrip (ParserState * state, const gchar * line)
         state->state = 1;
       return NULL;
     case 1:
+    {
+      GstClockTime ts_start, ts_end;
+      gchar *end_time;
+
       /* looking for start_time --> end_time */
-      if (sscanf (line, "%u:%u:%u,%u --> %u:%u:%u,%u",
-              &h1, &m1, &s1, &ms1, &h2, &m2, &s2, &ms2) == 8) {
+      if ((end_time = strstr (line, " --> ")) &&
+          parse_subrip_time (line, &ts_start) &&
+          parse_subrip_time (end_time + strlen (" --> "), &ts_end) &&
+          state->start_time <= ts_end) {
         state->state = 2;
-        state->start_time =
-            (((guint64) h1) * 3600 + m1 * 60 + s1) * GST_SECOND +
-            ms1 * GST_MSECOND;
-        state->duration =
-            (((guint64) h2) * 3600 + m2 * 60 + s2) * GST_SECOND +
-            ms2 * GST_MSECOND - state->start_time;
+        state->start_time = ts_start;
+        state->duration = ts_end - ts_start;
       } else {
-        GST_DEBUG ("error parsing subrip time line");
+        GST_DEBUG ("error parsing subrip time line '%s'", line);
         state->state = 0;
       }
       return NULL;
+    }
     case 2:
-    {                           /* No need to parse that text if it's out of segment */
-      gint64 clip_start = 0, clip_stop = 0;
+    {
+      /* No need to parse that text if it's out of segment */
+      guint64 clip_start = 0, clip_stop = 0;
       gboolean in_seg = FALSE;
 
       /* Check our segment start/stop */
@@ -576,8 +922,7 @@ parse_subrip (ParserState * state, const gchar * line)
         return NULL;
       }
     }
-      /* looking for subtitle text; empty line ends this
-       * subtitle entry */
+      /* looking for subtitle text; empty line ends this subtitle entry */
       if (state->buf->len)
         g_string_append_c (state->buf, '\n');
       g_string_append (state->buf, line);
@@ -586,7 +931,9 @@ parse_subrip (ParserState * state, const gchar * line)
         g_string_truncate (state->buf, 0);
         state->state = 0;
         subrip_unescape_formatting (ret);
+        subrip_remove_unhandled_tags (ret);
         strip_trailing_newlines (ret);
+        subrip_fix_up_markup (&ret);
         return ret;
       }
       return NULL;
@@ -595,6 +942,94 @@ parse_subrip (ParserState * state, const gchar * line)
   }
 }
 
+static void
+unescape_newlines_br (gchar * read)
+{
+  gchar *write = read;
+
+  /* Replace all occurences of '[br]' with a newline as version 2
+   * of the subviewer format uses this for newlines */
+
+  if (read[0] == '\0' || read[1] == '\0' || read[2] == '\0' || read[3] == '\0')
+    return;
+
+  do {
+    if (strncmp (read, "[br]", 4) == 0) {
+      *write = '\n';
+      read += 4;
+    } else {
+      *write = *read;
+      read++;
+    }
+    write++;
+  } while (*read);
+
+  *write = '\0';
+}
+
+static gchar *
+parse_subviewer (ParserState * state, const gchar * line)
+{
+  guint h1, m1, s1, ms1;
+  guint h2, m2, s2, ms2;
+  gchar *ret;
+
+  /* TODO: Maybe also parse the fields in the header, especially DELAY.
+   * For examples see the unit test or
+   * http://www.doom9.org/index.html?/sub.htm */
+
+  switch (state->state) {
+    case 0:
+      /* looking for start_time,end_time */
+      if (sscanf (line, "%u:%u:%u.%u,%u:%u:%u.%u",
+              &h1, &m1, &s1, &ms1, &h2, &m2, &s2, &ms2) == 8) {
+        state->state = 1;
+        state->start_time =
+            (((guint64) h1) * 3600 + m1 * 60 + s1) * GST_SECOND +
+            ms1 * GST_MSECOND;
+        state->duration =
+            (((guint64) h2) * 3600 + m2 * 60 + s2) * GST_SECOND +
+            ms2 * GST_MSECOND - state->start_time;
+      }
+      return NULL;
+    case 1:
+    {
+      /* No need to parse that text if it's out of segment */
+      guint64 clip_start = 0, clip_stop = 0;
+      gboolean in_seg = FALSE;
+
+      /* Check our segment start/stop */
+      in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
+          state->start_time, state->start_time + state->duration,
+          &clip_start, &clip_stop);
+
+      if (in_seg) {
+        state->start_time = clip_start;
+        state->duration = clip_stop - clip_start;
+      } else {
+        state->state = 0;
+        return NULL;
+      }
+    }
+      /* looking for subtitle text; empty line ends this subtitle entry */
+      if (state->buf->len)
+        g_string_append_c (state->buf, '\n');
+      g_string_append (state->buf, line);
+      if (strlen (line) == 0) {
+        ret = g_strdup (state->buf->str);
+        unescape_newlines_br (ret);
+        strip_trailing_newlines (ret);
+        g_string_truncate (state->buf, 0);
+        state->state = 0;
+        return ret;
+      }
+      return NULL;
+    default:
+      g_assert_not_reached ();
+      return NULL;
+  }
+}
+
 static gchar *
 parse_mpsub (ParserState * state, const gchar * line)
 {
@@ -612,7 +1047,7 @@ parse_mpsub (ParserState * state, const gchar * line)
       return NULL;
     case 1:
     {                           /* No need to parse that text if it's out of segment */
-      gint64 clip_start = 0, clip_stop = 0;
+      guint64 clip_start = 0, clip_stop = 0;
       gboolean in_seg = FALSE;
 
       /* Check our segment start/stop */
@@ -646,6 +1081,73 @@ parse_mpsub (ParserState * state, const gchar * line)
   }
 }
 
+static const gchar *
+dks_skip_timestamp (const gchar * line)
+{
+  while (*line && *line != ']')
+    line++;
+  if (*line == ']')
+    line++;
+  return line;
+}
+
+static gchar *
+parse_dks (ParserState * state, const gchar * line)
+{
+  guint h, m, s;
+
+  switch (state->state) {
+    case 0:
+      /* Looking for the start time and text */
+      if (sscanf (line, "[%u:%u:%u]", &h, &m, &s) == 3) {
+        const gchar *text;
+        state->start_time = (((guint64) h) * 3600 + m * 60 + s) * GST_SECOND;
+        text = dks_skip_timestamp (line);
+        if (*text) {
+          state->state = 1;
+          g_string_append (state->buf, text);
+        }
+      }
+      return NULL;
+    case 1:
+    {
+      guint64 clip_start = 0, clip_stop = 0;
+      gboolean in_seg;
+      gchar *ret;
+
+      /* Looking for the end time */
+      if (sscanf (line, "[%u:%u:%u]", &h, &m, &s) == 3) {
+        state->state = 0;
+        state->duration = (((guint64) h) * 3600 + m * 60 + s) * GST_SECOND -
+            state->start_time;
+      } else {
+        GST_WARNING ("Failed to parse subtitle end time");
+        return NULL;
+      }
+
+      /* Check if this subtitle is out of the current segment */
+      in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
+          state->start_time, state->start_time + state->duration,
+          &clip_start, &clip_stop);
+
+      if (!in_seg) {
+        return NULL;
+      }
+
+      state->start_time = clip_start;
+      state->duration = clip_stop - clip_start;
+
+      ret = g_strdup (state->buf->str);
+      g_string_truncate (state->buf, 0);
+      unescape_newlines_br (ret);
+      return ret;
+    }
+    default:
+      g_assert_not_reached ();
+      return NULL;
+  }
+}
+
 static void
 parser_state_init (ParserState * state)
 {
@@ -659,20 +1161,77 @@ parser_state_init (ParserState * state)
 
   state->start_time = 0;
   state->duration = 0;
+  state->max_duration = 0;      /* no limit */
   state->state = 0;
   state->segment = NULL;
 }
 
 static void
-parser_state_dispose (ParserState * state)
+parser_state_dispose (GstSubParse * self, ParserState * state)
 {
   if (state->buf) {
     g_string_free (state->buf, TRUE);
     state->buf = NULL;
   }
   if (state->user_data) {
-    sami_context_reset (state);
+    switch (self->parser_type) {
+#ifndef GST_DISABLE_XML
+      case GST_SUB_PARSE_FORMAT_SAMI:
+        sami_context_reset (state);
+        break;
+#endif
+      default:
+        break;
+    }
+  }
+}
+
+/* regex type enum */
+typedef enum
+{
+  GST_SUB_PARSE_REGEX_UNKNOWN = 0,
+  GST_SUB_PARSE_REGEX_MDVDSUB = 1,
+  GST_SUB_PARSE_REGEX_SUBRIP = 2,
+  GST_SUB_PARSE_REGEX_DKS = 3,
+} GstSubParseRegex;
+
+static gpointer
+gst_sub_parse_data_format_autodetect_regex_once (GstSubParseRegex regtype)
+{
+  gpointer result = NULL;
+  GError *gerr = NULL;
+  switch (regtype) {
+    case GST_SUB_PARSE_REGEX_MDVDSUB:
+      result =
+          (gpointer) g_regex_new ("^\\{[0-9]+\\}\\{[0-9]+\\}",
+          G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &gerr);
+      if (result == NULL) {
+        g_warning ("Compilation of mdvd regex failed: %s", gerr->message);
+        g_error_free (gerr);
+      }
+      break;
+    case GST_SUB_PARSE_REGEX_SUBRIP:
+      result = (gpointer) g_regex_new ("^([ 0-9]){0,3}[0-9]\\s*(\x0d)?\x0a"
+          "[ 0-9][0-9]:[ 0-9][0-9]:[ 0-9][0-9][,.][ 0-9]{0,2}[0-9]"
+          " +--> +([ 0-9])?[0-9]:[ 0-9][0-9]:[ 0-9][0-9][,.][ 0-9]{0,2}[0-9]",
+          G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &gerr);
+      if (result == NULL) {
+        g_warning ("Compilation of subrip regex failed: %s", gerr->message);
+        g_error_free (gerr);
+      }
+      break;
+    case GST_SUB_PARSE_REGEX_DKS:
+      result = (gpointer) g_regex_new ("^\\[[0-9]+:[0-9]+:[0-9]+\\].*",
+          G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &gerr);
+      if (result == NULL) {
+        g_warning ("Compilation of dks regex failed: %s", gerr->message);
+        g_error_free (gerr);
+      }
+      break;
+    default:
+      GST_WARNING ("Trying to allocate regex of unknown type %u", regtype);
   }
+  return result;
 }
 
 /*
@@ -684,44 +1243,75 @@ parser_state_dispose (ParserState * state)
 static GstSubParseFormat
 gst_sub_parse_data_format_autodetect (gchar * match_str)
 {
-  static gboolean need_init_regexps = TRUE;
-  static regex_t mdvd_rx;
-  static regex_t subrip_rx;
-
-  /* initialize the regexps used the first time around */
-  if (need_init_regexps) {
-    int err;
-    char errstr[128];
-
-    need_init_regexps = FALSE;
-    if ((err = regcomp (&mdvd_rx, "^\\{[0-9]+\\}\\{[0-9]+\\}",
-                REG_EXTENDED | REG_NEWLINE | REG_NOSUB) != 0) ||
-        (err = regcomp (&subrip_rx, "^[1-9]([0-9]){0,3}(\x0d)?\x0a"
-                "[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{3}"
-                " --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{3}",
-                REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) != 0) {
-      regerror (err, &subrip_rx, errstr, 127);
-      GST_WARNING ("Compilation of subrip regex failed: %s", errstr);
-    }
-  }
-
-  if (regexec (&mdvd_rx, match_str, 0, NULL, 0) == 0) {
+  guint n1, n2, n3;
+
+  static GOnce mdvd_rx_once = G_ONCE_INIT;
+  static GOnce subrip_rx_once = G_ONCE_INIT;
+  static GOnce dks_rx_once = G_ONCE_INIT;
+
+  GRegex *mdvd_grx;
+  GRegex *subrip_grx;
+  GRegex *dks_grx;
+
+  g_once (&mdvd_rx_once,
+      (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
+      (gpointer) GST_SUB_PARSE_REGEX_MDVDSUB);
+  g_once (&subrip_rx_once,
+      (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
+      (gpointer) GST_SUB_PARSE_REGEX_SUBRIP);
+  g_once (&dks_rx_once,
+      (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
+      (gpointer) GST_SUB_PARSE_REGEX_DKS);
+
+  mdvd_grx = (GRegex *) mdvd_rx_once.retval;
+  subrip_grx = (GRegex *) subrip_rx_once.retval;
+  dks_grx = (GRegex *) dks_rx_once.retval;
+
+  if (g_regex_match (mdvd_grx, match_str, 0, NULL) == TRUE) {
     GST_LOG ("MicroDVD (frame based) format detected");
     return GST_SUB_PARSE_FORMAT_MDVDSUB;
   }
-  if (regexec (&subrip_rx, match_str, 0, NULL, 0) == 0) {
+  if (g_regex_match (subrip_grx, match_str, 0, NULL) == TRUE) {
     GST_LOG ("SubRip (time based) format detected");
     return GST_SUB_PARSE_FORMAT_SUBRIP;
   }
+  if (g_regex_match (dks_grx, match_str, 0, NULL) == TRUE) {
+    GST_LOG ("DKS (time based) format detected");
+    return GST_SUB_PARSE_FORMAT_DKS;
+  }
+
   if (!strncmp (match_str, "FORMAT=TIME", 11)) {
     GST_LOG ("MPSub (time based) format detected");
     return GST_SUB_PARSE_FORMAT_MPSUB;
   }
+#ifndef GST_DISABLE_XML
   if (strstr (match_str, "<SAMI>") != NULL ||
       strstr (match_str, "<sami>") != NULL) {
     GST_LOG ("SAMI (time based) format detected");
     return GST_SUB_PARSE_FORMAT_SAMI;
   }
+#endif
+  /* we're boldly assuming the first subtitle appears within the first hour */
+  if (sscanf (match_str, "0:%02u:%02u:", &n1, &n2) == 2 ||
+      sscanf (match_str, "0:%02u:%02u=", &n1, &n2) == 2 ||
+      sscanf (match_str, "00:%02u:%02u:", &n1, &n2) == 2 ||
+      sscanf (match_str, "00:%02u:%02u=", &n1, &n2) == 2 ||
+      sscanf (match_str, "00:%02u:%02u,%u=", &n1, &n2, &n3) == 3) {
+    GST_LOG ("TMPlayer (time based) format detected");
+    return GST_SUB_PARSE_FORMAT_TMPLAYER;
+  }
+  if (sscanf (match_str, "[%u][%u]", &n1, &n2) == 2) {
+    GST_LOG ("MPL2 (time based) format detected");
+    return GST_SUB_PARSE_FORMAT_MPL2;
+  }
+  if (strstr (match_str, "[INFORMATION]") != NULL) {
+    GST_LOG ("SubViewer (time based) format detected");
+    return GST_SUB_PARSE_FORMAT_SUBVIEWER;
+  }
+  if (strstr (match_str, "{QTtext}") != NULL) {
+    GST_LOG ("QTtext (time based) format detected");
+    return GST_SUB_PARSE_FORMAT_QTTEXT;
+  }
 
   GST_DEBUG ("no subtitle format detected");
   return GST_SUB_PARSE_FORMAT_UNKNOWN;
@@ -733,7 +1323,7 @@ gst_sub_parse_format_autodetect (GstSubParse * self)
   gchar *data;
   GstSubParseFormat format;
 
-  if (strlen (self->textbuf->str) < 35) {
+  if (strlen (self->textbuf->str) < 30) {
     GST_DEBUG ("File too small to be a subtitles file");
     return NULL;
   }
@@ -743,6 +1333,7 @@ gst_sub_parse_format_autodetect (GstSubParse * self)
   g_free (data);
 
   self->parser_type = format;
+  self->subtitle_codec = gst_sub_parse_get_format_description (format);
   parser_state_init (&self->state);
 
   switch (format) {
@@ -755,10 +1346,29 @@ gst_sub_parse_format_autodetect (GstSubParse * self)
     case GST_SUB_PARSE_FORMAT_MPSUB:
       self->parse_line = parse_mpsub;
       return gst_caps_new_simple ("text/plain", NULL);
+#ifndef GST_DISABLE_XML
     case GST_SUB_PARSE_FORMAT_SAMI:
       self->parse_line = parse_sami;
       sami_context_init (&self->state);
       return gst_caps_new_simple ("text/x-pango-markup", NULL);
+#endif
+    case GST_SUB_PARSE_FORMAT_TMPLAYER:
+      self->parse_line = parse_tmplayer;
+      self->state.max_duration = 5 * GST_SECOND;
+      return gst_caps_new_simple ("text/plain", NULL);
+    case GST_SUB_PARSE_FORMAT_MPL2:
+      self->parse_line = parse_mpl2;
+      return gst_caps_new_simple ("text/x-pango-markup", NULL);
+    case GST_SUB_PARSE_FORMAT_DKS:
+      self->parse_line = parse_dks;
+      return gst_caps_new_simple ("text/plain", NULL);
+    case GST_SUB_PARSE_FORMAT_SUBVIEWER:
+      self->parse_line = parse_subviewer;
+      return gst_caps_new_simple ("text/plain", NULL);
+    case GST_SUB_PARSE_FORMAT_QTTEXT:
+      self->parse_line = parse_qttext;
+      qttext_context_init (&self->state);
+      return gst_caps_new_simple ("text/x-pango-markup", NULL);
     case GST_SUB_PARSE_FORMAT_UNKNOWN:
     default:
       GST_DEBUG ("no subtitle format detected");
@@ -771,19 +1381,52 @@ gst_sub_parse_format_autodetect (GstSubParse * self)
 static void
 feed_textbuf (GstSubParse * self, GstBuffer * buf)
 {
-  if (GST_BUFFER_OFFSET (buf) != self->offset) {
+  gboolean discont;
+  gsize consumed;
+  gchar *input = NULL;
+  const guint8 *data;
+  gsize avail;
+
+  discont = GST_BUFFER_IS_DISCONT (buf);
+
+  if (GST_BUFFER_OFFSET_IS_VALID (buf) &&
+      GST_BUFFER_OFFSET (buf) != self->offset) {
+    self->offset = GST_BUFFER_OFFSET (buf);
+    discont = TRUE;
+  }
+
+  if (discont) {
+    GST_INFO ("discontinuity");
     /* flush the parser state */
     parser_state_init (&self->state);
     g_string_truncate (self->textbuf, 0);
-    sami_context_reset (&self->state);
+    gst_adapter_clear (self->adapter);
+#ifndef GST_DISABLE_XML
+    if (self->parser_type == GST_SUB_PARSE_FORMAT_SAMI)
+      sami_context_reset (&self->state);
+#endif
+    /* we could set a flag to make sure that the next buffer we push out also
+     * has the DISCONT flag set, but there's no point really given that it's
+     * subtitles which are discontinuous by nature. */
   }
 
-  self->textbuf = g_string_append_len (self->textbuf,
-      (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
-  self->offset = GST_BUFFER_OFFSET (buf) + GST_BUFFER_SIZE (buf);
+  self->offset = GST_BUFFER_OFFSET (buf) + gst_buffer_get_size (buf);
   self->next_offset = self->offset;
 
-  gst_buffer_unref (buf);
+  gst_adapter_push (self->adapter, buf);
+
+  avail = gst_adapter_available (self->adapter);
+  data = gst_adapter_map (self->adapter, avail);
+  input = convert_encoding (self, (const gchar *) data, avail, &consumed);
+
+  if (input && consumed > 0) {
+    self->textbuf = g_string_append (self->textbuf, input);
+    gst_adapter_unmap (self->adapter, consumed);
+  } else {
+    gst_adapter_unmap (self->adapter, 0);
+  }
+
+  g_free (input);
 }
 
 static GstFlowReturn
@@ -791,7 +1434,17 @@ handle_buffer (GstSubParse * self, GstBuffer * buf)
 {
   GstFlowReturn ret = GST_FLOW_OK;
   GstCaps *caps = NULL;
-  gchar *line, *subtitle;
+  gchar *line, *subtitle, *data;
+  gsize size;
+
+  if (self->first_buffer) {
+    data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
+    self->detected_encoding = detect_encoding (data, size);
+    gst_buffer_unmap (buf, data, size);
+    self->first_buffer = FALSE;
+    self->state.fps_n = self->fps_n;
+    self->state.fps_d = self->fps_d;
+  }
 
   feed_textbuf (self, buf);
 
@@ -805,43 +1458,70 @@ handle_buffer (GstSubParse * self, GstBuffer * buf)
       return GST_FLOW_UNEXPECTED;
     }
     gst_caps_unref (caps);
+
+    /* push tags */
+    if (self->subtitle_codec != NULL) {
+      GstTagList *tags;
+
+      tags = gst_tag_list_new ();
+      gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
+          self->subtitle_codec, NULL);
+      gst_element_found_tags_for_pad (GST_ELEMENT (self), self->srcpad, tags);
+    }
   }
 
-  while ((line = get_next_line (self)) && !self->flushing) {
+  while (!self->flushing && (line = get_next_line (self))) {
+    guint offset = 0;
+
     /* Set segment on our parser state machine */
-    self->state.segment = self->segment;
+    self->state.segment = &self->segment;
     /* Now parse the line, out of segment lines will just return NULL */
-    GST_DEBUG ("Parsing line '%s'", line);
-    subtitle = self->parse_line (&self->state, line);
+    GST_LOG_OBJECT (self, "Parsing line '%s'", line + offset);
+    subtitle = self->parse_line (&self->state, line + offset);
     g_free (line);
 
     if (subtitle) {
       guint subtitle_len = strlen (subtitle);
 
-      ret = gst_pad_alloc_buffer_and_set_caps (self->srcpad,
-          GST_BUFFER_OFFSET_NONE, subtitle_len,
-          GST_PAD_CAPS (self->srcpad), &buf);
-
-      if (ret == GST_FLOW_OK) {
-        memcpy (GST_BUFFER_DATA (buf), subtitle, subtitle_len);
-        GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
-        GST_BUFFER_DURATION (buf) = self->state.duration;
+      /* +1 for terminating NUL character */
+      buf = gst_buffer_new_and_alloc (subtitle_len + 1);
 
-        gst_segment_set_last_stop (self->segment, GST_FORMAT_TIME,
-            self->state.start_time);
+      /* copy terminating NUL character as well */
+      gst_buffer_fill (buf, 0, subtitle, subtitle_len + 1);
+      gst_buffer_set_size (buf, subtitle_len);
 
-        GST_DEBUG ("Sending text '%s', %" GST_TIME_FORMAT " + %"
-            GST_TIME_FORMAT, subtitle, GST_TIME_ARGS (self->state.start_time),
-            GST_TIME_ARGS (self->state.duration));
+      GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
+      GST_BUFFER_DURATION (buf) = self->state.duration;
 
-        ret = gst_pad_push (self->srcpad, buf);
+      /* in some cases (e.g. tmplayer) we can only determine the duration
+       * of a text chunk from the timestamp of the next text chunk; in those
+       * cases, we probably want to limit the duration to something
+       * reasonable, so we don't end up showing some text for e.g. 40 seconds
+       * just because nothing else is being said during that time */
+      if (self->state.max_duration > 0 && GST_BUFFER_DURATION_IS_VALID (buf)) {
+        if (GST_BUFFER_DURATION (buf) > self->state.max_duration)
+          GST_BUFFER_DURATION (buf) = self->state.max_duration;
       }
 
+      self->segment.position = self->state.start_time;
+
+      GST_DEBUG_OBJECT (self, "Sending text '%s', %" GST_TIME_FORMAT " + %"
+          GST_TIME_FORMAT, subtitle, GST_TIME_ARGS (self->state.start_time),
+          GST_TIME_ARGS (self->state.duration));
+
+      ret = gst_pad_push (self->srcpad, buf);
+
+      /* move this forward (the tmplayer parser needs this) */
+      if (self->state.duration != GST_CLOCK_TIME_NONE)
+        self->state.start_time += self->state.duration;
+
       g_free (subtitle);
       subtitle = NULL;
 
-      if (GST_FLOW_IS_FATAL (ret))
+      if (ret != GST_FLOW_OK) {
+        GST_DEBUG_OBJECT (self, "flow: %s", gst_flow_get_name (ret));
         break;
+      }
     }
   }
 
@@ -854,22 +1534,19 @@ gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf)
   GstFlowReturn ret;
   GstSubParse *self;
 
-  GST_DEBUG ("gst_sub_parse_chain");
-  self = GST_SUBPARSE (gst_pad_get_parent (sinkpad));
+  self = GST_SUBPARSE (GST_PAD_PARENT (sinkpad));
 
   /* Push newsegment if needed */
   if (self->need_segment) {
-    gst_pad_push_event (self->srcpad, gst_event_new_new_segment (FALSE,
-            self->segment->rate, self->segment->format,
-            self->segment->last_stop, self->segment->stop,
-            self->segment->time));
+    GST_LOG_OBJECT (self, "pushing newsegment event with %" GST_SEGMENT_FORMAT,
+        &self->segment);
+
+    gst_pad_push_event (self->srcpad, gst_event_new_segment (&self->segment));
     self->need_segment = FALSE;
   }
 
   ret = handle_buffer (self, buf);
 
-  gst_object_unref (self);
-
   return ret;
 }
 
@@ -885,35 +1562,37 @@ gst_sub_parse_sink_event (GstPad * pad, GstEvent * event)
     case GST_EVENT_EOS:{
       /* Make sure the last subrip chunk is pushed out even
        * if the file does not have an empty line at the end */
-      if (self->parser_type == GST_SUB_PARSE_FORMAT_SUBRIP) {
-        GstBuffer *buf = gst_buffer_new_and_alloc (1 + 1);
+      if (self->parser_type == GST_SUB_PARSE_FORMAT_SUBRIP ||
+          self->parser_type == GST_SUB_PARSE_FORMAT_TMPLAYER ||
+          self->parser_type == GST_SUB_PARSE_FORMAT_MPL2 ||
+          self->parser_type == GST_SUB_PARSE_FORMAT_QTTEXT) {
+        gchar term_chars[] = { '\n', '\n', '\0' };
+        GstBuffer *buf = gst_buffer_new_and_alloc (2 + 1);
 
         GST_DEBUG ("EOS. Pushing remaining text (if any)");
-        GST_BUFFER_DATA (buf)[0] = '\n';
-        GST_BUFFER_DATA (buf)[1] = '\0';        /* play it safe */
-        GST_BUFFER_SIZE (buf) = 1;
+        gst_buffer_fill (buf, 0, term_chars, 3);
+        gst_buffer_set_size (buf, 2);
+
         GST_BUFFER_OFFSET (buf) = self->offset;
         gst_sub_parse_chain (pad, buf);
       }
       ret = gst_pad_event_default (pad, event);
       break;
     }
-    case GST_EVENT_NEWSEGMENT:
+    case GST_EVENT_SEGMENT:
     {
-      GstFormat format;
-      gdouble rate;
-      gint64 start, stop, time;
-      gboolean update;
-
-      GST_DEBUG_OBJECT (self, "received new segment");
-
-      gst_event_parse_new_segment (event, &update, &rate, &format, &start,
-          &stop, &time);
-
-      /* now copy over the values */
-      gst_segment_set_newsegment (self->segment, update, rate, format,
-          start, stop, time);
-
+      gst_event_copy_segment (event, &self->segment);
+      GST_DEBUG_OBJECT (self, "newsegment (%s)",
+          gst_format_get_name (self->segment.format));
+
+      /* if not time format, we'll either start with a 0 timestamp anyway or
+       * it's following a seek in which case we'll have saved the requested
+       * seek segment and don't want to overwrite it (remember that on a seek
+       * we always just seek back to the start in BYTES format and just throw
+       * away all text that's before the requested position; if the subtitles
+       * come from an upstream demuxer, it won't be able to handle our BYTES
+       * seek request and instead send us a newsegment from the seek request
+       * it received via its video pads instead, so all is fine then too) */
       ret = TRUE;
       gst_event_unref (event);
       break;
@@ -956,19 +1635,23 @@ gst_sub_parse_change_state (GstElement * element, GstStateChange transition)
       self->next_offset = 0;
       self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
       self->valid_utf8 = TRUE;
+      self->first_buffer = TRUE;
+      g_free (self->detected_encoding);
+      self->detected_encoding = NULL;
       g_string_truncate (self->textbuf, 0);
+      gst_adapter_clear (self->adapter);
       break;
     default:
       break;
   }
 
-  ret = parent_class->change_state (element, transition);
+  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
   if (ret == GST_STATE_CHANGE_FAILURE)
     return ret;
 
   switch (transition) {
     case GST_STATE_CHANGE_PAUSED_TO_READY:
-      parser_state_dispose (&self->state);
+      parser_state_dispose (self, &self->state);
       self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
       break;
     default:
@@ -982,11 +1665,30 @@ gst_sub_parse_change_state (GstElement * element, GstStateChange transition)
  * Typefind support.
  */
 
-static GstStaticCaps smi_caps = GST_STATIC_CAPS ("application/x-subtitle-sami");
+/* FIXME 0.11: these caps are ugly, use app/x-subtitle + type field or so;
+ * also, give different  subtitle formats really different types */
+static GstStaticCaps mpl2_caps =
+GST_STATIC_CAPS ("application/x-subtitle-mpl2");
+#define SUB_CAPS (gst_static_caps_get (&sub_caps))
+
+static GstStaticCaps tmp_caps =
+GST_STATIC_CAPS ("application/x-subtitle-tmplayer");
+#define TMP_CAPS (gst_static_caps_get (&tmp_caps))
+
 static GstStaticCaps sub_caps = GST_STATIC_CAPS ("application/x-subtitle");
+#define MPL2_CAPS (gst_static_caps_get (&mpl2_caps))
 
-#define SUB_CAPS (gst_static_caps_get (&sub_caps))
+#ifndef GST_DISABLE_XML
+static GstStaticCaps smi_caps = GST_STATIC_CAPS ("application/x-subtitle-sami");
 #define SAMI_CAPS (gst_static_caps_get (&smi_caps))
+#endif
+
+static GstStaticCaps dks_caps = GST_STATIC_CAPS ("application/x-subtitle-dks");
+#define DKS_CAPS (gst_static_caps_get (&dks_caps))
+
+static GstStaticCaps qttext_caps =
+GST_STATIC_CAPS ("application/x-subtitle-qttext");
+#define QTTEXT_CAPS (gst_static_caps_get (&qttext_caps))
 
 static void
 gst_subparse_type_find (GstTypeFind * tf, gpointer private)
@@ -995,12 +1697,57 @@ gst_subparse_type_find (GstTypeFind * tf, gpointer private)
   const guint8 *data;
   GstCaps *caps;
   gchar *str;
+  gchar *encoding = NULL;
+  const gchar *end;
 
-  if (!(data = gst_type_find_peek (tf, 0, 36)))
+  if (!(data = gst_type_find_peek (tf, 0, 129)))
     return;
 
   /* make sure string passed to _autodetect() is NUL-terminated */
-  str = g_strndup ((gchar *) data, 35);
+  str = g_malloc0 (129);
+  memcpy (str, data, 128);
+
+  if ((encoding = detect_encoding (str, 128)) != NULL) {
+    gchar *converted_str;
+    GError *err = NULL;
+    gsize tmp;
+
+    converted_str = gst_convert_to_utf8 (str, 128, encoding, &tmp, &err);
+    if (converted_str == NULL) {
+      GST_DEBUG ("Encoding '%s' detected but conversion failed: %s", encoding,
+          err->message);
+      g_error_free (err);
+      g_free (encoding);
+    } else {
+      g_free (str);
+      str = converted_str;
+      g_free (encoding);
+    }
+  }
+
+  /* Check if at least the first 120 chars are valid UTF8,
+   * otherwise convert as always */
+  if (!g_utf8_validate (str, 128, &end) && (end - str) < 120) {
+    gchar *converted_str;
+    GError *err = NULL;
+    gsize tmp;
+    const gchar *enc;
+
+    enc = g_getenv ("GST_SUBTITLE_ENCODING");
+    if (enc == NULL || *enc == '\0') {
+      /* if local encoding is UTF-8 and no encoding specified
+       * via the environment variable, assume ISO-8859-15 */
+      if (g_get_charset (&enc)) {
+        enc = "ISO-8859-15";
+      }
+    }
+    converted_str = gst_convert_to_utf8 (str, 128, enc, &tmp, &err);
+    if (converted_str != NULL) {
+      g_free (str);
+      str = converted_str;
+    }
+  }
+
   format = gst_sub_parse_data_format_autodetect (str);
   g_free (str);
 
@@ -1017,10 +1764,36 @@ gst_subparse_type_find (GstTypeFind * tf, gpointer private)
       GST_DEBUG ("MPSub format detected");
       caps = SUB_CAPS;
       break;
+#ifndef GST_DISABLE_XML
     case GST_SUB_PARSE_FORMAT_SAMI:
       GST_DEBUG ("SAMI (time-based) format detected");
       caps = SAMI_CAPS;
       break;
+#endif
+    case GST_SUB_PARSE_FORMAT_TMPLAYER:
+      GST_DEBUG ("TMPlayer (time based) format detected");
+      caps = TMP_CAPS;
+      break;
+      /* FIXME: our MPL2 typefinding is not really good enough to warrant
+       * returning a high probability (however, since we registered our
+       * typefinder here with a rank of MARGINAL we should pretty much only
+       * be called if most other typefinders have already run */
+    case GST_SUB_PARSE_FORMAT_MPL2:
+      GST_DEBUG ("MPL2 (time based) format detected");
+      caps = MPL2_CAPS;
+      break;
+    case GST_SUB_PARSE_FORMAT_SUBVIEWER:
+      GST_DEBUG ("SubViewer format detected");
+      caps = SUB_CAPS;
+      break;
+    case GST_SUB_PARSE_FORMAT_DKS:
+      GST_DEBUG ("DKS format detected");
+      caps = DKS_CAPS;
+      break;
+    case GST_SUB_PARSE_FORMAT_QTTEXT:
+      GST_DEBUG ("QTtext format detected");
+      caps = QTTEXT_CAPS;
+      break;
     default:
     case GST_SUB_PARSE_FORMAT_UNKNOWN:
       GST_DEBUG ("no subtitle format detected");
@@ -1034,12 +1807,13 @@ gst_subparse_type_find (GstTypeFind * tf, gpointer private)
 static gboolean
 plugin_init (GstPlugin * plugin)
 {
-  static gchar *sub_exts[] = { "srt", "sub", "mpsub", "mdvd", "smi", NULL };
+  static const gchar *sub_exts[] =
+      { "srt", "sub", "mpsub", "mdvd", "smi", "txt", "dks", NULL };
 
   GST_DEBUG_CATEGORY_INIT (sub_parse_debug, "subparse", 0, ".sub parser");
 
   if (!gst_type_find_register (plugin, "subparse_typefind", GST_RANK_MARGINAL,
-          gst_subparse_type_find, sub_exts, SUB_CAPS, NULL, NULL))
+          gst_subparse_type_find, (gchar **) sub_exts, SUB_CAPS, NULL, NULL))
     return FALSE;
 
   if (!gst_element_register (plugin, "subparse",