|
Packit |
971217 |
/* GStreamer mpl2 format subtitle parser
|
|
Packit |
971217 |
* Copyright (C) 2006 Kamil Pawlowski <kamilpe gmail com>
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is free software; you can redistribute it and/or
|
|
Packit |
971217 |
* modify it under the terms of the GNU Library General Public
|
|
Packit |
971217 |
* License as published by the Free Software Foundation; either
|
|
Packit |
971217 |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
971217 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
971217 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
971217 |
* Library General Public License for more details.
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* You should have received a copy of the GNU Library General Public
|
|
Packit |
971217 |
* License along with this library; if not, write to the
|
|
Packit |
971217 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
Packit |
971217 |
* Boston, MA 02110-1301, USA.
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include "mpl2parse.h"
|
|
Packit |
971217 |
|
|
Packit |
971217 |
#include <stdio.h>
|
|
Packit |
971217 |
#include <string.h>
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* From http://lists.mplayerhq.hu/pipermail/mplayer-users/2003-February/030222.html
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* [123][456] Sample subtitle
|
|
Packit |
971217 |
* [1234][5678] Line 1|Line 2
|
|
Packit |
971217 |
* [12345][67890] /Italic|Normal
|
|
Packit |
971217 |
* [12345][67890] /Italic|/Italic
|
|
Packit |
971217 |
* [12345][67890] Normal|/Italic
|
|
Packit |
971217 |
*
|
|
Packit |
971217 |
* (The space between the last ']' bracket and the text appears to be optional)
|
|
Packit |
971217 |
*/
|
|
Packit |
971217 |
|
|
Packit |
971217 |
static gchar *
|
|
Packit |
971217 |
mpl2_parse_line (ParserState * state, const gchar * line, guint line_num)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
GString *markup;
|
|
Packit |
971217 |
gint dc_start, dc_stop;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* parse subtitle file line */
|
|
Packit |
971217 |
if (sscanf (line, "[%u][%u]", &dc_start, &dc_stop) != 2) {
|
|
Packit |
971217 |
GST_WARNING ("failed to extract timestamps for line '%s'", line);
|
|
Packit |
971217 |
return NULL;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_LOG ("line format %u %u", dc_start, dc_stop);
|
|
Packit |
971217 |
state->start_time = GST_SECOND / 10 * dc_start;
|
|
Packit |
971217 |
state->duration = (GST_SECOND / 10 * dc_stop) - state->start_time;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* skip brackets with timestamps */
|
|
Packit |
971217 |
line = strchr (line, ']') + 1;
|
|
Packit |
971217 |
line = strchr (line, ']') + 1;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
markup = g_string_new (NULL);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
while (1) {
|
|
Packit |
971217 |
const gchar *sep;
|
|
Packit |
971217 |
gchar *line_chunk_escaped;
|
|
Packit |
971217 |
gboolean italics;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* skip leading white spaces */
|
|
Packit |
971217 |
while (*line == ' ' || *line == '\t')
|
|
Packit |
971217 |
++line;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* a '/' at the beginning indicates italics */
|
|
Packit |
971217 |
if (*line == '/') {
|
|
Packit |
971217 |
italics = TRUE;
|
|
Packit |
971217 |
g_string_append (markup, "");
|
|
Packit |
971217 |
++line;
|
|
Packit |
971217 |
} else {
|
|
Packit |
971217 |
italics = FALSE;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if ((sep = strchr (line, '|')))
|
|
Packit |
971217 |
line_chunk_escaped = g_markup_escape_text (line, sep - line);
|
|
Packit |
971217 |
else
|
|
Packit |
971217 |
line_chunk_escaped = g_markup_escape_text (line, -1);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
GST_LOG ("escaped line: %s", line_chunk_escaped);
|
|
Packit |
971217 |
g_string_append (markup, line_chunk_escaped);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
g_free (line_chunk_escaped);
|
|
Packit |
971217 |
|
|
Packit |
971217 |
if (italics)
|
|
Packit |
971217 |
g_string_append (markup, "");
|
|
Packit |
971217 |
if (sep == NULL)
|
|
Packit |
971217 |
break;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
/* move after the '|' and append another line */
|
|
Packit |
971217 |
g_string_append (markup, "\n");
|
|
Packit |
971217 |
line = sep + 1;
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
return g_strstrip (g_string_free (markup, FALSE));
|
|
Packit |
971217 |
}
|
|
Packit |
971217 |
|
|
Packit |
971217 |
gchar *
|
|
Packit |
971217 |
parse_mpl2 (ParserState * state, const gchar * line)
|
|
Packit |
971217 |
{
|
|
Packit |
971217 |
gchar *ret;
|
|
Packit |
971217 |
|
|
Packit |
971217 |
ret = mpl2_parse_line (state, line, state->state);
|
|
Packit |
971217 |
++state->state;
|
|
Packit |
971217 |
return ret;
|
|
Packit |
971217 |
}
|