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