tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.git] / 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_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>");
67
68   GST_DEBUG_CATEGORY_INIT (ssa_parse_debug, "ssaparse", 0,
69       "SSA subtitle parser");
70 }
71
72 static void
73 gst_ssa_parse_dispose (GObject * object)
74 {
75   GstSsaParse *parse = GST_SSA_PARSE (object);
76
77   g_free (parse->ini);
78   parse->ini = NULL;
79
80   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
81 }
82
83 static void
84 gst_ssa_parse_init (GstSsaParse * parse, GstSsaParseClass * klass)
85 {
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);
94
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));
102
103   parse->ini = NULL;
104   parse->framed = FALSE;
105   parse->send_tags = FALSE;
106 }
107
108 static void
109 gst_ssa_parse_class_init (GstSsaParseClass * klass)
110 {
111   GObjectClass *object_class = G_OBJECT_CLASS (klass);
112   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
113
114   object_class->dispose = gst_ssa_parse_dispose;
115
116   element_class->change_state = GST_DEBUG_FUNCPTR (gst_ssa_parse_change_state);
117 }
118
119 static gboolean
120 gst_ssa_parse_src_event (GstPad * pad, GstEvent * event)
121 {
122   return gst_pad_event_default (pad, event);
123 }
124
125 static gboolean
126 gst_ssa_parse_sink_event (GstPad * pad, GstEvent * event)
127 {
128   return gst_pad_event_default (pad, event);
129 }
130
131 static gboolean
132 gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps)
133 {
134   GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
135   const GValue *val;
136   GstStructure *s;
137   const guchar bom_utf8[] = { 0xEF, 0xBB, 0xBF };
138   GstBuffer *priv;
139   gchar *data;
140   guint size;
141
142   s = gst_caps_get_structure (caps, 0);
143   val = gst_structure_get_value (s, "codec_data");
144   if (val == NULL) {
145     parse->framed = FALSE;
146     GST_ERROR ("Only SSA subtitles embedded in containers are supported");
147     return FALSE;
148   }
149
150   parse->framed = TRUE;
151   parse->send_tags = TRUE;
152
153   priv = (GstBuffer *) gst_value_get_mini_object (val);
154   g_return_val_if_fail (priv != NULL, FALSE);
155
156   gst_buffer_ref (priv);
157
158   data = (gchar *) GST_BUFFER_DATA (priv);
159   size = GST_BUFFER_SIZE (priv);
160   /* skip UTF-8 BOM */
161   if (size >= 3 && memcmp (data, bom_utf8, 3) == 0) {
162     data += 3;
163     size -= 3;
164   }
165
166   if (!strstr (data, "[Script Info]")) {
167     GST_WARNING_OBJECT (parse, "Invalid Init section - no Script Info header");
168     gst_buffer_unref (priv);
169     return FALSE;
170   }
171
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);
175     return FALSE;
176   }
177
178   /* FIXME: parse initial section */
179   parse->ini = g_strndup (data, size);
180   GST_LOG_OBJECT (parse, "Init section:\n%s", parse->ini);
181
182   gst_buffer_unref (priv);
183
184   return TRUE;
185 }
186
187 static gboolean
188 gst_ssa_parse_remove_override_codes (GstSsaParse * parse, gchar * txt)
189 {
190   gchar *t, *end;
191   gboolean removed_any = FALSE;
192
193   while ((t = strchr (txt, '{'))) {
194     end = strchr (txt, '}');
195     if (end == NULL) {
196       GST_WARNING_OBJECT (parse, "Missing { for style override code");
197       return removed_any;
198     }
199     /* move terminating NUL character forward as well */
200     g_memmove (t, end + 1, strlen (end + 1) + 1);
201     removed_any = TRUE;
202   }
203
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"))) {
207     t[0] = ' ';
208     t[1] = '\n';
209   }
210   while ((t = strstr (txt, "\\N"))) {
211     t[0] = ' ';
212     t[1] = '\n';
213   }
214   while ((t = strstr (txt, "\\h"))) {
215     t[0] = ' ';
216     t[1] = ' ';
217   }
218
219   return removed_any;
220 }
221
222 /**
223  * gst_ssa_parse_push_line:
224  * @parse: caller element
225  * @txt: text to push
226  * @start: timestamp for the buffer
227  * @duration: duration for the buffer
228  *
229  * Parse the text in a buffer with the given properties and
230  * push it to the srcpad of the @parse element
231  *
232  * Returns: result of the push of the created buffer
233  */
234 static GstFlowReturn
235 gst_ssa_parse_push_line (GstSsaParse * parse, gchar * txt,
236     GstClockTime start, GstClockTime duration)
237 {
238   GstFlowReturn ret;
239   GstBuffer *buf;
240   gchar *t, *escaped;
241   gint num, i, len;
242
243   num = atoi (txt);
244   GST_LOG_OBJECT (parse, "Parsing line #%d at %" GST_TIME_FORMAT,
245       num, GST_TIME_ARGS (start));
246
247   /* skip all non-text fields before the actual text */
248   t = txt;
249   for (i = 0; i < 8; ++i) {
250     t = strchr (t, ',');
251     if (t == NULL)
252       return GST_FLOW_ERROR;
253     ++t;
254   }
255
256   GST_LOG_OBJECT (parse, "Text : %s", t);
257
258   if (gst_ssa_parse_remove_override_codes (parse, t)) {
259     GST_LOG_OBJECT (parse, "Clean: %s", t);
260   }
261
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);
265
266   len = strlen (escaped);
267
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;
272   g_free (escaped);
273
274   GST_BUFFER_TIMESTAMP (buf) = start;
275   GST_BUFFER_DURATION (buf) = duration;
276
277   gst_buffer_set_caps (buf, GST_PAD_CAPS (parse->srcpad));
278
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));
282
283   ret = gst_pad_push (parse->srcpad, buf);
284
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));
288   }
289
290   return ret;
291 }
292
293 static GstFlowReturn
294 gst_ssa_parse_chain (GstPad * sinkpad, GstBuffer * buf)
295 {
296   GstFlowReturn ret;
297   GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
298   GstClockTime ts;
299   gchar *txt;
300
301   if (G_UNLIKELY (!parse->framed))
302     goto not_framed;
303
304   if (G_UNLIKELY (parse->send_tags)) {
305     GstTagList *tags;
306
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;
312   }
313
314   /* make double-sure it's 0-terminated and all */
315   txt = g_strndup ((gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
316   if (txt == NULL)
317     goto empty_text;
318
319   ts = GST_BUFFER_TIMESTAMP (buf);
320   ret = gst_ssa_parse_push_line (parse, txt, ts, GST_BUFFER_DURATION (buf));
321
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));
326     ret = GST_FLOW_OK;
327   }
328
329   gst_buffer_unref (buf);
330   g_free (txt);
331
332   return ret;
333
334 /* ERRORS */
335 not_framed:
336   {
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;
341   }
342 empty_text:
343   {
344     GST_ELEMENT_WARNING (parse, STREAM, FORMAT, (NULL),
345         ("Received empty subtitle"));
346     gst_buffer_unref (buf);
347     return GST_FLOW_OK;
348   }
349 }
350
351 static GstStateChangeReturn
352 gst_ssa_parse_change_state (GstElement * element, GstStateChange transition)
353 {
354   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
355   GstSsaParse *parse = GST_SSA_PARSE (element);
356
357   switch (transition) {
358     case GST_STATE_CHANGE_READY_TO_PAUSED:
359       break;
360     default:
361       break;
362   }
363
364   ret = parent_class->change_state (element, transition);
365   if (ret == GST_STATE_CHANGE_FAILURE)
366     return ret;
367
368   switch (transition) {
369     case GST_STATE_CHANGE_PAUSED_TO_READY:
370       g_free (parse->ini);
371       parse->ini = NULL;
372       parse->framed = FALSE;
373       break;
374     default:
375       break;
376   }
377
378   return ret;
379 }