Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst / subparse / gstssaparse.c
1 /* GStreamer SSA subtitle parser
2  * Copyright (c) 2006 Tim-Philipp Müller <tim centricular net>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* Super-primitive SSA parser - we just want the text and ignore
21  * everything else like styles and timing codes etc. for now */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdlib.h>             /* atoi() */
28 #include <string.h>
29
30 #include "gstssaparse.h"
31
32 GST_DEBUG_CATEGORY_STATIC (ssa_parse_debug);
33 #define GST_CAT_DEFAULT ssa_parse_debug
34
35 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("application/x-ssa; application/x-ass")
39     );
40
41 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("text/x-pango-markup")
45     );
46
47 GST_BOILERPLATE (GstSsaParse, gst_ssa_parse, GstElement, GST_TYPE_ELEMENT);
48
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);
55
56 static void
57 gst_ssa_parse_base_init (gpointer klass)
58 {
59   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
60
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>");
69
70   GST_DEBUG_CATEGORY_INIT (ssa_parse_debug, "ssaparse", 0,
71       "SSA subtitle parser");
72 }
73
74 static void
75 gst_ssa_parse_dispose (GObject * object)
76 {
77   GstSsaParse *parse = GST_SSA_PARSE (object);
78
79   g_free (parse->ini);
80   parse->ini = NULL;
81
82   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
83 }
84
85 static void
86 gst_ssa_parse_init (GstSsaParse * parse, GstSsaParseClass * klass)
87 {
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);
96
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));
104
105   parse->ini = NULL;
106   parse->framed = FALSE;
107   parse->send_tags = FALSE;
108 }
109
110 static void
111 gst_ssa_parse_class_init (GstSsaParseClass * klass)
112 {
113   GObjectClass *object_class = G_OBJECT_CLASS (klass);
114   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
115
116   object_class->dispose = gst_ssa_parse_dispose;
117
118   element_class->change_state = GST_DEBUG_FUNCPTR (gst_ssa_parse_change_state);
119 }
120
121 static gboolean
122 gst_ssa_parse_src_event (GstPad * pad, GstEvent * event)
123 {
124   return gst_pad_event_default (pad, event);
125 }
126
127 static gboolean
128 gst_ssa_parse_sink_event (GstPad * pad, GstEvent * event)
129 {
130   return gst_pad_event_default (pad, event);
131 }
132
133 static gboolean
134 gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps)
135 {
136   GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
137   const GValue *val;
138   GstStructure *s;
139   const guchar bom_utf8[] = { 0xEF, 0xBB, 0xBF };
140   GstBuffer *priv;
141   gchar *data;
142   guint size;
143
144   s = gst_caps_get_structure (caps, 0);
145   val = gst_structure_get_value (s, "codec_data");
146   if (val == NULL) {
147     parse->framed = FALSE;
148     GST_ERROR ("Only SSA subtitles embedded in containers are supported");
149     return FALSE;
150   }
151
152   parse->framed = TRUE;
153   parse->send_tags = TRUE;
154
155   priv = (GstBuffer *) gst_value_get_mini_object (val);
156   g_return_val_if_fail (priv != NULL, FALSE);
157
158   gst_buffer_ref (priv);
159
160   data = (gchar *) GST_BUFFER_DATA (priv);
161   size = GST_BUFFER_SIZE (priv);
162   /* skip UTF-8 BOM */
163   if (size >= 3 && memcmp (data, bom_utf8, 3) == 0) {
164     data += 3;
165     size -= 3;
166   }
167
168   if (!strstr (data, "[Script Info]")) {
169     GST_WARNING_OBJECT (parse, "Invalid Init section - no Script Info header");
170     gst_buffer_unref (priv);
171     return FALSE;
172   }
173
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);
177     return FALSE;
178   }
179
180   /* FIXME: parse initial section */
181   parse->ini = g_strndup (data, size);
182   GST_LOG_OBJECT (parse, "Init section:\n%s", parse->ini);
183
184   gst_buffer_unref (priv);
185
186   return TRUE;
187 }
188
189 static gboolean
190 gst_ssa_parse_remove_override_codes (GstSsaParse * parse, gchar * txt)
191 {
192   gchar *t, *end;
193   gboolean removed_any = FALSE;
194
195   while ((t = strchr (txt, '{'))) {
196     end = strchr (txt, '}');
197     if (end == NULL) {
198       GST_WARNING_OBJECT (parse, "Missing { for style override code");
199       return removed_any;
200     }
201     /* move terminating NUL character forward as well */
202     g_memmove (t, end + 1, strlen (end + 1) + 1);
203     removed_any = TRUE;
204   }
205
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"))) {
209     t[0] = ' ';
210     t[1] = '\n';
211   }
212   while ((t = strstr (txt, "\\N"))) {
213     t[0] = ' ';
214     t[1] = '\n';
215   }
216   while ((t = strstr (txt, "\\h"))) {
217     t[0] = ' ';
218     t[1] = ' ';
219   }
220
221   return removed_any;
222 }
223
224 /**
225  * gst_ssa_parse_push_line:
226  * @parse: caller element
227  * @txt: text to push
228  * @start: timestamp for the buffer
229  * @duration: duration for the buffer
230  *
231  * Parse the text in a buffer with the given properties and
232  * push it to the srcpad of the @parse element
233  *
234  * Returns: result of the push of the created buffer
235  */
236 static GstFlowReturn
237 gst_ssa_parse_push_line (GstSsaParse * parse, gchar * txt,
238     GstClockTime start, GstClockTime duration)
239 {
240   GstFlowReturn ret;
241   GstBuffer *buf;
242   gchar *t, *escaped;
243   gint num, i, len;
244
245   num = atoi (txt);
246   GST_LOG_OBJECT (parse, "Parsing line #%d at %" GST_TIME_FORMAT,
247       num, GST_TIME_ARGS (start));
248
249   /* skip all non-text fields before the actual text */
250   t = txt;
251   for (i = 0; i < 8; ++i) {
252     t = strchr (t, ',');
253     if (t == NULL)
254       return GST_FLOW_ERROR;
255     ++t;
256   }
257
258   GST_LOG_OBJECT (parse, "Text : %s", t);
259
260   if (gst_ssa_parse_remove_override_codes (parse, t)) {
261     GST_LOG_OBJECT (parse, "Clean: %s", t);
262   }
263
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);
267
268   len = strlen (escaped);
269
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;
274   g_free (escaped);
275
276   GST_BUFFER_TIMESTAMP (buf) = start;
277   GST_BUFFER_DURATION (buf) = duration;
278
279   gst_buffer_set_caps (buf, GST_PAD_CAPS (parse->srcpad));
280
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));
284
285   ret = gst_pad_push (parse->srcpad, buf);
286
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));
290   }
291
292   return ret;
293 }
294
295 static GstFlowReturn
296 gst_ssa_parse_chain (GstPad * sinkpad, GstBuffer * buf)
297 {
298   GstFlowReturn ret;
299   GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
300   GstClockTime ts;
301   gchar *txt;
302
303   if (G_UNLIKELY (!parse->framed))
304     goto not_framed;
305
306   if (G_UNLIKELY (parse->send_tags)) {
307     GstTagList *tags;
308
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;
314   }
315
316   /* make double-sure it's 0-terminated and all */
317   txt = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
318   if (txt == NULL)
319     goto empty_text;
320
321   ts = GST_BUFFER_TIMESTAMP (buf);
322   ret = gst_ssa_parse_push_line (parse, txt, ts, GST_BUFFER_DURATION (buf));
323
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));
328     ret = GST_FLOW_OK;
329   }
330
331   gst_buffer_unref (buf);
332   g_free (txt);
333
334   return ret;
335
336 /* ERRORS */
337 not_framed:
338   {
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;
343   }
344 empty_text:
345   {
346     GST_ELEMENT_WARNING (parse, STREAM, FORMAT, (NULL),
347         ("Received empty subtitle"));
348     gst_buffer_unref (buf);
349     return GST_FLOW_OK;
350   }
351 }
352
353 static GstStateChangeReturn
354 gst_ssa_parse_change_state (GstElement * element, GstStateChange transition)
355 {
356   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
357   GstSsaParse *parse = GST_SSA_PARSE (element);
358
359   switch (transition) {
360     case GST_STATE_CHANGE_READY_TO_PAUSED:
361       break;
362     default:
363       break;
364   }
365
366   ret = parent_class->change_state (element, transition);
367   if (ret == GST_STATE_CHANGE_FAILURE)
368     return ret;
369
370   switch (transition) {
371     case GST_STATE_CHANGE_PAUSED_TO_READY:
372       g_free (parse->ini);
373       parse->ini = NULL;
374       parse->framed = FALSE;
375       break;
376     default:
377       break;
378   }
379
380   return ret;
381 }