1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2004 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5 * Copyright (C) 2016 Philippe Normand <pnormand@igalia.com>
6 * Copyright (C) 2016 Jan Schmidt <jan@centricular.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <glib.h>
33
34 #include "gstsubparse.h"
35 #include "gstssaparse.h"
36 #include "samiparse.h"
37 #include "tmplayerparse.h"
38 #include "mpl2parse.h"
39 #include "qttextparse.h"
40
41 GST_DEBUG_CATEGORY (sub_parse_debug);
42
43 #define DEFAULT_ENCODING NULL
44 #define ATTRIBUTE_REGEX "\\s?[a-zA-Z0-9\\. \t\\(\\)]*"
45 static const gchar *allowed_srt_tags[] = { "i", "b", "u", NULL };
46 static const gchar *allowed_vtt_tags[] =
47 { "i", "b", "c", "u", "v", "ruby", "rt", NULL };
48
49 enum
50 {
51 PROP_0,
52 PROP_ENCODING,
53 PROP_VIDEOFPS
54 };
55
56 static void
57 gst_sub_parse_set_property (GObject * object, guint prop_id,
58 const GValue * value, GParamSpec * pspec);
59 static void
60 gst_sub_parse_get_property (GObject * object, guint prop_id,
61 GValue * value, GParamSpec * pspec);
62
63
64 static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
65 GST_PAD_SINK,
66 GST_PAD_ALWAYS,
67 GST_STATIC_CAPS ("application/x-subtitle; application/x-subtitle-sami; "
68 "application/x-subtitle-tmplayer; application/x-subtitle-mpl2; "
69 "application/x-subtitle-dks; application/x-subtitle-qttext;"
70 "application/x-subtitle-lrc; application/x-subtitle-vtt")
71 );
72
73 static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
74 GST_PAD_SRC,
75 GST_PAD_ALWAYS,
76 GST_STATIC_CAPS ("text/x-raw, format= { pango-markup, utf8 }")
77 );
78
79
80 static gboolean gst_sub_parse_src_event (GstPad * pad, GstObject * parent,
81 GstEvent * event);
82 static gboolean gst_sub_parse_src_query (GstPad * pad, GstObject * parent,
83 GstQuery * query);
84 static gboolean gst_sub_parse_sink_event (GstPad * pad, GstObject * parent,
85 GstEvent * event);
86
87 static GstStateChangeReturn gst_sub_parse_change_state (GstElement * element,
88 GstStateChange transition);
89
90 static GstFlowReturn gst_sub_parse_chain (GstPad * sinkpad, GstObject * parent,
91 GstBuffer * buf);
92
93 #define gst_sub_parse_parent_class parent_class
94 G_DEFINE_TYPE (GstSubParse, gst_sub_parse, GST_TYPE_ELEMENT);
95
96 static void
gst_sub_parse_dispose(GObject * object)97 gst_sub_parse_dispose (GObject * object)
98 {
99 GstSubParse *subparse = GST_SUBPARSE (object);
100
101 GST_DEBUG_OBJECT (subparse, "cleaning up subtitle parser");
102
103 if (subparse->encoding) {
104 g_free (subparse->encoding);
105 subparse->encoding = NULL;
106 }
107
108 if (subparse->detected_encoding) {
109 g_free (subparse->detected_encoding);
110 subparse->detected_encoding = NULL;
111 }
112
113 if (subparse->adapter) {
114 g_object_unref (subparse->adapter);
115 subparse->adapter = NULL;
116 }
117
118 if (subparse->textbuf) {
119 g_string_free (subparse->textbuf, TRUE);
120 subparse->textbuf = NULL;
121 }
122
123 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
124 }
125
126 static void
gst_sub_parse_class_init(GstSubParseClass * klass)127 gst_sub_parse_class_init (GstSubParseClass * klass)
128 {
129 GObjectClass *object_class = G_OBJECT_CLASS (klass);
130 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
131
132 object_class->dispose = gst_sub_parse_dispose;
133 object_class->set_property = gst_sub_parse_set_property;
134 object_class->get_property = gst_sub_parse_get_property;
135
136 gst_element_class_add_static_pad_template (element_class, &sink_templ);
137 gst_element_class_add_static_pad_template (element_class, &src_templ);
138 gst_element_class_set_static_metadata (element_class,
139 "Subtitle parser", "Codec/Parser/Subtitle",
140 "Parses subtitle (.sub) files into text streams",
141 "Gustavo J. A. M. Carneiro <gjc@inescporto.pt>, "
142 "GStreamer maintainers <gstreamer-devel@lists.freedesktop.org>");
143
144 element_class->change_state = gst_sub_parse_change_state;
145
146 g_object_class_install_property (object_class, PROP_ENCODING,
147 g_param_spec_string ("subtitle-encoding", "subtitle charset encoding",
148 "Encoding to assume if input subtitles are not in UTF-8 or any other "
149 "Unicode encoding. If not set, the GST_SUBTITLE_ENCODING environment "
150 "variable will be checked for an encoding to use. If that is not set "
151 "either, ISO-8859-15 will be assumed.", DEFAULT_ENCODING,
152 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153
154 g_object_class_install_property (object_class, PROP_VIDEOFPS,
155 gst_param_spec_fraction ("video-fps", "Video framerate",
156 "Framerate of the video stream. This is needed by some subtitle "
157 "formats to synchronize subtitles and video properly. If not set "
158 "and the subtitle format requires it subtitles may be out of sync.",
159 0, 1, G_MAXINT, 1, 24000, 1001,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 }
162
163 static void
gst_sub_parse_init(GstSubParse * subparse)164 gst_sub_parse_init (GstSubParse * subparse)
165 {
166 subparse->sinkpad = gst_pad_new_from_static_template (&sink_templ, "sink");
167 gst_pad_set_chain_function (subparse->sinkpad,
168 GST_DEBUG_FUNCPTR (gst_sub_parse_chain));
169 gst_pad_set_event_function (subparse->sinkpad,
170 GST_DEBUG_FUNCPTR (gst_sub_parse_sink_event));
171 gst_element_add_pad (GST_ELEMENT (subparse), subparse->sinkpad);
172
173 subparse->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
174 gst_pad_set_event_function (subparse->srcpad,
175 GST_DEBUG_FUNCPTR (gst_sub_parse_src_event));
176 gst_pad_set_query_function (subparse->srcpad,
177 GST_DEBUG_FUNCPTR (gst_sub_parse_src_query));
178 gst_element_add_pad (GST_ELEMENT (subparse), subparse->srcpad);
179
180 subparse->textbuf = g_string_new (NULL);
181 subparse->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
182 subparse->flushing = FALSE;
183 gst_segment_init (&subparse->segment, GST_FORMAT_TIME);
184 subparse->need_segment = TRUE;
185 subparse->encoding = g_strdup (DEFAULT_ENCODING);
186 subparse->detected_encoding = NULL;
187 subparse->adapter = gst_adapter_new ();
188
189 subparse->fps_n = 24000;
190 subparse->fps_d = 1001;
191 }
192
193 /*
194 * Source pad functions.
195 */
196
197 static gboolean
gst_sub_parse_src_query(GstPad * pad,GstObject * parent,GstQuery * query)198 gst_sub_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
199 {
200 GstSubParse *self = GST_SUBPARSE (parent);
201 gboolean ret = FALSE;
202
203 GST_DEBUG ("Handling %s query", GST_QUERY_TYPE_NAME (query));
204
205 switch (GST_QUERY_TYPE (query)) {
206 case GST_QUERY_POSITION:{
207 GstFormat fmt;
208
209 gst_query_parse_position (query, &fmt, NULL);
210 if (fmt != GST_FORMAT_TIME) {
211 ret = gst_pad_peer_query (self->sinkpad, query);
212 } else {
213 ret = TRUE;
214 gst_query_set_position (query, GST_FORMAT_TIME, self->segment.position);
215 }
216 break;
217 }
218 case GST_QUERY_SEEKING:
219 {
220 GstFormat fmt;
221 gboolean seekable = FALSE;
222
223 ret = TRUE;
224
225 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
226 if (fmt == GST_FORMAT_TIME) {
227 GstQuery *peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
228
229 seekable = gst_pad_peer_query (self->sinkpad, peerquery);
230 if (seekable)
231 gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
232 gst_query_unref (peerquery);
233 }
234
235 gst_query_set_seeking (query, fmt, seekable, seekable ? 0 : -1, -1);
236 break;
237 }
238 default:
239 ret = gst_pad_query_default (pad, parent, query);
240 break;
241 }
242
243 return ret;
244 }
245
246 static gboolean
gst_sub_parse_src_event(GstPad * pad,GstObject * parent,GstEvent * event)247 gst_sub_parse_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
248 {
249 GstSubParse *self = GST_SUBPARSE (parent);
250 gboolean ret = FALSE;
251
252 GST_DEBUG ("Handling %s event", GST_EVENT_TYPE_NAME (event));
253
254 switch (GST_EVENT_TYPE (event)) {
255 case GST_EVENT_SEEK:
256 {
257 GstFormat format;
258 GstSeekFlags flags;
259 GstSeekType start_type, stop_type;
260 gint64 start, stop;
261 gdouble rate;
262 gboolean update;
263
264 gst_event_parse_seek (event, &rate, &format, &flags,
265 &start_type, &start, &stop_type, &stop);
266
267 if (format != GST_FORMAT_TIME) {
268 GST_WARNING_OBJECT (self, "we only support seeking in TIME format");
269 gst_event_unref (event);
270 goto beach;
271 }
272
273 /* Convert that seek to a seeking in bytes at position 0,
274 FIXME: could use an index */
275 ret = gst_pad_push_event (self->sinkpad,
276 gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
277 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, 0));
278
279 if (ret) {
280 /* Apply the seek to our segment */
281 gst_segment_do_seek (&self->segment, rate, format, flags,
282 start_type, start, stop_type, stop, &update);
283
284 GST_DEBUG_OBJECT (self, "segment after seek: %" GST_SEGMENT_FORMAT,
285 &self->segment);
286
287 /* will mark need_segment when receiving segment from upstream,
288 * after FLUSH and all that has happened,
289 * rather than racing with chain */
290 } else {
291 GST_WARNING_OBJECT (self, "seek to 0 bytes failed");
292 }
293
294 gst_event_unref (event);
295 break;
296 }
297 default:
298 ret = gst_pad_event_default (pad, parent, event);
299 break;
300 }
301
302 beach:
303 return ret;
304 }
305
306 static void
gst_sub_parse_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)307 gst_sub_parse_set_property (GObject * object, guint prop_id,
308 const GValue * value, GParamSpec * pspec)
309 {
310 GstSubParse *subparse = GST_SUBPARSE (object);
311
312 GST_OBJECT_LOCK (subparse);
313 switch (prop_id) {
314 case PROP_ENCODING:
315 g_free (subparse->encoding);
316 subparse->encoding = g_value_dup_string (value);
317 GST_LOG_OBJECT (object, "subtitle encoding set to %s",
318 GST_STR_NULL (subparse->encoding));
319 break;
320 case PROP_VIDEOFPS:
321 {
322 subparse->fps_n = gst_value_get_fraction_numerator (value);
323 subparse->fps_d = gst_value_get_fraction_denominator (value);
324 GST_DEBUG_OBJECT (object, "video framerate set to %d/%d", subparse->fps_n,
325 subparse->fps_d);
326
327 if (!subparse->state.have_internal_fps) {
328 subparse->state.fps_n = subparse->fps_n;
329 subparse->state.fps_d = subparse->fps_d;
330 }
331 break;
332 }
333 default:
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
335 break;
336 }
337 GST_OBJECT_UNLOCK (subparse);
338 }
339
340 static void
gst_sub_parse_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)341 gst_sub_parse_get_property (GObject * object, guint prop_id,
342 GValue * value, GParamSpec * pspec)
343 {
344 GstSubParse *subparse = GST_SUBPARSE (object);
345
346 GST_OBJECT_LOCK (subparse);
347 switch (prop_id) {
348 case PROP_ENCODING:
349 g_value_set_string (value, subparse->encoding);
350 break;
351 case PROP_VIDEOFPS:
352 gst_value_set_fraction (value, subparse->fps_n, subparse->fps_d);
353 break;
354 default:
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 break;
357 }
358 GST_OBJECT_UNLOCK (subparse);
359 }
360
361 static const gchar *
gst_sub_parse_get_format_description(GstSubParseFormat format)362 gst_sub_parse_get_format_description (GstSubParseFormat format)
363 {
364 switch (format) {
365 case GST_SUB_PARSE_FORMAT_MDVDSUB:
366 return "MicroDVD";
367 case GST_SUB_PARSE_FORMAT_SUBRIP:
368 return "SubRip";
369 case GST_SUB_PARSE_FORMAT_MPSUB:
370 return "MPSub";
371 case GST_SUB_PARSE_FORMAT_SAMI:
372 return "SAMI";
373 case GST_SUB_PARSE_FORMAT_TMPLAYER:
374 return "TMPlayer";
375 case GST_SUB_PARSE_FORMAT_MPL2:
376 return "MPL2";
377 case GST_SUB_PARSE_FORMAT_SUBVIEWER:
378 return "SubViewer";
379 case GST_SUB_PARSE_FORMAT_DKS:
380 return "DKS";
381 case GST_SUB_PARSE_FORMAT_VTT:
382 return "WebVTT";
383 case GST_SUB_PARSE_FORMAT_QTTEXT:
384 return "QTtext";
385 case GST_SUB_PARSE_FORMAT_LRC:
386 return "LRC";
387 default:
388 case GST_SUB_PARSE_FORMAT_UNKNOWN:
389 break;
390 }
391 return NULL;
392 }
393
394 static gchar *
gst_convert_to_utf8(const gchar * str,gsize len,const gchar * encoding,gsize * consumed,GError ** err)395 gst_convert_to_utf8 (const gchar * str, gsize len, const gchar * encoding,
396 gsize * consumed, GError ** err)
397 {
398 gchar *ret = NULL;
399
400 *consumed = 0;
401 /* The char cast is necessary in glib < 2.24 */
402 ret =
403 g_convert_with_fallback (str, len, "UTF-8", encoding, (char *) "*",
404 consumed, NULL, err);
405 if (ret == NULL)
406 return ret;
407
408 /* + 3 to skip UTF-8 BOM if it was added */
409 len = strlen (ret);
410 if (len >= 3 && (guint8) ret[0] == 0xEF && (guint8) ret[1] == 0xBB
411 && (guint8) ret[2] == 0xBF)
412 memmove (ret, ret + 3, len + 1 - 3);
413
414 return ret;
415 }
416
417 static gchar *
detect_encoding(const gchar * str,gsize len)418 detect_encoding (const gchar * str, gsize len)
419 {
420 if (len >= 3 && (guint8) str[0] == 0xEF && (guint8) str[1] == 0xBB
421 && (guint8) str[2] == 0xBF)
422 return g_strdup ("UTF-8");
423
424 if (len >= 2 && (guint8) str[0] == 0xFE && (guint8) str[1] == 0xFF)
425 return g_strdup ("UTF-16BE");
426
427 if (len >= 2 && (guint8) str[0] == 0xFF && (guint8) str[1] == 0xFE)
428 return g_strdup ("UTF-16LE");
429
430 if (len >= 4 && (guint8) str[0] == 0x00 && (guint8) str[1] == 0x00
431 && (guint8) str[2] == 0xFE && (guint8) str[3] == 0xFF)
432 return g_strdup ("UTF-32BE");
433
434 if (len >= 4 && (guint8) str[0] == 0xFF && (guint8) str[1] == 0xFE
435 && (guint8) str[2] == 0x00 && (guint8) str[3] == 0x00)
436 return g_strdup ("UTF-32LE");
437
438 return NULL;
439 }
440
441 static gchar *
convert_encoding(GstSubParse * self,const gchar * str,gsize len,gsize * consumed)442 convert_encoding (GstSubParse * self, const gchar * str, gsize len,
443 gsize * consumed)
444 {
445 const gchar *encoding;
446 GError *err = NULL;
447 gchar *ret = NULL;
448
449 *consumed = 0;
450
451 /* First try any detected encoding */
452 if (self->detected_encoding) {
453 ret =
454 gst_convert_to_utf8 (str, len, self->detected_encoding, consumed, &err);
455
456 if (!err)
457 return ret;
458
459 GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
460 self->detected_encoding, err->message);
461 g_free (self->detected_encoding);
462 self->detected_encoding = NULL;
463 g_clear_error (&err);
464 }
465
466 /* Otherwise check if it's UTF8 */
467 if (self->valid_utf8) {
468 if (g_utf8_validate (str, len, NULL)) {
469 GST_LOG_OBJECT (self, "valid UTF-8, no conversion needed");
470 *consumed = len;
471 return g_strndup (str, len);
472 }
473 GST_INFO_OBJECT (self, "invalid UTF-8!");
474 self->valid_utf8 = FALSE;
475 }
476
477 /* Else try fallback */
478 encoding = self->encoding;
479 if (encoding == NULL || *encoding == '\0') {
480 encoding = g_getenv ("GST_SUBTITLE_ENCODING");
481 }
482 if (encoding == NULL || *encoding == '\0') {
483 /* if local encoding is UTF-8 and no encoding specified
484 * via the environment variable, assume ISO-8859-15 */
485 if (g_get_charset (&encoding)) {
486 encoding = "ISO-8859-15";
487 }
488 }
489
490 ret = gst_convert_to_utf8 (str, len, encoding, consumed, &err);
491
492 if (err) {
493 GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
494 encoding, err->message);
495 g_clear_error (&err);
496
497 /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
498 ret = gst_convert_to_utf8 (str, len, "ISO-8859-15", consumed, NULL);
499 }
500
501 GST_LOG_OBJECT (self,
502 "successfully converted %" G_GSIZE_FORMAT " characters from %s to UTF-8"
503 "%s", len, encoding, (err) ? " , using ISO-8859-15 as fallback" : "");
504
505 return ret;
506 }
507
508 static gchar *
get_next_line(GstSubParse * self)509 get_next_line (GstSubParse * self)
510 {
511 char *line = NULL;
512 const char *line_end;
513 int line_len;
514 gboolean have_r = FALSE;
515
516 line_end = strchr (self->textbuf->str, '\n');
517
518 if (!line_end) {
519 /* end-of-line not found; return for more data */
520 return NULL;
521 }
522
523 /* get rid of '\r' */
524 if (line_end != self->textbuf->str && *(line_end - 1) == '\r') {
525 line_end--;
526 have_r = TRUE;
527 }
528
529 line_len = line_end - self->textbuf->str;
530 line = g_strndup (self->textbuf->str, line_len);
531 self->textbuf = g_string_erase (self->textbuf, 0,
532 line_len + (have_r ? 2 : 1));
533 return line;
534 }
535
536 static gchar *
parse_mdvdsub(ParserState * state,const gchar * line)537 parse_mdvdsub (ParserState * state, const gchar * line)
538 {
539 const gchar *line_split;
540 gchar *line_chunk;
541 guint start_frame, end_frame;
542 guint64 clip_start = 0, clip_stop = 0;
543 gboolean in_seg = FALSE;
544 GString *markup;
545 gchar *ret;
546
547 /* style variables */
548 gboolean italic;
549 gboolean bold;
550 guint fontsize;
551 gdouble fps = 0.0;
552
553 if (sscanf (line, "{%u}{%u}", &start_frame, &end_frame) != 2) {
554 g_warning ("Parse of the following line, assumed to be in microdvd .sub"
555 " format, failed:\n%s", line);
556 return NULL;
557 }
558
559 /* skip the {%u}{%u} part */
560 line = strchr (line, '}') + 1;
561 line = strchr (line, '}') + 1;
562
563 /* see if there's a first line with a framerate */
564 if (start_frame == 1 && end_frame == 1) {
565 gchar *rest, *end = NULL;
566
567 rest = g_strdup (line);
568 g_strdelimit (rest, ",", '.');
569 fps = g_ascii_strtod (rest, &end);
570 if (end != rest) {
571 gst_util_double_to_fraction (fps, &state->fps_n, &state->fps_d);
572 GST_INFO ("framerate from file: %d/%d ('%s')", state->fps_n,
573 state->fps_d, rest);
574 }
575 g_free (rest);
576 return NULL;
577 }
578
579 state->start_time =
580 gst_util_uint64_scale (start_frame, GST_SECOND * state->fps_d,
581 state->fps_n);
582 state->duration =
583 gst_util_uint64_scale (end_frame - start_frame, GST_SECOND * state->fps_d,
584 state->fps_n);
585
586 /* Check our segment start/stop */
587 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
588 state->start_time, state->start_time + state->duration, &clip_start,
589 &clip_stop);
590
591 /* No need to parse that text if it's out of segment */
592 if (in_seg) {
593 state->start_time = clip_start;
594 state->duration = clip_stop - clip_start;
595 } else {
596 return NULL;
597 }
598
599 markup = g_string_new (NULL);
600 while (1) {
601 italic = FALSE;
602 bold = FALSE;
603 fontsize = 0;
604 /* parse style markup */
605 if (strncmp (line, "{y:i}", 5) == 0) {
606 italic = TRUE;
607 line = strchr (line, '}') + 1;
608 }
609 if (strncmp (line, "{y:b}", 5) == 0) {
610 bold = TRUE;
611 line = strchr (line, '}') + 1;
612 }
613 if (sscanf (line, "{s:%u}", &fontsize) == 1) {
614 line = strchr (line, '}') + 1;
615 }
616 /* forward slashes at beginning/end signify italics too */
617 if (g_str_has_prefix (line, "/")) {
618 italic = TRUE;
619 ++line;
620 }
621 if ((line_split = strchr (line, '|')))
622 line_chunk = g_markup_escape_text (line, line_split - line);
623 else
624 line_chunk = g_markup_escape_text (line, strlen (line));
625
626 /* Remove italics markers at end of line/stanza (CHECKME: are end slashes
627 * always at the end of a line or can they span multiple lines?) */
628 if (g_str_has_suffix (line_chunk, "/")) {
629 line_chunk[strlen (line_chunk) - 1] = '\0';
630 }
631
632 markup = g_string_append (markup, "<span");
633 if (italic)
634 g_string_append (markup, " style=\"italic\"");
635 if (bold)
636 g_string_append (markup, " weight=\"bold\"");
637 if (fontsize)
638 g_string_append_printf (markup, " size=\"%u\"", fontsize * 1000);
639 g_string_append_printf (markup, ">%s</span>", line_chunk);
640 g_free (line_chunk);
641 if (line_split) {
642 g_string_append (markup, "\n");
643 line = line_split + 1;
644 } else {
645 break;
646 }
647 }
648 ret = markup->str;
649 g_string_free (markup, FALSE);
650 GST_DEBUG ("parse_mdvdsub returning (%f+%f): %s",
651 state->start_time / (double) GST_SECOND,
652 state->duration / (double) GST_SECOND, ret);
653 return ret;
654 }
655
656 static void
strip_trailing_newlines(gchar * txt)657 strip_trailing_newlines (gchar * txt)
658 {
659 if (txt) {
660 guint len;
661
662 len = strlen (txt);
663 while (len > 1 && txt[len - 1] == '\n') {
664 txt[len - 1] = '\0';
665 --len;
666 }
667 }
668 }
669
670 /* we want to escape text in general, but retain basic markup like
671 * <i></i>, <u></u>, and <b></b>. The easiest and safest way is to
672 * just unescape a white list of allowed markups again after
673 * escaping everything (the text between these simple markers isn't
674 * necessarily escaped, so it seems best to do it like this) */
675 static void
subrip_unescape_formatting(gchar * txt,gconstpointer allowed_tags_ptr,gboolean allows_tag_attributes)676 subrip_unescape_formatting (gchar * txt, gconstpointer allowed_tags_ptr,
677 gboolean allows_tag_attributes)
678 {
679 gchar *res;
680 GRegex *tag_regex;
681 gchar *allowed_tags_pattern, *search_pattern;
682 const gchar *replace_pattern;
683
684 /* No processing needed if no escaped tag marker found in the string. */
685 if (strstr (txt, "<") == NULL)
686 return;
687
688 /* Build a list of alternates for our regexp.
689 * FIXME: Could be built once and stored */
690 allowed_tags_pattern = g_strjoinv ("|", (gchar **) allowed_tags_ptr);
691 /* Look for starting/ending escaped tags with optional attributes. */
692 search_pattern = g_strdup_printf ("<(/)?\\ *(%s)(%s)>",
693 allowed_tags_pattern, ATTRIBUTE_REGEX);
694 /* And unescape appropriately */
695 if (allows_tag_attributes) {
696 replace_pattern = "<\\1\\2\\3>";
697 } else {
698 replace_pattern = "<\\1\\2>";
699 }
700
701 tag_regex = g_regex_new (search_pattern, 0, 0, NULL);
702 res = g_regex_replace (tag_regex, txt, strlen (txt), 0,
703 replace_pattern, 0, NULL);
704
705 /* res will always be shorter than the input or identical, so this
706 * copy is OK */
707 strcpy (txt, res);
708
709 g_free (res);
710 g_free (search_pattern);
711 g_free (allowed_tags_pattern);
712
713 g_regex_unref (tag_regex);
714 }
715
716
717 static gboolean
subrip_remove_unhandled_tag(gchar * start,gchar * stop)718 subrip_remove_unhandled_tag (gchar * start, gchar * stop)
719 {
720 gchar *tag, saved;
721
722 tag = start + strlen ("<");
723 if (*tag == '/')
724 ++tag;
725
726 if (g_ascii_tolower (*tag) < 'a' || g_ascii_tolower (*tag) > 'z')
727 return FALSE;
728
729 saved = *stop;
730 *stop = '\0';
731 GST_LOG ("removing unhandled tag '%s'", start);
732 *stop = saved;
733 memmove (start, stop, strlen (stop) + 1);
734 return TRUE;
735 }
736
737 /* remove tags we haven't explicitly allowed earlier on, like font tags
738 * for example */
739 static void
subrip_remove_unhandled_tags(gchar * txt)740 subrip_remove_unhandled_tags (gchar * txt)
741 {
742 gchar *pos, *gt;
743
744 for (pos = txt; pos != NULL && *pos != '\0'; ++pos) {
745 if (strncmp (pos, "<", 4) == 0 && (gt = strstr (pos + 4, ">"))) {
746 if (subrip_remove_unhandled_tag (pos, gt + strlen (">")))
747 --pos;
748 }
749 }
750 }
751
752 /* we only allow a fixed set of tags like <i>, <u> and <b>, so let's
753 * take a simple approach. This code assumes the input has been
754 * escaped and subrip_unescape_formatting() has then been run over the
755 * input! This function adds missing closing markup tags and removes
756 * broken closing tags for tags that have never been opened. */
757 static void
subrip_fix_up_markup(gchar ** p_txt,gconstpointer allowed_tags_ptr)758 subrip_fix_up_markup (gchar ** p_txt, gconstpointer allowed_tags_ptr)
759 {
760 gchar *cur, *next_tag;
761 GPtrArray *open_tags = NULL;
762 guint num_open_tags = 0;
763 const gchar *iter_tag;
764 guint offset = 0;
765 guint index;
766 gchar *cur_tag;
767 gchar *end_tag;
768 GRegex *tag_regex;
769 GMatchInfo *match_info;
770 gchar **allowed_tags = (gchar **) allowed_tags_ptr;
771
772 g_assert (*p_txt != NULL);
773
774 open_tags = g_ptr_array_new_with_free_func (g_free);
775 cur = *p_txt;
776 while (*cur != '\0') {
777 next_tag = strchr (cur, '<');
778 if (next_tag == NULL)
779 break;
780 offset = 0;
781 index = 0;
782 while (index < g_strv_length (allowed_tags)) {
783 iter_tag = allowed_tags[index];
784 /* Look for a white listed tag */
785 cur_tag = g_strconcat ("<", iter_tag, ATTRIBUTE_REGEX, ">", NULL);
786 tag_regex = g_regex_new (cur_tag, 0, 0, NULL);
787 (void) g_regex_match (tag_regex, next_tag, 0, &match_info);
788
789 if (g_match_info_matches (match_info)) {
790 gint start_pos, end_pos;
791 gchar *word = g_match_info_fetch (match_info, 0);
792 g_match_info_fetch_pos (match_info, 0, &start_pos, &end_pos);
793 if (start_pos == 0) {
794 offset = strlen (word);
795 }
796 g_free (word);
797 }
798 g_match_info_free (match_info);
799 g_regex_unref (tag_regex);
800 g_free (cur_tag);
801 index++;
802 if (offset) {
803 /* OK we found a tag, let's keep track of it */
804 g_ptr_array_add (open_tags, g_ascii_strdown (iter_tag, -1));
805 ++num_open_tags;
806 break;
807 }
808 }
809
810 if (offset) {
811 next_tag += offset;
812 cur = next_tag;
813 continue;
814 }
815
816 if (*next_tag == '<' && *(next_tag + 1) == '/') {
817 end_tag = strchr (cur, '>');
818 if (end_tag) {
819 const gchar *last = NULL;
820 if (num_open_tags > 0)
821 last = g_ptr_array_index (open_tags, num_open_tags - 1);
822 if (num_open_tags == 0
823 || g_ascii_strncasecmp (end_tag - 1, last, strlen (last))) {
824 GST_LOG ("broken input, closing tag '%s' is not open", next_tag);
825 /* Move everything after the tag end, including closing \0 */
826 memmove (next_tag, end_tag + 1, strlen (end_tag));
827 cur = next_tag;
828 continue;
829 } else {
830 --num_open_tags;
831 g_ptr_array_remove_index (open_tags, num_open_tags);
832 }
833 }
834 }
835 ++next_tag;
836 cur = next_tag;
837 }
838
839 if (num_open_tags > 0) {
840 GString *s;
841
842 s = g_string_new (*p_txt);
843 while (num_open_tags > 0) {
844 GST_LOG ("adding missing closing tag '%s'",
845 (char *) g_ptr_array_index (open_tags, num_open_tags - 1));
846 g_string_append_c (s, '<');
847 g_string_append_c (s, '/');
848 g_string_append (s, g_ptr_array_index (open_tags, num_open_tags - 1));
849 g_string_append_c (s, '>');
850 --num_open_tags;
851 }
852 g_free (*p_txt);
853 *p_txt = g_string_free (s, FALSE);
854 }
855 g_ptr_array_free (open_tags, TRUE);
856 }
857
858 static gboolean
parse_subrip_time(const gchar * ts_string,GstClockTime * t)859 parse_subrip_time (const gchar * ts_string, GstClockTime * t)
860 {
861 gchar s[128] = { '\0', };
862 gchar *end, *p;
863 guint hour, min, sec, msec, len;
864
865 while (*ts_string == ' ')
866 ++ts_string;
867
868 g_strlcpy (s, ts_string, sizeof (s));
869 if ((end = strstr (s, "-->")))
870 *end = '\0';
871 g_strchomp (s);
872
873 /* ms may be in these formats:
874 * hh:mm:ss,500 = 500ms
875 * hh:mm:ss, 5 = 5ms
876 * hh:mm:ss, 5 = 50ms
877 * hh:mm:ss, 50 = 50ms
878 * hh:mm:ss,5 = 500ms
879 * and the same with . instead of ,.
880 * sscanf() doesn't differentiate between ' 5' and '5' so munge
881 * the white spaces within the timestamp to '0' (I'm sure there's a
882 * way to make sscanf() do this for us, but how?)
883 */
884 g_strdelimit (s, " ", '0');
885 g_strdelimit (s, ".", ',');
886
887 /* make sure we have exactly three digits after he comma */
888 p = strchr (s, ',');
889 if (p == NULL) {
890 /* If there isn't a ',' the timestamp is broken */
891 /* https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/issues/532#note_100179 */
892 GST_WARNING ("failed to parse subrip timestamp string '%s'", s);
893 return FALSE;
894 }
895
896 ++p;
897 len = strlen (p);
898 if (len > 3) {
899 p[3] = '\0';
900 } else
901 while (len < 3) {
902 g_strlcat (&p[len], "0", 2);
903 ++len;
904 }
905
906 GST_LOG ("parsing timestamp '%s'", s);
907 if (sscanf (s, "%u:%u:%u,%u", &hour, &min, &sec, &msec) != 4) {
908 GST_WARNING ("failed to parse subrip timestamp string '%s'", s);
909 return FALSE;
910 }
911
912 *t = ((hour * 3600) + (min * 60) + sec) * GST_SECOND + msec * GST_MSECOND;
913 return TRUE;
914 }
915
916 /* cue settings are part of the WebVTT specification. They are
917 * declared after the time interval in the first line of the
918 * cue. Example: 00:00:01,000 --> 00:00:02,000 D:vertical-lr A:start
919 * See also http://www.whatwg.org/specs/web-apps/current-work/webvtt.html
920 */
921 static void
parse_webvtt_cue_settings(ParserState * state,const gchar * settings)922 parse_webvtt_cue_settings (ParserState * state, const gchar * settings)
923 {
924 gchar **splitted_settings = g_strsplit_set (settings, " \t", -1);
925 gint i = 0;
926 gint16 text_position, text_size;
927 gint16 line_position;
928 gboolean vertical_found = FALSE;
929 gboolean alignment_found = FALSE;
930
931 while (i < g_strv_length (splitted_settings)) {
932 gboolean valid_tag = FALSE;
933 switch (splitted_settings[i][0]) {
934 case 'T':
935 if (sscanf (splitted_settings[i], "T:%" G_GINT16_FORMAT "%%",
936 &text_position) > 0) {
937 state->text_position = (guint8) text_position;
938 valid_tag = TRUE;
939 }
940 break;
941 case 'D':
942 if (strlen (splitted_settings[i]) > 2) {
943 vertical_found = TRUE;
944 g_free (state->vertical);
945 state->vertical = g_strdup (splitted_settings[i] + 2);
946 valid_tag = TRUE;
947 }
948 break;
949 case 'L':
950 if (g_str_has_suffix (splitted_settings[i], "%")) {
951 if (sscanf (splitted_settings[i], "L:%" G_GINT16_FORMAT "%%",
952 &line_position) > 0) {
953 state->line_position = line_position;
954 valid_tag = TRUE;
955 }
956 } else {
957 if (sscanf (splitted_settings[i], "L:%" G_GINT16_FORMAT,
958 &line_position) > 0) {
959 state->line_number = line_position;
960 valid_tag = TRUE;
961 }
962 }
963 break;
964 case 'S':
965 if (sscanf (splitted_settings[i], "S:%" G_GINT16_FORMAT "%%",
966 &text_size) > 0) {
967 state->text_size = (guint8) text_size;
968 valid_tag = TRUE;
969 }
970 break;
971 case 'A':
972 if (strlen (splitted_settings[i]) > 2) {
973 g_free (state->alignment);
974 state->alignment = g_strdup (splitted_settings[i] + 2);
975 alignment_found = TRUE;
976 valid_tag = TRUE;
977 }
978 break;
979 default:
980 break;
981 }
982 if (!valid_tag) {
983 GST_LOG ("Invalid or unrecognised setting found: %s",
984 splitted_settings[i]);
985 }
986 i++;
987 }
988 g_strfreev (splitted_settings);
989 if (!vertical_found) {
990 g_free (state->vertical);
991 state->vertical = g_strdup ("");
992 }
993 if (!alignment_found) {
994 g_free (state->alignment);
995 state->alignment = g_strdup ("");
996 }
997 }
998
999 static gchar *
parse_subrip(ParserState * state,const gchar * line)1000 parse_subrip (ParserState * state, const gchar * line)
1001 {
1002 gchar *ret;
1003
1004 switch (state->state) {
1005 case 0:{
1006 char *endptr;
1007 guint64 id;
1008
1009 /* looking for a single integer as a Cue ID, but we
1010 * don't actually use it */
1011 errno = 0;
1012 id = g_ascii_strtoull (line, &endptr, 10);
1013 if (id == G_MAXUINT64 && errno == ERANGE)
1014 state->state = 1;
1015 else if (id == 0 && errno == EINVAL)
1016 state->state = 1;
1017 else if (endptr != line && *endptr == '\0')
1018 state->state = 1;
1019 return NULL;
1020 }
1021 case 1:
1022 {
1023 GstClockTime ts_start, ts_end;
1024 gchar *end_time;
1025
1026 /* looking for start_time --> end_time */
1027 if ((end_time = strstr (line, " --> ")) &&
1028 parse_subrip_time (line, &ts_start) &&
1029 parse_subrip_time (end_time + strlen (" --> "), &ts_end) &&
1030 state->start_time <= ts_end) {
1031 state->state = 2;
1032 state->start_time = ts_start;
1033 state->duration = ts_end - ts_start;
1034 } else {
1035 GST_DEBUG ("error parsing subrip time line '%s'", line);
1036 state->state = 0;
1037 }
1038 return NULL;
1039 }
1040 case 2:
1041 {
1042 /* No need to parse that text if it's out of segment */
1043 guint64 clip_start = 0, clip_stop = 0;
1044 gboolean in_seg = FALSE;
1045
1046 /* Check our segment start/stop */
1047 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
1048 state->start_time, state->start_time + state->duration,
1049 &clip_start, &clip_stop);
1050
1051 if (in_seg) {
1052 state->start_time = clip_start;
1053 state->duration = clip_stop - clip_start;
1054 } else {
1055 state->state = 0;
1056 return NULL;
1057 }
1058 }
1059 /* looking for subtitle text; empty line ends this subtitle entry */
1060 if (state->buf->len)
1061 g_string_append_c (state->buf, '\n');
1062 g_string_append (state->buf, line);
1063 if (strlen (line) == 0) {
1064 ret = g_markup_escape_text (state->buf->str, state->buf->len);
1065 g_string_truncate (state->buf, 0);
1066 state->state = 0;
1067 subrip_unescape_formatting (ret, state->allowed_tags,
1068 state->allows_tag_attributes);
1069 subrip_remove_unhandled_tags (ret);
1070 strip_trailing_newlines (ret);
1071 subrip_fix_up_markup (&ret, state->allowed_tags);
1072 return ret;
1073 }
1074 return NULL;
1075 default:
1076 g_return_val_if_reached (NULL);
1077 }
1078 }
1079
1080 static gchar *
parse_lrc(ParserState * state,const gchar * line)1081 parse_lrc (ParserState * state, const gchar * line)
1082 {
1083 gint m, s, c;
1084 const gchar *start;
1085 gint milli;
1086
1087 if (line[0] != '[')
1088 return NULL;
1089
1090 if (sscanf (line, "[%u:%02u.%03u]", &m, &s, &c) != 3 &&
1091 sscanf (line, "[%u:%02u.%02u]", &m, &s, &c) != 3)
1092 return NULL;
1093
1094 start = strchr (line, ']');
1095 if (start - line == 9)
1096 milli = 10;
1097 else
1098 milli = 1;
1099
1100 state->start_time = gst_util_uint64_scale (m, 60 * GST_SECOND, 1)
1101 + gst_util_uint64_scale (s, GST_SECOND, 1)
1102 + gst_util_uint64_scale (c, milli * GST_MSECOND, 1);
1103 state->duration = GST_CLOCK_TIME_NONE;
1104
1105 return g_strdup (start + 1);
1106 }
1107
1108 /* WebVTT is a new subtitle format for the upcoming HTML5 video track
1109 * element. This format is similar to Subrip, the biggest differences
1110 * are that there can be cue settings detailing how to display the cue
1111 * text and more markup tags are allowed.
1112 * See also http://www.whatwg.org/specs/web-apps/current-work/webvtt.html
1113 */
1114 static gchar *
parse_webvtt(ParserState * state,const gchar * line)1115 parse_webvtt (ParserState * state, const gchar * line)
1116 {
1117 /* Cue IDs are optional in WebVTT, but not in subrip,
1118 * so when in state 0 (cue ID), also check if we're
1119 * already at the start --> end time marker */
1120 if (state->state == 0 || state->state == 1) {
1121 GstClockTime ts_start, ts_end;
1122 gchar *end_time;
1123 gchar *cue_settings = NULL;
1124
1125 /* looking for start_time --> end_time */
1126 if ((end_time = strstr (line, " --> ")) &&
1127 parse_subrip_time (line, &ts_start) &&
1128 parse_subrip_time (end_time + strlen (" --> "), &ts_end) &&
1129 state->start_time <= ts_end) {
1130 state->state = 2;
1131 state->start_time = ts_start;
1132 state->duration = ts_end - ts_start;
1133 cue_settings = strstr (end_time + strlen (" --> "), " ");
1134 } else {
1135 GST_DEBUG ("error parsing subrip time line '%s'", line);
1136 state->state = 0;
1137 }
1138
1139 state->text_position = 0;
1140 state->text_size = 0;
1141 state->line_position = 0;
1142 state->line_number = 0;
1143
1144 if (cue_settings)
1145 parse_webvtt_cue_settings (state, cue_settings + 1);
1146 else {
1147 g_free (state->vertical);
1148 state->vertical = g_strdup ("");
1149 g_free (state->alignment);
1150 state->alignment = g_strdup ("");
1151 }
1152
1153 return NULL;
1154 } else
1155 return parse_subrip (state, line);
1156 }
1157
1158 static void
unescape_newlines_br(gchar * read)1159 unescape_newlines_br (gchar * read)
1160 {
1161 gchar *write = read;
1162
1163 /* Replace all occurences of '[br]' with a newline as version 2
1164 * of the subviewer format uses this for newlines */
1165
1166 if (read[0] == '\0' || read[1] == '\0' || read[2] == '\0' || read[3] == '\0')
1167 return;
1168
1169 do {
1170 if (strncmp (read, "[br]", 4) == 0) {
1171 *write = '\n';
1172 read += 4;
1173 } else {
1174 *write = *read;
1175 read++;
1176 }
1177 write++;
1178 } while (*read);
1179
1180 *write = '\0';
1181 }
1182
1183 static gchar *
parse_subviewer(ParserState * state,const gchar * line)1184 parse_subviewer (ParserState * state, const gchar * line)
1185 {
1186 guint h1, m1, s1, ms1;
1187 guint h2, m2, s2, ms2;
1188 gchar *ret;
1189
1190 /* TODO: Maybe also parse the fields in the header, especially DELAY.
1191 * For examples see the unit test or
1192 * http://www.doom9.org/index.html?/sub.htm */
1193
1194 switch (state->state) {
1195 case 0:
1196 /* looking for start_time,end_time */
1197 if (sscanf (line, "%u:%u:%u.%u,%u:%u:%u.%u",
1198 &h1, &m1, &s1, &ms1, &h2, &m2, &s2, &ms2) == 8) {
1199 state->state = 1;
1200 state->start_time =
1201 (((guint64) h1) * 3600 + m1 * 60 + s1) * GST_SECOND +
1202 ms1 * GST_MSECOND;
1203 state->duration =
1204 (((guint64) h2) * 3600 + m2 * 60 + s2) * GST_SECOND +
1205 ms2 * GST_MSECOND - state->start_time;
1206 }
1207 return NULL;
1208 case 1:
1209 {
1210 /* No need to parse that text if it's out of segment */
1211 guint64 clip_start = 0, clip_stop = 0;
1212 gboolean in_seg = FALSE;
1213
1214 /* Check our segment start/stop */
1215 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
1216 state->start_time, state->start_time + state->duration,
1217 &clip_start, &clip_stop);
1218
1219 if (in_seg) {
1220 state->start_time = clip_start;
1221 state->duration = clip_stop - clip_start;
1222 } else {
1223 state->state = 0;
1224 return NULL;
1225 }
1226 }
1227 /* looking for subtitle text; empty line ends this subtitle entry */
1228 if (state->buf->len)
1229 g_string_append_c (state->buf, '\n');
1230 g_string_append (state->buf, line);
1231 if (strlen (line) == 0) {
1232 ret = g_strdup (state->buf->str);
1233 unescape_newlines_br (ret);
1234 strip_trailing_newlines (ret);
1235 g_string_truncate (state->buf, 0);
1236 state->state = 0;
1237 return ret;
1238 }
1239 return NULL;
1240 default:
1241 g_assert_not_reached ();
1242 return NULL;
1243 }
1244 }
1245
1246 static gchar *
parse_mpsub(ParserState * state,const gchar * line)1247 parse_mpsub (ParserState * state, const gchar * line)
1248 {
1249 gchar *ret;
1250 float t1, t2;
1251
1252 switch (state->state) {
1253 case 0:
1254 /* looking for two floats (offset, duration) */
1255 if (sscanf (line, "%f %f", &t1, &t2) == 2) {
1256 state->state = 1;
1257 state->start_time += state->duration + GST_SECOND * t1;
1258 state->duration = GST_SECOND * t2;
1259 }
1260 return NULL;
1261 case 1:
1262 { /* No need to parse that text if it's out of segment */
1263 guint64 clip_start = 0, clip_stop = 0;
1264 gboolean in_seg = FALSE;
1265
1266 /* Check our segment start/stop */
1267 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
1268 state->start_time, state->start_time + state->duration,
1269 &clip_start, &clip_stop);
1270
1271 if (in_seg) {
1272 state->start_time = clip_start;
1273 state->duration = clip_stop - clip_start;
1274 } else {
1275 state->state = 0;
1276 return NULL;
1277 }
1278 }
1279 /* looking for subtitle text; empty line ends this
1280 * subtitle entry */
1281 if (state->buf->len)
1282 g_string_append_c (state->buf, '\n');
1283 g_string_append (state->buf, line);
1284 if (strlen (line) == 0) {
1285 ret = g_strdup (state->buf->str);
1286 g_string_truncate (state->buf, 0);
1287 state->state = 0;
1288 return ret;
1289 }
1290 return NULL;
1291 default:
1292 g_assert_not_reached ();
1293 return NULL;
1294 }
1295 }
1296
1297 static const gchar *
dks_skip_timestamp(const gchar * line)1298 dks_skip_timestamp (const gchar * line)
1299 {
1300 while (*line && *line != ']')
1301 line++;
1302 if (*line == ']')
1303 line++;
1304 return line;
1305 }
1306
1307 static gchar *
parse_dks(ParserState * state,const gchar * line)1308 parse_dks (ParserState * state, const gchar * line)
1309 {
1310 guint h, m, s;
1311
1312 switch (state->state) {
1313 case 0:
1314 /* Looking for the start time and text */
1315 if (sscanf (line, "[%u:%u:%u]", &h, &m, &s) == 3) {
1316 const gchar *text;
1317 state->start_time = (((guint64) h) * 3600 + m * 60 + s) * GST_SECOND;
1318 text = dks_skip_timestamp (line);
1319 if (*text) {
1320 state->state = 1;
1321 g_string_append (state->buf, text);
1322 }
1323 }
1324 return NULL;
1325 case 1:
1326 {
1327 guint64 clip_start = 0, clip_stop = 0;
1328 gboolean in_seg;
1329 gchar *ret;
1330
1331 /* Looking for the end time */
1332 if (sscanf (line, "[%u:%u:%u]", &h, &m, &s) == 3) {
1333 state->state = 0;
1334 state->duration = (((guint64) h) * 3600 + m * 60 + s) * GST_SECOND -
1335 state->start_time;
1336 } else {
1337 GST_WARNING ("Failed to parse subtitle end time");
1338 return NULL;
1339 }
1340
1341 /* Check if this subtitle is out of the current segment */
1342 in_seg = gst_segment_clip (state->segment, GST_FORMAT_TIME,
1343 state->start_time, state->start_time + state->duration,
1344 &clip_start, &clip_stop);
1345
1346 if (!in_seg) {
1347 return NULL;
1348 }
1349
1350 state->start_time = clip_start;
1351 state->duration = clip_stop - clip_start;
1352
1353 ret = g_strdup (state->buf->str);
1354 g_string_truncate (state->buf, 0);
1355 unescape_newlines_br (ret);
1356 return ret;
1357 }
1358 default:
1359 g_assert_not_reached ();
1360 return NULL;
1361 }
1362 }
1363
1364 static void
parser_state_init(ParserState * state)1365 parser_state_init (ParserState * state)
1366 {
1367 GST_DEBUG ("initialising parser");
1368
1369 if (state->buf) {
1370 g_string_truncate (state->buf, 0);
1371 } else {
1372 state->buf = g_string_new (NULL);
1373 }
1374
1375 state->start_time = 0;
1376 state->duration = 0;
1377 state->max_duration = 0; /* no limit */
1378 state->state = 0;
1379 state->segment = NULL;
1380 }
1381
1382 static void
parser_state_dispose(GstSubParse * self,ParserState * state)1383 parser_state_dispose (GstSubParse * self, ParserState * state)
1384 {
1385 if (state->buf) {
1386 g_string_free (state->buf, TRUE);
1387 state->buf = NULL;
1388 }
1389
1390 g_free (state->vertical);
1391 state->vertical = NULL;
1392 g_free (state->alignment);
1393 state->alignment = NULL;
1394
1395 if (state->user_data) {
1396 switch (self->parser_type) {
1397 case GST_SUB_PARSE_FORMAT_QTTEXT:
1398 qttext_context_deinit (state);
1399 break;
1400 case GST_SUB_PARSE_FORMAT_SAMI:
1401 sami_context_deinit (state);
1402 break;
1403 default:
1404 break;
1405 }
1406 }
1407 state->allowed_tags = NULL;
1408 }
1409
1410 /* regex type enum */
1411 typedef enum
1412 {
1413 GST_SUB_PARSE_REGEX_UNKNOWN = 0,
1414 GST_SUB_PARSE_REGEX_MDVDSUB = 1,
1415 GST_SUB_PARSE_REGEX_SUBRIP = 2,
1416 GST_SUB_PARSE_REGEX_DKS = 3,
1417 GST_SUB_PARSE_REGEX_VTT = 4,
1418 } GstSubParseRegex;
1419
1420 static gpointer
gst_sub_parse_data_format_autodetect_regex_once(GstSubParseRegex regtype)1421 gst_sub_parse_data_format_autodetect_regex_once (GstSubParseRegex regtype)
1422 {
1423 gpointer result = NULL;
1424 GError *gerr = NULL;
1425 switch (regtype) {
1426 case GST_SUB_PARSE_REGEX_MDVDSUB:
1427 result =
1428 (gpointer) g_regex_new ("^\\{[0-9]+\\}\\{[0-9]+\\}",
1429 G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &gerr);
1430 if (result == NULL) {
1431 g_warning ("Compilation of mdvd regex failed: %s", gerr->message);
1432 g_clear_error (&gerr);
1433 }
1434 break;
1435 case GST_SUB_PARSE_REGEX_SUBRIP:
1436 result = (gpointer)
1437 g_regex_new ("^[\\s\\n]*[\\n]? {0,3}[ 0-9]{1,4}\\s*(\x0d)?\x0a"
1438 " ?[0-9]{1,2}: ?[0-9]{1,2}: ?[0-9]{1,2}[,.] {0,2}[0-9]{1,3}"
1439 " +--> +[0-9]{1,2}: ?[0-9]{1,2}: ?[0-9]{1,2}[,.] {0,2}[0-9]{1,2}",
1440 G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &gerr);
1441 if (result == NULL) {
1442 g_warning ("Compilation of subrip regex failed: %s", gerr->message);
1443 g_clear_error (&gerr);
1444 }
1445 break;
1446 case GST_SUB_PARSE_REGEX_DKS:
1447 result = (gpointer) g_regex_new ("^\\[[0-9]+:[0-9]+:[0-9]+\\].*",
1448 G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, &gerr);
1449 if (result == NULL) {
1450 g_warning ("Compilation of dks regex failed: %s", gerr->message);
1451 g_clear_error (&gerr);
1452 }
1453 break;
1454 case GST_SUB_PARSE_REGEX_VTT:
1455 result = (gpointer)
1456 g_regex_new ("^(\\xef\\xbb\\xbf)?WEBVTT[\\xa\\xd\\x20\\x9]", 0, 0,
1457 &gerr);
1458 if (result == NULL) {
1459 g_warning ("Compilation of vtt regex failed: %s", gerr->message);
1460 g_error_free (gerr);
1461 }
1462 break;
1463
1464 default:
1465 GST_WARNING ("Trying to allocate regex of unknown type %u", regtype);
1466 }
1467 return result;
1468 }
1469
1470 /*
1471 * FIXME: maybe we should pass along a second argument, the preceding
1472 * text buffer, because that is how this originally worked, even though
1473 * I don't really see the use of that.
1474 */
1475
1476 static GstSubParseFormat
gst_sub_parse_data_format_autodetect(gchar * match_str)1477 gst_sub_parse_data_format_autodetect (gchar * match_str)
1478 {
1479 guint n1, n2, n3;
1480
1481 static GOnce mdvd_rx_once = G_ONCE_INIT;
1482 static GOnce subrip_rx_once = G_ONCE_INIT;
1483 static GOnce dks_rx_once = G_ONCE_INIT;
1484 static GOnce vtt_rx_once = G_ONCE_INIT;
1485
1486 GRegex *mdvd_grx;
1487 GRegex *subrip_grx;
1488 GRegex *dks_grx;
1489 GRegex *vtt_grx;
1490
1491 g_once (&mdvd_rx_once,
1492 (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
1493 (gpointer) GST_SUB_PARSE_REGEX_MDVDSUB);
1494 g_once (&subrip_rx_once,
1495 (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
1496 (gpointer) GST_SUB_PARSE_REGEX_SUBRIP);
1497 g_once (&dks_rx_once,
1498 (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
1499 (gpointer) GST_SUB_PARSE_REGEX_DKS);
1500 g_once (&vtt_rx_once,
1501 (GThreadFunc) gst_sub_parse_data_format_autodetect_regex_once,
1502 (gpointer) GST_SUB_PARSE_REGEX_VTT);
1503
1504 mdvd_grx = (GRegex *) mdvd_rx_once.retval;
1505 subrip_grx = (GRegex *) subrip_rx_once.retval;
1506 dks_grx = (GRegex *) dks_rx_once.retval;
1507 vtt_grx = (GRegex *) vtt_rx_once.retval;
1508
1509 if (g_regex_match (mdvd_grx, match_str, 0, NULL)) {
1510 GST_LOG ("MicroDVD (frame based) format detected");
1511 return GST_SUB_PARSE_FORMAT_MDVDSUB;
1512 }
1513 if (g_regex_match (subrip_grx, match_str, 0, NULL)) {
1514 GST_LOG ("SubRip (time based) format detected");
1515 return GST_SUB_PARSE_FORMAT_SUBRIP;
1516 }
1517 if (g_regex_match (dks_grx, match_str, 0, NULL)) {
1518 GST_LOG ("DKS (time based) format detected");
1519 return GST_SUB_PARSE_FORMAT_DKS;
1520 }
1521 if (g_regex_match (vtt_grx, match_str, 0, NULL) == TRUE) {
1522 GST_LOG ("WebVTT (time based) format detected");
1523 return GST_SUB_PARSE_FORMAT_VTT;
1524 }
1525
1526 if (!strncmp (match_str, "FORMAT=TIME", 11)) {
1527 GST_LOG ("MPSub (time based) format detected");
1528 return GST_SUB_PARSE_FORMAT_MPSUB;
1529 }
1530 if (strstr (match_str, "<SAMI>") != NULL ||
1531 strstr (match_str, "<sami>") != NULL) {
1532 GST_LOG ("SAMI (time based) format detected");
1533 return GST_SUB_PARSE_FORMAT_SAMI;
1534 }
1535 /* we're boldly assuming the first subtitle appears within the first hour */
1536 if (sscanf (match_str, "0:%02u:%02u:", &n1, &n2) == 2 ||
1537 sscanf (match_str, "0:%02u:%02u=", &n1, &n2) == 2 ||
1538 sscanf (match_str, "00:%02u:%02u:", &n1, &n2) == 2 ||
1539 sscanf (match_str, "00:%02u:%02u=", &n1, &n2) == 2 ||
1540 sscanf (match_str, "00:%02u:%02u,%u=", &n1, &n2, &n3) == 3) {
1541 GST_LOG ("TMPlayer (time based) format detected");
1542 return GST_SUB_PARSE_FORMAT_TMPLAYER;
1543 }
1544 if (sscanf (match_str, "[%u][%u]", &n1, &n2) == 2) {
1545 GST_LOG ("MPL2 (time based) format detected");
1546 return GST_SUB_PARSE_FORMAT_MPL2;
1547 }
1548 if (strstr (match_str, "[INFORMATION]") != NULL) {
1549 GST_LOG ("SubViewer (time based) format detected");
1550 return GST_SUB_PARSE_FORMAT_SUBVIEWER;
1551 }
1552 if (strstr (match_str, "{QTtext}") != NULL) {
1553 GST_LOG ("QTtext (time based) format detected");
1554 return GST_SUB_PARSE_FORMAT_QTTEXT;
1555 }
1556 /* We assume the LRC file starts immediately */
1557 if (match_str[0] == '[') {
1558 gboolean all_lines_good = TRUE;
1559 gchar **split;
1560 gchar **ptr;
1561
1562 ptr = split = g_strsplit (match_str, "\n", -1);
1563 while (*ptr && *(ptr + 1)) {
1564 gchar *str = *ptr;
1565 gint len = strlen (str);
1566
1567 if (sscanf (str, "[%u:%02u.%02u]", &n1, &n2, &n3) == 3 ||
1568 sscanf (str, "[%u:%02u.%03u]", &n1, &n2, &n3) == 3) {
1569 all_lines_good = TRUE;
1570 } else if (str[len - 1] == ']' && strchr (str, ':') != NULL) {
1571 all_lines_good = TRUE;
1572 } else {
1573 all_lines_good = FALSE;
1574 break;
1575 }
1576
1577 ptr++;
1578 }
1579 g_strfreev (split);
1580
1581 if (all_lines_good)
1582 return GST_SUB_PARSE_FORMAT_LRC;
1583 }
1584
1585 GST_DEBUG ("no subtitle format detected");
1586 return GST_SUB_PARSE_FORMAT_UNKNOWN;
1587 }
1588
1589 static GstCaps *
gst_sub_parse_format_autodetect(GstSubParse * self)1590 gst_sub_parse_format_autodetect (GstSubParse * self)
1591 {
1592 gchar *data;
1593 GstSubParseFormat format;
1594
1595 if (strlen (self->textbuf->str) < 30) {
1596 GST_DEBUG ("File too small to be a subtitles file");
1597 return NULL;
1598 }
1599
1600 data = g_strndup (self->textbuf->str, 35);
1601 format = gst_sub_parse_data_format_autodetect (data);
1602 g_free (data);
1603
1604 self->parser_type = format;
1605 self->subtitle_codec = gst_sub_parse_get_format_description (format);
1606 parser_state_init (&self->state);
1607 self->state.allowed_tags = NULL;
1608
1609 switch (format) {
1610 case GST_SUB_PARSE_FORMAT_MDVDSUB:
1611 self->parse_line = parse_mdvdsub;
1612 return gst_caps_new_simple ("text/x-raw",
1613 "format", G_TYPE_STRING, "pango-markup", NULL);
1614 case GST_SUB_PARSE_FORMAT_SUBRIP:
1615 self->state.allowed_tags = (gpointer) allowed_srt_tags;
1616 self->state.allows_tag_attributes = FALSE;
1617 self->parse_line = parse_subrip;
1618 return gst_caps_new_simple ("text/x-raw",
1619 "format", G_TYPE_STRING, "pango-markup", NULL);
1620 case GST_SUB_PARSE_FORMAT_MPSUB:
1621 self->parse_line = parse_mpsub;
1622 return gst_caps_new_simple ("text/x-raw",
1623 "format", G_TYPE_STRING, "utf8", NULL);
1624 case GST_SUB_PARSE_FORMAT_SAMI:
1625 self->parse_line = parse_sami;
1626 sami_context_init (&self->state);
1627 return gst_caps_new_simple ("text/x-raw",
1628 "format", G_TYPE_STRING, "pango-markup", NULL);
1629 case GST_SUB_PARSE_FORMAT_TMPLAYER:
1630 self->parse_line = parse_tmplayer;
1631 self->state.max_duration = 5 * GST_SECOND;
1632 return gst_caps_new_simple ("text/x-raw",
1633 "format", G_TYPE_STRING, "utf8", NULL);
1634 case GST_SUB_PARSE_FORMAT_MPL2:
1635 self->parse_line = parse_mpl2;
1636 return gst_caps_new_simple ("text/x-raw",
1637 "format", G_TYPE_STRING, "pango-markup", NULL);
1638 case GST_SUB_PARSE_FORMAT_DKS:
1639 self->parse_line = parse_dks;
1640 return gst_caps_new_simple ("text/x-raw",
1641 "format", G_TYPE_STRING, "utf8", NULL);
1642 case GST_SUB_PARSE_FORMAT_VTT:
1643 self->state.allowed_tags = (gpointer) allowed_vtt_tags;
1644 self->state.allows_tag_attributes = TRUE;
1645 self->parse_line = parse_webvtt;
1646 return gst_caps_new_simple ("text/x-raw",
1647 "format", G_TYPE_STRING, "pango-markup", NULL);
1648 case GST_SUB_PARSE_FORMAT_SUBVIEWER:
1649 self->parse_line = parse_subviewer;
1650 return gst_caps_new_simple ("text/x-raw",
1651 "format", G_TYPE_STRING, "utf8", NULL);
1652 case GST_SUB_PARSE_FORMAT_QTTEXT:
1653 self->parse_line = parse_qttext;
1654 qttext_context_init (&self->state);
1655 return gst_caps_new_simple ("text/x-raw",
1656 "format", G_TYPE_STRING, "pango-markup", NULL);
1657 case GST_SUB_PARSE_FORMAT_LRC:
1658 self->parse_line = parse_lrc;
1659 return gst_caps_new_simple ("text/x-raw",
1660 "format", G_TYPE_STRING, "utf8", NULL);
1661 case GST_SUB_PARSE_FORMAT_UNKNOWN:
1662 default:
1663 GST_DEBUG ("no subtitle format detected");
1664 GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE,
1665 ("The input is not a valid/supported subtitle file"), (NULL));
1666 return NULL;
1667 }
1668 }
1669
1670 static void
feed_textbuf(GstSubParse * self,GstBuffer * buf)1671 feed_textbuf (GstSubParse * self, GstBuffer * buf)
1672 {
1673 gboolean discont;
1674 gsize consumed;
1675 gchar *input = NULL;
1676 const guint8 *data;
1677 gsize avail;
1678
1679 discont = GST_BUFFER_IS_DISCONT (buf);
1680
1681 if (GST_BUFFER_OFFSET_IS_VALID (buf) &&
1682 GST_BUFFER_OFFSET (buf) != self->offset) {
1683 self->offset = GST_BUFFER_OFFSET (buf);
1684 discont = TRUE;
1685 }
1686
1687 if (discont) {
1688 GST_INFO ("discontinuity");
1689 /* flush the parser state */
1690 parser_state_init (&self->state);
1691 g_string_truncate (self->textbuf, 0);
1692 gst_adapter_clear (self->adapter);
1693 if (self->parser_type == GST_SUB_PARSE_FORMAT_SAMI)
1694 sami_context_reset (&self->state);
1695 /* we could set a flag to make sure that the next buffer we push out also
1696 * has the DISCONT flag set, but there's no point really given that it's
1697 * subtitles which are discontinuous by nature. */
1698 }
1699
1700 self->offset += gst_buffer_get_size (buf);
1701
1702 gst_adapter_push (self->adapter, buf);
1703
1704 avail = gst_adapter_available (self->adapter);
1705 data = gst_adapter_map (self->adapter, avail);
1706 input = convert_encoding (self, (const gchar *) data, avail, &consumed);
1707
1708 if (input && consumed > 0) {
1709 self->textbuf = g_string_append (self->textbuf, input);
1710 gst_adapter_unmap (self->adapter);
1711 gst_adapter_flush (self->adapter, consumed);
1712 } else {
1713 gst_adapter_unmap (self->adapter);
1714 }
1715
1716 g_free (input);
1717 }
1718
1719 static GstFlowReturn
handle_buffer(GstSubParse * self,GstBuffer * buf)1720 handle_buffer (GstSubParse * self, GstBuffer * buf)
1721 {
1722 GstFlowReturn ret = GST_FLOW_OK;
1723 GstCaps *caps = NULL;
1724 gchar *line, *subtitle;
1725 gboolean need_tags = FALSE;
1726
1727 if (self->first_buffer) {
1728 GstMapInfo map;
1729
1730 gst_buffer_map (buf, &map, GST_MAP_READ);
1731 self->detected_encoding = detect_encoding ((gchar *) map.data, map.size);
1732 gst_buffer_unmap (buf, &map);
1733 self->first_buffer = FALSE;
1734 self->state.fps_n = self->fps_n;
1735 self->state.fps_d = self->fps_d;
1736 }
1737
1738 feed_textbuf (self, buf);
1739
1740 /* make sure we know the format */
1741 if (G_UNLIKELY (self->parser_type == GST_SUB_PARSE_FORMAT_UNKNOWN)) {
1742 if (!(caps = gst_sub_parse_format_autodetect (self))) {
1743 return GST_FLOW_EOS;
1744 }
1745 if (!gst_pad_set_caps (self->srcpad, caps)) {
1746 gst_caps_unref (caps);
1747 return GST_FLOW_EOS;
1748 }
1749 gst_caps_unref (caps);
1750 need_tags = TRUE;
1751 }
1752
1753 /* Push newsegment if needed */
1754 if (self->need_segment) {
1755 GST_LOG_OBJECT (self, "pushing newsegment event with %" GST_SEGMENT_FORMAT,
1756 &self->segment);
1757
1758 gst_pad_push_event (self->srcpad, gst_event_new_segment (&self->segment));
1759 self->need_segment = FALSE;
1760 }
1761
1762 if (need_tags) {
1763 /* push tags */
1764 if (self->subtitle_codec != NULL) {
1765 GstTagList *tags;
1766
1767 tags = gst_tag_list_new (GST_TAG_SUBTITLE_CODEC, self->subtitle_codec,
1768 NULL);
1769 gst_pad_push_event (self->srcpad, gst_event_new_tag (tags));
1770 }
1771 }
1772
1773 while (!self->flushing && (line = get_next_line (self))) {
1774 guint offset = 0;
1775
1776 /* Set segment on our parser state machine */
1777 self->state.segment = &self->segment;
1778 /* Now parse the line, out of segment lines will just return NULL */
1779 GST_LOG_OBJECT (self, "State %d. Parsing line '%s'", self->state.state,
1780 line + offset);
1781 subtitle = self->parse_line (&self->state, line + offset);
1782 g_free (line);
1783
1784 if (subtitle) {
1785 guint subtitle_len = strlen (subtitle);
1786
1787 /* +1 for terminating NUL character */
1788 buf = gst_buffer_new_and_alloc (subtitle_len + 1);
1789
1790 /* copy terminating NUL character as well */
1791 gst_buffer_fill (buf, 0, subtitle, subtitle_len + 1);
1792 gst_buffer_set_size (buf, subtitle_len);
1793
1794 GST_BUFFER_TIMESTAMP (buf) = self->state.start_time;
1795 GST_BUFFER_DURATION (buf) = self->state.duration;
1796
1797 /* in some cases (e.g. tmplayer) we can only determine the duration
1798 * of a text chunk from the timestamp of the next text chunk; in those
1799 * cases, we probably want to limit the duration to something
1800 * reasonable, so we don't end up showing some text for e.g. 40 seconds
1801 * just because nothing else is being said during that time */
1802 if (self->state.max_duration > 0 && GST_BUFFER_DURATION_IS_VALID (buf)) {
1803 if (GST_BUFFER_DURATION (buf) > self->state.max_duration)
1804 GST_BUFFER_DURATION (buf) = self->state.max_duration;
1805 }
1806
1807 self->segment.position = self->state.start_time;
1808
1809 GST_DEBUG_OBJECT (self, "Sending text '%s', %" GST_TIME_FORMAT " + %"
1810 GST_TIME_FORMAT, subtitle, GST_TIME_ARGS (self->state.start_time),
1811 GST_TIME_ARGS (self->state.duration));
1812
1813 g_free (self->state.vertical);
1814 self->state.vertical = NULL;
1815 g_free (self->state.alignment);
1816 self->state.alignment = NULL;
1817
1818 ret = gst_pad_push (self->srcpad, buf);
1819
1820 /* move this forward (the tmplayer parser needs this) */
1821 if (self->state.duration != GST_CLOCK_TIME_NONE)
1822 self->state.start_time += self->state.duration;
1823
1824 g_free (subtitle);
1825 subtitle = NULL;
1826
1827 if (ret != GST_FLOW_OK) {
1828 GST_DEBUG_OBJECT (self, "flow: %s", gst_flow_get_name (ret));
1829 break;
1830 }
1831 }
1832 }
1833
1834 return ret;
1835 }
1836
1837 static GstFlowReturn
gst_sub_parse_chain(GstPad * sinkpad,GstObject * parent,GstBuffer * buf)1838 gst_sub_parse_chain (GstPad * sinkpad, GstObject * parent, GstBuffer * buf)
1839 {
1840 GstFlowReturn ret;
1841 GstSubParse *self;
1842
1843 self = GST_SUBPARSE (parent);
1844
1845 ret = handle_buffer (self, buf);
1846
1847 return ret;
1848 }
1849
1850 static gboolean
gst_sub_parse_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)1851 gst_sub_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1852 {
1853 GstSubParse *self = GST_SUBPARSE (parent);
1854 gboolean ret = FALSE;
1855
1856 GST_LOG_OBJECT (self, "%s event", GST_EVENT_TYPE_NAME (event));
1857
1858 switch (GST_EVENT_TYPE (event)) {
1859 case GST_EVENT_STREAM_GROUP_DONE:
1860 case GST_EVENT_EOS:{
1861 /* Make sure the last subrip chunk is pushed out even
1862 * if the file does not have an empty line at the end */
1863 if (self->parser_type == GST_SUB_PARSE_FORMAT_SUBRIP ||
1864 self->parser_type == GST_SUB_PARSE_FORMAT_TMPLAYER ||
1865 self->parser_type == GST_SUB_PARSE_FORMAT_MPL2 ||
1866 self->parser_type == GST_SUB_PARSE_FORMAT_QTTEXT ||
1867 self->parser_type == GST_SUB_PARSE_FORMAT_VTT) {
1868 gchar term_chars[] = { '\n', '\n', '\0' };
1869 GstBuffer *buf = gst_buffer_new_and_alloc (2 + 1);
1870
1871 GST_DEBUG_OBJECT (self, "%s: force pushing of any remaining text",
1872 GST_EVENT_TYPE_NAME (event));
1873
1874 gst_buffer_fill (buf, 0, term_chars, 3);
1875 gst_buffer_set_size (buf, 2);
1876
1877 GST_BUFFER_OFFSET (buf) = self->offset;
1878 gst_sub_parse_chain (pad, parent, buf);
1879 }
1880 ret = gst_pad_event_default (pad, parent, event);
1881 break;
1882 }
1883 case GST_EVENT_SEGMENT:
1884 {
1885 const GstSegment *s;
1886 gst_event_parse_segment (event, &s);
1887 if (s->format == GST_FORMAT_TIME)
1888 gst_event_copy_segment (event, &self->segment);
1889 GST_DEBUG_OBJECT (self, "newsegment (%s)",
1890 gst_format_get_name (self->segment.format));
1891
1892 /* if not time format, we'll either start with a 0 timestamp anyway or
1893 * it's following a seek in which case we'll have saved the requested
1894 * seek segment and don't want to overwrite it (remember that on a seek
1895 * we always just seek back to the start in BYTES format and just throw
1896 * away all text that's before the requested position; if the subtitles
1897 * come from an upstream demuxer, it won't be able to handle our BYTES
1898 * seek request and instead send us a newsegment from the seek request
1899 * it received via its video pads instead, so all is fine then too) */
1900 ret = TRUE;
1901 gst_event_unref (event);
1902 /* in either case, let's not simply discard this event;
1903 * trigger sending of the saved requested seek segment
1904 * or the one taken here from upstream */
1905 self->need_segment = TRUE;
1906 break;
1907 }
1908 case GST_EVENT_FLUSH_START:
1909 {
1910 self->flushing = TRUE;
1911
1912 ret = gst_pad_event_default (pad, parent, event);
1913 break;
1914 }
1915 case GST_EVENT_FLUSH_STOP:
1916 {
1917 self->flushing = FALSE;
1918
1919 ret = gst_pad_event_default (pad, parent, event);
1920 break;
1921 }
1922 default:
1923 ret = gst_pad_event_default (pad, parent, event);
1924 break;
1925 }
1926
1927 return ret;
1928 }
1929
1930
1931 static GstStateChangeReturn
gst_sub_parse_change_state(GstElement * element,GstStateChange transition)1932 gst_sub_parse_change_state (GstElement * element, GstStateChange transition)
1933 {
1934 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1935 GstSubParse *self = GST_SUBPARSE (element);
1936
1937 switch (transition) {
1938 case GST_STATE_CHANGE_READY_TO_PAUSED:
1939 /* format detection will init the parser state */
1940 self->offset = 0;
1941 self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
1942 self->valid_utf8 = TRUE;
1943 self->first_buffer = TRUE;
1944 g_free (self->detected_encoding);
1945 self->detected_encoding = NULL;
1946 g_string_truncate (self->textbuf, 0);
1947 gst_adapter_clear (self->adapter);
1948 break;
1949 default:
1950 break;
1951 }
1952
1953 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1954 if (ret == GST_STATE_CHANGE_FAILURE)
1955 return ret;
1956
1957 switch (transition) {
1958 case GST_STATE_CHANGE_PAUSED_TO_READY:
1959 parser_state_dispose (self, &self->state);
1960 self->parser_type = GST_SUB_PARSE_FORMAT_UNKNOWN;
1961 break;
1962 default:
1963 break;
1964 }
1965
1966 return ret;
1967 }
1968
1969 /*
1970 * Typefind support.
1971 */
1972
1973 /* FIXME 0.11: these caps are ugly, use app/x-subtitle + type field or so;
1974 * also, give different subtitle formats really different types */
1975 static GstStaticCaps mpl2_caps =
1976 GST_STATIC_CAPS ("application/x-subtitle-mpl2");
1977 #define SUB_CAPS (gst_static_caps_get (&sub_caps))
1978
1979 static GstStaticCaps tmp_caps =
1980 GST_STATIC_CAPS ("application/x-subtitle-tmplayer");
1981 #define TMP_CAPS (gst_static_caps_get (&tmp_caps))
1982
1983 static GstStaticCaps sub_caps = GST_STATIC_CAPS ("application/x-subtitle");
1984 #define MPL2_CAPS (gst_static_caps_get (&mpl2_caps))
1985
1986 static GstStaticCaps smi_caps = GST_STATIC_CAPS ("application/x-subtitle-sami");
1987 #define SAMI_CAPS (gst_static_caps_get (&smi_caps))
1988
1989 static GstStaticCaps dks_caps = GST_STATIC_CAPS ("application/x-subtitle-dks");
1990 #define DKS_CAPS (gst_static_caps_get (&dks_caps))
1991
1992 static GstStaticCaps vtt_caps = GST_STATIC_CAPS ("application/x-subtitle-vtt");
1993 #define VTT_CAPS (gst_static_caps_get (&vtt_caps))
1994
1995 static GstStaticCaps qttext_caps =
1996 GST_STATIC_CAPS ("application/x-subtitle-qttext");
1997 #define QTTEXT_CAPS (gst_static_caps_get (&qttext_caps))
1998
1999 static GstStaticCaps lrc_caps = GST_STATIC_CAPS ("application/x-subtitle-lrc");
2000 #define LRC_CAPS (gst_static_caps_get (&lrc_caps))
2001
2002 static void
gst_subparse_type_find(GstTypeFind * tf,gpointer private)2003 gst_subparse_type_find (GstTypeFind * tf, gpointer private)
2004 {
2005 GstSubParseFormat format;
2006 const guint8 *data;
2007 GstCaps *caps;
2008 gchar *str;
2009 gchar *encoding = NULL;
2010 const gchar *end;
2011
2012 if (!(data = gst_type_find_peek (tf, 0, 129)))
2013 return;
2014
2015 /* make sure string passed to _autodetect() is NUL-terminated */
2016 str = g_malloc0 (129);
2017 memcpy (str, data, 128);
2018
2019 if ((encoding = detect_encoding (str, 128)) != NULL) {
2020 gchar *converted_str;
2021 GError *err = NULL;
2022 gsize tmp;
2023
2024 converted_str = gst_convert_to_utf8 (str, 128, encoding, &tmp, &err);
2025 if (converted_str == NULL) {
2026 GST_DEBUG ("Encoding '%s' detected but conversion failed: %s", encoding,
2027 err->message);
2028 g_clear_error (&err);
2029 } else {
2030 g_free (str);
2031 str = converted_str;
2032 }
2033 g_free (encoding);
2034 }
2035
2036 /* Check if at least the first 120 chars are valid UTF8,
2037 * otherwise convert as always */
2038 if (!g_utf8_validate (str, 128, &end) && (end - str) < 120) {
2039 gchar *converted_str;
2040 gsize tmp;
2041 const gchar *enc;
2042
2043 enc = g_getenv ("GST_SUBTITLE_ENCODING");
2044 if (enc == NULL || *enc == '\0') {
2045 /* if local encoding is UTF-8 and no encoding specified
2046 * via the environment variable, assume ISO-8859-15 */
2047 if (g_get_charset (&enc)) {
2048 enc = "ISO-8859-15";
2049 }
2050 }
2051 converted_str = gst_convert_to_utf8 (str, 128, enc, &tmp, NULL);
2052 if (converted_str != NULL) {
2053 g_free (str);
2054 str = converted_str;
2055 }
2056 }
2057
2058 format = gst_sub_parse_data_format_autodetect (str);
2059 g_free (str);
2060
2061 switch (format) {
2062 case GST_SUB_PARSE_FORMAT_MDVDSUB:
2063 GST_DEBUG ("MicroDVD format detected");
2064 caps = SUB_CAPS;
2065 break;
2066 case GST_SUB_PARSE_FORMAT_SUBRIP:
2067 GST_DEBUG ("SubRip format detected");
2068 caps = SUB_CAPS;
2069 break;
2070 case GST_SUB_PARSE_FORMAT_MPSUB:
2071 GST_DEBUG ("MPSub format detected");
2072 caps = SUB_CAPS;
2073 break;
2074 case GST_SUB_PARSE_FORMAT_SAMI:
2075 GST_DEBUG ("SAMI (time-based) format detected");
2076 caps = SAMI_CAPS;
2077 break;
2078 case GST_SUB_PARSE_FORMAT_TMPLAYER:
2079 GST_DEBUG ("TMPlayer (time based) format detected");
2080 caps = TMP_CAPS;
2081 break;
2082 /* FIXME: our MPL2 typefinding is not really good enough to warrant
2083 * returning a high probability (however, since we registered our
2084 * typefinder here with a rank of MARGINAL we should pretty much only
2085 * be called if most other typefinders have already run */
2086 case GST_SUB_PARSE_FORMAT_MPL2:
2087 GST_DEBUG ("MPL2 (time based) format detected");
2088 caps = MPL2_CAPS;
2089 break;
2090 case GST_SUB_PARSE_FORMAT_SUBVIEWER:
2091 GST_DEBUG ("SubViewer format detected");
2092 caps = SUB_CAPS;
2093 break;
2094 case GST_SUB_PARSE_FORMAT_DKS:
2095 GST_DEBUG ("DKS format detected");
2096 caps = DKS_CAPS;
2097 break;
2098 case GST_SUB_PARSE_FORMAT_QTTEXT:
2099 GST_DEBUG ("QTtext format detected");
2100 caps = QTTEXT_CAPS;
2101 break;
2102 case GST_SUB_PARSE_FORMAT_LRC:
2103 GST_DEBUG ("LRC format detected");
2104 caps = LRC_CAPS;
2105 break;
2106 case GST_SUB_PARSE_FORMAT_VTT:
2107 GST_DEBUG ("WebVTT format detected");
2108 caps = VTT_CAPS;
2109 break;
2110 default:
2111 case GST_SUB_PARSE_FORMAT_UNKNOWN:
2112 GST_DEBUG ("no subtitle format detected");
2113 return;
2114 }
2115
2116 /* if we're here, it's ok */
2117 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, caps);
2118 }
2119
2120 static gboolean
plugin_init(GstPlugin * plugin)2121 plugin_init (GstPlugin * plugin)
2122 {
2123 GST_DEBUG_CATEGORY_INIT (sub_parse_debug, "subparse", 0, ".sub parser");
2124
2125 if (!gst_type_find_register (plugin, "subparse_typefind", GST_RANK_MARGINAL,
2126 gst_subparse_type_find, "srt,sub,mpsub,mdvd,smi,txt,dks,vtt",
2127 SUB_CAPS, NULL, NULL))
2128 return FALSE;
2129
2130 if (!gst_element_register (plugin, "subparse",
2131 GST_RANK_PRIMARY, GST_TYPE_SUBPARSE) ||
2132 !gst_element_register (plugin, "ssaparse",
2133 GST_RANK_PRIMARY, GST_TYPE_SSA_PARSE)) {
2134 return FALSE;
2135 }
2136
2137 return TRUE;
2138 }
2139
2140 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2141 GST_VERSION_MINOR,
2142 subparse,
2143 "Subtitle parsing",
2144 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
2145