1 /* GStreamer SSA subtitle parser
2 * Copyright (c) 2006 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* Super-primitive SSA parser - we just want the text and ignore
21 * everything else like styles and timing codes etc. for now */
27 #include <stdlib.h> /* atoi() */
30 #include "gstssaparse.h"
32 GST_DEBUG_CATEGORY_STATIC (ssa_parse_debug);
33 #define GST_CAT_DEFAULT ssa_parse_debug
35 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
38 GST_STATIC_CAPS ("application/x-ssa; application/x-ass")
41 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("text/x-pango-markup")
47 GST_BOILERPLATE (GstSsaParse, gst_ssa_parse, GstElement, GST_TYPE_ELEMENT);
49 static GstStateChangeReturn gst_ssa_parse_change_state (GstElement *
50 element, GstStateChange transition);
51 static gboolean gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps);
52 static gboolean gst_ssa_parse_src_event (GstPad * pad, GstEvent * event);
53 static gboolean gst_ssa_parse_sink_event (GstPad * pad, GstEvent * event);
54 static GstFlowReturn gst_ssa_parse_chain (GstPad * sinkpad, GstBuffer * buf);
57 gst_ssa_parse_base_init (gpointer klass)
59 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
61 gst_element_class_add_static_pad_template (element_class, &sink_templ);
62 gst_element_class_add_static_pad_template (element_class, &src_templ);
63 gst_element_class_set_details_simple (element_class,
64 "SSA Subtitle Parser", "Codec/Parser/Subtitle",
65 "Parses SSA subtitle streams",
66 "Tim-Philipp Müller <tim centricular net>");
68 GST_DEBUG_CATEGORY_INIT (ssa_parse_debug, "ssaparse", 0,
69 "SSA subtitle parser");
73 gst_ssa_parse_dispose (GObject * object)
75 GstSsaParse *parse = GST_SSA_PARSE (object);
80 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
84 gst_ssa_parse_init (GstSsaParse * parse, GstSsaParseClass * klass)
86 parse->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
87 gst_pad_set_setcaps_function (parse->sinkpad,
88 GST_DEBUG_FUNCPTR (gst_ssa_parse_setcaps));
89 gst_pad_set_chain_function (parse->sinkpad,
90 GST_DEBUG_FUNCPTR (gst_ssa_parse_chain));
91 gst_pad_set_event_function (parse->sinkpad,
92 GST_DEBUG_FUNCPTR (gst_ssa_parse_sink_event));
93 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
95 parse->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
96 gst_pad_set_event_function (parse->srcpad,
97 GST_DEBUG_FUNCPTR (gst_ssa_parse_src_event));
98 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
99 gst_pad_use_fixed_caps (parse->srcpad);
100 gst_pad_set_caps (parse->srcpad,
101 gst_static_pad_template_get_caps (&src_templ));
104 parse->framed = FALSE;
105 parse->send_tags = FALSE;
109 gst_ssa_parse_class_init (GstSsaParseClass * klass)
111 GObjectClass *object_class = G_OBJECT_CLASS (klass);
112 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
114 object_class->dispose = gst_ssa_parse_dispose;
116 element_class->change_state = GST_DEBUG_FUNCPTR (gst_ssa_parse_change_state);
120 gst_ssa_parse_src_event (GstPad * pad, GstEvent * event)
122 return gst_pad_event_default (pad, event);
126 gst_ssa_parse_sink_event (GstPad * pad, GstEvent * event)
128 return gst_pad_event_default (pad, event);
132 gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps)
134 GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
137 const guchar bom_utf8[] = { 0xEF, 0xBB, 0xBF };
142 s = gst_caps_get_structure (caps, 0);
143 val = gst_structure_get_value (s, "codec_data");
145 parse->framed = FALSE;
146 GST_ERROR ("Only SSA subtitles embedded in containers are supported");
150 parse->framed = TRUE;
151 parse->send_tags = TRUE;
153 priv = (GstBuffer *) gst_value_get_mini_object (val);
154 g_return_val_if_fail (priv != NULL, FALSE);
156 gst_buffer_ref (priv);
158 data = (gchar *) GST_BUFFER_DATA (priv);
159 size = GST_BUFFER_SIZE (priv);
161 if (size >= 3 && memcmp (data, bom_utf8, 3) == 0) {
166 if (!strstr (data, "[Script Info]")) {
167 GST_WARNING_OBJECT (parse, "Invalid Init section - no Script Info header");
168 gst_buffer_unref (priv);
172 if (!g_utf8_validate (data, size, NULL)) {
173 GST_WARNING_OBJECT (parse, "Init section is not valid UTF-8");
174 gst_buffer_unref (priv);
178 /* FIXME: parse initial section */
179 parse->ini = g_strndup (data, size);
180 GST_LOG_OBJECT (parse, "Init section:\n%s", parse->ini);
182 gst_buffer_unref (priv);
188 gst_ssa_parse_remove_override_codes (GstSsaParse * parse, gchar * txt)
191 gboolean removed_any = FALSE;
193 while ((t = strchr (txt, '{'))) {
194 end = strchr (txt, '}');
196 GST_WARNING_OBJECT (parse, "Missing { for style override code");
199 /* move terminating NUL character forward as well */
200 g_memmove (t, end + 1, strlen (end + 1) + 1);
204 /* these may occur outside of curly brackets. We don't handle the different
205 * wrapping modes yet, so just remove these markers from the text for now */
206 while ((t = strstr (txt, "\\n"))) {
210 while ((t = strstr (txt, "\\N"))) {
214 while ((t = strstr (txt, "\\h"))) {
223 * gst_ssa_parse_push_line:
224 * @parse: caller element
226 * @start: timestamp for the buffer
227 * @duration: duration for the buffer
229 * Parse the text in a buffer with the given properties and
230 * push it to the srcpad of the @parse element
232 * Returns: result of the push of the created buffer
235 gst_ssa_parse_push_line (GstSsaParse * parse, gchar * txt,
236 GstClockTime start, GstClockTime duration)
244 GST_LOG_OBJECT (parse, "Parsing line #%d at %" GST_TIME_FORMAT,
245 num, GST_TIME_ARGS (start));
247 /* skip all non-text fields before the actual text */
249 for (i = 0; i < 8; ++i) {
252 return GST_FLOW_ERROR;
256 GST_LOG_OBJECT (parse, "Text : %s", t);
258 if (gst_ssa_parse_remove_override_codes (parse, t)) {
259 GST_LOG_OBJECT (parse, "Clean: %s", t);
262 /* we claim to output pango markup, so we must escape the
263 * text even if we don't actually use any pango markup yet */
264 escaped = g_markup_printf_escaped ("%s", t);
266 len = strlen (escaped);
268 /* allocate enough for a terminating NUL, but don't include it in buf size */
269 buf = gst_buffer_new_and_alloc (len + 1);
270 memcpy (GST_BUFFER_DATA (buf), escaped, len + 1);
271 GST_BUFFER_SIZE (buf) = len;
274 GST_BUFFER_TIMESTAMP (buf) = start;
275 GST_BUFFER_DURATION (buf) = duration;
277 gst_buffer_set_caps (buf, GST_PAD_CAPS (parse->srcpad));
279 GST_LOG_OBJECT (parse, "Pushing buffer with timestamp %" GST_TIME_FORMAT
280 " and duration %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
281 GST_TIME_ARGS (duration));
283 ret = gst_pad_push (parse->srcpad, buf);
285 if (ret != GST_FLOW_OK) {
286 GST_DEBUG_OBJECT (parse, "Push of text '%s' returned flow %s", txt,
287 gst_flow_get_name (ret));
294 gst_ssa_parse_chain (GstPad * sinkpad, GstBuffer * buf)
297 GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
301 if (G_UNLIKELY (!parse->framed))
304 if (G_UNLIKELY (parse->send_tags)) {
307 tags = gst_tag_list_new ();
308 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
309 "SubStation Alpha", NULL);
310 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, tags);
311 parse->send_tags = FALSE;
314 /* make double-sure it's 0-terminated and all */
315 txt = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
319 ts = GST_BUFFER_TIMESTAMP (buf);
320 ret = gst_ssa_parse_push_line (parse, txt, ts, GST_BUFFER_DURATION (buf));
322 if (ret != GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (ts)) {
323 /* just advance time without sending anything */
324 gst_pad_push_event (parse->srcpad,
325 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, ts, -1, ts));
329 gst_buffer_unref (buf);
337 GST_ELEMENT_ERROR (parse, STREAM, FORMAT, (NULL),
338 ("Only SSA subtitles embedded in containers are supported"));
339 gst_buffer_unref (buf);
340 return GST_FLOW_NOT_NEGOTIATED;
344 GST_ELEMENT_WARNING (parse, STREAM, FORMAT, (NULL),
345 ("Received empty subtitle"));
346 gst_buffer_unref (buf);
351 static GstStateChangeReturn
352 gst_ssa_parse_change_state (GstElement * element, GstStateChange transition)
354 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
355 GstSsaParse *parse = GST_SSA_PARSE (element);
357 switch (transition) {
358 case GST_STATE_CHANGE_READY_TO_PAUSED:
364 ret = parent_class->change_state (element, transition);
365 if (ret == GST_STATE_CHANGE_FAILURE)
368 switch (transition) {
369 case GST_STATE_CHANGE_PAUSED_TO_READY:
372 parse->framed = FALSE;