Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / gst / subparse / gstsubparse.c
index 8b4a24c..c910cb2 100644 (file)
@@ -34,6 +34,7 @@
 #include "samiparse.h"
 #include "tmplayerparse.h"
 #include "mpl2parse.h"
+#include "qttextparse.h"
 
 GST_DEBUG_CATEGORY (sub_parse_debug);
 
@@ -42,7 +43,8 @@ GST_DEBUG_CATEGORY (sub_parse_debug);
 enum
 {
   PROP_0,
-  PROP_ENCODING
+  PROP_ENCODING,
+  PROP_VIDEOFPS
 };
 
 static void
@@ -53,26 +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"
-    "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
-
 #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; "
         "application/x-subtitle-tmplayer; application/x-subtitle-mpl2; "
-        "application/x-subtitle-dks")
+        "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; application/x-subtitle-dks")
+    GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-dks; "
+        "application/x-subtitle-tmplayer; application/x-subtitle-mpl2; "
+        "application/x-subtitle-qttext")
     );
 #endif
 
@@ -82,9 +79,6 @@ 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);
@@ -95,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)
@@ -141,6 +99,19 @@ gst_sub_parse_dispose (GObject * object)
 
   GST_DEBUG_OBJECT (subparse, "cleaning up subtitle parser");
 
+  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;
@@ -160,9 +131,6 @@ gst_sub_parse_dispose (GObject * object)
     g_string_free (subparse->textbuf, TRUE);
     subparse->textbuf = NULL;
   }
-#ifndef GST_DISABLE_XML
-  sami_context_deinit (&subparse->state);
-#endif
 
   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
 }
@@ -173,12 +141,20 @@ 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,
@@ -188,6 +164,14 @@ gst_sub_parse_class_init (GstSubParseClass * klass)
           "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
@@ -215,6 +199,9 @@ gst_sub_parse_init (GstSubParse * subparse)
   subparse->encoding = g_strdup (DEFAULT_ENCODING);
   subparse->detected_encoding = NULL;
   subparse->adapter = gst_adapter_new ();
+
+  subparse->fps_n = 24000;
+  subparse->fps_d = 1001;
 }
 
 /*
@@ -238,9 +225,9 @@ gst_sub_parse_src_query (GstPad * pad, GstQuery * query)
         ret = gst_pad_peer_query (self->sinkpad, query);
       } else {
         ret = TRUE;
-        gst_query_set_position (query, GST_FORMAT_TIME,
-            self->segment.last_stop);
+        gst_query_set_position (query, GST_FORMAT_TIME, self->segment.position);
       }
+      break;
     }
     case GST_QUERY_SEEKING:
     {
@@ -260,7 +247,6 @@ gst_sub_parse_src_query (GstPad * pad, GstQuery * query)
       }
 
       gst_query_set_seeking (query, fmt, seekable, seekable ? 0 : -1, -1);
-
       break;
     }
     default:
@@ -285,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) {
@@ -302,12 +289,12 @@ 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 after seek: %" GST_SEGMENT_FORMAT,
@@ -348,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;
@@ -366,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;
@@ -373,7 +376,7 @@ gst_sub_parse_get_property (GObject * object, guint prop_id,
   GST_OBJECT_UNLOCK (subparse);
 }
 
-static gchar *
+static const gchar *
 gst_sub_parse_get_format_description (GstSubParseFormat format)
 {
   switch (format) {
@@ -393,6 +396,8 @@ gst_sub_parse_get_format_description (GstSubParseFormat format)
       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;
@@ -407,9 +412,10 @@ gst_convert_to_utf8 (const gchar * str, gsize len, const gchar * encoding,
   gchar *ret = NULL;
 
   *consumed = 0;
+  /* The char cast is necessary in glib < 2.24 */
   ret =
-      g_convert_with_fallback (str, len, "UTF-8", encoding, "*", consumed, NULL,
-      err);
+      g_convert_with_fallback (str, len, "UTF-8", encoding, (char *) "*",
+      consumed, NULL, err);
   if (ret == NULL)
     return ret;
 
