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