1 /* GStreamer AVI GAB2 subtitle parser
2 * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
3 * Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-avisubtitle
26 * Parses the subtitle stream from an avi file.
28 * <title>Example launch line</title>
31 * gst-launch filesrc location=subtitle.avi ! avidemux name=demux ! queue ! avisubtitle ! subparse ! textoverlay name=overlay ! ffmpegcolorspace ! autovideosink demux. ! queue ! decodebin ! overlay.
33 * This plays an avi file with a video and subtitle stream.
37 * Last reviewed on 2008-02-01
40 /* example of a subtitle chunk in an avi file
41 * 00000000: 47 41 42 32 00 02 00 10 00 00 00 45 00 6e 00 67 GAB2.......E.n.g
42 * 00000010: 00 6c 00 69 00 73 00 68 00 00 00 04 00 8e 00 00 .l.i.s.h........
43 * 00000020: 00 ef bb bf 31 0d 0a 30 30 3a 30 30 3a 30 30 2c ....1..00:00:00,
44 * 00000030: 31 30 30 20 2d 2d 3e 20 30 30 3a 30 30 3a 30 32 100 --> 00:00:02
45 * 00000040: 2c 30 30 30 0d 0a 3c 62 3e 41 6e 20 55 54 46 38 ,000..<b>An UTF8
46 * 00000050: 20 53 75 62 74 69 74 6c 65 20 77 69 74 68 20 42 Subtitle with B
47 * 00000060: 4f 4d 3c 2f 62 3e 0d 0a 0d 0a 32 0d 0a 30 30 3a OM</b>....2..00:
48 * 00000070: 30 30 3a 30 32 2c 31 30 30 20 2d 2d 3e 20 30 30 00:02,100 --> 00
49 * 00000080: 3a 30 30 3a 30 34 2c 30 30 30 0d 0a 53 6f 6d 65 :00:04,000..Some
50 * 00000090: 74 68 69 6e 67 20 6e 6f 6e 41 53 43 49 49 20 2d thing nonASCII -
51 * 000000a0: 20 c2 b5 c3 b6 c3 a4 c3 bc c3 9f 0d 0a 0d 0a ..............
60 #include "gstavisubtitle.h"
62 GST_DEBUG_CATEGORY_STATIC (avisubtitle_debug);
63 #define GST_CAT_DEFAULT avisubtitle_debug
65 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_STATIC_CAPS ("application/x-subtitle-avi")
71 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
74 GST_STATIC_CAPS ("application/x-subtitle")
77 static void gst_avi_subtitle_title_tag (GstAviSubtitle * sub, gchar * title);
78 static GstFlowReturn gst_avi_subtitle_chain (GstPad * pad, GstObject * parent,
80 static GstStateChangeReturn gst_avi_subtitle_change_state (GstElement * element,
81 GstStateChange transition);
82 static gboolean gst_avi_subtitle_send_event (GstElement * element,
85 #define gst_avi_subtitle_parent_class parent_class
86 G_DEFINE_TYPE (GstAviSubtitle, gst_avi_subtitle, GST_TYPE_ELEMENT);
88 #define IS_BOM_UTF8(data) ((GST_READ_UINT32_BE(data) >> 8) == 0xEFBBBF)
89 #define IS_BOM_UTF16_BE(data) (GST_READ_UINT16_BE(data) == 0xFEFF)
90 #define IS_BOM_UTF16_LE(data) (GST_READ_UINT16_LE(data) == 0xFEFF)
91 #define IS_BOM_UTF32_BE(data) (GST_READ_UINT32_BE(data) == 0xFEFF)
92 #define IS_BOM_UTF32_LE(data) (GST_READ_UINT32_LE(data) == 0xFEFF)
95 gst_avi_subtitle_extract_file (GstAviSubtitle * sub, GstBuffer * buffer,
96 guint offset, guint len)
98 const gchar *input_enc = NULL;
99 GstBuffer *ret = NULL;
103 gst_buffer_map (buffer, &map, GST_MAP_READ);
104 data = (gchar *) (map.data + offset);
106 if (len >= (3 + 1) && IS_BOM_UTF8 (data) &&
107 g_utf8_validate (data + 3, len - 3, NULL)) {
109 gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset + 3,
111 } else if (len >= 2 && IS_BOM_UTF16_BE (data)) {
112 input_enc = "UTF-16BE";
115 } else if (len >= 2 && IS_BOM_UTF16_LE (data)) {
116 input_enc = "UTF-16LE";
119 } else if (len >= 4 && IS_BOM_UTF32_BE (data)) {
120 input_enc = "UTF-32BE";
123 } else if (len >= 4 && IS_BOM_UTF32_LE (data)) {
124 input_enc = "UTF-32LE";
127 } else if (g_utf8_validate (data, len, NULL)) {
128 /* not specified, check if it's UTF-8 */
129 ret = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset, len);
131 /* we could fall back to gst_tag_freeform_to_utf8() here */
132 GST_WARNING_OBJECT (sub, "unspecified encoding, and not UTF-8");
137 g_return_val_if_fail (ret != NULL || input_enc != NULL, NULL);
144 GST_DEBUG_OBJECT (sub, "converting subtitles from %s to UTF-8", input_enc);
145 utf8 = g_convert (data, len, "UTF-8", input_enc, NULL, NULL, &err);
148 GST_WARNING_OBJECT (sub, "conversion to UTF-8 failed : %s", err->message);
154 ret = gst_buffer_new ();
155 slen = strlen (utf8);
156 gst_buffer_take_memory (ret, -1,
157 gst_memory_new_wrapped (0, utf8, g_free, slen, 0, slen));
159 GST_BUFFER_OFFSET (ret) = 0;
163 gst_buffer_unmap (buffer, &map);
169 * gst_avi_subtitle_title_tag:
170 * @sub: subtitle element
171 * @title: the title of this subtitle stream
173 * Send an event to the srcpad of the @sub element with the title
174 * of the subtitle stream as a GST_TAG_TITLE
177 gst_avi_subtitle_title_tag (GstAviSubtitle * sub, gchar * title)
179 gst_pad_push_event (sub->src,
180 gst_event_new_tag (gst_tag_list_new (GST_TAG_TITLE, title, NULL)));
184 gst_avi_subtitle_parse_gab2_chunk (GstAviSubtitle * sub, GstBuffer * buf)
191 gst_buffer_map (buf, &map, GST_MAP_READ);
193 /* check the magic word "GAB2\0", and the next word must be 2 */
194 if (map.size < 12 || memcmp (map.data, "GAB2\0\2\0", 5 + 2) != 0)
195 goto wrong_magic_word;
197 /* read 'name' of subtitle */
198 name_length = GST_READ_UINT32_LE (map.data + 5 + 2);
199 GST_LOG_OBJECT (sub, "length of name: %u", name_length);
200 if (map.size <= 17 + name_length)
201 goto wrong_name_length;
204 g_convert ((gchar *) map.data + 11, name_length, "UTF-8", "UTF-16LE",
208 GST_LOG_OBJECT (sub, "subtitle name: %s", name_utf8);
209 gst_avi_subtitle_title_tag (sub, name_utf8);
213 /* next word must be 4 */
214 if (GST_READ_UINT16_LE (map.data + 11 + name_length) != 0x4)
215 goto wrong_fixed_word_2;
217 file_length = GST_READ_UINT32_LE (map.data + 13 + name_length);
218 GST_LOG_OBJECT (sub, "length srt/ssa file: %u", file_length);
220 if (map.size < (17 + name_length + file_length))
221 goto wrong_total_length;
223 /* store this, so we can send it again after a seek; note that we shouldn't
224 * assume all the remaining data in the chunk is subtitle data, there may
225 * be padding at the end for some reason, so only parse file_length bytes */
227 gst_avi_subtitle_extract_file (sub, buf, 17 + name_length, file_length);
229 if (sub->subfile == NULL)
232 gst_buffer_unmap (buf, &map);
239 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL), ("Wrong magic word"));
240 gst_buffer_unmap (buf, &map);
241 return GST_FLOW_ERROR;
245 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
246 ("name doesn't fit in buffer (%" G_GSIZE_FORMAT " < %d)", map.size,
248 gst_buffer_unmap (buf, &map);
249 return GST_FLOW_ERROR;
253 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
254 ("wrong fixed word: expected %u, got %u", 4,
255 GST_READ_UINT16_LE (map.data + 11 + name_length)));
256 gst_buffer_unmap (buf, &map);
257 return GST_FLOW_ERROR;
261 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
262 ("buffer size is wrong: need %d bytes, have %" G_GSIZE_FORMAT " bytes",
263 17 + name_length + file_length, map.size));
264 gst_buffer_unmap (buf, &map);
265 return GST_FLOW_ERROR;
269 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
270 ("could not extract subtitles"));
271 gst_buffer_unmap (buf, &map);
272 return GST_FLOW_ERROR;
277 gst_avi_subtitle_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
279 GstAviSubtitle *sub = GST_AVI_SUBTITLE (parent);
282 if (sub->subfile != NULL) {
283 GST_WARNING_OBJECT (sub, "Got more buffers than expected, dropping");
288 /* we expect exactly one buffer with the whole srt/ssa file in it */
289 ret = gst_avi_subtitle_parse_gab2_chunk (sub, buffer);
290 if (ret != GST_FLOW_OK)
293 /* now push the subtitle data downstream */
294 ret = gst_pad_push (sub->src, gst_buffer_ref (sub->subfile));
298 gst_buffer_unref (buffer);
303 gst_avi_subtitle_send_event (GstElement * element, GstEvent * event)
305 GstAviSubtitle *avisubtitle = GST_AVI_SUBTITLE (element);
306 gboolean ret = FALSE;
308 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
309 if (avisubtitle->subfile) {
310 if (gst_pad_push (avisubtitle->src,
311 gst_buffer_ref (avisubtitle->subfile)) == GST_FLOW_OK)
315 gst_event_unref (event);
320 gst_avi_subtitle_class_init (GstAviSubtitleClass * klass)
322 GstElementClass *gstelement_class = (GstElementClass *) klass;
324 GST_DEBUG_CATEGORY_INIT (avisubtitle_debug, "avisubtitle", 0,
325 "parse avi subtitle stream");
327 gstelement_class->change_state =
328 GST_DEBUG_FUNCPTR (gst_avi_subtitle_change_state);
329 gstelement_class->send_event =
330 GST_DEBUG_FUNCPTR (gst_avi_subtitle_send_event);
332 gst_element_class_add_pad_template (gstelement_class,
333 gst_static_pad_template_get (&sink_template));
334 gst_element_class_add_pad_template (gstelement_class,
335 gst_static_pad_template_get (&src_template));
337 gst_element_class_set_details_simple (gstelement_class,
338 "Avi subtitle parser", "Codec/Parser/Subtitle",
339 "Parse avi subtitle stream", "Thijs Vermeir <thijsvermeir@gmail.com>");
343 gst_avi_subtitle_init (GstAviSubtitle * self)
347 self->src = gst_pad_new_from_static_template (&src_template, "src");
348 gst_element_add_pad (GST_ELEMENT (self), self->src);
350 self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
351 gst_pad_set_chain_function (self->sink,
352 GST_DEBUG_FUNCPTR (gst_avi_subtitle_chain));
354 caps = gst_static_pad_template_get_caps (&src_template);
355 gst_pad_set_caps (self->src, caps);
356 gst_caps_unref (caps);
358 gst_pad_use_fixed_caps (self->src);
359 gst_element_add_pad (GST_ELEMENT (self), self->sink);
361 self->subfile = NULL;
364 static GstStateChangeReturn
365 gst_avi_subtitle_change_state (GstElement * element, GstStateChange transition)
367 GstStateChangeReturn ret;
368 GstAviSubtitle *sub = GST_AVI_SUBTITLE (element);
370 switch (transition) {
371 case GST_STATE_CHANGE_NULL_TO_READY:
372 case GST_STATE_CHANGE_READY_TO_PAUSED:
377 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
378 if (ret == GST_STATE_CHANGE_FAILURE)
381 switch (transition) {
382 case GST_STATE_CHANGE_PAUSED_TO_READY:
384 gst_buffer_unref (sub->subfile);