@@ -547,7 +553,7 @@ 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;
   GString *markup;
   gchar *ret;
@@ -556,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"
@@ -568,27 +575,27 @@ parse_mdvdsub (ParserState * state, const gchar * line)
   line = strchr (line, '}') + 1;
 
   /* see if there's a first line with a framerate */
-  if (state->fps == 0.0 && start_frame == 1 && end_frame == 1) {
+  if (start_frame == 1 && end_frame == 1) {
     gchar *rest, *end = NULL;
 
     rest = g_strdup (line);
     g_strdelimit (rest, ",", '.');
-    state->fps = g_ascii_strtod (rest, &end);
-    if (end == rest)
-      state->fps = 0.0;
-    GST_INFO ("framerate from file: %f ('%s')", state->fps, 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;
   }
 
-  if (state->fps == 0.0) {
-    /* FIXME: hardcoded for now, is there a better way/assumption? */
-    state->fps = 24000.0 / 1001.0;
-    GST_INFO ("no framerate specified, assuming %f", state->fps);
-  }
-
-  state->start_time = start_frame / state->fps * GST_SECOND;
-  state->duration = (end_frame - start_frame) / state->fps * GST_SECOND;
+  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,
@@ -899,7 +906,7 @@ parse_subrip (ParserState * state, const gchar * line)
     case 2:
     {
       /* 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 */
@@ -988,7 +995,7 @@ parse_subviewer (ParserState * state, const gchar * line)
     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 */
@@ -1040,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 */
@@ -1104,7 +1111,7 @@ parse_dks (ParserState * state, const gchar * line)
       return NULL;
     case 1:
     {
-      gint64 clip_start = 0, clip_stop = 0;
+      guint64 clip_start = 0, clip_stop = 0;
       gboolean in_seg;
       gchar *ret;
 
@@ -1160,17 +1167,23 @@ parser_state_init (ParserState * state)
 }
 
 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;
   }
-#ifndef GST_DISABLE_XML
   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 */
@@ -1190,7 +1203,8 @@ gst_sub_parse_data_format_autodetect_regex_once (GstSubParseRegex regtype)
   switch (regtype) {
     case GST_SUB_PARSE_REGEX_MDVDSUB:
       result =
-          (gpointer) g_regex_new ("^\\{[0-9]+\\}\\{[0-9]+\\}", 0, 0, &gerr);
+          (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);
@@ -1200,15 +1214,15 @@ gst_sub_parse_data_format_autodetect_regex_once (GstSubParseRegex regtype)
       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]",
-          0, 0, &gerr);
+          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]+].*",
-          0, 0, &gerr);
+      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);
@@ -1294,6 +1308,10 @@ gst_sub_parse_data_format_autodetect (gchar * match_str)
     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;
@@ -1347,6 +1365,10 @@ gst_sub_parse_format_autodetect (GstSubParse * self)
     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");
@@ -1362,6 +1384,8 @@ feed_textbuf (GstSubParse * self, GstBuffer * buf)
   gboolean discont;
   gsize consumed;
   gchar *input = NULL;
+  const guint8 *data;
+  gsize avail;
 
   discont = GST_BUFFER_IS_DISCONT (buf);
 
@@ -1378,26 +1402,28 @@ feed_textbuf (GstSubParse * self, GstBuffer * buf)
     g_string_truncate (self->textbuf, 0);
     gst_adapter_clear (self->adapter);
 #ifndef GST_DISABLE_XML
-    sami_context_reset (&self->state);
+    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->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_adapter_push (self->adapter, buf);
 
-  input =
-      convert_encoding (self, (const gchar *) gst_adapter_peek (self->adapter,
-          gst_adapter_available (self->adapter)),
-      (gsize) gst_adapter_available (self->adapter), &consumed);
+  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_flush (self->adapter, consumed);
+    gst_adapter_unmap (self->adapter, consumed);
+  } else {
+    gst_adapter_unmap (self->adapter, 0);
   }
 
   g_free (input);
