docstrings: port ulinks to markdown links
[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 explicitely 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 "gstkate.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 #define gst_kate_dec_parent_class parent_class
119 G_DEFINE_TYPE (GstKateDec, gst_kate_dec, GST_TYPE_ELEMENT);
120
121 static void gst_kate_dec_set_property (GObject * object, guint prop_id,
122     const GValue * value, GParamSpec * pspec);
123 static void gst_kate_dec_get_property (GObject * object, guint prop_id,
124     GValue * value, GParamSpec * pspec);
125
126 static GstFlowReturn gst_kate_dec_chain (GstPad * pad, GstObject * parent,
127     GstBuffer * buf);
128 static GstStateChangeReturn gst_kate_dec_change_state (GstElement * element,
129     GstStateChange transition);
130 static gboolean gst_kate_dec_sink_query (GstPad * pad, GstObject * parent,
131     GstQuery * query);
132 static gboolean gst_kate_dec_sink_event (GstPad * pad, GstObject * parent,
133     GstEvent * event);
134 static gboolean gst_kate_dec_sink_handle_event (GstPad * pad,
135     GstObject * parent, GstEvent * event);
136 static gboolean gst_kate_dec_src_query (GstPad * pad, GstObject * parent,
137     GstQuery * query);
138
139 /* initialize the plugin's class */
140 static void
141 gst_kate_dec_class_init (GstKateDecClass * klass)
142 {
143   GObjectClass *gobject_class;
144   GstElementClass *gstelement_class;
145
146   gobject_class = (GObjectClass *) klass;
147   gstelement_class = (GstElementClass *) klass;
148
149   gobject_class->set_property = gst_kate_dec_set_property;
150   gobject_class->get_property = gst_kate_dec_get_property;
151
152   gst_kate_util_install_decoder_base_properties (gobject_class);
153
154   g_object_class_install_property (gobject_class, ARG_REMOVE_MARKUP,
155       g_param_spec_boolean ("remove-markup", "Remove markup",
156           "Remove markup from decoded text ?", FALSE,
157           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158
159   gstelement_class->change_state =
160       GST_DEBUG_FUNCPTR (gst_kate_dec_change_state);
161
162   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
163   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
164
165   gst_element_class_set_static_metadata (gstelement_class,
166       "Kate stream text decoder", "Codec/Decoder/Subtitle",
167       "Decodes Kate text streams",
168       "Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>");
169 }
170
171 /* initialize the new element
172  * instantiate pads and add them to element
173  * set functions
174  * initialize structure
175  */
176 static void
177 gst_kate_dec_init (GstKateDec * dec)
178 {
179   GST_DEBUG_OBJECT (dec, "gst_kate_dec_init");
180
181   dec->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
182   gst_pad_set_chain_function (dec->sinkpad,
183       GST_DEBUG_FUNCPTR (gst_kate_dec_chain));
184   gst_pad_set_query_function (dec->sinkpad,
185       GST_DEBUG_FUNCPTR (gst_kate_dec_sink_query));
186   gst_pad_set_event_function (dec->sinkpad,
187       GST_DEBUG_FUNCPTR (gst_kate_dec_sink_event));
188   gst_pad_use_fixed_caps (dec->sinkpad);
189   gst_pad_set_caps (dec->sinkpad,
190       gst_static_pad_template_get_caps (&sink_factory));
191   gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
192
193   dec->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
194   gst_pad_set_query_function (dec->srcpad,
195       GST_DEBUG_FUNCPTR (gst_kate_dec_src_query));
196   gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
197
198   gst_kate_util_decode_base_init (&dec->decoder, TRUE);
199
200   dec->src_caps = NULL;
201   dec->output_format = GST_KATE_FORMAT_UNDEFINED;
202   dec->remove_markup = FALSE;
203 }
204
205 static void
206 gst_kate_dec_set_property (GObject * object, guint prop_id,
207     const GValue * value, GParamSpec * pspec)
208 {
209   switch (prop_id) {
210     default:
211       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212       break;
213   }
214 }
215
216 static void
217 gst_kate_dec_get_property (GObject * object, guint prop_id,
218     GValue * value, GParamSpec * pspec)
219 {
220   GstKateDec *kd = GST_KATE_DEC (object);
221
222   switch (prop_id) {
223     case ARG_REMOVE_MARKUP:
224       g_value_set_boolean (value, kd->remove_markup);
225       break;
226     default:
227       if (!gst_kate_util_decoder_base_get_property (&kd->decoder, object,
228               prop_id, value, pspec)) {
229         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       }
231       break;
232   }
233 }
234
235 static GstFlowReturn
236 gst_kate_dec_handle_kate_event (GstKateDec * kd, const kate_event * ev)
237 {
238   GstFlowReturn rflow = GST_FLOW_OK;
239   GstKateFormat format = GST_KATE_FORMAT_UNDEFINED;
240   gchar *escaped;
241   GstBuffer *buffer;
242   size_t len;
243   gboolean plain = TRUE;
244
245   if (kd->remove_markup && ev->text_markup_type != kate_markup_none) {
246     size_t len0 = ev->len + 1;
247     escaped = g_strdup (ev->text);
248     if (escaped) {
249       kate_text_remove_markup (ev->text_encoding, escaped, &len0);
250     }
251     plain = TRUE;
252   } else if (ev->text_markup_type == kate_markup_none) {
253     /* no pango markup yet, escape text */
254     /* TODO: actually do the pango thing */
255     escaped = g_strdup (ev->text);
256     plain = TRUE;
257   } else {
258     escaped = g_strdup (ev->text);
259     plain = FALSE;
260   }
261
262   if (G_LIKELY (escaped)) {
263     len = strlen (escaped);
264     if (len > 0) {
265       GST_DEBUG_OBJECT (kd, "kate event: %s, escaped %s", ev->text, escaped);
266       buffer = gst_buffer_new_and_alloc (len + 1);
267       if (G_LIKELY (buffer)) {
268         GstCaps *caps;
269         if (plain)
270           format = GST_KATE_FORMAT_TEXT_UTF8;
271         else
272           format = GST_KATE_FORMAT_TEXT_PANGO_MARKUP;
273         if (format != kd->output_format) {
274           caps = gst_caps_new_simple ("text/x-raw", "format", G_TYPE_STRING,
275               (format == GST_KATE_FORMAT_TEXT_UTF8) ? "utf8" : "pango-markup",
276               NULL);
277           gst_pad_push_event (kd->srcpad, gst_event_new_caps (caps));
278           gst_caps_unref (caps);
279           kd->output_format = format;
280         }
281         /* allocate and copy the NULs, but don't include them in passed size */
282         gst_buffer_fill (buffer, 0, escaped, len + 1);
283         gst_buffer_resize (buffer, 0, len);
284         GST_BUFFER_TIMESTAMP (buffer) = ev->start_time * GST_SECOND;
285         GST_BUFFER_DURATION (buffer) =
286             (ev->end_time - ev->start_time) * GST_SECOND;
287         rflow = gst_pad_push (kd->srcpad, buffer);
288         if (rflow == GST_FLOW_NOT_LINKED) {
289           GST_DEBUG_OBJECT (kd, "source pad not linked, ignored");
290         } else if (rflow != GST_FLOW_OK) {
291           GST_WARNING_OBJECT (kd, "failed to push buffer: %s",
292               gst_flow_get_name (rflow));
293         }
294       } else {
295         GST_ELEMENT_ERROR (kd, STREAM, DECODE, (NULL),
296             ("Failed to create buffer"));
297         rflow = GST_FLOW_ERROR;
298       }
299     } else {
300       GST_WARNING_OBJECT (kd, "Empty string, nothing to do");
301       rflow = GST_FLOW_OK;
302     }
303     g_free (escaped);
304   } else {
305     GST_ELEMENT_ERROR (kd, STREAM, DECODE, (NULL),
306         ("Failed to allocate string"));
307     rflow = GST_FLOW_ERROR;
308   }
309
310   /* if there's a background paletted bitmap, construct a DVD SPU for it */
311   if (ev->bitmap && ev->palette) {
312     GstBuffer *buffer = gst_kate_spu_encode_spu (kd, ev);
313     if (buffer) {
314       GstCaps *caps;
315
316       GST_BUFFER_TIMESTAMP (buffer) = ev->start_time * GST_SECOND;
317       GST_BUFFER_DURATION (buffer) =
318           (ev->end_time - ev->start_time) * GST_SECOND;
319
320       if (kd->output_format != GST_KATE_FORMAT_SPU) {
321         caps = gst_caps_new_empty_simple (GST_KATE_SPU_MIME_TYPE);
322         gst_pad_push_event (kd->srcpad, gst_event_new_caps (caps));
323         gst_caps_unref (caps);
324         kd->output_format = GST_KATE_FORMAT_SPU;
325       }
326
327       rflow = gst_pad_push (kd->srcpad, buffer);
328       if (rflow == GST_FLOW_NOT_LINKED) {
329         GST_DEBUG_OBJECT (kd, "source pad not linked, ignored");
330       } else if (rflow != GST_FLOW_OK) {
331         GST_WARNING_OBJECT (kd, "failed to push buffer: %s",
332             gst_flow_get_name (rflow));
333       }
334     } else {
335       GST_ELEMENT_ERROR (kd, STREAM, DECODE, (NULL),
336           ("failed to create SPU from paletted bitmap"));
337       rflow = GST_FLOW_ERROR;
338     }
339   }
340   return rflow;
341 }
342
343 /* GstElement vmethod implementations */
344
345 /* chain function
346  * this function does the actual processing
347  */
348
349 static GstFlowReturn
350 gst_kate_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
351 {
352   GstKateDec *kd = GST_KATE_DEC (parent);
353   const kate_event *ev = NULL;
354   GstFlowReturn rflow = GST_FLOW_OK;
355
356   if (!gst_kate_util_decoder_base_update_segment (&kd->decoder,
357           GST_ELEMENT_CAST (kd), buf)) {
358     GST_WARNING_OBJECT (kd, "Out of segment!");
359     goto not_in_seg;
360   }
361
362   rflow =
363       gst_kate_util_decoder_base_chain_kate_packet (&kd->decoder,
364       GST_ELEMENT_CAST (kd), pad, buf, kd->srcpad, kd->srcpad, &kd->src_caps,
365       &ev);
366   if (G_UNLIKELY (rflow != GST_FLOW_OK)) {
367     gst_buffer_unref (buf);
368     return rflow;
369   }
370
371   if (ev) {
372     rflow = gst_kate_dec_handle_kate_event (kd, ev);
373   }
374
375 not_in_seg:
376   gst_buffer_unref (buf);
377   return rflow;
378 }
379
380 static GstStateChangeReturn
381 gst_kate_dec_change_state (GstElement * element, GstStateChange transition)
382 {
383   GstStateChangeReturn ret;
384   GstKateDec *kd = GST_KATE_DEC (element);
385
386   ret = gst_kate_decoder_base_change_state (&kd->decoder, element,
387       parent_class, transition);
388
389   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
390     gst_caps_replace (&kd->src_caps, NULL);
391   }
392
393   return ret;
394 }
395
396 gboolean
397 gst_kate_dec_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
398 {
399   GstKateDec *kd = GST_KATE_DEC (parent);
400   gboolean res =
401       gst_kate_decoder_base_sink_query (&kd->decoder, GST_ELEMENT_CAST (kd),
402       pad, parent, query);
403   return res;
404 }
405
406 static gboolean
407 gst_kate_dec_set_caps (GstKateDec * kd, GstCaps * caps)
408 {
409   GstStructure *structure = gst_caps_get_structure (caps, 0);
410   GstFlowReturn rflow = GST_FLOW_OK;
411
412   if (gst_structure_has_field (structure, "streamheader")) {
413     const GValue *value;
414     GstBuffer *buf;
415     const kate_event *ev;
416
417     value = gst_structure_get_value (structure, "streamheader");
418
419     if (GST_VALUE_HOLDS_BUFFER (value)) {
420       buf = gst_value_get_buffer (value);
421
422       gst_kate_util_decoder_base_chain_kate_packet (&kd->decoder,
423           GST_ELEMENT_CAST (kd), kd->sinkpad, buf, kd->srcpad, kd->srcpad,
424           &kd->src_caps, &ev);
425
426       if (ev) {
427         rflow = gst_kate_dec_handle_kate_event (kd, ev);
428       }
429     } else if (GST_VALUE_HOLDS_ARRAY (value)) {
430       gint i, size = gst_value_array_get_size (value);
431
432       for (i = 0; i < size; i++) {
433         const GValue *v = gst_value_array_get_value (value, i);
434
435         buf = gst_value_get_buffer (v);
436         gst_kate_util_decoder_base_chain_kate_packet (&kd->decoder,
437             GST_ELEMENT_CAST (kd), kd->sinkpad, buf, kd->srcpad, kd->srcpad,
438             &kd->src_caps, &ev);
439
440         if (ev) {
441           rflow = gst_kate_dec_handle_kate_event (kd, ev);
442           if (rflow != GST_FLOW_OK && rflow != GST_FLOW_NOT_LINKED)
443             break;
444         }
445       }
446     } else {
447       GST_WARNING_OBJECT (kd, "Unhandled streamheader type: %s",
448           G_VALUE_TYPE_NAME (value));
449     }
450   }
451
452   return rflow == GST_FLOW_OK || rflow == GST_FLOW_NOT_LINKED;
453 }
454
455 static gboolean
456 gst_kate_dec_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
457 {
458   GstKateDec *kd = GST_KATE_DEC (parent);
459   gboolean res = TRUE;
460
461   GST_LOG_OBJECT (pad, "Event on sink pad: %" GST_PTR_FORMAT, event);
462
463   switch (GST_EVENT_TYPE (event)) {
464     case GST_EVENT_CAPS:{
465       GstCaps *caps;
466
467       gst_event_parse_caps (event, &caps);
468       gst_kate_dec_set_caps (kd, caps);
469       break;
470     }
471     default:
472       break;
473   }
474
475   /* Delay events till we've set caps */
476   if (gst_kate_util_decoder_base_queue_event (&kd->decoder, event,
477           &gst_kate_dec_sink_handle_event, parent, pad)) {
478     return TRUE;
479   }
480
481   res = gst_kate_dec_sink_handle_event (pad, parent, event);
482
483   return res;
484 }
485
486 static gboolean
487 gst_kate_dec_sink_handle_event (GstPad * pad, GstObject * parent,
488     GstEvent * event)
489 {
490   GstKateDec *kd = GST_KATE_DEC (parent);
491
492   GST_LOG_OBJECT (pad, "Handling event on sink pad: %s",
493       GST_EVENT_TYPE_NAME (event));
494
495   switch (GST_EVENT_TYPE (event)) {
496     case GST_EVENT_SEGMENT:
497       break;
498
499     case GST_EVENT_FLUSH_START:
500       gst_kate_util_decoder_base_set_flushing (&kd->decoder, TRUE);
501       break;
502
503     case GST_EVENT_FLUSH_STOP:
504       gst_kate_util_decoder_base_set_flushing (&kd->decoder, FALSE);
505       break;
506
507     case GST_EVENT_TAG:{
508       GstTagList *tags;
509       gst_event_parse_tag (event, &tags);
510       gst_kate_util_decoder_base_add_tags (&kd->decoder, tags, FALSE);
511       gst_event_unref (event);
512       event = gst_kate_util_decoder_base_get_tag_event (&kd->decoder);
513       break;
514     }
515     default:
516       break;
517   }
518
519   return gst_pad_event_default (pad, parent, event);
520 }
521
522 static gboolean
523 gst_kate_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
524 {
525   GstKateDec *kd = GST_KATE_DEC (parent);
526   gboolean res = TRUE;
527
528   GST_LOG_OBJECT (pad, "Handling query on src pad: %s",
529       GST_QUERY_TYPE_NAME (query));
530
531   switch (GST_QUERY_TYPE (query)) {
532     case GST_QUERY_CAPS:{
533       GstCaps *caps;
534
535       if (kd->src_caps) {
536         GST_DEBUG_OBJECT (kd, "We have src caps %" GST_PTR_FORMAT,
537             kd->src_caps);
538         caps = gst_caps_copy (kd->src_caps);
539       } else {
540         GST_DEBUG_OBJECT (kd, "We have no src caps, using template caps");
541         caps = gst_static_pad_template_get_caps (&src_factory);
542       }
543
544       gst_query_set_caps_result (query, caps);
545       gst_caps_unref (caps);
546       break;
547     }
548     default:
549       res = gst_pad_query_default (pad, parent, query);
550       break;
551   }
552
553   return res;
554 }