1 /* GStreamer mpl2 format subtitle parser
2 * Copyright (C) 2006 Kamil Pawlowski <kamilpe gmail com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "mpl2parse.h"
21
22 #include <stdio.h>
23 #include <string.h>
24
25 /* From http://lists.mplayerhq.hu/pipermail/mplayer-users/2003-February/030222.html
26 *
27 * [123][456] Sample subtitle
28 * [1234][5678] Line 1|Line 2
29 * [12345][67890] /Italic|Normal
30 * [12345][67890] /Italic|/Italic
31 * [12345][67890] Normal|/Italic
32 *
33 * (The space between the last ']' bracket and the text appears to be optional)
34 */
35
36 static gchar *
mpl2_parse_line(ParserState * state,const gchar * line,guint line_num)37 mpl2_parse_line (ParserState * state, const gchar * line, guint line_num)
38 {
39 GString *markup;
40 gint dc_start, dc_stop;
41
42 /* parse subtitle file line */
43 if (sscanf (line, "[%u][%u]", &dc_start, &dc_stop) != 2) {
44 GST_WARNING ("failed to extract timestamps for line '%s'", line);
45 return NULL;
46 }
47
48 GST_LOG ("line format %u %u", dc_start, dc_stop);
49 state->start_time = GST_SECOND / 10 * dc_start;
50 state->duration = (GST_SECOND / 10 * dc_stop) - state->start_time;
51
52 /* skip brackets with timestamps */
53 line = strchr (line, ']') + 1;
54 line = strchr (line, ']') + 1;
55
56 markup = g_string_new (NULL);
57
58 while (1) {
59 const gchar *sep;
60 gchar *line_chunk_escaped;
61 gboolean italics;
62
63 /* skip leading white spaces */
64 while (*line == ' ' || *line == '\t')
65 ++line;
66
67 /* a '/' at the beginning indicates italics */
68 if (*line == '/') {
69 italics = TRUE;
70 g_string_append (markup, "<i>");
71 ++line;
72 } else {
73 italics = FALSE;
74 }
75
76 if ((sep = strchr (line, '|')))
77 line_chunk_escaped = g_markup_escape_text (line, sep - line);
78 else
79 line_chunk_escaped = g_markup_escape_text (line, -1);
80
81 GST_LOG ("escaped line: %s", line_chunk_escaped);
82 g_string_append (markup, line_chunk_escaped);
83
84 g_free (line_chunk_escaped);
85
86 if (italics)
87 g_string_append (markup, "</i>");
88 if (sep == NULL)
89 break;
90
91 /* move after the '|' and append another line */
92 g_string_append (markup, "\n");
93 line = sep + 1;
94 }
95
96 return g_strstrip (g_string_free (markup, FALSE));
97 }
98
99 gchar *
parse_mpl2(ParserState * state,const gchar * line)100 parse_mpl2 (ParserState * state, const gchar * line)
101 {
102 gchar *ret;
103
104 ret = mpl2_parse_line (state, line, state->state);
105 ++state->state;
106 return ret;
107 }
108