gst_plugins_base/gst/subparse/tmplayerparse.c
changeset 0 0e761a78d257
child 7 567bb019e3e3
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer tmplayer format subtitle parser
       
     2  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
       
     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., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 #include "tmplayerparse.h"
       
    21 
       
    22 #include <string.h>
       
    23 
       
    24 /* From http://forum.doom9.org/archive/index.php/t-81059.html:
       
    25  * 
       
    26  * TMPlayer format, which comes in five varieties:
       
    27  * 
       
    28  * time-base 00:00:00:
       
    29  * 00:00:50:This is the Earth at a time|when the dinosaurs roamed...
       
    30  * 00:00:53:
       
    31  * 00:00:54:a lush and fertile planet.
       
    32  * 00:00:56:
       
    33  * 
       
    34  * time-base 0:00:00:
       
    35  * 0:00:50:This is the Earth at a time|when the dinosaurs roamed...
       
    36  * 0:00:53:
       
    37  * 0:00:54:a lush and fertile planet.
       
    38  * 0:00:56:
       
    39  * 
       
    40  * time-base 00:00:00=
       
    41  * 00:00:50=This is the Earth at a time|when the dinosaurs roamed...
       
    42  * 00:00:53=
       
    43  * 00:00:54=a lush and fertile planet.
       
    44  * 00:00:56=
       
    45  * 
       
    46  * time-base 0:00:00=
       
    47  * 0:00:50=This is the Earth at a time|when the dinosaurs roamed...
       
    48  * 0:00:53=
       
    49  * 0:00:54=a lush and fertile planet.
       
    50  * 0:00:56=
       
    51  * 
       
    52  * and multiline time-base 00:00:00,1=
       
    53  * 00:00:50,1=This is the Earth at a time
       
    54  * 00:00:50,2=when the dinosaurs roamed...
       
    55  * 00:00:53,1=
       
    56  * 00:00:54,1=a lush and fertile planet.
       
    57  * 00:00:56,1=
       
    58  */
       
    59 
       
    60 static gchar *
       
    61 tmplayer_parse_line (ParserState * state, const gchar * line, guint line_num)
       
    62 {
       
    63   GstClockTime ts = GST_CLOCK_TIME_NONE;
       
    64   const gchar *text_start = NULL;
       
    65   gboolean multiline = FALSE;
       
    66   gchar *ret = NULL;
       
    67   gchar divc = '\0';
       
    68   guint h, m, s, l = 1;
       
    69 
       
    70   if (sscanf (line, "%u:%02u:%02u,%u%c", &h, &m, &s, &l, &divc) == 5 &&
       
    71       (divc == '=')) {
       
    72     GST_LOG ("multiline format %u %u %u %u", h, m, s, l);
       
    73     ts = GST_SECOND * ((((h * 60) + m) * 60) + s);
       
    74     text_start = strchr (line, '=');
       
    75     multiline = TRUE;
       
    76   } else if (sscanf (line, "%u:%02u:%02u%c", &h, &m, &s, &divc) == 4 &&
       
    77       (divc == '=' || divc == ':')) {
       
    78     GST_LOG ("single line format %u %u %u %u %c", h, m, s, l, divc);
       
    79     ts = GST_SECOND * ((((h * 60) + m) * 60) + s);
       
    80     text_start = strchr (line + 6, divc);
       
    81   } else {
       
    82     GST_WARNING ("failed to parse line: '%s'", line);
       
    83     return NULL;
       
    84   }
       
    85 
       
    86   if (text_start == NULL || text_start[1] == '\0' ||
       
    87       (l == 1 && state->buf->len > 0)) {
       
    88     if (GST_CLOCK_TIME_IS_VALID (state->start_time) &&
       
    89         state->start_time < ts && line_num > 0) {
       
    90       ret = g_strndup (state->buf->str, state->buf->len);
       
    91       g_strdelimit (ret, "|", '\n');
       
    92       g_string_truncate (state->buf, 0);
       
    93       state->duration = ts - state->start_time;
       
    94     } else if (line_num > 0) {
       
    95       GST_WARNING ("end of subtitle unit but no valid start time?!");
       
    96     }
       
    97   } else {
       
    98     if (l > 1)
       
    99       g_string_append_c (state->buf, '\n');
       
   100     g_string_append (state->buf, text_start + 1);
       
   101     state->start_time = ts;
       
   102   }
       
   103 
       
   104   return ret;
       
   105 }
       
   106 #ifdef __SYMBIAN32__
       
   107 EXPORT_C
       
   108 #endif
       
   109 
       
   110 
       
   111 gchar *
       
   112 parse_tmplayer (ParserState * state, const gchar * line)
       
   113 {
       
   114   gchar *ret;
       
   115 
       
   116   /* GST_LOG ("Parsing: %s", line); */
       
   117 
       
   118   ret = tmplayer_parse_line (state, line, state->state);
       
   119   ++state->state;
       
   120 
       
   121   return ret;
       
   122 }