summaryrefslogtreecommitdiff
path: root/gst/subparse/qttextparse.c
blob: b48daf913139625057d050d39bb3fa7422d2ae0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* GStreamer QTtext subtitle parser
 * Copyright (c) 2009 Thiago Santos <thiago.sousa.santos collabora co uk>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* References:
 * http://www.apple.com/quicktime/tutorials/texttracks.html
 * http://www.apple.com/quicktime/tutorials/textdescriptors.html
 */

#include "qttextparse.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MIN_TO_NSEC  (60 * GST_SECOND)
#define HOUR_TO_NSEC (60 * MIN_TO_NSEC)

#define GST_QTTEXT_CONTEXT(state) ((GstQTTextContext *) (state)->user_data)

typedef struct _GstQTTextContext GstQTTextContext;

struct _GstQTTextContext
{
  /* timing variables */
  gint timescale;
  gboolean absolute;
  guint64 start_time;

  gboolean markup_open;
  gboolean need_markup;

  gchar *font;
  gint font_size;
  gchar *bg_color;
  gchar *fg_color;

  gboolean bold;
  gboolean italic;
};

void
qttext_context_init (ParserState * state)
{
  GstQTTextContext *context;

  state->user_data = g_new0 (GstQTTextContext, 1);

  context = GST_QTTEXT_CONTEXT (state);

  /* we use 1000 as a default */
  context->timescale = 1000;
  context->absolute = TRUE;

  context->markup_open = FALSE;
  context->need_markup = FALSE;

  context->font_size = 12;
}

void
qttext_context_deinit (ParserState * state)
{
  if (state->user_data != NULL) {
    GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
    g_free (context->font);
    g_free (context->bg_color);
    g_free (context->fg_color);

    g_free (state->user_data);
    state->user_data = NULL;
  }
}

/*
 * Reads the string right after the ':'
 */
static gchar *
read_str (const gchar * line, const gchar * end)
{
  gint index = 0;

  while (line[index] != ':' && line[index] != '}') {
    index++;
  }
  if (line[index] != ':')
    return NULL;
  index++;
  while (line[index] == ' ')
    index++;

  return g_strndup (line + index, (end - (line + index)));
}

/* search for the ':' and parse the number right after it */
static gint
read_int (const gchar * line)
{
  gint index = 0;
  while (line[index] != ':' && line[index] != '}') {
    index++;
  }
  if (line[index] != ':')
    return 0;
  index++;
  return atoi (line + index);
}

/* skip the ':' and then match the following string
 * with 'match', but only if it before 'upto' */
static gboolean
string_match (const gchar * line, const gchar * match, const gchar * upto)
{
  gchar *result = strstr (line, match);
  return (result < upto);
}

/*
 * Reads the color values and stores them in r, g and b.
 */
static gboolean
read_color (const gchar * line, gint * r, gint * g, gint * b)
{
  gint index = 0;
  while (line[index] != ':' && line[index] != '}') {
    index++;
  }
  if (line[index] != ':')
    return FALSE;
  index++;

  *r = atoi (line + index);

  while (line[index] != '}' && line[index] != ',') {
    index++;
  }
  if (line[index] != ',')
    return FALSE;
  index++;

  *g = atoi (line + index);

  while (line[index] != '}' && line[index] != ',') {
    index++;
  }
  if (line[index] != ',')
    return FALSE;
  index++;

  *b = atoi (line + index);

  return TRUE;
}

static gchar *
make_color (gint r, gint g, gint b)
{
  /* qttext goes up to 65535, while pango goes to 255 */
  r /= 256;
  g /= 256;
  b /= 256;
  return g_strdup_printf ("#%02X%02X%02X", r, g, b);
}

