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, GstBuffer * buffer);
79 static GstStateChangeReturn gst_avi_subtitle_change_state (GstElement * element,
80 GstStateChange transition);
81 static gboolean gst_avi_subtitle_send_event (GstElement * element,
84 GST_BOILERPLATE (GstAviSubtitle, gst_avi_subtitle, GstElement,
87 #define IS_BOM_UTF8(data) ((GST_READ_UINT32_BE(data) >> 8) == 0xEFBBBF)
88 #define IS_BOM_UTF16_BE(data) (GST_READ_UINT16_BE(data) == 0xFEFF)
89 #define IS_BOM_UTF16_LE(data) (GST_READ_UINT16_LE(data) == 0xFEFF)
90 #define IS_BOM_UTF32_BE(data) (GST_READ_UINT32_BE(data) == 0xFEFF)
91 #define IS_BOM_UTF32_LE(data) (GST_READ_UINT32_LE(data) == 0xFEFF)
94 gst_avi_subtitle_extract_file (GstAviSubtitle * sub, GstBuffer * buffer,
95 guint offset, guint len)
97 const gchar *input_enc = NULL;
98 GstBuffer *ret = NULL;
101 data = (gchar *) GST_BUFFER_DATA (buffer) + offset;
103 if (len >= (3 + 1) && IS_BOM_UTF8 (data) &&
104 g_utf8_validate (data + 3, len - 3, NULL)) {
105 ret = gst_buffer_create_sub (buffer, offset + 3, len - 3);
106 } else if (len >= 2 && IS_BOM_UTF16_BE (data)) {
107 input_enc = "UTF-16BE";
110 } else if (len >= 2 && IS_BOM_UTF16_LE (data)) {
111 input_enc = "UTF-16LE";
114 } else if (len >= 4 && IS_BOM_UTF32_BE (data)) {
115 input_enc = "UTF-32BE";
118 } else if (len >= 4 && IS_BOM_UTF32_LE (data)) {
119 input_enc = "UTF-32LE";
122 } else if (g_utf8_validate (data, len, NULL)) {
123 /* not specified, check if it's UTF-8 */
124 ret = gst_buffer_create_sub (buffer, offset, len);
126 /* we could fall back to gst_tag_freeform_to_utf8() here */
127 GST_WARNING_OBJECT (sub, "unspecified encoding, and not UTF-8");
131 g_return_val_if_fail (ret != NULL || input_enc != NULL, NULL);
137 GST_DEBUG_OBJECT (sub, "converting subtitles from %s to UTF-8", input_enc);
138 utf8 = g_convert (data, len, "UTF-8", input_enc, NULL, NULL, &err);
141 GST_WARNING_OBJECT (sub, "conversion to UTF-8 failed : %s", err->message);
146 ret = gst_buffer_new ();
147 GST_BUFFER_DATA (ret) = (guint8 *) utf8;
148 GST_BUFFER_MALLOCDATA (ret) = (guint8 *) utf8;
149 GST_BUFFER_SIZE (ret) = strlen (utf8);
150 GST_BUFFER_OFFSET (ret) = 0;
153 GST_BUFFER_CAPS (ret) = gst_caps_new_simple ("application/x-subtitle", NULL);
158 * gst_avi_subtitle_title_tag:
159 * @sub: subtitle element
160 * @title: the title of this subtitle stream
162 * Send an event to the srcpad of the @sub element with the title
163 * of the subtitle stream as a GST_TAG_TITLE
166 gst_avi_subtitle_title_tag (GstAviSubtitle * sub, gchar * title)
168 GstTagList *temp_list = gst_tag_list_new ();
170 gst_tag_list_add (temp_list, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, title,
172 gst_pad_push_event (sub->src, gst_event_new_tag (temp_list));
176 gst_avi_subtitle_parse_gab2_chunk (GstAviSubtitle * sub, GstBuffer * buf)
184 data = GST_BUFFER_DATA (buf);
185 size = GST_BUFFER_SIZE (buf);
187 /* check the magic word "GAB2\0", and the next word must be 2 */
188 if (size < 12 || memcmp (data, "GAB2\0\2\0", 5 + 2) != 0)
189 goto wrong_magic_word;
191 /* read 'name' of subtitle */
192 name_length = GST_READ_UINT32_LE (data + 5 + 2);
193 GST_LOG_OBJECT (sub, "length of name: %u", name_length);
194 if (size <= 17 + name_length)
195 goto wrong_name_length;
197 name_utf8 = g_convert ((gchar *) data + 11, name_length, "UTF-8", "UTF-16LE",
201 GST_LOG_OBJECT (sub, "subtitle name: %s", name_utf8);
202 gst_avi_subtitle_title_tag (sub, name_utf8);
206 /* next word must be 4 */
207 if (GST_READ_UINT16_LE (data + 11 + name_length) != 0x4)
208 goto wrong_fixed_word_2;
210 file_length = GST_READ_UINT32_LE (data + 13 + name_length);
211 GST_LOG_OBJECT (sub, "length srt/ssa file: %u", file_length);
213 if (size < (17 + name_length + file_length))
214 goto wrong_total_length;
216 /* store this, so we can send it again after a seek; note that we shouldn't
217 * assume all the remaining data in the chunk is subtitle data, there may
218 * be padding at the end for some reason, so only parse file_length bytes */
220 gst_avi_subtitle_extract_file (sub, buf, 17 + name_length, file_length);
222 if (sub->subfile == NULL)
230 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL), ("Wrong magic word"));
231 return GST_FLOW_ERROR;
235 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
236 ("name doesn't fit in buffer (%d < %d)", size, 17 + name_length));
237 return GST_FLOW_ERROR;
241 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
242 ("wrong fixed word: expected %u, got %u", 4,
243 GST_READ_UINT16_LE (data + 11 + name_length)));
244 return GST_FLOW_ERROR;
248 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
249 ("buffer size is wrong: need %d bytes, have %d bytes",
250 17 + name_length + file_length, size));
251 return GST_FLOW_ERROR;
255 GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
256 ("could not extract subtitles"));
257 return GST_FLOW_ERROR;
262 gst_avi_subtitle_chain (GstPad * pad, GstBuffer * buffer)
264 GstAviSubtitle *sub = GST_AVI_SUBTITLE (GST_PAD_PARENT (pad));
267 if (sub->subfile != NULL) {
268 GST_WARNING_OBJECT (sub, "Got more buffers than expected, dropping");
269 ret = GST_FLOW_UNEXPECTED;
273 /* we expect exactly one buffer with the whole srt/ssa file in it */
274 ret = gst_avi_subtitle_parse_gab2_chunk (sub, buffer);
275 if (ret != GST_FLOW_OK)
278 /* now push the subtitle data downstream */
279 ret = gst_pad_push (sub->src, gst_buffer_ref (sub->subfile));
283 gst_buffer_unref (buffer);
288 gst_avi_subtitle_send_event (GstElement * element, GstEvent * event)
290 GstAviSubtitle *avisubtitle = GST_AVI_SUBTITLE (element);
291 gboolean ret = FALSE;
293 if (GST_EVENT_TYPE (event) == GST_EVENT_SEEK) {
294 if (avisubtitle->subfile) {
295 if (gst_pad_push (avisubtitle->src,
296 gst_buffer_ref (avisubtitle->subfile)) == GST_FLOW_OK)
300 gst_event_unref (event);
305 gst_avi_subtitle_base_init (gpointer klass)
307 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
309 GST_DEBUG_CATEGORY_INIT (avisubtitle_debug, "avisubtitle", 0,
310 "parse avi subtitle stream");
312 gst_element_class_add_static_pad_template (element_class,
314 gst_element_class_add_static_pad_template (element_class, &src_template);
316 gst_element_class_set_details_simple (element_class,
317 "Avi subtitle parser", "Codec/Parser/Subtitle",
318 "Parse avi subtitle stream", "Thijs Vermeir <thijsvermeir@gmail.com>");
322 gst_avi_subtitle_class_init (GstAviSubtitleClass * klass)
324 GstElementClass *gstelement_class = (GstElementClass *) klass;
326 gstelement_class->change_state =
327 GST_DEBUG_FUNCPTR (gst_avi_subtitle_change_state);
328 gstelement_class->send_event =
329 GST_DEBUG_FUNCPTR (gst_avi_subtitle_send_event);
333 gst_avi_subtitle_init (GstAviSubtitle * self, GstAviSubtitleClass * klass)
337 self->src = gst_pad_new_from_static_template (&src_template, "src");
338 gst_element_add_pad (GST_ELEMENT (self), self->src);
340 self->sink = gst_pad_new_from_static_template (&sink_template, "sink");
341 gst_pad_set_chain_function (self->sink,
342 GST_DEBUG_FUNCPTR (gst_avi_subtitle_chain));
344 caps = gst_static_pad_template_get_caps (&src_template);
345 gst_pad_set_caps (self->src, caps);
346 gst_caps_unref (caps);
348 gst_pad_use_fixed_caps (self->src);
349 gst_element_add_pad (GST_ELEMENT (self), self->sink);
351 self->subfile = NULL;
354 static GstStateChangeReturn
355 gst_avi_subtitle_change_state (GstElement * element, GstStateChange transition)
357 GstStateChangeReturn ret;
358 GstAviSubtitle *sub = GST_AVI_SUBTITLE (element);
360 switch (transition) {
361 case GST_STATE_CHANGE_NULL_TO_READY:
362 case GST_STATE_CHANGE_READY_TO_PAUSED:
367 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
368 if (ret == GST_STATE_CHANGE_FAILURE)
371 switch (transition) {
372 case GST_STATE_CHANGE_PAUSED_TO_READY:
374 gst_buffer_unref (sub->subfile);