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