@@ -1408,13 +1434,16 @@ 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) {
-    self->detected_encoding =
-        detect_encoding ((gchar *) GST_BUFFER_DATA (buf),
-        GST_BUFFER_SIZE (buf));
+    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);
@@ -1455,36 +1484,32 @@ handle_buffer (GstSubParse * self, GstBuffer * buf)
       guint subtitle_len = strlen (subtitle);
 
       /* +1 for terminating NUL character */
-      ret = gst_pad_alloc_buffer_and_set_caps (self->srcpad,
-          GST_BUFFER_OFFSET_NONE, subtitle_len + 1,
-          GST_PAD_CAPS (self->srcpad), &buf);
-
-      if (ret == GST_FLOW_OK) {
-        /* copy terminating NUL character as well */
-        memcpy (GST_BUFFER_DATA (buf), subtitle, subtitle_len + 1);
-        GST_BUFFER_SIZE (buf) = subtitle_len;
-        GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
-        GST_BUFFER_DURATION (buf) = self->state.duration;
-
-        /* 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;
-        }
+      buf = gst_buffer_new_and_alloc (subtitle_len + 1);
+
+      /* copy terminating NUL character as well */
+      gst_buffer_fill (buf, 0, subtitle, subtitle_len + 1);
+      gst_buffer_set_size (buf, subtitle_len);
+
+      GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
+      GST_BUFFER_DURATION (buf) = self->state.duration;
+
+      /* 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;
+      }
 
-        gst_segment_set_last_stop (&self->segment, GST_FORMAT_TIME,
-            self->state.start_time);
+      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));
+      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);
-      }
+      ret = gst_pad_push (self->srcpad, buf);
 
       /* move this forward (the tmplayer parser needs this) */
       if (self->state.duration != GST_CLOCK_TIME_NONE)
@@ -1516,9 +1541,7 @@ gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf)
     GST_LOG_OBJECT (self, "pushing newsegment event with %" GST_SEGMENT_FORMAT,
         &self->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_pad_push_event (self->srcpad, gst_event_new_segment (&self->segment));
     self->need_segment = FALSE;
   }
 
@@ -1541,46 +1564,35 @@ gst_sub_parse_sink_event (GstPad * pad, GstEvent * event)
        * if the file does not have an empty line at the end */
       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_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] = '\n';
-        GST_BUFFER_DATA (buf)[2] = '\0';        /* play it safe */
-        GST_BUFFER_SIZE (buf) = 2;
+        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_event_parse_new_segment (event, &update, &rate, &format, &start,
-          &stop, &time);
-
-      GST_DEBUG_OBJECT (self, "newsegment (%s)", gst_format_get_name (format));
-
-      if (format == GST_FORMAT_TIME) {
-        gst_segment_set_newsegment (&self->segment, update, rate, format,
-            start, stop, time);
-      } else {
-        /* 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) */
-      }
-
+      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;
@@ -1633,13 +1645,13 @@ gst_sub_parse_change_state (GstElement * element, GstStateChange transition)
       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:
@@ -1674,6 +1686,10 @@ static GstStaticCaps smi_caps = GST_STATIC_CAPS ("application/x-subtitle-sami");
 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)
 {
@@ -1726,12 +1742,7 @@ gst_subparse_type_find (GstTypeFind * tf, gpointer private)
       }
     }
     converted_str = gst_convert_to_utf8 (str, 128, enc, &tmp, &err);
-    if (converted_str == NULL) {
-      GST_DEBUG ("Charset conversion failed: %s", err->message);
-      g_error_free (err);
-      g_free (str);
-      return;
-    } else {
+    if (converted_str != NULL) {
       g_free (str);
       str = converted_str;
     }
@@ -1779,6 +1790,10 @@ gst_subparse_type_find (GstTypeFind * tf, gpointer private)
       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");
@@ -1792,14 +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", "txt",
-    "dks", 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",