2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (c) 2004 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include <sys/types.h>
30 #include "gstsubparse.h"
31 #include "gstssaparse.h"
32 #include "samiparse.h"
34 GST_DEBUG_CATEGORY_STATIC (sub_parse_debug);
35 #define GST_CAT_DEFAULT sub_parse_debug
37 #define DEFAULT_ENCODING NULL
46 gst_sub_parse_set_property (GObject * object, guint prop_id,
47 const GValue * value, GParamSpec * pspec);
49 gst_sub_parse_get_property (GObject * object, guint prop_id,
50 GValue * value, GParamSpec * pspec);
53 static const GstElementDetails sub_parse_details =
54 GST_ELEMENT_DETAILS ("Subtitle parser",
55 "Codec/Parser/Subtitle",
56 "Parses subtitle (.sub) files into text streams",
57 "Gustavo J. A. M. Carneiro <gjc@inescporto.pt>\n"
58 "Ronald S. Bultje <rbultje@ronald.bitfreak.net>");
60 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
61 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-sami")
67 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
70 GST_STATIC_CAPS ("application/x-subtitle")
74 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS ("text/plain; text/x-pango-markup")
80 static void gst_sub_parse_base_init (GstSubParseClass * klass);
81 static void gst_sub_parse_class_init (GstSubParseClass * klass);
82 static void gst_sub_parse_init (GstSubParse * subparse);
84 static gboolean gst_sub_parse_src_event (GstPad * pad, GstEvent * event);
85 static gboolean gst_sub_parse_sink_event (GstPad * pad, GstEvent * event);
87 static GstStateChangeReturn gst_sub_parse_change_state (GstElement * element,
88 GstStateChange transition);
90 static GstFlowReturn gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf);
92 static GstElementClass *parent_class = NULL;
95 gst_sub_parse_get_type (void)
97 static GType sub_parse_type = 0;
99 if (!sub_parse_type) {
100 static const GTypeInfo sub_parse_info = {
101 sizeof (GstSubParseClass),
102 (GBaseInitFunc) gst_sub_parse_base_init,
104 (GClassInitFunc) gst_sub_parse_class_init,
107 sizeof (GstSubParse),
109 (GInstanceInitFunc) gst_sub_parse_init,
112 sub_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
113 "GstSubParse", &sub_parse_info, 0);
116 return sub_parse_type;
120 gst_sub_parse_base_init (GstSubParseClass * klass)
122 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
124 gst_element_class_add_pad_template (element_class,
125 gst_static_pad_template_get (&sink_templ));
126 gst_element_class_add_pad_template (element_class,
127 gst_static_pad_template_get (&src_templ));
128 gst_element_class_set_details (element_class, &sub_parse_details);
132 gst_sub_parse_dispose (GObject * object)
134 GstSubParse *subparse = GST_SUBPARSE (object);
136 GST_DEBUG_OBJECT (subparse, "cleaning up subtitle parser");
138 if (subparse->segment) {
139 gst_segment_free (subparse->segment);
140 subparse->segment = NULL;
142 if (subparse->encoding) {
143 g_free (subparse->encoding);
144 subparse->encoding = NULL;
146 if (subparse->textbuf) {
147 g_string_free (subparse->textbuf, TRUE);
148 subparse->textbuf = NULL;
150 sami_context_deinit (&subparse->state);
152 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
156 gst_sub_parse_class_init (GstSubParseClass * klass)
158 GObjectClass *object_class = G_OBJECT_CLASS (klass);
159 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
161 parent_class = g_type_class_peek_parent (klass);
163 object_class->dispose = gst_sub_parse_dispose;
164 object_class->set_property = gst_sub_parse_set_property;
165 object_class->get_property = gst_sub_parse_get_property;
167 element_class->change_state = gst_sub_parse_change_state;
169 g_object_class_install_property (object_class, PROP_ENCODING,
170 g_param_spec_string ("subtitle-encoding", "subtitle charset encoding",
171 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
172 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
173 "be checked for an encoding to use. If that is not set either, "
174 "ISO-8859-15 will be assumed.", DEFAULT_ENCODING, G_PARAM_READWRITE));
178 gst_sub_parse_init (GstSubParse * subparse)
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);
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);
192 subparse->textbuf = g_string_new (NULL);
193 subparse->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
194 subparse->flushing = FALSE;
195 subparse->segment = gst_segment_new ();
196 if (subparse->segment) {
197 gst_segment_init (subparse->segment, GST_FORMAT_TIME);
198 subparse->need_segment = TRUE;
200 GST_WARNING_OBJECT (subparse, "segment creation failed");
201 g_assert_not_reached ();
203 subparse->encoding = g_strdup (DEFAULT_ENCODING);
207 * Source pad functions.
211 gst_sub_parse_src_event (GstPad * pad, GstEvent * event)
213 GstSubParse *self = GST_SUBPARSE (gst_pad_get_parent (pad));
214 gboolean ret = FALSE;
216 GST_DEBUG ("Handling %s event", GST_EVENT_TYPE_NAME (event));
218 switch (GST_EVENT_TYPE (event)) {
222 GstSeekType start_type, stop_type;
227 gst_event_parse_seek (event, &rate, &format, &self->segment_flags,
228 &start_type, &start, &stop_type, &stop);
230 if (format != GST_FORMAT_TIME) {
231 GST_WARNING_OBJECT (self, "we only support seeking in TIME format");
232 gst_event_unref (event);
236 /* Convert that seek to a seeking in bytes at position 0,
237 FIXME: could use an index */
238 ret = gst_pad_push_event (self->sinkpad,
239 gst_event_new_seek (rate, GST_FORMAT_BYTES, self->segment_flags,
240 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, 0));
243 /* Apply the seek to our segment */
244 gst_segment_set_seek (self->segment, rate, format, self->segment_flags,
245 start_type, start, stop_type, stop, &update);
247 GST_DEBUG_OBJECT (self, "segment configured from %" GST_TIME_FORMAT
248 " to %" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT,
249 GST_TIME_ARGS (self->segment->start),
250 GST_TIME_ARGS (self->segment->stop),
251 GST_TIME_ARGS (self->segment->last_stop));
253 self->next_offset = 0;
255 self->need_segment = TRUE;
257 GST_WARNING_OBJECT (self, "seek to 0 bytes failed");
260 gst_event_unref (event);
264 ret = gst_pad_event_default (pad, event);
269 gst_object_unref (self);
275 gst_sub_parse_set_property (GObject * object, guint prop_id,
276 const GValue * value, GParamSpec * pspec)
278 GstSubParse *subparse = GST_SUBPARSE (object);
280 GST_OBJECT_LOCK (subparse);
283 g_free (subparse->encoding);
284 subparse->encoding = g_value_dup_string (value);
285 GST_LOG_OBJECT (object, "subtitle encoding set to %s",
286 GST_STR_NULL (subparse->encoding));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 GST_OBJECT_UNLOCK (subparse);
296 gst_sub_parse_get_property (GObject * object, guint prop_id,
297 GValue * value, GParamSpec * pspec)
299 GstSubParse *subparse = GST_SUBPARSE (object);
301 GST_OBJECT_LOCK (subparse);
304 g_value_set_string (value, subparse->encoding);
307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 GST_OBJECT_UNLOCK (subparse);
314 convert_encoding (GstSubParse * self, const gchar * str, gsize len)
316 const gchar *encoding;
320 if (self->valid_utf8) {
321 if (g_utf8_validate (str, len, NULL)) {
322 GST_LOG_OBJECT (self, "valid UTF-8, no conversion needed");
323 return g_strndup (str, len);
325 GST_INFO_OBJECT (self, "invalid UTF-8!");
326 self->valid_utf8 = FALSE;
329 encoding = self->encoding;
330 if (encoding == NULL || *encoding == '\0') {
331 encoding = g_getenv ("GST_SUBTITLE_ENCODING");
333 if (encoding == NULL || *encoding == '\0') {
334 /* if local encoding is UTF-8 and no encoding specified
335 * via the environment variable, assume ISO-8859-15 */
336 if (g_get_charset (&encoding)) {
337 encoding = "ISO-8859-15";
341 ret = g_convert_with_fallback (str, len, "UTF-8", encoding, "*", NULL,
345 GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
346 encoding, err->message);
349 /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
350 ret = g_convert_with_fallback (str, len, "UTF-8", "ISO-8859-15", "*",
354 GST_LOG_OBJECT (self, "successfully converted %d characters from %s to UTF-8"
355 "%s", len, encoding, (err) ? " , using ISO-8859-15 as fallback" : "");
361 get_next_line (GstSubParse * self)
364 const char *line_end;
366 gboolean have_r = FALSE;
368 line_end = strchr (self->textbuf->str, '\n');
371 /* end-of-line not found; return for more data */
375 /* get rid of '\r' */
376 if (line_end != self->textbuf->str && *(line_end - 1) == '\r') {
381 line_len = line_end - self->textbuf->str;
382 line = convert_encoding (self, self->textbuf->str, line_len);
383 self->textbuf = g_string_erase (self->textbuf, 0,
384 line_len + (have_r ? 2 : 1));
389 parse_mdvdsub (ParserState * state, const gchar * line)
391 const gchar *line_split;
393 guint start_frame, end_frame;
394 gint64 clip_start = 0, clip_stop = 0;
395 gboolean in_seg = FALSE;
397 /* FIXME: hardcoded for now, but detecting the correct value is
398 * not going to be easy, I suspect... */
399 const double frames_per_sec = 24000 / 1001.;
403 /* style variables */
408 if (sscanf (line, "{%u}{%u}", &start_frame, &end_frame) != 2) {
409 g_warning ("Parse of the following line, assumed to be in microdvd .sub"
410 " format, failed:\n%s", line);
414 state->start_time = (start_frame - 1000) / frames_per_sec * GST_SECOND;
415 state->duration = (end_frame - start_frame) / frames_per_sec * GST_SECOND;
417 /* Check our segment start/stop */
418 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
419 state->start_time, state->start_time + state->duration, &clip_start,
422 /* No need to parse that text if it's out of segment */
424 state->start_time = clip_start;
425 state->duration = clip_stop - clip_start;
430 /* skip the {%u}{%u} part */
431 line = strchr (line, '}') + 1;
432 line = strchr (line, '}') + 1;
434 markup = g_string_new (NULL);
439 /* parse style markup */
440 if (strncmp (line, "{y:i}", 5) == 0) {
442 line = strchr (line, '}') + 1;
444 if (strncmp (line, "{y:b}", 5) == 0) {
446 line = strchr (line, '}') + 1;
448 if (sscanf (line, "{s:%u}", &fontsize) == 1) {
449 line = strchr (line, '}') + 1;
451 if ((line_split = strchr (line, '|')))
452 line_chunk = g_markup_escape_text (line, line_split - line);
454 line_chunk = g_markup_escape_text (line, strlen (line));
455 markup = g_string_append (markup, "<span");
457 g_string_append (markup, " style=\"italic\"");
459 g_string_append (markup, " weight=\"bold\"");
461 g_string_append_printf (markup, " size=\"%u\"", fontsize * 1000);
462 g_string_append_printf (markup, ">%s</span>", line_chunk);
465 g_string_append (markup, "\n");
466 line = line_split + 1;
472 g_string_free (markup, FALSE);
473 GST_DEBUG ("parse_mdvdsub returning (%f+%f): %s",
474 state->start_time / (double) GST_SECOND,
475 state->duration / (double) GST_SECOND, ret);
479 /* we want to escape text in general, but retain basic markup like
480 * <i></i>, <u></u>, and <b></b>. The easiest and safest way is to
481 * just unescape a white list of allowed markups again after
482 * escaping everything (the text between these simple markers isn't
483 * necessarily escaped, so it seems best to do it like this) */
485 subrip_unescape_formatting (gchar * txt)
489 for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
490 if (g_ascii_strncasecmp (pos, "<u>", 9) == 0 ||
491 g_ascii_strncasecmp (pos, "<i>", 9) == 0 ||
492 g_ascii_strncasecmp (pos, "<b>", 9) == 0) {
494 pos[1] = g_ascii_tolower (pos[4]);
496 /* move NUL terminator as well */
497 g_memmove (pos + 3, pos + 9, strlen (pos + 9) + 1);
502 for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
503 if (g_ascii_strncasecmp (pos, "</u>", 10) == 0 ||
504 g_ascii_strncasecmp (pos, "</i>", 10) == 0 ||
505 g_ascii_strncasecmp (pos, "</b>", 10) == 0) {
508 pos[2] = g_ascii_tolower (pos[5]);
510 /* move NUL terminator as well */
511 g_memmove (pos + 4, pos + 10, strlen (pos + 10) + 1);
518 parse_subrip (ParserState * state, const gchar * line)
520 guint h1, m1, s1, ms1;
521 guint h2, m2, s2, ms2;
525 switch (state->state) {
527 /* looking for a single integer */
528 if (sscanf (line, "%u", &subnum) == 1)
532 /* looking for start_time --> end_time */
533 if (sscanf (line, "%u:%u:%u,%u --> %u:%u:%u,%u",
534 &h1, &m1, &s1, &ms1, &h2, &m2, &s2, &ms2) == 8) {
537 (((guint64) h1) * 3600 + m1 * 60 + s1) * GST_SECOND +
540 (((guint64) h2) * 3600 + m2 * 60 + s2) * GST_SECOND +
541 ms2 * GST_MSECOND - state->start_time;
543 GST_DEBUG ("error parsing subrip time line");
548 { /* No need to parse that text if it's out of segment */
549 gint64 clip_start = 0, clip_stop = 0;
550 gboolean in_seg = FALSE;
552 /* Check our segment start/stop */
553 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
554 state->start_time, state->start_time + state->duration,
555 &clip_start, &clip_stop);
558 state->start_time = clip_start;
559 state->duration = clip_stop - clip_start;
565 /* looking for subtitle text; empty line ends this
568 g_string_append_c (state->buf, '\n');
569 g_string_append (state->buf, line);
570 if (strlen (line) == 0) {
571 ret = g_markup_escape_text (state->buf->str, state->buf->len);
572 g_string_truncate (state->buf, 0);
574 subrip_unescape_formatting (ret);
579 g_return_val_if_reached (NULL);
584 parse_mpsub (ParserState * state, const gchar * line)
589 switch (state->state) {
591 /* looking for two floats (offset, duration) */
592 if (sscanf (line, "%f %f", &t1, &t2) == 2) {
594 state->start_time += state->duration + GST_SECOND * t1;
595 state->duration = GST_SECOND * t2;
599 { /* No need to parse that text if it's out of segment */
600 gint64 clip_start = 0, clip_stop = 0;
601 gboolean in_seg = FALSE;
603 /* Check our segment start/stop */
604 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
605 state->start_time, state->start_time + state->duration,
606 &clip_start, &clip_stop);
609 state->start_time = clip_start;
610 state->duration = clip_stop - clip_start;
616 /* looking for subtitle text; empty line ends this
619 g_string_append_c (state->buf, '\n');
620 g_string_append (state->buf, line);
621 if (strlen (line) == 0) {
622 ret = g_strdup (state->buf->str);
623 g_string_truncate (state->buf, 0);
629 g_assert_not_reached ();
635 parser_state_init (ParserState * state)
637 GST_DEBUG ("initialising parser");
640 g_string_truncate (state->buf, 0);
642 state->buf = g_string_new (NULL);
645 state->start_time = 0;
648 state->segment = NULL;
652 parser_state_dispose (ParserState * state)
655 g_string_free (state->buf, TRUE);
658 if (state->user_data) {
659 sami_context_reset (state);
664 * FIXME: maybe we should pass along a second argument, the preceding
665 * text buffer, because that is how this originally worked, even though
666 * I don't really see the use of that.
669 static GstSubParseFormat
670 gst_sub_parse_data_format_autodetect (gchar * match_str)
672 static gboolean need_init_regexps = TRUE;
673 static regex_t mdvd_rx;
674 static regex_t subrip_rx;
676 /* initialize the regexps used the first time around */
677 if (need_init_regexps) {
681 need_init_regexps = FALSE;
682 if ((err = regcomp (&mdvd_rx, "^\\{[0-9]+\\}\\{[0-9]+\\}",
683 REG_EXTENDED | REG_NEWLINE | REG_NOSUB) != 0) ||
684 (err = regcomp (&subrip_rx, "^[1-9]([0-9]){0,3}(\x0d)?\x0a"
685 "[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{3}"
686 " --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{3}",
687 REG_EXTENDED | REG_NEWLINE | REG_NOSUB)) != 0) {
688 regerror (err, &subrip_rx, errstr, 127);
689 GST_WARNING ("Compilation of subrip regex failed: %s", errstr);
693 if (regexec (&mdvd_rx, match_str, 0, NULL, 0) == 0) {
694 GST_LOG ("MicroDVD (frame based) format detected");
695 return GST_SUB_PARSE_FORMAT_MDVDSUB;
697 if (regexec (&subrip_rx, match_str, 0, NULL, 0) == 0) {
698 GST_LOG ("SubRip (time based) format detected");
699 return GST_SUB_PARSE_FORMAT_SUBRIP;
701 if (!strncmp (match_str, "FORMAT=TIME", 11)) {
702 GST_LOG ("MPSub (time based) format detected");
703 return GST_SUB_PARSE_FORMAT_MPSUB;
705 if (strstr (match_str, "<SAMI>") != NULL ||
706 strstr (match_str, "<sami>") != NULL) {
707 GST_LOG ("SAMI (time based) format detected");
708 return GST_SUB_PARSE_FORMAT_SAMI;
711 GST_DEBUG ("no subtitle format detected");
712 return GST_SUB_PARSE_FORMAT_UNKNOWN;
716 gst_sub_parse_format_autodetect (GstSubParse * self)
719 GstSubParseFormat format;
721 if (strlen (self->textbuf->str) < 35) {
722 GST_DEBUG ("File too small to be a subtitles file");
726 data = g_strndup (self->textbuf->str, 35);
727 format = gst_sub_parse_data_format_autodetect (data);
730 self->parser_type = format;
731 parser_state_init (&self->state);
734 case GST_SUB_PARSE_FORMAT_MDVDSUB:
735 self->parse_line = parse_mdvdsub;
736 return gst_caps_new_simple ("text/x-pango-markup", NULL);
737 case GST_SUB_PARSE_FORMAT_SUBRIP:
738 self->parse_line = parse_subrip;
739 return gst_caps_new_simple ("text/x-pango-markup", NULL);
740 case GST_SUB_PARSE_FORMAT_MPSUB:
741 self->parse_line = parse_mpsub;
742 return gst_caps_new_simple ("text/plain", NULL);
743 case GST_SUB_PARSE_FORMAT_SAMI:
744 self->parse_line = parse_sami;
745 sami_context_init (&self->state);
746 return gst_caps_new_simple ("text/x-pango-markup", NULL);
747 case GST_SUB_PARSE_FORMAT_UNKNOWN:
749 GST_DEBUG ("no subtitle format detected");
750 GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE,
751 ("The input is not a valid/supported subtitle file"), (NULL));
757 feed_textbuf (GstSubParse * self, GstBuffer * buf)
759 if (GST_BUFFER_OFFSET (buf) != self->offset) {
760 /* flush the parser state */
761 parser_state_init (&self->state);
762 g_string_truncate (self->textbuf, 0);
763 sami_context_reset (&self->state);
766 self->textbuf = g_string_append_len (self->textbuf,
767 (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
768 self->offset = GST_BUFFER_OFFSET (buf) + GST_BUFFER_SIZE (buf);
769 self->next_offset = self->offset;
771 gst_buffer_unref (buf);
775 handle_buffer (GstSubParse * self, GstBuffer * buf)
777 GstFlowReturn ret = GST_FLOW_OK;
778 GstCaps *caps = NULL;
779 gchar *line, *subtitle;
781 feed_textbuf (self, buf);
783 /* make sure we know the format */
784 if (G_UNLIKELY (self->parser_type == GST_SUB_PARSE_FORMAT_UNKNOWN)) {
785 if (!(caps = gst_sub_parse_format_autodetect (self))) {
786 return GST_FLOW_UNEXPECTED;
788 if (!gst_pad_set_caps (self->srcpad, caps)) {
789 gst_caps_unref (caps);
790 return GST_FLOW_UNEXPECTED;
792 gst_caps_unref (caps);
795 while ((line = get_next_line (self)) && !self->flushing) {
796 /* Set segment on our parser state machine */
797 self->state.segment = self->segment;
798 /* Now parse the line, out of segment lines will just return NULL */
799 GST_DEBUG ("Parsing line '%s'", line);
800 subtitle = self->parse_line (&self->state, line);
804 guint subtitle_len = strlen (subtitle);
806 ret = gst_pad_alloc_buffer_and_set_caps (self->srcpad,
807 GST_BUFFER_OFFSET_NONE, subtitle_len,
808 GST_PAD_CAPS (self->srcpad), &buf);
810 if (ret == GST_FLOW_OK) {
811 memcpy (GST_BUFFER_DATA (buf), subtitle, subtitle_len);
812 GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
813 GST_BUFFER_DURATION (buf) = self->state.duration;
815 gst_segment_set_last_stop (self->segment, GST_FORMAT_TIME,
816 self->state.start_time);
818 GST_DEBUG ("Sending text '%s', %" GST_TIME_FORMAT " + %"
819 GST_TIME_FORMAT, subtitle, GST_TIME_ARGS (self->state.start_time),
820 GST_TIME_ARGS (self->state.duration));
822 ret = gst_pad_push (self->srcpad, buf);
828 if (GST_FLOW_IS_FATAL (ret))
837 gst_sub_parse_chain (GstPad * sinkpad, GstBuffer * buf)
842 GST_DEBUG ("gst_sub_parse_chain");
843 self = GST_SUBPARSE (gst_pad_get_parent (sinkpad));
845 /* Push newsegment if needed */
846 if (self->need_segment) {
847 gst_pad_push_event (self->srcpad, gst_event_new_new_segment (FALSE,
848 self->segment->rate, self->segment->format,
849 self->segment->last_stop, self->segment->stop,
850 self->segment->time));
851 self->need_segment = FALSE;
854 ret = handle_buffer (self, buf);
856 gst_object_unref (self);
862 gst_sub_parse_sink_event (GstPad * pad, GstEvent * event)
864 GstSubParse *self = GST_SUBPARSE (gst_pad_get_parent (pad));
865 gboolean ret = FALSE;
867 GST_DEBUG ("Handling %s event", GST_EVENT_TYPE_NAME (event));
869 switch (GST_EVENT_TYPE (event)) {
871 /* Make sure the last subrip chunk is pushed out even
872 * if the file does not have an empty line at the end */
873 if (self->parser_type == GST_SUB_PARSE_FORMAT_SUBRIP) {
874 GstBuffer *buf = gst_buffer_new_and_alloc (1 + 1);
876 GST_DEBUG ("EOS. Pushing remaining text (if any)");
877 GST_BUFFER_DATA (buf)[0] = '\n';
878 GST_BUFFER_DATA (buf)[1] = '\0'; /* play it safe */
879 GST_BUFFER_SIZE (buf) = 1;
880 GST_BUFFER_OFFSET (buf) = self->offset;
881 gst_sub_parse_chain (pad, buf);
883 ret = gst_pad_event_default (pad, event);
886 case GST_EVENT_NEWSEGMENT:
890 gint64 start, stop, time;
893 GST_DEBUG_OBJECT (self, "received new segment");
895 gst_event_parse_new_segment (event, &update, &rate, &format, &start,
898 /* now copy over the values */
899 gst_segment_set_newsegment (self->segment, update, rate, format,
903 gst_event_unref (event);
906 case GST_EVENT_FLUSH_START:
908 self->flushing = TRUE;
910 ret = gst_pad_event_default (pad, event);
913 case GST_EVENT_FLUSH_STOP:
915 self->flushing = FALSE;
917 ret = gst_pad_event_default (pad, event);
921 ret = gst_pad_event_default (pad, event);
925 gst_object_unref (self);
931 static GstStateChangeReturn
932 gst_sub_parse_change_state (GstElement * element, GstStateChange transition)
934 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
935 GstSubParse *self = GST_SUBPARSE (element);
937 switch (transition) {
938 case GST_STATE_CHANGE_READY_TO_PAUSED:
939 /* format detection will init the parser state */
941 self->next_offset = 0;
942 self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
943 self->valid_utf8 = TRUE;
944 g_string_truncate (self->textbuf, 0);
950 ret = parent_class->change_state (element, transition);
951 if (ret == GST_STATE_CHANGE_FAILURE)
954 switch (transition) {
955 case GST_STATE_CHANGE_PAUSED_TO_READY:
956 parser_state_dispose (&self->state);
957 self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
970 static GstStaticCaps smi_caps = GST_STATIC_CAPS ("application/x-subtitle-sami");
971 static GstStaticCaps sub_caps = GST_STATIC_CAPS ("application/x-subtitle");
973 #define SUB_CAPS (gst_static_caps_get (&sub_caps))
974 #define SAMI_CAPS (gst_static_caps_get (&smi_caps))
977 gst_subparse_type_find (GstTypeFind * tf, gpointer private)
979 GstSubParseFormat format;
984 if (!(data = gst_type_find_peek (tf, 0, 36)))
987 /* make sure string passed to _autodetect() is NUL-terminated */
988 str = g_strndup ((gchar *) data, 35);
989 format = gst_sub_parse_data_format_autodetect (str);
993 case GST_SUB_PARSE_FORMAT_MDVDSUB:
994 GST_DEBUG ("MicroDVD format detected");
997 case GST_SUB_PARSE_FORMAT_SUBRIP:
998 GST_DEBUG ("SubRip format detected");
1001 case GST_SUB_PARSE_FORMAT_MPSUB:
1002 GST_DEBUG ("MPSub format detected");
1005 case GST_SUB_PARSE_FORMAT_SAMI:
1006 GST_DEBUG ("SAMI (time-based) format detected");
1010 case GST_SUB_PARSE_FORMAT_UNKNOWN:
1011 GST_DEBUG ("no subtitle format detected");
1015 /* if we're here, it's ok */
1016 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, caps);
1020 plugin_init (GstPlugin * plugin)
1022 static gchar *sub_exts[] = { "srt", "sub", "mpsub", "mdvd", "smi", NULL };
1024 GST_DEBUG_CATEGORY_INIT (sub_parse_debug, "subparse", 0, ".sub parser");
1026 if (!gst_type_find_register (plugin, "subparse_typefind", GST_RANK_MARGINAL,
1027 gst_subparse_type_find, sub_exts, SUB_CAPS, NULL, NULL))
1030 if (!gst_element_register (plugin, "subparse",
1031 GST_RANK_PRIMARY, GST_TYPE_SUBPARSE) ||
1032 !gst_element_register (plugin, "ssaparse",
1033 GST_RANK_PRIMARY, GST_TYPE_SSA_PARSE)) {
1040 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1044 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)