Use G_PARAM_STATIC_STRINGS everywhere for GParamSpecs that use static strings (i...
[platform/upstream/gstreamer.git] / gst / subparse / gstsubparse.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2004 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <regex.h>
30
31 #include "gstsubparse.h"
32 #include "gstssaparse.h"
33 #include "samiparse.h"
34 #include "tmplayerparse.h"
35 #include "mpl2parse.h"
36
37 GST_DEBUG_CATEGORY (sub_parse_debug);
38
39 #define DEFAULT_ENCODING   NULL
40
41 enum
42 {
43   PROP_0,
44   PROP_ENCODING
45 };
46
47 static void
48 gst_sub_parse_set_property (GObject * object, guint prop_id,
49     const GValue * value, GParamSpec * pspec);
50 static void
51 gst_sub_parse_get_property (GObject * object, guint prop_id,
52     GValue * value, GParamSpec * pspec);
53
54
55 static const GstElementDetails sub_parse_details =
56 GST_ELEMENT_DETAILS ("Subtitle parser",
57     "Codec/Parser/Subtitle",
58     "Parses subtitle (.sub) files into text streams",
59     "Gustavo J. A. M. Carneiro <gjc@inescporto.pt>\n"
60     "Ronald S. Bultje <rbultje@ronald.bitfreak.net>");
61
62 #ifndef GST_DISABLE_XML
63 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
64     GST_PAD_SINK,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-sami; "
67         "application/x-subtitle-tmplayer; application/x-subtitle-mpl2")
68     );
69 #else
70 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("application/x-subtitle")
74     );
75 #endif
76
77 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
78     GST_PAD_SRC,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS ("text/plain; text/x-pango-markup")
81     );
82
83 static void gst_sub_parse_base_init (GstSubParseClass * klass);
84 static void gst_sub_parse_class_init (GstSubParseClass * klass);
85 static void gst_sub_parse_init (GstSubParse * subparse);
86
87 static gboolean gst_sub_parse_src_event (GstPad * pad, GstEvent * event);
88 static gboolean gst_sub_parse_sink_event (GstPad * pad, GstEvent * event);
89
90 static GstStateChangeReturn gst_sub_parse_change_state (GstElement * element,
91     GstStateChange transition);
92
93 static GstFlowReturn gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf);
94
95 static GstElementClass *parent_class = NULL;
96
97 GType
98 gst_sub_parse_get_type (void)
99 {
100   static GType sub_parse_type = 0;
101
102   if (!sub_parse_type) {
103     static const GTypeInfo sub_parse_info = {
104       sizeof (GstSubParseClass),
105       (GBaseInitFunc) gst_sub_parse_base_init,
106       NULL,
107       (GClassInitFunc) gst_sub_parse_class_init,
108       NULL,
109       NULL,
110       sizeof (GstSubParse),
111       0,
112       (GInstanceInitFunc) gst_sub_parse_init,
113     };
114
115     sub_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
116         "GstSubParse", &sub_parse_info, 0);
117   }
118
119   return sub_parse_type;
120 }
121
122 static void
123 gst_sub_parse_base_init (GstSubParseClass * klass)
124 {
125   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
126
127   gst_element_class_add_pad_template (element_class,
128       gst_static_pad_template_get (&sink_templ));
129   gst_element_class_add_pad_template (element_class,
130       gst_static_pad_template_get (&src_templ));
131   gst_element_class_set_details (element_class, &sub_parse_details);
132 }
133
134 static void
135 gst_sub_parse_dispose (GObject * object)
136 {
137   GstSubParse *subparse = GST_SUBPARSE (object);
138
139   GST_DEBUG_OBJECT (subparse, "cleaning up subtitle parser");
140
141   if (subparse->encoding) {
142     g_free (subparse->encoding);
143     subparse->encoding = NULL;
144   }
145   if (subparse->textbuf) {
146     g_string_free (subparse->textbuf, TRUE);
147     subparse->textbuf = NULL;
148   }
149   sami_context_deinit (&subparse->state);
150
151   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
152 }
153
154 static void
155 gst_sub_parse_class_init (GstSubParseClass * klass)
156 {
157   GObjectClass *object_class = G_OBJECT_CLASS (klass);
158   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
159
160   parent_class = g_type_class_peek_parent (klass);
161
162   object_class->dispose = gst_sub_parse_dispose;
163   object_class->set_property = gst_sub_parse_set_property;
164   object_class->get_property = gst_sub_parse_get_property;
165
166   element_class->change_state = gst_sub_parse_change_state;
167
168   g_object_class_install_property (object_class, PROP_ENCODING,
169       g_param_spec_string ("subtitle-encoding", "subtitle charset encoding",
170           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
171           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
172           "be checked for an encoding to use. If that is not set either, "
173           "ISO-8859-15 will be assumed.", DEFAULT_ENCODING,
174           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 }
176
177 static void
178 gst_sub_parse_init (GstSubParse * subparse)
179 {
180   subparse->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
181   gst_pad_set_chain_function (subparse->sinkpad,
182       GST_DEBUG_FUNCPTR (gst_sub_parse_chain));
183   gst_pad_set_event_function (subparse->sinkpad,
184       GST_DEBUG_FUNCPTR (gst_sub_parse_sink_event));
185   gst_element_add_pad (GST_ELEMENT (subparse), subparse->sinkpad);
186
187   subparse->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
188   gst_pad_set_event_function (subparse->srcpad,
189       GST_DEBUG_FUNCPTR (gst_sub_parse_src_event));
190   gst_element_add_pad (GST_ELEMENT (subparse), subparse->srcpad);
191
192   subparse->textbuf = g_string_new (NULL);
193   subparse->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
194   subparse->flushing = FALSE;
195   gst_segment_init (&subparse->segment, GST_FORMAT_TIME);
196   subparse->need_segment = TRUE;
197   subparse->encoding = g_strdup (DEFAULT_ENCODING);
198 }
199
200 /*
201  * Source pad functions.
202  */
203
204 static gboolean
205 gst_sub_parse_src_event (GstPad * pad, GstEvent * event)
206 {
207   GstSubParse *self = GST_SUBPARSE (gst_pad_get_parent (pad));
208   gboolean ret = FALSE;
209
210   GST_DEBUG ("Handling %s event", GST_EVENT_TYPE_NAME (event));
211
212   switch (GST_EVENT_TYPE (event)) {
213     case GST_EVENT_SEEK:
214     {
215       GstFormat format;
216       GstSeekType start_type, stop_type;
217       gint64 start, stop;
218       gdouble rate;
219       gboolean update;
220
221       gst_event_parse_seek (event, &rate, &format, &self->segment_flags,
222           &start_type, &start, &stop_type, &stop);
223
224       if (format != GST_FORMAT_TIME) {
225         GST_WARNING_OBJECT (self, "we only support seeking in TIME format");
226         gst_event_unref (event);
227         goto beach;
228       }
229
230       /* Convert that seek to a seeking in bytes at position 0,
231          FIXME: could use an index */
232       ret = gst_pad_push_event (self->sinkpad,
233           gst_event_new_seek (rate, GST_FORMAT_BYTES, self->segment_flags,
234               GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, 0));
235
236       if (ret) {
237         /* Apply the seek to our segment */
238         gst_segment_set_seek (&self->segment, rate, format, self->segment_flags,
239             start_type, start, stop_type, stop, &update);
240
241         GST_DEBUG_OBJECT (self, "segment after seek: %" GST_SEGMENT_FORMAT,
242             &self->segment);
243
244         self->next_offset = 0;
245
246         self->need_segment = TRUE;
247       } else {
248         GST_WARNING_OBJECT (self, "seek to 0 bytes failed");
249       }
250
251       gst_event_unref (event);
252       break;
253     }
254     default:
255       ret = gst_pad_event_default (pad, event);
256       break;
257   }
258
259 beach:
260   gst_object_unref (self);
261
262   return ret;
263 }
264
265 static void
266 gst_sub_parse_set_property (GObject * object, guint prop_id,
267     const GValue * value, GParamSpec * pspec)
268 {
269   GstSubParse *subparse = GST_SUBPARSE (object);
270
271   GST_OBJECT_LOCK (subparse);
272   switch (prop_id) {
273     case PROP_ENCODING:
274       g_free (subparse->encoding);
275       subparse->encoding = g_value_dup_string (value);
276       GST_LOG_OBJECT (object, "subtitle encoding set to %s",
277           GST_STR_NULL (subparse->encoding));
278       break;
279     default:
280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281       break;
282   }
283   GST_OBJECT_UNLOCK (subparse);
284 }
285
286 static void
287 gst_sub_parse_get_property (GObject * object, guint prop_id,
288     GValue * value, GParamSpec * pspec)
289 {
290   GstSubParse *subparse = GST_SUBPARSE (object);
291
292   GST_OBJECT_LOCK (subparse);
293   switch (prop_id) {
294     case PROP_ENCODING:
295       g_value_set_string (value, subparse->encoding);
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300   }
301   GST_OBJECT_UNLOCK (subparse);
302 }
303
304 static gchar *
305 convert_encoding (GstSubParse * self, const gchar * str, gsize len)
306 {
307   const gchar *encoding;
308   GError *err = NULL;
309   gchar *ret;
310
311   if (self->valid_utf8) {
312     if (g_utf8_validate (str, len, NULL)) {
313       GST_LOG_OBJECT (self, "valid UTF-8, no conversion needed");
314       return g_strndup (str, len);
315     }
316     GST_INFO_OBJECT (self, "invalid UTF-8!");
317     self->valid_utf8 = FALSE;
318   }
319
320   encoding = self->encoding;
321   if (encoding == NULL || *encoding == '\0') {
322     encoding = g_getenv ("GST_SUBTITLE_ENCODING");
323   }
324   if (encoding == NULL || *encoding == '\0') {
325     /* if local encoding is UTF-8 and no encoding specified
326      * via the environment variable, assume ISO-8859-15 */
327     if (g_get_charset (&encoding)) {
328       encoding = "ISO-8859-15";
329     }
330   }
331
332   ret = g_convert_with_fallback (str, len, "UTF-8", encoding, "*", NULL,
333       NULL, &err);
334
335   if (err) {
336     GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
337         encoding, err->message);
338     g_error_free (err);
339
340     /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
341     ret = g_convert_with_fallback (str, len, "UTF-8", "ISO-8859-15", "*",
342         NULL, NULL, NULL);
343   }
344
345   GST_LOG_OBJECT (self,
346       "successfully converted %" G_GSIZE_FORMAT " characters from %s to UTF-8"
347       "%s", len, encoding, (err) ? " , using ISO-8859-15 as fallback" : "");
348
349   return ret;
350 }
351
352 static gchar *
353 get_next_line (GstSubParse * self)
354 {
355   char *line = NULL;
356   const char *line_end;
357   int line_len;
358   gboolean have_r = FALSE;
359
360   line_end = strchr (self->textbuf->str, '\n');
361
362   if (!line_end) {
363     /* end-of-line not found; return for more data */
364     return NULL;
365   }
366
367   /* get rid of '\r' */
368   if (line_end != self->textbuf->str && *(line_end - 1) == '\r') {
369     line_end--;
370     have_r = TRUE;
371   }
372
373   line_len = line_end - self->textbuf->str;
374   line = convert_encoding (self, self->textbuf->str, line_len);
375   self->textbuf = g_string_erase (self->textbuf, 0,
376       line_len + (have_r ? 2 : 1));
377   return line;
378 }
379
380 static gchar *
381 parse_mdvdsub (ParserState * state, const gchar * line)
382 {
383   const gchar *line_split;
384   gchar *line_chunk;
385   guint start_frame, end_frame;
386   gint64 clip_start = 0, clip_stop = 0;
387   gboolean in_seg = FALSE;
388   GString *markup;
389   gchar *ret;
390
391   /* style variables */
392   gboolean italic;
393   gboolean bold;
394   guint fontsize;
395
396   if (sscanf (line, "{%u}{%u}", &start_frame, &end_frame) != 2) {
397     g_warning ("Parse of the following line, assumed to be in microdvd .sub"
398         " format, failed:\n%s", line);
399     return NULL;
400   }
401
402   /* skip the {%u}{%u} part */
403   line = strchr (line, '}') + 1;
404   line = strchr (line, '}') + 1;
405
406   /* see if there's a first line with a framerate */
407   if (state->fps == 0.0 && start_frame == 1 && end_frame == 1) {
408     gchar *rest, *end = NULL;
409
410     rest = g_strdup (line);
411     g_strdelimit (rest, ",", '.');
412     state->fps = g_ascii_strtod (rest, &end);
413     if (end == rest)
414       state->fps = 0.0;
415     GST_INFO ("framerate from file: %f ('%s')", state->fps, rest);
416     g_free (rest);
417     return NULL;
418   }
419
420   if (state->fps == 0.0) {
421     /* FIXME: hardcoded for now, is there a better way/assumption? */
422     state->fps = 24000.0 / 1001.0;
423     GST_INFO ("no framerate specified, assuming %f", state->fps);
424   }
425
426   state->start_time = start_frame / state->fps * GST_SECOND;
427   state->duration = (end_frame - start_frame) / state->fps * GST_SECOND;
428
429   /* Check our segment start/stop */
430   in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
431       state->start_time, state->start_time + state->duration, &clip_start,
432       &clip_stop);
433
434   /* No need to parse that text if it's out of segment */
435   if (in_seg) {
436     state->start_time = clip_start;
437     state->duration = clip_stop - clip_start;
438   } else {
439     return NULL;
440   }
441
442   markup = g_string_new (NULL);
443   while (1) {
444     italic = FALSE;
445     bold = FALSE;
446     fontsize = 0;
447     /* parse style markup */
448     if (strncmp (line, "{y:i}", 5) == 0) {
449       italic = TRUE;
450       line = strchr (line, '}') + 1;
451     }
452     if (strncmp (line, "{y:b}", 5) == 0) {
453       bold = TRUE;
454       line = strchr (line, '}') + 1;
455     }
456     if (sscanf (line, "{s:%u}", &fontsize) == 1) {
457       line = strchr (line, '}') + 1;
458     }
459     /* forward slashes at beginning/end signify italics too */
460     if (g_str_has_prefix (line, "/")) {
461       italic = TRUE;
462       ++line;
463     }
464     if ((line_split = strchr (line, '|')))
465       line_chunk = g_markup_escape_text (line, line_split - line);
466     else
467       line_chunk = g_markup_escape_text (line, strlen (line));
468
469     /* Remove italics markers at end of line/stanza (CHECKME: are end slashes
470      * always at the end of a line or can they span multiple lines?) */
471     if (g_str_has_suffix (line_chunk, "/")) {
472       line_chunk[strlen (line_chunk) - 1] = '\0';
473     }
474
475     markup = g_string_append (markup, "<span");
476     if (italic)
477       g_string_append (markup, " style=\"italic\"");
478     if (bold)
479       g_string_append (markup, " weight=\"bold\"");
480     if (fontsize)
481       g_string_append_printf (markup, " size=\"%u\"", fontsize * 1000);
482     g_string_append_printf (markup, ">%s</span>", line_chunk);
483     g_free (line_chunk);
484     if (line_split) {
485       g_string_append (markup, "\n");
486       line = line_split + 1;
487     } else {
488       break;
489     }
490   }
491   ret = markup->str;
492   g_string_free (markup, FALSE);
493   GST_DEBUG ("parse_mdvdsub returning (%f+%f): %s",
494       state->start_time / (double) GST_SECOND,
495       state->duration / (double) GST_SECOND, ret);
496   return ret;
497 }
498
499 static void
500 strip_trailing_newlines (gchar * txt)
501 {
502   if (txt) {
503     guint len;
504
505     len = strlen (txt);
506     while (len > 1 && txt[len - 1] == '\n') {
507       txt[len - 1] = '\0';
508       --len;
509     }
510   }
511 }
512
513 /* we want to escape text in general, but retain basic markup like
514  * <i></i>, <u></u>, and <b></b>. The easiest and safest way is to
515  * just unescape a white list of allowed markups again after
516  * escaping everything (the text between these simple markers isn't
517  * necessarily escaped, so it seems best to do it like this) */
518 static void
519 subrip_unescape_formatting (gchar * txt)
520 {
521   gchar *pos;
522
523   for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
524     if (g_ascii_strncasecmp (pos, "&lt;u&gt;", 9) == 0 ||
525         g_ascii_strncasecmp (pos, "&lt;i&gt;", 9) == 0 ||
526         g_ascii_strncasecmp (pos, "&lt;b&gt;", 9) == 0) {
527       pos[0] = '<';
528       pos[1] = g_ascii_tolower (pos[4]);
529       pos[2] = '>';
530       /* move NUL terminator as well */
531       g_memmove (pos + 3, pos + 9, strlen (pos + 9) + 1);
532       pos += 2;
533     }
534   }
535
536   for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
537     if (g_ascii_strncasecmp (pos, "&lt;/u&gt;", 10) == 0 ||
538         g_ascii_strncasecmp (pos, "&lt;/i&gt;", 10) == 0 ||
539         g_ascii_strncasecmp (pos, "&lt;/b&gt;", 10) == 0) {
540       pos[0] = '<';
541       pos[1] = '/';
542       pos[2] = g_ascii_tolower (pos[5]);
543       pos[3] = '>';
544       /* move NUL terminator as well */
545       g_memmove (pos + 4, pos + 10, strlen (pos + 10) + 1);
546       pos += 3;
547     }
548   }
549 }
550
551
552 static gboolean
553 subrip_remove_unhandled_tag (gchar * start, gchar * stop)
554 {
555   gchar *tag, saved;
556
557   tag = start + strlen ("&lt;");
558   if (*tag == '/')
559     ++tag;
560
561   if (g_ascii_tolower (*tag) < 'a' || g_ascii_tolower (*tag) > 'z')
562     return FALSE;
563
564   saved = *stop;
565   *stop = '\0';
566   GST_LOG ("removing unhandled tag '%s'", start);
567   *stop = saved;
568   g_memmove (start, stop, strlen (stop) + 1);
569   return TRUE;
570 }
571
572 /* remove tags we haven't explicitly allowed earlier on, like font tags
573  * for example */
574 static void
575 subrip_remove_unhandled_tags (gchar * txt)
576 {
577   gchar *pos, *gt;
578
579   for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
580     if (strncmp (pos, "&lt;", 4) == 0 && (gt = strstr (pos + 4, "&gt;"))) {
581       if (subrip_remove_unhandled_tag (pos, gt + strlen ("&gt;")))
582         --pos;
583     }
584   }
585 }
586
587 /* we only allow <i>, <u> and <b>, so let's take a simple approach. This code
588  * assumes the input has been escaped and subrip_unescape_formatting() has then
589  * been run over the input! This function adds missing closing markup tags and
590  * removes broken closing tags for tags that have never been opened. */
591 static void
592 subrip_fix_up_markup (gchar ** p_txt)
593 {
594   gchar *cur, *next_tag;
595   gchar open_tags[32];
596   guint num_open_tags = 0;
597
598   g_assert (*p_txt != NULL);
599
600   cur = *p_txt;
601   while (*cur != '\0') {
602     next_tag = strchr (cur, '<');
603     if (next_tag == NULL)
604       break;
605     ++next_tag;
606     switch (*next_tag) {
607       case '/':{
608         ++next_tag;
609         if (num_open_tags == 0 || open_tags[num_open_tags - 1] != *next_tag) {
610           GST_LOG ("broken input, closing tag '%c' is not open", *next_tag);
611           g_memmove (next_tag - 2, next_tag + 2, strlen (next_tag + 2) + 1);
612           next_tag -= 2;
613         } else {
614           /* it's all good, closing tag which is open */
615           --num_open_tags;
616         }
617         break;
618       }
619       case 'i':
620       case 'b':
621       case 'u':
622         if (num_open_tags == G_N_ELEMENTS (open_tags))
623           return;               /* something dodgy is going on, stop parsing */
624         open_tags[num_open_tags] = *next_tag;
625         ++num_open_tags;
626         break;
627       default:
628         GST_ERROR ("unexpected tag '%c' (%s)", *next_tag, next_tag);
629         g_assert_not_reached ();
630         break;
631     }
632     cur = next_tag;
633   }
634
635   if (num_open_tags > 0) {
636     GString *s;
637
638     s = g_string_new (*p_txt);
639     while (num_open_tags > 0) {
640       GST_LOG ("adding missing closing tag '%c'", open_tags[num_open_tags - 1]);
641       g_string_append_c (s, '<');
642       g_string_append_c (s, '/');
643       g_string_append_c (s, open_tags[num_open_tags - 1]);
644       g_string_append_c (s, '>');
645       --num_open_tags;
646     }
647     g_free (*p_txt);
648     *p_txt = g_string_free (s, FALSE);
649   }
650 }
651
652 static gchar *
653 parse_subrip (ParserState * state, const gchar * line)
654 {
655   guint h1, m1, s1, ms1;
656   guint h2, m2, s2, ms2;
657   int subnum;
658   gchar *ret;
659
660   switch (state->state) {
661     case 0:
662       /* looking for a single integer */
663       if (sscanf (line, "%u", &subnum) == 1)
664         state->state = 1;
665       return NULL;
666     case 1:
667       /* looking for start_time --> end_time */
668       if (sscanf (line, "%u:%u:%u,%u --> %u:%u:%u,%u",
669               &h1, &m1, &s1, &ms1, &h2, &m2, &s2, &ms2) == 8) {
670         state->state = 2;
671         state->start_time =
672             (((guint64) h1) * 3600 + m1 * 60 + s1) * GST_SECOND +
673             ms1 * GST_MSECOND;
674         state->duration =
675             (((guint64) h2) * 3600 + m2 * 60 + s2) * GST_SECOND +
676             ms2 * GST_MSECOND - state->start_time;
677       } else {
678         GST_DEBUG ("error parsing subrip time line");
679         state->state = 0;
680       }
681       return NULL;
682     case 2:
683     {
684       /* No need to parse that text if it's out of segment */
685       gint64 clip_start = 0, clip_stop = 0;
686       gboolean in_seg = FALSE;
687
688       /* Check our segment start/stop */
689       in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
690           state->start_time, state->start_time + state->duration,
691           &clip_start, &clip_stop);
692
693       if (in_seg) {
694         state->start_time = clip_start;
695         state->duration = clip_stop - clip_start;
696       } else {
697         state->state = 0;
698         return NULL;
699       }
700     }
701       /* looking for subtitle text; empty line ends this subtitle entry */
702       if (state->buf->len)
703         g_string_append_c (state->buf, '\n');
704       g_string_append (state->buf, line);
705       if (strlen (line) == 0) {
706         ret = g_markup_escape_text (state->buf->str, state->buf->len);
707         g_string_truncate (state->buf, 0);
708         state->state = 0;
709         subrip_unescape_formatting (ret);
710         subrip_remove_unhandled_tags (ret);
711         strip_trailing_newlines (ret);
712         subrip_fix_up_markup (&ret);
713         return ret;
714       }
715       return NULL;
716     default:
717       g_return_val_if_reached (NULL);
718   }
719 }
720
721 static void
722 subviewer_unescape_newlines (gchar * read)
723 {
724   gchar *write = read;
725
726   /* Replace all occurences of '[br]' with a newline as version 2
727    * of the subviewer format uses this for newlines */
728
729   if (read[0] == '\0' || read[1] == '\0' || read[2] == '\0' || read[3] == '\0')
730     return;
731
732   do {
733     if (strncmp (read, "[br]", 4) == 0) {
734       *write = '\n';
735       read += 4;
736     } else {
737       *write = *read;
738       read++;
739     }
740     write++;
741   } while (*read);
742
743   *write = '\0';
744 }
745
746 static gchar *
747 parse_subviewer (ParserState * state, const gchar * line)
748 {
749   guint h1, m1, s1, ms1;
750   guint h2, m2, s2, ms2;
751   gchar *ret;
752
753   /* TODO: Maybe also parse the fields in the header, especially DELAY.
754    * For examples see the unit test or
755    * http://www.doom9.org/index.html?/sub.htm */
756
757   switch (state->state) {
758     case 0:
759       /* looking for start_time,end_time */
760       if (sscanf (line, "%u:%u:%u.%u,%u:%u:%u.%u",
761               &h1, &m1, &s1, &ms1, &h2, &m2, &s2, &ms2) == 8) {
762         state->state = 1;
763         state->start_time =
764             (((guint64) h1) * 3600 + m1 * 60 + s1) * GST_SECOND +
765             ms1 * GST_MSECOND;
766         state->duration =
767             (((guint64) h2) * 3600 + m2 * 60 + s2) * GST_SECOND +
768             ms2 * GST_MSECOND - state->start_time;
769       }
770       return NULL;
771     case 1:
772     {
773       /* No need to parse that text if it's out of segment */
774       gint64 clip_start = 0, clip_stop = 0;
775       gboolean in_seg = FALSE;
776
777       /* Check our segment start/stop */
778       in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
779           state->start_time, state->start_time + state->duration,
780           &clip_start, &clip_stop);
781
782       if (in_seg) {
783         state->start_time = clip_start;
784         state->duration = clip_stop - clip_start;
785       } else {
786         state->state = 0;
787         return NULL;
788       }
789     }
790       /* looking for subtitle text; empty line ends this subtitle entry */
791       if (state->buf->len)
792         g_string_append_c (state->buf, '\n');
793       g_string_append (state->buf, line);
794       if (strlen (line) == 0) {
795         ret = g_strdup (state->buf->str);
796         subviewer_unescape_newlines (ret);
797         strip_trailing_newlines (ret);
798         g_string_truncate (state->buf, 0);
799         state->state = 0;
800         return ret;
801       }
802       return NULL;
803     default:
804       g_assert_not_reached ();
805       return NULL;
806   }
807 }
808
809 static gchar *
810 parse_mpsub (ParserState * state, const gchar * line)
811 {
812   gchar *ret;
813   float t1, t2;
814
815   switch (state->state) {
816     case 0:
817       /* looking for two floats (offset, duration) */
818       if (sscanf (line, "%f %f", &t1, &t2) == 2) {
819         state->state = 1;
820         state->start_time += state->duration + GST_SECOND * t1;
821         state->duration = GST_SECOND * t2;
822       }
823       return NULL;
824     case 1:
825     {                           /* No need to parse that text if it's out of segment */
826       gint64 clip_start = 0, clip_stop = 0;
827       gboolean in_seg = FALSE;
828
829       /* Check our segment start/stop */
830       in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
831           state->start_time, state->start_time + state->duration,
832           &clip_start, &clip_stop);
833
834       if (in_seg) {
835         state->start_time = clip_start;
836         state->duration = clip_stop - clip_start;
837       } else {
838         state->state = 0;
839         return NULL;
840       }
841     }
842       /* looking for subtitle text; empty line ends this
843        * subtitle entry */
844       if (state->buf->len)
845         g_string_append_c (state->buf, '\n');
846       g_string_append (state->buf, line);
847       if (strlen (line) == 0) {
848         ret = g_strdup (state->buf->str);
849         g_string_truncate (state->buf, 0);
850         state->state = 0;
851         return ret;
852       }
853       return NULL;
854     default:
855       g_assert_not_reached ();
856       return NULL;
857   }
858 }
859
860 static void
861 parser_state_init (ParserState * state)
862 {
863   GST_DEBUG ("initialising parser");
864
865   if (state->buf) {
866     g_string_truncate (state->buf, 0);
867   } else {
868     state->buf = g_string_new (NULL);
869   }
870
871   state->start_time = 0;
872   state->duration = 0;
873   state->state = 0;
874   state->segment = NULL;
875 }
876
877 static void
878 parser_state_dispose (ParserState * state)
879 {
880   if (state->buf) {
881     g_string_free (state->buf, TRUE);
882     state->buf = NULL;
883   }
884   if (state->user_data) {
885     sami_context_reset (state);
886   }
887 }
888
889 /*
890  * FIXME: maybe we should pass along a second argument, the preceding
891  * text buffer, because that is how this originally worked, even though
892  * I don't really see the use of that.
893  */
894
895 static GstSubParseFormat
896 gst_sub_parse_data_format_autodetect (gchar * match_str)
897 {
898   static gboolean need_init_regexps = TRUE;
899   static regex_t mdvd_rx;
900   static regex_t subrip_rx;
901   guint n1, n2, n3;
902
903   /* initialize the regexps used the first time around */
904   if (need_init_regexps) {
905     int err;
906     char errstr[128];
907
908     need_init_regexps = FALSE;
909     if ((err = regcomp (&mdvd_rx, "^\\{[0-9]+\\}\\{[0-9]+\\}",
910                 REG_EXTENDED | REG_NEWLINE | REG_NOSUB) != 0) ||
911         (err = regcomp (&subrip_rx, "^[0-9]([0-9]){0,3}(\x0d)?\x0a"
912                 "[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{3}"
913                 " --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{3}",
914                 REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) != 0) {
915       regerror (err, &subrip_rx, errstr, 127);
916       GST_WARNING ("Compilation of subrip regex failed: %s", errstr);
917     }
918   }
919
920   if (regexec (&mdvd_rx, match_str, 0, NULL, 0) == 0) {
921     GST_LOG ("MicroDVD (frame based) format detected");
922     return GST_SUB_PARSE_FORMAT_MDVDSUB;
923   }
924   if (regexec (&subrip_rx, match_str, 0, NULL, 0) == 0) {
925     GST_LOG ("SubRip (time based) format detected");
926     return GST_SUB_PARSE_FORMAT_SUBRIP;
927   }
928
929   if (!strncmp (match_str, "FORMAT=TIME", 11)) {
930     GST_LOG ("MPSub (time based) format detected");
931     return GST_SUB_PARSE_FORMAT_MPSUB;
932   }
933   if (strstr (match_str, "<SAMI>") != NULL ||
934       strstr (match_str, "<sami>") != NULL) {
935     GST_LOG ("SAMI (time based) format detected");
936     return GST_SUB_PARSE_FORMAT_SAMI;
937   }
938   /* we're boldly assuming the first subtitle appears within the first hour */
939   if (sscanf (match_str, "0:%02u:%02u:", &n1, &n2) == 2 ||
940       sscanf (match_str, "0:%02u:%02u=", &n1, &n2) == 2 ||
941       sscanf (match_str, "00:%02u:%02u:", &n1, &n2) == 2 ||
942       sscanf (match_str, "00:%02u:%02u=", &n1, &n2) == 2 ||
943       sscanf (match_str, "00:%02u:%02u,%u=", &n1, &n2, &n3) == 3) {
944     GST_LOG ("TMPlayer (time based) format detected");
945     return GST_SUB_PARSE_FORMAT_TMPLAYER;
946   }
947   if (sscanf (match_str, "[%u][%u]", &n1, &n2) == 2) {
948     GST_LOG ("MPL2 (time based) format detected");
949     return GST_SUB_PARSE_FORMAT_MPL2;
950   }
951   if (strstr (match_str, "[INFORMATION]") != NULL) {
952     GST_LOG ("SubViewer (time based) format detected");
953     return GST_SUB_PARSE_FORMAT_SUBVIEWER;
954   }
955
956   GST_DEBUG ("no subtitle format detected");
957   return GST_SUB_PARSE_FORMAT_UNKNOWN;
958 }
959
960 static GstCaps *
961 gst_sub_parse_format_autodetect (GstSubParse * self)
962 {
963   gchar *data;
964   GstSubParseFormat format;
965
966   if (strlen (self->textbuf->str) < 35) {
967     GST_DEBUG ("File too small to be a subtitles file");
968     return NULL;
969   }
970
971   data = g_strndup (self->textbuf->str, 35);
972   format = gst_sub_parse_data_format_autodetect (data);
973   g_free (data);
974
975   self->parser_type = format;
976   parser_state_init (&self->state);
977
978   switch (format) {
979     case GST_SUB_PARSE_FORMAT_MDVDSUB:
980       self->parse_line = parse_mdvdsub;
981       return gst_caps_new_simple ("text/x-pango-markup", NULL);
982     case GST_SUB_PARSE_FORMAT_SUBRIP:
983       self->parse_line = parse_subrip;
984       return gst_caps_new_simple ("text/x-pango-markup", NULL);
985     case GST_SUB_PARSE_FORMAT_MPSUB:
986       self->parse_line = parse_mpsub;
987       return gst_caps_new_simple ("text/plain", NULL);
988     case GST_SUB_PARSE_FORMAT_SAMI:
989       self->parse_line = parse_sami;
990       sami_context_init (&self->state);
991       return gst_caps_new_simple ("text/x-pango-markup", NULL);
992     case GST_SUB_PARSE_FORMAT_TMPLAYER:
993       self->parse_line = parse_tmplayer;
994       return gst_caps_new_simple ("text/plain", NULL);
995     case GST_SUB_PARSE_FORMAT_MPL2:
996       self->parse_line = parse_mpl2;
997       return gst_caps_new_simple ("text/x-pango-markup", NULL);
998     case GST_SUB_PARSE_FORMAT_SUBVIEWER:
999       self->parse_line = parse_subviewer;
1000       return gst_caps_new_simple ("text/plain", NULL);
1001     case GST_SUB_PARSE_FORMAT_UNKNOWN:
1002     default:
1003       GST_DEBUG ("no subtitle format detected");
1004       GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE,
1005           ("The input is not a valid/supported subtitle file"), (NULL));
1006       return NULL;
1007   }
1008 }
1009
1010 static void
1011 feed_textbuf (GstSubParse * self, GstBuffer * buf)
1012 {
1013   if (GST_BUFFER_OFFSET (buf) != self->offset) {
1014     /* flush the parser state */
1015     parser_state_init (&self->state);
1016     g_string_truncate (self->textbuf, 0);
1017     sami_context_reset (&self->state);
1018   }
1019
1020   self->textbuf = g_string_append_len (self->textbuf,
1021       (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1022   self->offset = GST_BUFFER_OFFSET (buf) + GST_BUFFER_SIZE (buf);
1023   self->next_offset = self->offset;
1024
1025   gst_buffer_unref (buf);
1026 }
1027
1028 static GstFlowReturn
1029 handle_buffer (GstSubParse * self, GstBuffer * buf)
1030 {
1031   GstFlowReturn ret = GST_FLOW_OK;
1032   GstCaps *caps = NULL;
1033   gchar *line, *subtitle;
1034
1035   feed_textbuf (self, buf);
1036
1037   /* make sure we know the format */
1038   if (G_UNLIKELY (self->parser_type == GST_SUB_PARSE_FORMAT_UNKNOWN)) {
1039     if (!(caps = gst_sub_parse_format_autodetect (self))) {
1040       return GST_FLOW_UNEXPECTED;
1041     }
1042     if (!gst_pad_set_caps (self->srcpad, caps)) {
1043       gst_caps_unref (caps);
1044       return GST_FLOW_UNEXPECTED;
1045     }
1046     gst_caps_unref (caps);
1047   }
1048
1049   while ((line = get_next_line (self)) && !self->flushing) {
1050     /* Set segment on our parser state machine */
1051     self->state.segment = &self->segment;
1052     /* Now parse the line, out of segment lines will just return NULL */
1053     GST_LOG_OBJECT (self, "Parsing line '%s'", line);
1054     subtitle = self->parse_line (&self->state, line);
1055     g_free (line);
1056
1057     if (subtitle) {
1058       guint subtitle_len = strlen (subtitle);
1059
1060       /* +1 for terminating NUL character */
1061       ret = gst_pad_alloc_buffer_and_set_caps (self->srcpad,
1062           GST_BUFFER_OFFSET_NONE, subtitle_len + 1,
1063           GST_PAD_CAPS (self->srcpad), &buf);
1064
1065       if (ret == GST_FLOW_OK) {
1066         /* copy terminating NUL character as well */
1067         memcpy (GST_BUFFER_DATA (buf), subtitle, subtitle_len + 1);
1068         GST_BUFFER_SIZE (buf) = subtitle_len;
1069         GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
1070         GST_BUFFER_DURATION (buf) = self->state.duration;
1071
1072         gst_segment_set_last_stop (&self->segment, GST_FORMAT_TIME,
1073             self->state.start_time);
1074
1075         GST_DEBUG_OBJECT (self, "Sending text '%s', %" GST_TIME_FORMAT " + %"
1076             GST_TIME_FORMAT, subtitle, GST_TIME_ARGS (self->state.start_time),
1077             GST_TIME_ARGS (self->state.duration));
1078
1079         ret = gst_pad_push (self->srcpad, buf);
1080       }
1081
1082       g_free (subtitle);
1083       subtitle = NULL;
1084
1085       if (ret != GST_FLOW_OK) {
1086         GST_DEBUG_OBJECT (self, "flow: %s", gst_flow_get_name (ret));
1087         break;
1088       }
1089     }
1090   }
1091
1092   return ret;
1093 }
1094
1095 static GstFlowReturn
1096 gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf)
1097 {
1098   GstFlowReturn ret;
1099   GstSubParse *self;
1100
1101   self = GST_SUBPARSE (GST_PAD_PARENT (sinkpad));
1102
1103   /* Push newsegment if needed */
1104   if (self->need_segment) {
1105     GST_LOG_OBJECT (self, "pushing newsegment event with %" GST_SEGMENT_FORMAT,
1106         &self->segment);
1107
1108     gst_pad_push_event (self->srcpad, gst_event_new_new_segment (FALSE,
1109             self->segment.rate, self->segment.format,
1110             self->segment.last_stop, self->segment.stop, self->segment.time));
1111     self->need_segment = FALSE;
1112   }
1113
1114   ret = handle_buffer (self, buf);
1115
1116   return ret;
1117 }
1118
1119 static gboolean
1120 gst_sub_parse_sink_event (GstPad * pad, GstEvent * event)
1121 {
1122   GstSubParse *self = GST_SUBPARSE (gst_pad_get_parent (pad));
1123   gboolean ret = FALSE;
1124
1125   GST_DEBUG ("Handling %s event", GST_EVENT_TYPE_NAME (event));
1126
1127   switch (GST_EVENT_TYPE (event)) {
1128     case GST_EVENT_EOS:{
1129       /* Make sure the last subrip chunk is pushed out even
1130        * if the file does not have an empty line at the end */
1131       if (self->parser_type == GST_SUB_PARSE_FORMAT_SUBRIP ||
1132           self->parser_type == GST_SUB_PARSE_FORMAT_MPL2) {
1133         GstBuffer *buf = gst_buffer_new_and_alloc (1 + 1);
1134
1135         GST_DEBUG ("EOS. Pushing remaining text (if any)");
1136         GST_BUFFER_DATA (buf)[0] = '\n';
1137         GST_BUFFER_DATA (buf)[1] = '\0';        /* play it safe */
1138         GST_BUFFER_SIZE (buf) = 1;
1139         GST_BUFFER_OFFSET (buf) = self->offset;
1140         gst_sub_parse_chain (pad, buf);
1141       }
1142       ret = gst_pad_event_default (pad, event);
1143       break;
1144     }
1145     case GST_EVENT_NEWSEGMENT:
1146     {
1147       GstFormat format;
1148       gdouble rate;
1149       gint64 start, stop, time;
1150       gboolean update;
1151
1152       gst_event_parse_new_segment (event, &update, &rate, &format, &start,
1153           &stop, &time);
1154
1155       GST_DEBUG_OBJECT (self, "newsegment (%s)", gst_format_get_name (format));
1156
1157       if (format == GST_FORMAT_TIME) {
1158         gst_segment_set_newsegment (&self->segment, update, rate, format,
1159             start, stop, time);
1160       } else {
1161         /* if not time format, we'll either start with a 0 timestamp anyway or
1162          * it's following a seek in which case we'll have saved the requested
1163          * seek segment and don't want to overwrite it (remember that on a seek
1164          * we always just seek back to the start in BYTES format and just throw
1165          * away all text that's before the requested position; if the subtitles
1166          * come from an upstream demuxer, it won't be able to handle our BYTES
1167          * seek request and instead send us a newsegment from the seek request
1168          * it received via its video pads instead, so all is fine then too) */
1169       }
1170
1171       ret = TRUE;
1172       gst_event_unref (event);
1173       break;
1174     }
1175     case GST_EVENT_FLUSH_START:
1176     {
1177       self->flushing = TRUE;
1178
1179       ret = gst_pad_event_default (pad, event);
1180       break;
1181     }
1182     case GST_EVENT_FLUSH_STOP:
1183     {
1184       self->flushing = FALSE;
1185
1186       ret = gst_pad_event_default (pad, event);
1187       break;
1188     }
1189     default:
1190       ret = gst_pad_event_default (pad, event);
1191       break;
1192   }
1193
1194   gst_object_unref (self);
1195
1196   return ret;
1197 }
1198
1199
1200 static GstStateChangeReturn
1201 gst_sub_parse_change_state (GstElement * element, GstStateChange transition)
1202 {
1203   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1204   GstSubParse *self = GST_SUBPARSE (element);
1205
1206   switch (transition) {
1207     case GST_STATE_CHANGE_READY_TO_PAUSED:
1208       /* format detection will init the parser state */
1209       self->offset = 0;
1210       self->next_offset = 0;
1211       self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
1212       self->valid_utf8 = TRUE;
1213       g_string_truncate (self->textbuf, 0);
1214       break;
1215     default:
1216       break;
1217   }
1218
1219   ret = parent_class->change_state (element, transition);
1220   if (ret == GST_STATE_CHANGE_FAILURE)
1221     return ret;
1222
1223   switch (transition) {
1224     case GST_STATE_CHANGE_PAUSED_TO_READY:
1225       parser_state_dispose (&self->state);
1226       self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
1227       break;
1228     default:
1229       break;
1230   }
1231
1232   return ret;
1233 }
1234
1235 /*
1236  * Typefind support.
1237  */
1238
1239 /* FIXME 0.11: these caps are ugly, use app/x-subtitle + type field or so;
1240  * also, give different  subtitle formats really different types */
1241 static GstStaticCaps mpl2_caps =
1242 GST_STATIC_CAPS ("application/x-subtitle-mpl2");
1243 static GstStaticCaps tmp_caps =
1244 GST_STATIC_CAPS ("application/x-subtitle-tmplayer");
1245 static GstStaticCaps smi_caps = GST_STATIC_CAPS ("application/x-subtitle-sami");
1246 static GstStaticCaps sub_caps = GST_STATIC_CAPS ("application/x-subtitle");
1247
1248 #define SUB_CAPS (gst_static_caps_get (&sub_caps))
1249 #define SAMI_CAPS (gst_static_caps_get (&smi_caps))
1250 #define TMP_CAPS (gst_static_caps_get (&tmp_caps))
1251 #define MPL2_CAPS (gst_static_caps_get (&mpl2_caps))
1252
1253 static void
1254 gst_subparse_type_find (GstTypeFind * tf, gpointer private)
1255 {
1256   GstSubParseFormat format;
1257   const guint8 *data;
1258   GstCaps *caps;
1259   gchar *str;
1260
1261   if (!(data = gst_type_find_peek (tf, 0, 36)))
1262     return;
1263
1264   /* make sure string passed to _autodetect() is NUL-terminated */
1265   str = g_strndup ((gchar *) data, 35);
1266   format = gst_sub_parse_data_format_autodetect (str);
1267   g_free (str);
1268
1269   switch (format) {
1270     case GST_SUB_PARSE_FORMAT_MDVDSUB:
1271       GST_DEBUG ("MicroDVD format detected");
1272       caps = SUB_CAPS;
1273       break;
1274     case GST_SUB_PARSE_FORMAT_SUBRIP:
1275       GST_DEBUG ("SubRip format detected");
1276       caps = SUB_CAPS;
1277       break;
1278     case GST_SUB_PARSE_FORMAT_MPSUB:
1279       GST_DEBUG ("MPSub format detected");
1280       caps = SUB_CAPS;
1281       break;
1282     case GST_SUB_PARSE_FORMAT_SAMI:
1283       GST_DEBUG ("SAMI (time-based) format detected");
1284       caps = SAMI_CAPS;
1285       break;
1286     case GST_SUB_PARSE_FORMAT_TMPLAYER:
1287       GST_DEBUG ("TMPlayer (time based) format detected");
1288       caps = TMP_CAPS;
1289       break;
1290       /* FIXME: our MPL2 typefinding is not really good enough to warrant
1291        * returning a high probability (however, since we registered our
1292        * typefinder here with a rank of MARGINAL we should pretty much only
1293        * be called if most other typefinders have already run */
1294     case GST_SUB_PARSE_FORMAT_MPL2:
1295       GST_DEBUG ("MPL2 (time based) format detected");
1296       caps = MPL2_CAPS;
1297       break;
1298     case GST_SUB_PARSE_FORMAT_SUBVIEWER:
1299       GST_DEBUG ("SubViewer format detected");
1300       caps = SUB_CAPS;
1301       break;
1302     default:
1303     case GST_SUB_PARSE_FORMAT_UNKNOWN:
1304       GST_DEBUG ("no subtitle format detected");
1305       return;
1306   }
1307
1308   /* if we're here, it's ok */
1309   gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, caps);
1310 }
1311
1312 static gboolean
1313 plugin_init (GstPlugin * plugin)
1314 {
1315   static gchar *sub_exts[] = { "srt", "sub", "mpsub", "mdvd", "smi", "txt",
1316     NULL
1317   };
1318
1319   GST_DEBUG_CATEGORY_INIT (sub_parse_debug, "subparse", 0, ".sub parser");
1320
1321   if (!gst_type_find_register (plugin, "subparse_typefind", GST_RANK_MARGINAL,
1322           gst_subparse_type_find, sub_exts, SUB_CAPS, NULL, NULL))
1323     return FALSE;
1324
1325   if (!gst_element_register (plugin, "subparse",
1326           GST_RANK_PRIMARY, GST_TYPE_SUBPARSE) ||
1327       !gst_element_register (plugin, "ssaparse",
1328           GST_RANK_PRIMARY, GST_TYPE_SSA_PARSE)) {
1329     return FALSE;
1330   }
1331
1332   return TRUE;
1333 }
1334
1335 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1336     GST_VERSION_MINOR,
1337     "subparse",
1338     "Subtitle parsing",
1339     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)