static gboolean
qttext_parse_tag (ParserState * state, const gchar * line, gint * index)
{
  gchar *next;
  gint next_index;
  gint aux;
  gchar *str;
  gint r, g, b;
  GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);

  g_assert (line[*index] == '{');

  next = strchr (line + *index, '}');
  if (next == NULL) {
    goto error_out;
  } else {
    next_index = 1 + (next - line);
  }
  g_assert (line[next_index - 1] == '}');

  *index = *index + 1;          /* skip the { */

  /* now identify our tag */
  /* FIXME: those should be case unsensitive */
  /* TODO: there are other tags that could be added here */
  if (strncmp (line + *index, "QTtext", 6) == 0) {
    /* NOP */

  } else if (strncmp (line + *index, "font", 4) == 0) {
    str = read_str (line + *index + 4, line + next_index - 1);
    if (str) {
      g_free (context->font);
      context->font = str;
      context->need_markup = TRUE;
      GST_DEBUG ("Setting qttext font to %s", str);
    } else {
      GST_WARNING ("Failed to parse qttext font at line: %s", line);
    }

  } else if (strncmp (line + *index, "size", 4) == 0) {
    aux = read_int (line + *index + 4);
    if (aux == 0) {
      GST_WARNING ("Invalid size at line %s, using 12", line);
      context->font_size = 12;
    } else {
      GST_DEBUG ("Setting qttext font-size to: %d", aux);
      context->font_size = aux;
    }
    context->need_markup = TRUE;

  } else if (strncmp (line + *index, "textColor", 9) == 0) {
    if (read_color (line + *index + 9, &r, &g, &b)) {
      context->fg_color = make_color (r, g, b);
      GST_DEBUG ("Setting qttext fg color to %s", context->fg_color);
    } else {
      GST_WARNING ("Failed to read textColor at line %s", line);
    }
    context->need_markup = TRUE;

  } else if (strncmp (line + *index, "backColor", 9) == 0) {
    if (read_color (line + *index + 9, &r, &g, &b)) {
      context->bg_color = make_color (r, g, b);
      GST_DEBUG ("Setting qttext bg color to %s", context->bg_color);
    } else {
      GST_WARNING ("Failed to read backColor at line %s, disabling", line);
      g_free (context->bg_color);
      context->bg_color = NULL;
    }
    context->need_markup = TRUE;

  } else if (strncmp (line + *index, "plain", 5) == 0) {
    context->bold = FALSE;
    context->italic = FALSE;
    context->need_markup = TRUE;
    GST_DEBUG ("Setting qttext style to plain");

  } else if (strncmp (line + *index, "bold", 4) == 0) {
    context->bold = TRUE;
    context->italic = FALSE;
    context->need_markup = TRUE;
    GST_DEBUG ("Setting qttext style to bold");

  } else if (strncmp (line + *index, "italic", 6) == 0) {
    context->bold = FALSE;
    context->italic = TRUE;
    context->need_markup = TRUE;
    GST_DEBUG ("Setting qttext style to italic");

  } else if (strncmp (line + *index, "timescale", 9) == 0) {
    aux = read_int (line + *index + 9);
    if (aux == 0) {
      GST_WARNING ("Couldn't interpret timescale at line %s, using 1000", line);
      context->timescale = 1000;
    } else {
      GST_DEBUG ("Setting qttext timescale to: %d", aux);
      context->timescale = aux;
    }

  } else if (strncmp (line + *index, "timestamps", 10) == 0) {
    if (string_match (line + *index + 10, "relative", line + next_index)) {
      GST_DEBUG ("Setting qttext timestamps to relative");
      context->absolute = FALSE;
    } else {
      /* call it absolute otherwise */
      GST_DEBUG ("Setting qttext timestamps to absolute");
      context->absolute = TRUE;
    }

  } else {
    GST_WARNING ("Unused qttext tag starting at: %s", line + *index);
  }

  *index = next_index;
  return TRUE;

error_out:
  {
    GST_WARNING ("Failed to parse qttext tag at line %s", line);
    return FALSE;
  }
}

