1 /* GStreamer QTtext subtitle parser
2 * Copyright (c) 2009 Thiago Santos <thiago.sousa.santos collabora co uk>>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * http://www.apple.com/quicktime/tutorials/texttracks.html
22 * http://www.apple.com/quicktime/tutorials/textdescriptors.html
25 #include "qttextparse.h"
31 #define MIN_TO_NSEC (60 * GST_SECOND)
32 #define HOUR_TO_NSEC (60 * MIN_TO_NSEC)
34 #define GST_QTTEXT_CONTEXT(state) ((GstQTTextContext *) (state)->user_data)
36 typedef struct _GstQTTextContext GstQTTextContext;
38 struct _GstQTTextContext
40 /* timing variables */
58 qttext_context_init (ParserState * state)
60 GstQTTextContext *context;
62 state->user_data = g_new0 (GstQTTextContext, 1);
64 context = GST_QTTEXT_CONTEXT (state);
66 /* we use 1000 as a default */
67 context->timescale = 1000;
68 context->absolute = TRUE;
70 context->markup_open = FALSE;
71 context->need_markup = FALSE;
73 context->font_size = 12;
77 qttext_context_deinit (ParserState * state)
79 if (state->user_data != NULL) {
80 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
81 g_free (context->font);
82 g_free (context->bg_color);
83 g_free (context->fg_color);
85 g_free (state->user_data);
86 state->user_data = NULL;
91 * Reads the string right after the ':'
94 read_str (const gchar * line, const gchar * end)
98 while (line[index] != ':' && line[index] != '}') {
101 if (line[index] != ':')
104 while (line[index] == ' ')
107 return g_strndup (line + index, (end - (line + index)));
110 /* search for the ':' and parse the number right after it */
112 read_int (const gchar * line)
115 while (line[index] != ':' && line[index] != '}') {
118 if (line[index] != ':')
121 return atoi (line + index);
124 /* skip the ':' and then match the following string
125 * with 'match', but only if it before 'upto' */
127 string_match (const gchar * line, const gchar * match, const gchar * upto)
129 gchar *result = strstr (line, match);
130 return (result < upto);
134 * Reads the color values and stores them in r, g and b.
137 read_color (const gchar * line, gint * r, gint * g, gint * b)
140 while (line[index] != ':' && line[index] != '}') {
143 if (line[index] != ':')
147 *r = atoi (line + index);
149 while (line[index] != '}' && line[index] != ',') {
152 if (line[index] != ',')
156 *g = atoi (line + index);
158 while (line[index] != '}' && line[index] != ',') {
161 if (line[index] != ',')
165 *b = atoi (line + index);
171 make_color (gint r, gint g, gint b)
173 /* qttext goes up to 65535, while pango goes to 255 */
177 return g_strdup_printf ("#%02X%02X%02X", r, g, b);
181 qttext_parse_tag (ParserState * state, const gchar * line, gint * index)
188 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
190 g_assert (line[*index] == '{');
192 next = strchr (line + *index, '}');
196 next_index = 1 + (next - line);
198 g_assert (line[next_index - 1] == '}');
200 *index = *index + 1; /* skip the { */
202 /* now identify our tag */
203 /* FIXME: those should be case unsensitive */
204 /* TODO: there are other tags that could be added here */
205 if (strncmp (line + *index, "QTtext", 6) == 0) {
208 } else if (strncmp (line + *index, "font", 4) == 0) {
209 str = read_str (line + *index + 4, line + next_index - 1);
211 g_free (context->font);
213 context->need_markup = TRUE;
214 GST_DEBUG ("Setting qttext font to %s", str);
216 GST_WARNING ("Failed to parse qttext font at line: %s", line);
219 } else if (strncmp (line + *index, "size", 4) == 0) {
220 aux = read_int (line + *index + 4);
222 GST_WARNING ("Invalid size at line %s, using 12", line);
223 context->font_size = 12;
225 GST_DEBUG ("Setting qttext font-size to: %d", aux);
226 context->font_size = aux;
228 context->need_markup = TRUE;
230 } else if (strncmp (line + *index, "textColor", 9) == 0) {
231 if (read_color (line + *index + 9, &r, &g, &b)) {
232 context->fg_color = make_color (r, g, b);
233 GST_DEBUG ("Setting qttext fg color to %s", context->fg_color);
235 GST_WARNING ("Failed to read textColor at line %s", line);
237 context->need_markup = TRUE;
239 } else if (strncmp (line + *index, "backColor", 9) == 0) {
240 if (read_color (line + *index + 9, &r, &g, &b)) {
241 context->bg_color = make_color (r, g, b);
242 GST_DEBUG ("Setting qttext bg color to %s", context->bg_color);
244 GST_WARNING ("Failed to read backColor at line %s, disabling", line);
245 g_free (context->bg_color);
246 context->bg_color = NULL;
248 context->need_markup = TRUE;
250 } else if (strncmp (line + *index, "plain", 5) == 0) {
251 context->bold = FALSE;
252 context->italic = FALSE;
253 context->need_markup = TRUE;
254 GST_DEBUG ("Setting qttext style to plain");
256 } else if (strncmp (line + *index, "bold", 4) == 0) {
257 context->bold = TRUE;
258 context->italic = FALSE;
259 context->need_markup = TRUE;
260 GST_DEBUG ("Setting qttext style to bold");
262 } else if (strncmp (line + *index, "italic", 6) == 0) {
263 context->bold = FALSE;
264 context->italic = TRUE;
265 context->need_markup = TRUE;
266 GST_DEBUG ("Setting qttext style to italic");
268 } else if (strncmp (line + *index, "timescale", 9) == 0) {
269 aux = read_int (line + *index + 9);
271 GST_WARNING ("Couldn't interpret timescale at line %s, using 1000", line);
272 context->timescale = 1000;
274 GST_DEBUG ("Setting qttext timescale to: %d", aux);
275 context->timescale = aux;
278 } else if (strncmp (line + *index, "timestamps", 10) == 0) {
279 if (string_match (line + *index + 10, "relative", line + next_index)) {
280 GST_DEBUG ("Setting qttext timestamps to relative");
281 context->absolute = FALSE;
283 /* call it absolute otherwise */
284 GST_DEBUG ("Setting qttext timestamps to absolute");
285 context->absolute = TRUE;
289 GST_WARNING ("Unused qttext tag starting at: %s", line + *index);
297 GST_WARNING ("Failed to parse qttext tag at line %s", line);
303 qttext_parse_timestamp (ParserState * state, const gchar * line, gint index)
306 gint hour, min, sec, dec;
307 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
309 ret = sscanf (line + index, "[%d:%d:%d.%d]", &hour, &min, &sec, &dec);
310 if (ret != 3 && ret != 4) {
312 GST_WARNING ("Bad qttext timestamp found: %s", line);
317 /* be forgiving for missing decimal part */
321 /* parse the decimal part according to the timescale */
322 g_assert (context->timescale != 0);
323 dec = (GST_SECOND * dec) / context->timescale;
325 /* return the result */
326 return hour * HOUR_TO_NSEC + min * MIN_TO_NSEC + sec * GST_SECOND + dec;
330 qttext_open_markup (ParserState * state)
332 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
334 g_string_append (state->buf, "<span");
336 /* add your markup tags here */
338 g_string_append_printf (state->buf, " font='%s %d'", context->font,
341 g_string_append_printf (state->buf, " font='%d'", context->font_size);
343 if (context->bg_color)
344 g_string_append_printf (state->buf, " bgcolor='%s'", context->bg_color);
345 if (context->fg_color)
346 g_string_append_printf (state->buf, " color='%s'", context->fg_color);
349 g_string_append (state->buf, " weight='bold'");
351 g_string_append (state->buf, " style='italic'");
353 g_string_append (state->buf, ">");
357 qttext_prepare_text (ParserState * state)
359 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
360 if (state->buf == NULL) {
361 state->buf = g_string_sized_new (256); /* this should be enough */
363 g_string_append (state->buf, "\n");
366 /* if needed, add pango markup */
367 if (context->need_markup) {
368 if (context->markup_open) {
369 g_string_append (state->buf, "</span>");
371 qttext_open_markup (state);
372 context->markup_open = TRUE;
377 qttext_parse_text (ParserState * state, const gchar * line, gint index)
379 qttext_prepare_text (state);
380 g_string_append (state->buf, line + index);
384 qttext_get_text (ParserState * state)
387 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
388 if (state->buf == NULL)
391 if (context->markup_open) {
392 g_string_append (state->buf, "</span>");
394 ret = g_string_free (state->buf, FALSE);
396 context->markup_open = FALSE;
401 parse_qttext (ParserState * state, const gchar * line)
406 GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
409 while (line[i] != '\0') {
410 /* find first interesting character from 'i' onwards */
412 if (line[i] == '{') {
413 /* this is a tag, parse it */
414 if (!qttext_parse_tag (state, line, &i)) {
417 } else if (line[i] == '[') {
418 /* this is a time, convert it to a timestamp */
419 ts = qttext_parse_timestamp (state, line, i);
421 /* check if we have pending text to send, in case we prepare it */
423 ret = qttext_get_text (state);
424 if (context->absolute)
425 state->duration = ts - context->start_time;
427 state->duration = ts;
428 state->start_time = context->start_time;
433 /* this is an error */
435 if (context->absolute)
436 context->start_time = ts;
438 context->start_time += ts;
441 /* we assume there is nothing else on this line */
444 } else if (line[i] == ' ' || line[i] == '\t') {
447 /* this is the actual text, output the rest of the line as it */
448 qttext_parse_text (state, line, i);