tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / avi / gstavisubtitle.c
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>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-avisubtitle
23  *
24  * <refsect2>
25  * <para>
26  * Parses the subtitle stream from an avi file.
27  * </para>
28  * <title>Example launch line</title>
29  * <para>
30  * <programlisting>
31  * gst-launch filesrc location=subtitle.avi ! avidemux name=demux ! queue ! avisubtitle ! subparse ! textoverlay name=overlay ! ffmpegcolorspace ! autovideosink demux. ! queue ! decodebin ! overlay.
32  * </programlisting>
33  * This plays an avi file with a video and subtitle stream.
34  * </para>
35  * </refsect2>
36  *
37  * Last reviewed on 2008-02-01
38  */
39
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      ..............
52  */
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57
58 #include <string.h>
59
60 #include "gstavisubtitle.h"
61
62 GST_DEBUG_CATEGORY_STATIC (avisubtitle_debug);
63 #define GST_CAT_DEFAULT avisubtitle_debug
64
65 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
66     GST_PAD_SINK,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("application/x-subtitle-avi")
69     );
70
71 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS ("application/x-subtitle")
75     );
76
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,
82     GstEvent * event);
83
84 GST_BOILERPLATE (GstAviSubtitle, gst_avi_subtitle, GstElement,
85     GST_TYPE_ELEMENT);
86
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)
92
93 static GstBuffer *
94 gst_avi_subtitle_extract_file (GstAviSubtitle * sub, GstBuffer * buffer,
95     guint offset, guint len)
96 {
97   const gchar *input_enc = NULL;
98   GstBuffer *ret = NULL;
99   gchar *data;
100
101   data = (gchar *) GST_BUFFER_DATA (buffer) + offset;
102
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";
108     data += 2;
109     len -= 2;
110   } else if (len >= 2 && IS_BOM_UTF16_LE (data)) {
111     input_enc = "UTF-16LE";
112     data += 2;
113     len -= 2;
114   } else if (len >= 4 && IS_BOM_UTF32_BE (data)) {
115     input_enc = "UTF-32BE";
116     data += 4;
117     len -= 4;
118   } else if (len >= 4 && IS_BOM_UTF32_LE (data)) {
119     input_enc = "UTF-32LE";
120     data += 4;
121     len -= 4;
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);
125   } else {
126     /* we could fall back to gst_tag_freeform_to_utf8() here */
127     GST_WARNING_OBJECT (sub, "unspecified encoding, and not UTF-8");
128     return NULL;
129   }
130
131   g_return_val_if_fail (ret != NULL || input_enc != NULL, NULL);
132
133   if (input_enc) {
134     GError *err = NULL;
135     gchar *utf8;
136
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);
139
140     if (err != NULL) {
141       GST_WARNING_OBJECT (sub, "conversion to UTF-8 failed : %s", err->message);
142       g_error_free (err);
143       return NULL;
144     }
145
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;
151   }
152
153   GST_BUFFER_CAPS (ret) = gst_caps_new_simple ("application/x-subtitle", NULL);
154   return ret;
155 }
156
157 /**
158  * gst_avi_subtitle_title_tag:
159  * @sub: subtitle element
160  * @title: the title of this subtitle stream
161  *
162  * Send an event to the srcpad of the @sub element with the title
163  * of the subtitle stream as a GST_TAG_TITLE
164  */
165 static void
166 gst_avi_subtitle_title_tag (GstAviSubtitle * sub, gchar * title)
167 {
168   GstTagList *temp_list = gst_tag_list_new ();
169
170   gst_tag_list_add (temp_list, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, title,
171       NULL);
172   gst_pad_push_event (sub->src, gst_event_new_tag (temp_list));
173 }
174
175 static GstFlowReturn
176 gst_avi_subtitle_parse_gab2_chunk (GstAviSubtitle * sub, GstBuffer * buf)
177 {
178   const guint8 *data;
179   gchar *name_utf8;
180   guint name_length;
181   guint file_length;
182   guint size;
183
184   data = GST_BUFFER_DATA (buf);
185   size = GST_BUFFER_SIZE (buf);
186
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;
190
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;
196
197   name_utf8 = g_convert ((gchar *) data + 11, name_length, "UTF-8", "UTF-16LE",
198       NULL, NULL, NULL);
199
200   if (name_utf8) {
201     GST_LOG_OBJECT (sub, "subtitle name: %s", name_utf8);
202     gst_avi_subtitle_title_tag (sub, name_utf8);
203     g_free (name_utf8);
204   }
205
206   /* next word must be 4 */
207   if (GST_READ_UINT16_LE (data + 11 + name_length) != 0x4)
208     goto wrong_fixed_word_2;
209
210   file_length = GST_READ_UINT32_LE (data + 13 + name_length);
211   GST_LOG_OBJECT (sub, "length srt/ssa file: %u", file_length);
212
213   if (size < (17 + name_length + file_length))
214     goto wrong_total_length;
215
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 */
219   sub->subfile =
220       gst_avi_subtitle_extract_file (sub, buf, 17 + name_length, file_length);
221
222   if (sub->subfile == NULL)
223     goto extract_failed;
224
225   return GST_FLOW_OK;
226
227   /* ERRORS */
228 wrong_magic_word:
229   {
230     GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL), ("Wrong magic word"));
231     return GST_FLOW_ERROR;
232   }
233 wrong_name_length:
234   {
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;
238   }
239 wrong_fixed_word_2:
240   {
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;
245   }
246 wrong_total_length:
247   {
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;
252   }
253 extract_failed:
254   {
255     GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
256         ("could not extract subtitles"));
257     return GST_FLOW_ERROR;
258   }
259 }
260
261 static GstFlowReturn
262 gst_avi_subtitle_chain (GstPad * pad, GstBuffer * buffer)
263 {
264   GstAviSubtitle *sub = GST_AVI_SUBTITLE (GST_PAD_PARENT (pad));
265   GstFlowReturn ret;
266
267   if (sub->subfile != NULL) {
268     GST_WARNING_OBJECT (sub, "Got more buffers than expected, dropping");
269     ret = GST_FLOW_UNEXPECTED;
270     goto done;
271   }
272
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)
276     goto done;
277
278   /* now push the subtitle data downstream */
279   ret = gst_pad_push (sub->src, gst_buffer_ref (sub->subfile));
280
281 done:
282
283   gst_buffer_unref (buffer);
284   return ret;
285 }
286
287 static gboolean
288 gst_avi_subtitle_send_event (GstElement * element, GstEvent * event)
289 {
290   GstAviSubtitle *avisubtitle = GST_AVI_SUBTITLE (element);
291   gboolean ret = FALSE;
292
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)
297         ret = TRUE;
298     }
299   }
300   gst_event_unref (event);
301   return ret;
302 }
303
304 static void
305 gst_avi_subtitle_base_init (gpointer klass)
306 {
307   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
308
309   GST_DEBUG_CATEGORY_INIT (avisubtitle_debug, "avisubtitle", 0,
310       "parse avi subtitle stream");
311
312   gst_element_class_add_static_pad_template (element_class,
313       &sink_template);
314   gst_element_class_add_static_pad_template (element_class, &src_template);
315
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>");
319 }
320
321 static void
322 gst_avi_subtitle_class_init (GstAviSubtitleClass * klass)
323 {
324   GstElementClass *gstelement_class = (GstElementClass *) klass;
325
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);
330 }
331
332 static void
333 gst_avi_subtitle_init (GstAviSubtitle * self, GstAviSubtitleClass * klass)
334 {
335   GstCaps *caps;
336
337   self->src = gst_pad_new_from_static_template (&src_template, "src");
338   gst_element_add_pad (GST_ELEMENT (self), self->src);
339
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));
343
344   caps = gst_static_pad_template_get_caps (&src_template);
345   gst_pad_set_caps (self->src, caps);
346   gst_caps_unref (caps);
347
348   gst_pad_use_fixed_caps (self->src);
349   gst_element_add_pad (GST_ELEMENT (self), self->sink);
350
351   self->subfile = NULL;
352 }
353
354 static GstStateChangeReturn
355 gst_avi_subtitle_change_state (GstElement * element, GstStateChange transition)
356 {
357   GstStateChangeReturn ret;
358   GstAviSubtitle *sub = GST_AVI_SUBTITLE (element);
359
360   switch (transition) {
361     case GST_STATE_CHANGE_NULL_TO_READY:
362     case GST_STATE_CHANGE_READY_TO_PAUSED:
363     default:
364       break;
365   }
366
367   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
368   if (ret == GST_STATE_CHANGE_FAILURE)
369     return ret;
370
371   switch (transition) {
372     case GST_STATE_CHANGE_PAUSED_TO_READY:
373       if (sub->subfile) {
374         gst_buffer_unref (sub->subfile);
375         sub->subfile = NULL;
376       }
377       break;
378     default:
379       break;
380   }
381
382   return ret;
383 }