summaryrefslogtreecommitdiff
path: root/gst/subparse/tmplayerparse.c
blob: 8f48c6e9d4e7ce51c06eef175b48705360171065 (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
/* GStreamer tmplayer format subtitle parser
 * Copyright (C) 2006-2008 Tim-Philipp Müller <tim centricular net>
 *
 * 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.
 */

#include "tmplayerparse.h"

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

/* From http://forum.doom9.org/archive/index.php/t-81059.html:
 * 
 * TMPlayer format, which comes in five varieties:
 * 
 * time-base 00:00:00:
 * 00:00:50:This is the Earth at a time|when the dinosaurs roamed...
 * 00:00:53:
 * 00:00:54:a lush and fertile planet.
 * 00:00:56:
 * 
 * time-base 0:00:00:
 * 0:00:50:This is the Earth at a time|when the dinosaurs roamed...
 * 0:00:53:
 * 0:00:54:a lush and fertile planet.
 * 0:00:56:
 * 
 * time-base 00:00:00=
 * 00:00:50=This is the Earth at a time|when the dinosaurs roamed...
 * 00:00:53=
 * 00:00:54=a lush and fertile planet.
 * 00:00:56=
 * 
 * time-base 0:00:00=
 * 0:00:50=This is the Earth at a time|when the dinosaurs roamed...
 * 0:00:53=
 * 0:00:54=a lush and fertile planet.
 * 0:00:56=
 * 
 * and multiline time-base 00:00:00,1=
 * 00:00:50,1=This is the Earth at a time
 * 00:00:50,2=when the dinosaurs roamed...
 * 00:00:53,1=
 * 00:00:54,1=a lush and fertile planet.
 * 00:00:56,1=
 *
 * --------------------------------------------------------------------------
 *
 * And another variety (which is 'time-base 0:00:00:' but without empty lines):
 *
 * 00:00:01:This is the Earth at a time|when the dinosaurs roamed...
 * 00:00:03:a lush and fertile planet.
 * 00:00:06:More text here
 * 00:00:12:Yet another line
 *
 */

static gchar *
tmplayer_process_buffer (ParserState * state)
{
  gchar *ret;

  ret = g_strndup (state->buf->str, state->buf->len);
  g_strdelimit (ret, "|", '\n');
  g_string_truncate (state->buf, 0);
  return ret;
}

static gchar *
tmplayer_parse_line (ParserState * state, const gchar * line, guint line_num)
{
  GstClockTime ts = GST_CLOCK_TIME_NONE;
  const gchar *text_start = NULL;
  gboolean multiline = FALSE;
  gchar *ret = NULL;
  gchar divc = '\0';
  guint h, m, s, l = 1;

  if (sscanf (line, "%u:%02u:%02u,%u%c", &h, &m, &s, &l, &divc) == 5 &&
      (divc == '=')) {
    GST_LOG ("multiline format %u %u %u %u", h, m, s, l);
    ts = GST_SECOND * ((((h * 60) + m) * 60) + s);
    text_start = strchr (line, '=');
    multiline = TRUE;
  } else if (sscanf (line, "%u:%02u:%02u%c", &h, &m, &s, &divc) == 4 &&
      (divc == '=' || divc == ':')) {
    GST_LOG ("single line format %u %u %u %u %c", h, m, s, l, divc);
    ts = GST_SECOND * ((((h * 60) + m) * 60) + s);
    text_start = strchr (line + 6, divc);
  } else if (line[0] == '\0' && state->buf->len > 0 &&
      GST_CLOCK_TIME_IS_VALID (state->start_time)) {
    /* if we get an empty line (could be the end of the file, but doesn't have
     * to be), just push whatever is still in the buffer without a duration */
    GST_LOG ("empty line, and there's still text in the buffer");
    ret = tmplayer_process_buffer (state);
    state->duration = GST_CLOCK_TIME_NONE;
    return ret;
  } else {
    GST_WARNING ("failed to parse line: '%s'", line);
    return NULL;
  }

  /* if this is a line without text, or the first line in a multiline file,
   * process and return the data in the buffer, which is the previous line(s) */
  if (text_start == NULL || text_start[1] == '\0' ||
      (l == 1 && state->buf->len > 0)) {

    if (GST_CLOCK_TIME_IS_VALID (state->start_time) &&
        state->start_time < ts && line_num > 0) {
      ret = tmplayer_process_buffer (state);
      state->duration = ts - state->start_time;
      /* ..and append current line's text (if there is any) for the next round.
       * We don't have to store ts as pending_start_time, since we deduce the
       * durations from the start times anyway, so as long as the parser just
       * forwards state->start_time by duration after it pushes the line we
       * are about to return it will all be good. */
      g_string_append (state->buf, text_start + 1);
    } else if (line_num > 0) {
      GST_WARNING ("end of subtitle unit but no valid start time?!");
    }
  } else {
    if (l > 1)
      g_string_append_c (state->buf, '\n');
    g_string_append (state->buf, text_start + 1);
    state->start_time = ts;
  }

  GST_LOG ("returning: '%s'", GST_STR_NULL (ret));
  return ret;
}

gchar *
parse_tmplayer (ParserState * state, const gchar * line)
{
  gchar *ret;

  /* GST_LOG ("Parsing: %s", line); */

  ret = tmplayer_parse_line (state, line, state->state);
  ++state->state;

  return ret;
}