Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.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 #define gst_ssa_parse_parent_class parent_class
48 G_DEFINE_TYPE (GstSsaParse, gst_ssa_parse, GST_TYPE_ELEMENT);
49
50 static GstStateChangeReturn gst_ssa_parse_change_state (GstElement *
51     element, GstStateChange transition);
52 static gboolean gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps);
53 static gboolean gst_ssa_parse_src_event (GstPad * pad, GstObject * parent,
54     GstEvent * event);
55 static gboolean gst_ssa_parse_sink_event (GstPad * pad, GstObject * parent,
56     GstEvent * event);
57 static GstFlowReturn gst_ssa_parse_chain (GstPad * sinkpad, GstObject * parent,
58     GstBuffer * buf);
59
60 static void
61 gst_ssa_parse_dispose (GObject * object)
62 {
63   GstSsaParse *parse = GST_SSA_PARSE (object);
64
65   g_free (parse->ini);
66   parse->ini = NULL;
67
68   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
69 }
70
71 static void
72 gst_ssa_parse_init (GstSsaParse * parse)
73 {
74   parse->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
75   gst_pad_set_chain_function (parse->sinkpad,
76       GST_DEBUG_FUNCPTR (gst_ssa_parse_chain));
77   gst_pad_set_event_function (parse->sinkpad,
78       GST_DEBUG_FUNCPTR (gst_ssa_parse_sink_event));
79   gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
80
81   parse->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
82   gst_pad_set_event_function (parse->srcpad,
83       GST_DEBUG_FUNCPTR (gst_ssa_parse_src_event));
84   gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
85   gst_pad_use_fixed_caps (parse->srcpad);
86   gst_pad_set_caps (parse->srcpad,
87       gst_static_pad_template_get_caps (&src_templ));
88
89   parse->ini = NULL;
90   parse->framed = FALSE;
91   parse->send_tags = FALSE;
92 }
93
94 static void
95 gst_ssa_parse_class_init (GstSsaParseClass * klass)
96 {
97   GObjectClass *object_class = G_OBJECT_CLASS (klass);
98   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
99
100   object_class->dispose = gst_ssa_parse_dispose;
101
102   gst_element_class_add_pad_template (element_class,
103       gst_static_pad_template_get (&sink_templ));
104   gst_element_class_add_pad_template (element_class,
105       gst_static_pad_template_get (&src_templ));
106   gst_element_class_set_static_metadata (element_class,
107       "SSA Subtitle Parser", "Codec/Parser/Subtitle",
108       "Parses SSA subtitle streams",
109       "Tim-Philipp Müller <tim centricular net>");
110
111   GST_DEBUG_CATEGORY_INIT (ssa_parse_debug, "ssaparse", 0,
112       "SSA subtitle parser");
113
114   element_class->change_state = GST_DEBUG_FUNCPTR (gst_ssa_parse_change_state);
115 }
116
117 static gboolean
118 gst_ssa_parse_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
119 {
120   return gst_pad_event_default (pad, parent, event);
121 }
122
123 static gboolean
124 gst_ssa_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
125 {
126   gboolean res;
127
128   switch (GST_EVENT_TYPE (event)) {
129     case GST_EVENT_CAPS:
130     {
131       GstCaps *caps;
132
133       gst_event_parse_caps (event, &caps);
134       res = gst_ssa_parse_setcaps (pad, caps);
135       gst_event_unref (event);
136       break;
137     }
138     default:
139       res = gst_pad_event_default (pad, parent, event);
140       break;
141   }
142   return res;
143 }
144
145 static gboolean
146 gst_ssa_parse_setcaps (GstPad * sinkpad, GstCaps * caps)
147 {
148   GstSsaParse *parse = GST_SSA_PARSE (GST_PAD_PARENT (sinkpad));
149   GstCaps *outcaps;
150   const GValue *val;
151   GstStructure *s;
152   const guchar bom_utf8[] = { 0xEF, 0xBB, 0xBF };
153   GstBuffer *priv;
154   GstMapInfo map;
155   gchar *ptr;
156   gsize left;
157   gboolean ret;
158
159   s = gst_caps_get_structure (caps, 0);
160   val = gst_structure_get_value (s, "codec_data");
161   if (val == NULL) {
162     parse->framed = FALSE;
163     GST_ERROR ("Only SSA subtitles embedded in containers are supported");
164     return FALSE;
165   }
166
167   parse->framed = TRUE;
168   parse->send_tags = TRUE;
169
170   priv = (GstBuffer *) g_value_get_boxed (val);
171   g_return_val_if_fail (priv != NULL, FALSE);
172
173   gst_buffer_ref (priv);
174
175   gst_buffer_map (priv, &map, GST_MAP_READ);
176
177   ptr = (gchar *) map.data;
178   left = map.size;
179
180   /* skip UTF-8 BOM */
181   if (left >= 3 && memcmp (ptr, bom_utf8, 3) == 0) {
182     ptr += 3;
183     left -= 3;
184   }
185
186   if (!strstr (ptr, "[Script Info]"))
187     goto invalid_init;
188
189   if (!g_utf8_validate (ptr, left, NULL))
190     goto invalid_utf8;
191
192   /* FIXME: parse initial section */
193   parse->ini = g_strndup (ptr, left);
194   GST_LOG_OBJECT (parse, "Init section:\n%s", parse->ini);
195
196   gst_buffer_unmap (priv, &map);
197   gst_buffer_unref (priv);
198
199   outcaps = gst_caps_new_empty_simple ("text/x-pango-markup");
200   ret = gst_pad_set_caps (parse->srcpad, outcaps);
201   gst_caps_unref (outcaps);
202
203   return ret;
204
205   /* ERRORS */
206 invalid_init:
207   {
208     GST_WARNING_OBJECT (parse, "Invalid Init section - no Script Info header");
209     gst_buffer_unmap (priv, &map);
210     gst_buffer_unref (priv);
211     return FALSE;
212   }
213 invalid_utf8:
214   {
215     GST_WARNING_OBJECT (parse, "Init section is not valid UTF-8");
216     gst_buffer_unmap (priv, &map);
217     gst_buffer_unref (priv);
218     return FALSE;
219   }
220 }
221
222 static gboolean
223 gst_ssa_parse_remove_override_codes (GstSsaParse * parse, gchar * txt)
224 {
225   gchar *t, *end;
226   gboolean removed_any = FALSE;
227
228   while ((t = strchr (txt, '{'))) {
229     end = strchr (txt, '}');
230     if (end == NULL) {
231       GST_WARNING_OBJECT (parse, "Missing { for style override code");
232       return removed_any;
233     }
234     /* move terminating NUL character forward as well */
235     g_memmove (t, end + 1, strlen (end + 1) + 1);
236     removed_any = TRUE;
237   }
238
239   /* these may occur outside of curly brackets. We don't handle the different
240    * wrapping modes yet, so just remove these markers from the text for now */
241   while ((t = strstr (txt, "\\n"))) {
242     t[0] = ' ';
243     t[1] = '\n';
244   }
245   while ((t = strstr (txt, "\\N"))) {
246     t[0] = ' ';
247     t[1] = '\n';
248   }
249   while ((t = strstr (txt, "\\h"))) {
250     t[0] = ' ';
251     t[1] = ' ';
252   }
253
254   return removed_any;
255 }
256
257 /**
258  * gst_ssa_parse_push_line:
259  * @parse: caller element
260  * @txt: text to push
261  * @start: timestamp for the buffer
262  * @duration: duration for the buffer
263  *
264  * Parse the text in a buffer with the given properties and
265  * push it to the srcpad of the @parse element
266  *
267  * Returns: result of the push of the created buffer
268  */
269 static GstFlowReturn
270 gst_ssa_parse_push_line (GstSsaParse * parse, gchar * txt,
271     GstClockTime start, GstClockTime duration)
272 {
273   GstFlowReturn ret;
274   GstBuffer *buf;
275   gchar *t, *escaped;
276   gint num, i, len;
277
278   num = atoi (txt);
279   GST_LOG_OBJECT (parse, "Parsing line #%d at %" GST_TIME_FORMAT,
280       num, GST_TIME_ARGS (start));
281
282   /* skip all non-text fields before the actual text */
283   t = txt;
284   for (i = 0; i < 8; ++i) {
285     t = strchr (t, ',');
286     if (t == NULL)
287       return GST_FLOW_ERROR;
288     ++t;
289   }
290
291   GST_LOG_OBJECT (parse, "Text : %s", t);
292
293   if (gst_ssa_parse_remove_override_codes (parse, t)) {
294     GST_LOG_OBJECT (parse, "Clean: %s", t);
295   }
296
297   /* we claim to output pango markup, so we must escape the
298    * text even if we don't actually use any pango markup yet */
299   escaped = g_markup_printf_escaped ("%s", t);
300
301   len = strlen (escaped);
302
303   /* allocate enough for a terminating NUL, but don't include it in buf size */
304   buf = gst_buffer_new_and_alloc (len + 1);
305   gst_buffer_fill (buf, 0, escaped, len + 1);
306   gst_buffer_set_size (buf, len);
307   g_free (escaped);
308
309   GST_BUFFER_TIMESTAMP (buf) = start;
310   GST_BUFFER_DURATION (buf) = duration;
311
312   GST_LOG_OBJECT (parse, "Pushing buffer with timestamp %" GST_TIME_FORMAT
313       " and duration %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
314       GST_TIME_ARGS (duration));
315
316   ret = gst_pad_push (parse->srcpad, buf);
317
318   if (ret != GST_FLOW_OK) {
319     GST_DEBUG_OBJECT (parse, "Push of text '%s' returned flow %s", txt,
320         gst_flow_get_name (ret));
321   }
322
323   return ret;
324 }
325
326 static GstFlowReturn
327 gst_ssa_parse_chain (GstPad * sinkpad, GstObject * parent, GstBuffer * buf)
328 {
329   GstFlowReturn ret;
330   GstSsaParse *parse = GST_SSA_PARSE (parent);
331   GstClockTime ts;
332   gchar *txt;
333   GstMapInfo map;
334
335   if (G_UNLIKELY (!parse->framed))
336     goto not_framed;
337
338   if (G_UNLIKELY (parse->send_tags)) {
339     GstTagList *tags;
340
341     tags = gst_tag_list_new_empty ();
342     gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_SUBTITLE_CODEC,
343         "SubStation Alpha", NULL);
344     gst_pad_push_event (parse->srcpad, gst_event_new_tag (tags));
345     parse->send_tags = FALSE;
346   }
347
348   /* make double-sure it's 0-terminated and all */
349   gst_buffer_map (buf, &map, GST_MAP_READ);
350   txt = g_strndup ((gchar *) map.data, map.size);
351   gst_buffer_unmap (buf, &map);
352
353   if (txt == NULL)
354     goto empty_text;
355
356   ts = GST_BUFFER_TIMESTAMP (buf);
357   ret = gst_ssa_parse_push_line (parse, txt, ts, GST_BUFFER_DURATION (buf));
358
359   if (ret != GST_FLOW_OK && GST_CLOCK_TIME_IS_VALID (ts)) {
360     GstSegment segment;
361
362     /* just advance time without sending anything */
363     gst_segment_init (&segment, GST_FORMAT_TIME);
364     segment.start = ts;
365     segment.time = ts;
366     gst_pad_push_event (parse->srcpad, gst_event_new_segment (&segment));
367     ret = GST_FLOW_OK;
368   }
369
370   gst_buffer_unref (buf);
371   g_free (txt);
372
373   return ret;
374
375 /* ERRORS */
376 not_framed:
377   {
378     GST_ELEMENT_ERROR (parse, STREAM, FORMAT, (NULL),
379         ("Only SSA subtitles embedded in containers are supported"));
380     gst_buffer_unref (buf);
381     return GST_FLOW_NOT_NEGOTIATED;
382   }
383 empty_text:
384   {
385     GST_ELEMENT_WARNING (parse, STREAM, FORMAT, (NULL),
386         ("Received empty subtitle"));
387     gst_buffer_unref (buf);
388     return GST_FLOW_OK;
389   }
390 }
391
392 static GstStateChangeReturn
393 gst_ssa_parse_change_state (GstElement * element, GstStateChange transition)
394 {
395   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
396   GstSsaParse *parse = GST_SSA_PARSE (element);
397
398   switch (transition) {
399     case GST_STATE_CHANGE_READY_TO_PAUSED:
400       break;
401     default:
402       break;
403   }
404
405   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
406   if (ret == GST_STATE_CHANGE_FAILURE)
407     return ret;
408
409   switch (transition) {
410     case GST_STATE_CHANGE_PAUSED_TO_READY:
411       g_free (parse->ini);
412       parse->ini = NULL;
413       parse->framed = FALSE;
414       break;
415     default:
416       break;
417   }
418
419   return ret;
420 }