93854ed125e77ced03f7282f6722a0d4e71278a0
[platform/upstream/gstreamer.git] / ext / kate / gstkatedec.c
1 /*
2  * GStreamer
3  * Copyright 2005 Thomas Vander Stichele <thomas@apestaart.org>
4  * Copyright 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5  * Copyright 2008, 2009 Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Alternatively, the contents of this file may be used under the
26  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27  * which case the following provisions apply instead of the ones
28  * mentioned above:
29  *
30  * This library is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Library General Public
32  * License as published by the Free Software Foundation; either
33  * version 2 of the License, or (at your option) any later version.
34  *
35  * This library is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38  * Library General Public License for more details.
39  *
40  * You should have received a copy of the GNU Library General Public
41  * License along with this library; if not, write to the
42  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
43  * Boston, MA 02110-1301, USA.
44  */
45
46 /**
47  * SECTION:element-katedec
48  * @title: katedec
49  * @see_also: oggdemux
50  *
51  * This element decodes Kate streams.
52  *
53  * [Kate](http://libkate.googlecode.com/) is a free codec
54  * for text based data, such as subtitles. Any number of kate streams can be
55  * embedded in an Ogg stream.
56  *
57  * libkate (see above url) is needed to build this plugin.
58  *
59  * ## Example pipeline
60  *
61  * This explicitly decodes a Kate stream:
62  * |[
63  * gst-launch-1.0 filesrc location=test.ogg ! oggdemux ! katedec ! fakesink silent=TRUE
64  * ]|
65  *
66  * This will automatically detect and use any Kate streams multiplexed
67  * in an Ogg stream:
68  * |[
69  * gst-launch-1.0 playbin uri=file:///tmp/test.ogg
70  * ]|
71  *
72  */
73
74 #ifdef HAVE_CONFIG_H
75 #include "config.h"
76 #endif
77
78 #include <string.h>
79
80 #include <gst/gst.h>
81
82 #include "gstkateelements.h"
83 #include "gstkatespu.h"
84 #include "gstkatedec.h"
85
86 GST_DEBUG_CATEGORY_EXTERN (gst_katedec_debug);
87 #define GST_CAT_DEFAULT gst_katedec_debug
88
89 /* Filter signals and args */
90 enum
91 {
92   /* FILL ME */
93   LAST_SIGNAL
94 };
95
96 enum
97 {
98   ARG_REMOVE_MARKUP = DECODER_BASE_ARG_COUNT
99 };
100
101 /* We don't accept application/x-kate here on purpose for now, since we're
102  * only really interested in subtitle-like things for playback purposes, not
103  * cracktastic complex overlays or presentation images etc. - those should be
104  * fed into a tiger overlay plugin directly */
105 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS ("subtitle/x-kate")
109     );
110
111 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
112     GST_PAD_SRC,
113     GST_PAD_ALWAYS,
114     GST_STATIC_CAPS ("text/x-raw, format = { pango-markup, utf8 }; "
115         GST_KATE_SPU_MIME_TYPE)
116     );
117
118 GST_DEBUG_CATEGORY (gst_katedec_debug);
119
120 #define gst_kate_dec_parent_class parent_class
121 G_DEFINE_TYPE (GstKateDec, gst_kate_dec, GST_TYPE_ELEMENT);
122 #define _do_init \
123   kate_element_init (plugin); \
124   GST_DEBUG_CATEGORY_INIT (gst_katedec_debug, "katedec", 0, "Kate decoder");
125 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (katedec, "katedec", GST_RANK_PRIMARY,
126     GST_TYPE_KATE_DEC, _do_init);
127
128 static void gst_kate_dec_set_property (GObject * object, guint prop_id,
129     const GValue * value, GParamSpec * pspec);
130 static void gst_kate_dec_get_property (GObject * object, guint prop_id,
131     GValue * value, GParamSpec * pspec);
132
133 static GstFlowReturn gst_kate_dec_chain (GstPad * pad, GstObject * parent,
134     GstBuffer * buf);
135 static GstStateChangeReturn gst_kate_dec_change_state (GstElement * element,
136     GstStateChange transition);
137 static gboolean gst_kate_dec_sink_query (GstPad * pad, GstObject * parent,
138     GstQuery * query);
139 static gboolean gst_kate_dec_sink_event (GstPad * pad, GstObject * parent,
140     GstEvent * event);
141 static gboolean gst_kate_dec_sink_handle_event (GstPad * pad,
142     GstObject * parent, GstEvent * event);
143 static gboolean gst_kate_dec_src_query (GstPad * pad, GstObject * parent,
144     GstQuery * query);
145
146 /* initialize the plugin's class */
147 static void
148 gst_kate_dec_class_init (GstKateDecClass * klass)
149 {
150   GObjectClass *gobject_class;
151   GstElementClass *gstelement_class;
152
153   gobject_class = (GObjectClass *) klass;
154   gstelement_class = (GstElementClass *) klass;
155
156   gobject_class->set_property = gst_kate_dec_set_property;
157   gobject_class->get_property = gst_kate_dec_get_property;
158
159   gst_kate_util_install_decoder_base_properties (gobject_class);
160
161   g_object_class_install_property (gobject_class, ARG_REMOVE_MARKUP,
162       g_param_spec_boolean ("remove-markup", "Remove markup",
163           "Remove markup from decoded text ?", FALSE,
164           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165
166   gstelement_class->change_state =
167       GST_DEBUG_FUNCPTR (gst_kate_dec_change_state);
168
169   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
170   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
171
172   gst_element_class_set_static_metadata (gstelement_class,
173       "Kate stream text decoder", "Codec/Decoder/Subtitle",
174       "Decodes Kate text streams",
175       "Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>");
176 }
177
178 /* initialize the new element
179  * instantiate pads and add them to element
180  * set functions
181  * initialize structure
182  */
183 static void
184 gst_kate_dec_init (GstKateDec * dec)
185 {
186   GstCaps *tmp = NULL;
187   GST_DEBUG_OBJECT (dec, "gst_kate_dec_init");
188
189   dec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
190   gst_pad_set_chain_function (dec->sinkpad,
191       GST_DEBUG_FUNCPTR (gst_kate_dec_chain));
192   gst_pad_set_query_function (dec->sinkpad,
193       GST_DEBUG_FUNCPTR (gst_kate_dec_sink_query));
194   gst_pad_set_event_function (dec->sinkpad,
195       GST_DEBUG_FUNCPTR (gst_kate_dec_sink_event));
196   gst_pad_use_fixed_caps (dec->sinkpad);
197   gst_pad_set_caps (dec->sinkpad,
198       tmp = gst_static_pad_template_get_caps (&sink_factory));
199   gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
200
201   dec->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
202   gst_pad_set_query_function (dec->srcpad,
203       GST_DEBUG_FUNCPTR (gst_kate_dec_src_query));
204   gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
205
206   gst_kate_util_decode_base_init (&dec->decoder, TRUE);
207
208   gst_caps_unref(tmp);
209   dec->src_caps = NULL;
210   dec->output_format = GST_KATE_FORMAT_UNDEFINED;
211   dec->remove_markup = FALSE;
212 }
213
214 static void
215 gst_kate_dec_set_property (GObject * object, guint prop_id,
216     const GValue * value, GParamSpec * pspec)
217 {
218   switch (prop_id) {
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223 }
224
225 static void
226 gst_kate_dec_get_property (GObject * object, guint prop_id,
227     GValue * value, GParamSpec * pspec)
228 {
229   GstKateDec *kd = GST_KATE_DEC (object);
230
231   switch (prop_id) {
232     case ARG_REMOVE_MARKUP:
233       g_value_set_boolean (value, kd->remove_markup);
234       break;
235     default:
236       if (!gst_kate_util_decoder_base_get_property (&kd->decoder, object,
237               prop_id, value, pspec)) {
238         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239       }
240       break;
241   }
242 }
243
244 static GstFlowReturn
245 gst_kate_dec_handle_kate_event (GstKateDec * kd, const kate_event * ev)
246 {
247   GstFlowReturn rflow = GST_FLOW_OK;
248   GstKateFormat format = GST_KATE_FORMAT_UNDEFINED;
249   gchar *escaped;
250   GstBuffer *buffer;
251   size_t len;
252   gboolean plain = TRUE;
253
254   if (kd->remove_markup && ev->text_markup_type != kate_markup_none) {
255     size_t len0 = ev->len + 1;
256     escaped = g_strdup (ev->text);
257     if (escaped) {
258       kate_text_remove_markup (ev->text_encoding, escaped, &len0);
259     }
260     plain = TRUE;
261   } else if (ev->text_markup_type == kate_markup_none) {
262     /* no pango markup yet, escape text */
263     /* TODO: actually do the pango thing */
264     escaped = g_strdup (ev->text);
265     plain = TRUE;
266   } else {
267     escaped = g_strdup (ev->text);
268     plain = FALSE;
269   }
270
271   if (G_LIKELY (escaped)) {
272     len = strlen (escaped);
273     if (len > 0) {
274       GST_DEBUG_OBJECT (kd, "kate event: %s, escaped %s", ev->text, escaped);
275       buffer = gst_buffer_new_and_alloc (len + 1);
276       if (G_LIKELY (buffer)) {
277         GstCaps *caps;
278         if (plain)
279           format = GST_KATE_FORMAT_TEXT_UTF8;
280         else
281           format = GST_KATE_FORMAT_TEXT_PANGO_MARKUP;
282         if (format != kd->output_format) {
283           caps = gst_caps_new_simple ("text/x-raw", "format", G_TYPE_STRING,
284               (format == GST_KATE_FORMAT_TEXT_UTF8) ? "utf8" : "pango-markup",
285               NULL);
286           gst_pad_push_event (kd->srcpad, gst_event_new_caps (caps));
287           gst_caps_unref (caps);
288           kd->output_format = format;
289         }
290         /* allocate and copy the NULs, but don't include them in passed size */
291         gst_buffer_fill (buffer, 0, escaped, len + 1);
292         gst_buffer_resize (buffer, 0, len);
293         GST_BUFFER_TIMESTAMP (buffer) = ev->start_time * GST_SECOND;
294         GST_BUFFER_DURATION (buffer) =
295             (ev->end_time - ev->start_time) * GST_SECOND;
296         rflow = gst_pad_push (kd->srcpad, buffer);
297         if (rflow == GST_FLOW_NOT_LINKED) {
298           GST_DEBUG_OBJECT (kd, "source pad not linked, ignored");
299         } else if (rflow != GST_FLOW_OK) {
300           GST_WARNING_OBJECT (kd, "failed to push buffer: %s",
301               gst_flow_get_name (rflow));
302         }
303       } else {
304         GST_ELEMENT_ERROR (kd, STREAM, DECODE, (NULL),
305             ("Failed to create buffer"));
306         rflow = GST_FLOW_ERROR;
307       }
308     } else {
309       GST_WARNING_OBJECT (kd, "Empty string, nothing to do");
310       rflow = GST_FLOW_OK;
311     }
312     g_free (escaped);
313   } else {
314     GST_ELEMENT_ERROR (kd, STREAM, DECODE, (NULL),
315         ("Failed to allocate string"));
316     rflow = GST_FLOW_ERROR;
317   }
318
319   /* if there's a background paletted bitmap, construct a DVD SPU for it */
320   if (ev->bitmap && ev->palette) {
321     GstBuffer *buffer = gst_kate_spu_encode_spu (kd, ev);
322     if (buffer) {
323       GstCaps *caps;
324
325       GST_BUFFER_TIMESTAMP (buffer) = ev->start_time * GST_SECOND;
326       GST_BUFFER_DURATION (buffer) =
327           (ev->end_time - ev->start_time) * GST_SECOND;
328
329       if (kd->output_format != GST_KATE_FORMAT_SPU) {
330         caps = gst_caps_new_empty_simple (GST_KATE_SPU_MIME_TYPE);
331         gst_pad_push_event (kd->srcpad, gst_event_new_caps (caps));
332         gst_caps_unref (caps);
333         kd->output_format = GST_KATE_FORMAT_SPU;
334       }
335
336       rflow = gst_pad_push (kd->srcpad, buffer);
337       if (rflow == GST_FLOW_NOT_LINKED) {
338         GST_DEBUG_OBJECT (kd, "source pad not linked, ignored");
339       } else if (rflow != GST_FLOW_OK) {
340         GST_WARNING_OBJECT (kd, "failed to push buffer: %s",
341             gst_flow_get_name (rflow));
342       }
343     } else {
344       GST_ELEMENT_ERROR (kd, STREAM, DECODE, (NULL),
345           ("failed to create SPU from paletted bitmap"));
346       rflow = GST_FLOW_ERROR;
347     }
348   }
349   return rflow;
350 }
351
352 /* GstElement vmethod implementations */
353
354 /* chain function
355  * this function does the actual processing
356  */
357
358 static GstFlowReturn
359 gst_kate_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
360 {
361   GstKateDec *kd = GST_KATE_DEC (parent);
362   const kate_event *ev = NULL;
363   GstFlowReturn rflow = GST_FLOW_OK;
364
365   if (!gst_kate_util_decoder_base_update_segment (&kd->decoder,
366           GST_ELEMENT_CAST (kd), buf)) {
367     GST_WARNING_OBJECT (kd, "Out of segment!");
368     goto not_in_seg;
369   }
370
371   rflow =
372       gst_kate_util_decoder_base_chain_kate_packet (&kd->decoder,
373       GST_ELEMENT_CAST (kd), pad, buf, kd->srcpad, kd->srcpad, &kd->src_caps,
374       &ev);
375   if (G_UNLIKELY (rflow != GST_FLOW_OK)) {
376     gst_buffer_unref (buf);
377     return rflow;
378   }
379
380   if (ev) {
381     rflow = gst_kate_dec_handle_kate_event (kd, ev);
382   }
383
384 not_in_seg:
385   gst_buffer_unref (buf);
386   return rflow;
387 }
388
389 static GstStateChangeReturn
390 gst_kate_dec_change_state (GstElement * element, GstStateChange transition)
391 {
392   GstStateChangeReturn ret;
393   GstKateDec *kd = GST_KATE_DEC (element);
394
395   ret = gst_kate_decoder_base_change_state (&kd->decoder, element,
396       parent_class, transition);
397
398   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
399     gst_caps_replace (&kd->src_caps, NULL);
400   }
401
402   return ret;
403 }
404
405 gboolean
406 gst_kate_dec_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
407 {
408   GstKateDec *kd = GST_KATE_DEC (parent);
409   gboolean res =
410       gst_kate_decoder_base_sink_query (&kd->decoder, GST_ELEMENT_CAST (kd),
411       pad, parent, query);
412   return res;
413 }
414
415 static gboolean
416 gst_kate_dec_set_caps (GstKateDec * kd, GstCaps * caps)
417 {
418   GstStructure *structure = gst_caps_get_structure (caps, 0);
419   GstFlowReturn rflow = GST_FLOW_OK;
420
421   if (gst_structure_has_field (structure, "streamheader")) {
422     const GValue *value;
423     GstBuffer *buf;
424     const kate_event *ev;
425
426     value = gst_structure_get_value (structure, "streamheader");
427
428     if (GST_VALUE_HOLDS_BUFFER (value)) {
429       buf = gst_value_get_buffer (value);
430
431       gst_kate_util_decoder_base_chain_kate_packet (&kd->decoder,
432           GST_ELEMENT_CAST (kd), kd->sinkpad, buf, kd->srcpad, kd->srcpad,
433           &kd->src_caps, &ev);
434
435       if (ev) {
436         rflow = gst_kate_dec_handle_kate_event (kd, ev);
437       }
438     } else if (GST_VALUE_HOLDS_ARRAY (value)) {
439       gint i, size = gst_value_array_get_size (value);
440
441       for (i = 0; i < size; i++) {
442         const GValue *v = gst_value_array_get_value (value, i);
443
444         buf = gst_value_get_buffer (v);
445         gst_kate_util_decoder_base_chain_kate_packet (&kd->decoder,
446             GST_ELEMENT_CAST (kd), kd->sinkpad, buf, kd->srcpad, kd->srcpad,
447             &kd->src_caps, &ev);
448
449         if (ev) {
450           rflow = gst_kate_dec_handle_kate_event (kd, ev);
451           if (rflow != GST_FLOW_OK && rflow != GST_FLOW_NOT_LINKED)
452             break;
453         }
454       }
455     } else {
456       GST_WARNING_OBJECT (kd, "Unhandled streamheader type: %s",
457           G_VALUE_TYPE_NAME (value));
458     }
459   }
460
461   return rflow == GST_FLOW_OK || rflow == GST_FLOW_NOT_LINKED;
462 }
463
464 static gboolean
465 gst_kate_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
466 {
467   GstKateDec *kd = GST_KATE_DEC (parent);
468   gboolean res = TRUE;
469
470   GST_LOG_OBJECT (pad, "Event on sink pad: %" GST_PTR_FORMAT, event);
471
472   switch (GST_EVENT_TYPE (event)) {
473     case GST_EVENT_CAPS:{
474       GstCaps *caps;
475
476       gst_event_parse_caps (event, &caps);
477       gst_kate_dec_set_caps (kd, caps);
478       break;
479     }
480     default:
481       break;
482   }
483
484   /* Delay events till we've set caps */
485   if (gst_kate_util_decoder_base_queue_event (&kd->decoder, event,
486           &gst_kate_dec_sink_handle_event, parent, pad)) {
487     return TRUE;
488   }
489
490   res = gst_kate_dec_sink_handle_event (pad, parent, event);
491
492   return res;
493 }
494
495 static gboolean
496 gst_kate_dec_sink_handle_event (GstPad * pad, GstObject * parent,
497     GstEvent * event)
498 {
499   GstKateDec *kd = GST_KATE_DEC (parent);
500
501   GST_LOG_OBJECT (pad, "Handling event on sink pad: %s",
502       GST_EVENT_TYPE_NAME (event));
503
504   switch (GST_EVENT_TYPE (event)) {
505     case GST_EVENT_SEGMENT:
506       break;
507
508     case GST_EVENT_FLUSH_START:
509       gst_kate_util_decoder_base_set_flushing (&kd->decoder, TRUE);
510       break;
511
512     case GST_EVENT_FLUSH_STOP:
513       gst_kate_util_decoder_base_set_flushing (&kd->decoder, FALSE);
514       break;
515
516     case GST_EVENT_TAG:{
517       GstTagList *tags;
518       gst_event_parse_tag (event, &tags);
519       gst_kate_util_decoder_base_add_tags (&kd->decoder, tags, FALSE);
520       gst_event_unref (event);
521       event = gst_kate_util_decoder_base_get_tag_event (&kd->decoder);
522       break;
523     }
524     default:
525       break;
526   }
527
528   return gst_pad_event_default (pad, parent, event);
529 }
530
531 static gboolean
532 gst_kate_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
533 {
534   GstKateDec *kd = GST_KATE_DEC (parent);
535   gboolean res = TRUE;
536
537   GST_LOG_OBJECT (pad, "Handling query on src pad: %s",
538       GST_QUERY_TYPE_NAME (query));
539
540   switch (GST_QUERY_TYPE (query)) {
541     case GST_QUERY_CAPS:{
542       GstCaps *caps;
543
544       if (kd->src_caps) {
545         GST_DEBUG_OBJECT (kd, "We have src caps %" GST_PTR_FORMAT,
546             kd->src_caps);
547         caps = gst_caps_copy (kd->src_caps);
548       } else {
549         GST_DEBUG_OBJECT (kd, "We have no src caps, using template caps");
550         caps = gst_static_pad_template_get_caps (&src_factory);
551       }
552
553       gst_query_set_caps_result (query, caps);
554       gst_caps_unref (caps);
555       break;
556     }
557     default:
558       res = gst_pad_query_default (pad, parent, query);
559       break;
560   }
561
562   return res;
563 }