static guint64
qttext_parse_timestamp (ParserState * state, const gchar * line, gint index)
{
  int ret;
  gint hour, min, sec, dec;
  GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);

  ret = sscanf (line + index, "[%d:%d:%d.%d]", &hour, &min, &sec, &dec);
  if (ret != 3 && ret != 4) {
    /* bad timestamp */
    GST_WARNING ("Bad qttext timestamp found: %s", line);
    return 0;
  }

  if (ret == 3) {
    /* be forgiving for missing decimal part */
    dec = 0;
  }

  /* parse the decimal part according to the timescale */
  g_assert (context->timescale != 0);
  dec = (GST_SECOND * dec) / context->timescale;

  /* return the result */
  return hour * HOUR_TO_NSEC + min * MIN_TO_NSEC + sec * GST_SECOND + dec;
}

static void
qttext_open_markup (ParserState * state)
{
  GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);

  g_string_append (state->buf, "<span");

  /* add your markup tags here */
  if (context->font)
    g_string_append_printf (state->buf, " font='%s %d'", context->font,
        context->font_size);
  else
    g_string_append_printf (state->buf, " font='%d'", context->font_size);

  if (context->bg_color)
    g_string_append_printf (state->buf, " bgcolor='%s'", context->bg_color);
  if (context->fg_color)
    g_string_append_printf (state->buf, " color='%s'", context->fg_color);

  if (context->bold)
    g_string_append (state->buf, " weight='bold'");
  if (context->italic)
    g_string_append (state->buf, " style='italic'");

  g_string_append (state->buf, ">");
}

static void
qttext_prepare_text (ParserState * state)
{
  GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
  if (state->buf == NULL) {
    state->buf = g_string_sized_new (256);      /* this should be enough */
  } else {
    g_string_append (state->buf, "\n");
  }

  /* if needed, add pango markup */
  if (context->need_markup) {
    if (context->markup_open) {
      g_string_append (state->buf, "</span>");
    }
    qttext_open_markup (state);
    context->markup_open = TRUE;
  }
}

static void
qttext_parse_text (ParserState * state, const gchar * line, gint index)
{
  qttext_prepare_text (state);
  g_string_append (state->buf, line + index);
}

static gchar *
qttext_get_text (ParserState * state)
{
  gchar *ret;
  GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);
  if (state->buf == NULL)
    return NULL;

  if (context->markup_open) {
    g_string_append (state->buf, "</span>");
  }
  ret = g_string_free (state->buf, FALSE);
  state->buf = NULL;
  context->markup_open = FALSE;
  return ret;
}

gchar *
parse_qttext (ParserState * state, const gchar * line)
{
  gint i;
  guint64 ts;
  gchar *ret = NULL;
  GstQTTextContext *context = GST_QTTEXT_CONTEXT (state);

  i = 0;
  while (line[i] != '\0') {
    /* find first interesting character from 'i' onwards */

    if (line[i] == '{') {
      /* this is a tag, parse it */
      if (!qttext_parse_tag (state, line, &i)) {
        break;
      }
    } else if (line[i] == '[') {
      /* this is a time, convert it to a timestamp */
      ts = qttext_parse_timestamp (state, line, i);

      /* check if we have pending text to send, in case we prepare it */
      if (state->buf) {
        ret = qttext_get_text (state);
        if (context->absolute)
          state->duration = ts - context->start_time;
        else
          state->duration = ts;
        state->start_time = context->start_time;
      }
      state->buf = NULL;

      if (ts == 0) {
        /* this is an error */
      } else {
        if (context->absolute)
          context->start_time = ts;
        else
          context->start_time += ts;
      }

      /* we assume there is nothing else on this line */
      break;

    } else if (line[i] == ' ' || line[i] == '\t') {
      i++;                      /* NOP */
    } else {
      /* this is the actual text, output the rest of the line as it */
      qttext_parse_text (state, line, i);
      break;
    }
  }
  return ret;
}