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_pad_template (element_class,
62 gst_static_pad_template_get (&sink_templ));
63 gst_element_class_add_pad_template (element_class,
64 gst_static_pad_template_get (&src_templ));
65 gst_element_class_set_details_simple (element_class,
66 "SSA Subtitle Parser", "Codec/Parser/Subtitle",
67 "Parses SSA subtitle streams",
68 "Tim-Philipp Müller <tim centricular net>");
70 GST_DEBUG_CATEGORY_INIT (ssa_parse_debug, "ssaparse", 0,
71 "SSA subtitle parser");
75 gst_ssa_parse_dispose (GObject * object)
77 GstSsaParse *parse = GST_SSA_PARSE (object);
82 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
86 gst_ssa_parse_init (GstSsaParse * parse, GstSsaParseClass * klass)
88 parse->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
89 gst_pad_set_setcaps_function (parse->sinkpad,
90 GST_DEBUG_FUNCPTR (gst_ssa_parse_setcaps));
91 gst_pad_set_chain_function (parse->sinkpad,
92 GST_DEBUG_FUNCPTR (gst_ssa_parse_chain));
93 gst_pad_set_event_function (parse->sinkpad,
94 GST_DEBUG_FUNCPTR (gst_ssa_parse_sink_event));
95 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
97 parse->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
98 gst_pad_set_event_function (parse->srcpad,
99 GST_DEBUG_FUNCPTR (gst_ssa_parse_src_event));
100 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
101 gst_pad_use_fixed_caps (parse->srcpad);
102 gst_pad_set_caps (parse->srcpad,
103 gst_static_pad_template_get_caps (&src_templ));
106 parse->framed = FALSE;
107 parse->send_tags = FALSE;
111 gst_ssa_parse_class_init (GstSsaParseClass * klass)
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
114 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
116 object_class->dispose = gst_ssa_parse_dispose;
118 element_class->change_state = GST_DEBUG_FUNCPTR (gst_ssa_parse_change_state);
122 gst_ssa_parse_src_event (GstPad * pad, GstEvent * event)
124 return gst_pad_event_default (pad, event);
128 gst_ssa_parse_sink_event (GstPad * pad, GstEvent * event)
130 return gst_pad_event_default (pad, event);
134 gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps)
136 GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
139 const guchar bom_utf8[] = { 0xEF, 0xBB, 0xBF };
144 s = gst_caps_get_structure (caps, 0);
145 val = gst_structure_get_value (s, "codec_data");
147 parse->framed = FALSE;
148 GST_ERROR ("Only SSA subtitles embedded in containers are supported");
152 parse->framed = TRUE;
153 parse->send_tags = TRUE;
155 priv = (GstBuffer *) gst_value_get_mini_object (val);
156 g_return_val_if_fail (priv != NULL, FALSE);
158 gst_buffer_ref (priv);
160 data = (gchar *) GST_BUFFER_DATA (priv);
161 size = GST_BUFFER_SIZE (priv);
163 if (size >= 3 && memcmp (data, bom_utf8, 3) == 0) {
168 if (!strstr (data, "[Script Info]")) {
169 GST_WARNING_OBJECT (parse, "Invalid Init section - no Script Info header");
170 gst_buffer_unref (priv);
174 if (!g_utf8_validate (data, size, NULL)) {
175 GST_WARNING_OBJECT (parse, "Init section is not valid UTF-8");
176 gst_buffer_unref (priv);
180 /* FIXME: parse initial section */
181 parse->ini = g_strndup (data, size);
182 GST_LOG_OBJECT (parse, "Init section:\n%s", parse->ini);
184 gst_buffer_unref (priv);
190 gst_ssa_parse_remove_override_codes (GstSsaParse * parse, gchar * txt)
193 gboolean removed_any = FALSE;
195 while ((t = strchr (txt, '{'))) {
196 end = strchr (txt, '}');
198 GST_WARNING_OBJECT (parse, "Missing { for style override code");
201 /* move terminating NUL character forward as well */
202 g_memmove (t, end + 1, strlen (end + 1) + 1);
206 /* these may occur outside of curly brackets. We don't handle the different
207 * wrapping modes yet, so just remove these markers from the text for now */
208 while ((t = strstr (txt, "\\n"))) {
212 while ((t = strstr (txt, "\\N"))) {
216 while ((t = strstr (txt, "\\h"))) {
225 * gst_ssa_parse_push_line:
226 * @parse: caller element
228 * @start: timestamp for the buffer
229 * @duration: duration for the buffer
231 * Parse the text in a buffer with the given properties and
232 * push it to the srcpad of the @parse element
234 * Returns: result of the push of the created buffer
237 gst_ssa_parse_push_line (GstSsaParse * parse, gchar * txt,
238 GstClockTime start, GstClockTime duration)
246 GST_LOG_OBJECT (parse, "Parsing line #%d at %" GST_TIME_FORMAT,
247 num, GST_TIME_ARGS (start));
249 /* skip all non-text fields before the actual text */
251 for (i = 0; i < 8; ++i) {
254 return GST_FLOW_ERROR;
258 GST_LOG_OBJECT (parse, "Text : %s", t);
260 if (gst_ssa_parse_remove_override_codes (parse, t)) {
261 GST_LOG_OBJECT (parse, "Clean: %s", t);
264 /* we claim to output pango markup, so we must escape the
265 * text even if we don't actually use any pango markup yet */
266 escaped = g_markup_printf_escaped ("%s", t);
268 len = strlen (escaped);
270 /* allocate enough for a terminating NUL, but don't include it in buf size */
271 buf = gst_buffer_new_and_alloc (len + 1);
272 memcpy (GST_BUFFER_DATA (buf), escaped, len + 1);
273 GST_BUFFER_SIZE (buf) = len;
276 GST_BUFFER_TIMESTAMP (buf) = start;
277 GST_BUFFER_DURATION (buf) = duration;
279 gst_buffer_set_caps (buf, GST_PAD_CAPS (parse->srcpad));
281 GST_LOG_OBJECT (parse, "Pushing buffer with timestamp %" GST_TIME_FORMAT
282 " and duration %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
283 GST_TIME_ARGS (duration));
285 ret = gst_pad_push (parse->srcpad, buf);
287 if (ret != GST_FLOW_OK) {
288 GST_DEBUG_OBJECT (parse, "Push of text '%s' returned flow %s", txt,
289 gst_flow_get_name (ret));
296 gst_ssa_parse_chain (GstPad * sinkpad, GstBuffer * buf)
299 GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
303 if (G_UNLIKELY (!parse->framed))
306 if (G_UNLIKELY (parse->send_tags)) {
309 tags = gst_tag_list_new ();
310 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
311 "SubStation Alpha", NULL);
312 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, tags);
313 parse->send_tags = FALSE;
316 /* make double-sure it's 0-terminated and all */
317 txt = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
321 ts = GST_BUFFER_TIMESTAMP (buf);
322 ret = gst_ssa_parse_push_line (parse, txt, ts, GST_BUFFER_DURATION (buf));
324 if (ret != GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (ts)) {
325 /* just advance time without sending anything */
326 gst_pad_push_event (parse->srcpad,
327 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, ts, -1, ts));
331 gst_buffer_unref (buf);
339 GST_ELEMENT_ERROR (parse, STREAM, FORMAT, (NULL),
340 ("Only SSA subtitles embedded in containers are supported"));
341 gst_buffer_unref (buf);
342 return GST_FLOW_NOT_NEGOTIATED;
346 GST_ELEMENT_WARNING (parse, STREAM, FORMAT, (NULL),
347 ("Received empty subtitle"));
348 gst_buffer_unref (buf);
353 static GstStateChangeReturn
354 gst_ssa_parse_change_state (GstElement * element, GstStateChange transition)
356 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
357 GstSsaParse *parse = GST_SSA_PARSE (element);
359 switch (transition) {
360 case GST_STATE_CHANGE_READY_TO_PAUSED:
366 ret = parent_class->change_state (element, transition);
367 if (ret == GST_STATE_CHANGE_FAILURE)
370 switch (transition) {
371 case GST_STATE_CHANGE_PAUSED_TO_READY:
374 parse->framed = FALSE;