• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2008> Thijs Vermeir <thijsvermeir@gmail.com>
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <string.h>
25 
26 #include "gstsrtenc.h"
27 
28 GST_DEBUG_CATEGORY_STATIC (srtenc_debug);
29 #define GST_CAT_DEFAULT srtenc_debug
30 
31 enum
32 {
33   PROP_0,
34   PROP_TIMESTAMP,
35   PROP_DURATION
36 };
37 
38 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
39     GST_PAD_SRC,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("application/x-subtitle"));
42 
43 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("text/x-raw, format = { pango-markup, utf8 }"));
47 
48 static GstFlowReturn gst_srt_enc_chain (GstPad * pad, GstObject * parent,
49     GstBuffer * buf);
50 static gboolean gst_srt_enc_event (GstPad * pad, GstObject * parent,
51     GstEvent * event);
52 static void gst_srt_enc_get_property (GObject * object, guint prop_id,
53     GValue * value, GParamSpec * pspec);
54 static void gst_srt_enc_reset (GstSrtEnc * srtenc);
55 static void gst_srt_enc_set_property (GObject * object, guint prop_id,
56     const GValue * value, GParamSpec * pspec);
57 
58 #define parent_class gst_srt_enc_parent_class
59 G_DEFINE_TYPE (GstSrtEnc, gst_srt_enc, GST_TYPE_ELEMENT);
60 
61 static void
gst_srt_enc_append_timestamp_to_string(GstClockTime timestamp,GString * str)62 gst_srt_enc_append_timestamp_to_string (GstClockTime timestamp, GString * str)
63 {
64   guint h, m, s, ms;
65 
66   h = timestamp / (3600 * GST_SECOND);
67 
68   timestamp -= h * 3600 * GST_SECOND;
69   m = timestamp / (60 * GST_SECOND);
70 
71   timestamp -= m * 60 * GST_SECOND;
72   s = timestamp / GST_SECOND;
73 
74   timestamp -= s * GST_SECOND;
75   ms = timestamp / GST_MSECOND;
76 
77   g_string_append_printf (str, "%.2d:%.2d:%.2d,%.3d", h, m, s, ms);
78 }
79 
80 static GstFlowReturn
gst_srt_enc_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)81 gst_srt_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
82 {
83   GstSrtEnc *srtenc = GST_SRT_ENC (parent);
84   GstClockTime ts, dur = GST_SECOND;
85   GstBuffer *new_buffer;
86   GstMapInfo map_info;
87   GString *s;
88   gsize buf_size;
89 
90   gst_object_sync_values (GST_OBJECT (srtenc), GST_BUFFER_PTS (buf));
91 
92   ts = GST_BUFFER_PTS (buf) + srtenc->timestamp;
93   if (GST_BUFFER_DURATION_IS_VALID (buf))
94     dur = GST_BUFFER_DURATION (buf) + srtenc->duration;
95   else if (srtenc->duration > 0)
96     dur = srtenc->duration;
97   else
98     dur = GST_SECOND;
99 
100   buf_size = gst_buffer_get_size (buf);
101   s = g_string_sized_new (10 + 50 + buf_size + 2 + 1);
102 
103   /* stanza count */
104   g_string_append_printf (s, "%d\n", srtenc->counter++);
105 
106   /* start_time --> end_time */
107   gst_srt_enc_append_timestamp_to_string (ts, s);
108   g_string_append_printf (s, " --> ");
109   gst_srt_enc_append_timestamp_to_string (ts + dur, s);
110   g_string_append_c (s, '\n');
111 
112   /* text */
113   if (gst_buffer_map (buf, &map_info, GST_MAP_READ)) {
114     g_string_append_len (s, (const gchar *) map_info.data, map_info.size);
115     gst_buffer_unmap (buf, &map_info);
116   }
117 
118   g_string_append (s, "\n\n");
119 
120   buf_size = s->len;
121   new_buffer = gst_buffer_new_wrapped (g_string_free (s, FALSE), buf_size);
122 
123   GST_BUFFER_TIMESTAMP (new_buffer) = GST_BUFFER_TIMESTAMP (buf);
124   GST_BUFFER_DURATION (new_buffer) = GST_BUFFER_DURATION (buf);
125 
126   gst_buffer_unref (buf);
127 
128   return gst_pad_push (srtenc->srcpad, new_buffer);
129 }
130 
131 static gboolean
gst_srt_enc_event(GstPad * pad,GstObject * parent,GstEvent * event)132 gst_srt_enc_event (GstPad * pad, GstObject * parent, GstEvent * event)
133 {
134   GstSrtEnc *srtenc = GST_SRT_ENC (parent);
135   gboolean ret;
136 
137   switch (GST_EVENT_TYPE (event)) {
138     case GST_EVENT_CAPS:
139     {
140       GstCaps *caps;
141 
142       caps = gst_static_pad_template_get_caps (&src_template);
143       gst_pad_set_caps (srtenc->srcpad, caps);
144       gst_caps_unref (caps);
145       gst_event_unref (event);
146       ret = TRUE;
147       break;
148     }
149     default:
150       ret = gst_pad_event_default (pad, parent, event);
151       break;
152   }
153 
154   return ret;
155 }
156 
157 static void
gst_srt_enc_reset(GstSrtEnc * srtenc)158 gst_srt_enc_reset (GstSrtEnc * srtenc)
159 {
160   srtenc->counter = 1;
161 }
162 
163 static GstStateChangeReturn
gst_srt_enc_change_state(GstElement * element,GstStateChange transition)164 gst_srt_enc_change_state (GstElement * element, GstStateChange transition)
165 {
166   GstStateChangeReturn ret;
167   GstSrtEnc *srtenc = GST_SRT_ENC (element);
168 
169   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
170   if (ret == GST_STATE_CHANGE_FAILURE)
171     return ret;
172 
173   switch (transition) {
174     case GST_STATE_CHANGE_PAUSED_TO_READY:
175       gst_srt_enc_reset (srtenc);
176       break;
177     default:
178       break;
179   }
180 
181   return ret;
182 }
183 
184 static void
gst_srt_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)185 gst_srt_enc_get_property (GObject * object,
186     guint prop_id, GValue * value, GParamSpec * pspec)
187 {
188   GstSrtEnc *srtenc;
189 
190   srtenc = GST_SRT_ENC (object);
191 
192   switch (prop_id) {
193     case PROP_TIMESTAMP:
194       g_value_set_int64 (value, srtenc->timestamp);
195       break;
196     case PROP_DURATION:
197       g_value_set_int64 (value, srtenc->duration);
198       break;
199     default:
200       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201       break;
202   }
203 }
204 
205 static void
gst_srt_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)206 gst_srt_enc_set_property (GObject * object,
207     guint prop_id, const GValue * value, GParamSpec * pspec)
208 {
209 
210   GstSrtEnc *srtenc;
211 
212   srtenc = GST_SRT_ENC (object);
213 
214   switch (prop_id) {
215     case PROP_TIMESTAMP:
216       srtenc->timestamp = g_value_get_int64 (value);
217       break;
218     case PROP_DURATION:
219       srtenc->duration = g_value_get_int64 (value);
220       break;
221     default:
222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223       break;
224   }
225 }
226 
227 static void
gst_srt_enc_class_init(GstSrtEncClass * klass)228 gst_srt_enc_class_init (GstSrtEncClass * klass)
229 {
230   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
232 
233   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_srt_enc_set_property);
234   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_srt_enc_get_property);
235 
236   element_class->change_state = GST_DEBUG_FUNCPTR (gst_srt_enc_change_state);
237 
238   g_object_class_install_property (gobject_class, PROP_TIMESTAMP,
239       g_param_spec_int64 ("timestamp", "Offset for the starttime",
240           "Offset for the starttime for the subtitles", G_MININT64, G_MAXINT64,
241           0,
242           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
243   g_object_class_install_property (gobject_class, PROP_DURATION,
244       g_param_spec_int64 ("duration", "Offset for the duration",
245           "Offset for the duration of the subtitles", G_MININT64, G_MAXINT64,
246           0,
247           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
248 
249   gst_element_class_add_static_pad_template (element_class, &sink_template);
250   gst_element_class_add_static_pad_template (element_class, &src_template);
251 
252   gst_element_class_set_static_metadata (element_class,
253       "Srt encoder", "Codec/Encoder/Subtitle",
254       "Srt subtitle encoder", "Thijs Vermeir <thijsvermeir@gmail.com>");
255 
256   GST_DEBUG_CATEGORY_INIT (srtenc_debug, "srtenc", 0,
257       "SubRip subtitle encoder");
258 }
259 
260 static void
gst_srt_enc_init(GstSrtEnc * srtenc)261 gst_srt_enc_init (GstSrtEnc * srtenc)
262 {
263   gst_srt_enc_reset (srtenc);
264 
265   srtenc->srcpad = gst_pad_new_from_static_template (&src_template, "src");
266   gst_element_add_pad (GST_ELEMENT (srtenc), srtenc->srcpad);
267 
268   srtenc->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
269   gst_pad_set_chain_function (srtenc->sinkpad,
270       GST_DEBUG_FUNCPTR (gst_srt_enc_chain));
271   gst_pad_set_event_function (srtenc->sinkpad,
272       GST_DEBUG_FUNCPTR (gst_srt_enc_event));
273   gst_element_add_pad (GST_ELEMENT (srtenc), srtenc->sinkpad);
274 }
275