Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.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, GstObject * parent,
79     GstBuffer * buffer);
80 static GstStateChangeReturn gst_avi_subtitle_change_state (GstElement * element,
81     GstStateChange transition);
82 static gboolean gst_avi_subtitle_send_event (GstElement * element,
83     GstEvent * event);
84
85 #define gst_avi_subtitle_parent_class parent_class
86 G_DEFINE_TYPE (GstAviSubtitle, gst_avi_subtitle, GST_TYPE_ELEMENT);
87
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)
93
94 static GstBuffer *
95 gst_avi_subtitle_extract_file (GstAviSubtitle * sub, GstBuffer * buffer,
96     guint offset, guint len)
97 {
98   const gchar *input_enc = NULL;
99   GstBuffer *ret = NULL;
100   gchar *data;
101   GstMapInfo map;
102
103   gst_buffer_map (buffer, &map, GST_MAP_READ);
104   data = (gchar *) (map.data + offset);
105
106   if (len >= (3 + 1) && IS_BOM_UTF8 (data) &&
107       g_utf8_validate (data + 3, len - 3, NULL)) {
108     ret =
109         gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, offset + 3,
110         len - 3);
111   } else if (len >= 2 && IS_BOM_UTF16_BE (data)) {
112     input_enc = "UTF-16BE";
113     data += 2;
114     len -= 2;
115   } else if (len >= 2 && IS_BOM_UTF16_LE (data)) {
116     input_enc = "UTF-16LE";
117     data += 2;
118     len -= 2;
119   } else if (len >= 4 && IS_BOM_UTF32_BE (data)) {
120     input_enc = "UTF-32BE";
121     data += 4;
122     len -= 4;
123   } else if (len >= 4 && IS_BOM_UTF32_LE (data)) {
124     input_enc = "UTF-32LE";
125     data += 4;
126     len -= 4;
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);
130   } else {
131     /* we could fall back to gst_tag_freeform_to_utf8() here */
132     GST_WARNING_OBJECT (sub, "unspecified encoding, and not UTF-8");
133     ret = NULL;
134     goto done;
135   }
136
137   g_return_val_if_fail (ret != NULL || input_enc != NULL, NULL);
138
139   if (input_enc) {
140     GError *err = NULL;
141     gchar *utf8;
142     gsize slen;
143
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);
146
147     if (err != NULL) {
148       GST_WARNING_OBJECT (sub, "conversion to UTF-8 failed : %s", err->message);
149       g_error_free (err);
150       ret = NULL;
151       goto done;
152     }
153
154     ret = gst_buffer_new ();
155     slen = strlen (utf8);
156     gst_buffer_append_memory (ret,
157         gst_memory_new_wrapped (0, utf8, slen, 0, slen, utf8, g_free));
158
159     GST_BUFFER_OFFSET (ret) = 0;
160   }
161
162 done:
163   gst_buffer_unmap (buffer, &map);
164
165   return ret;
166 }
167
168 /**
169  * gst_avi_subtitle_title_tag:
170  * @sub: subtitle element
171  * @title: the title of this subtitle stream
172  *
173  * Send an event to the srcpad of the @sub element with the title
174  * of the subtitle stream as a GST_TAG_TITLE
175  */
176 static void
177 gst_avi_subtitle_title_tag (GstAviSubtitle * sub, gchar * title)
178 {
179   gst_pad_push_event (sub->src,
180       gst_event_new_tag (gst_tag_list_new (GST_TAG_TITLE, title, NULL)));
181 }
182
183 static GstFlowReturn
184 gst_avi_subtitle_parse_gab2_chunk (GstAviSubtitle * sub, GstBuffer * buf)
185 {
186   gchar *name_utf8;
187   guint name_length;
188   guint file_length;
189   GstMapInfo map;
190
191   gst_buffer_map (buf, &map, GST_MAP_READ);
192
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;
196
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;
202
203   name_utf8 =
204       g_convert ((gchar *) map.data + 11, name_length, "UTF-8", "UTF-16LE",
205       NULL, NULL, NULL);
206
207   if (name_utf8) {
208     GST_LOG_OBJECT (sub, "subtitle name: %s", name_utf8);
209     gst_avi_subtitle_title_tag (sub, name_utf8);
210     g_free (name_utf8);
211   }
212
213   /* next word must be 4 */
214   if (GST_READ_UINT16_LE (map.data + 11 + name_length) != 0x4)
215     goto wrong_fixed_word_2;
216
217   file_length = GST_READ_UINT32_LE (map.data + 13 + name_length);
218   GST_LOG_OBJECT (sub, "length srt/ssa file: %u", file_length);
219
220   if (map.size < (17 + name_length + file_length))
221     goto wrong_total_length;
222
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 */
226   sub->subfile =
227       gst_avi_subtitle_extract_file (sub, buf, 17 + name_length, file_length);
228
229   if (sub->subfile == NULL)
230     goto extract_failed;
231
232   gst_buffer_unmap (buf, &map);
233
234   return GST_FLOW_OK;
235
236   /* ERRORS */
237 wrong_magic_word:
238   {
239     GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL), ("Wrong magic word"));
240     gst_buffer_unmap (buf, &map);
241     return GST_FLOW_ERROR;
242   }
243 wrong_name_length:
244   {
245     GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
246         ("name doesn't fit in buffer (%" G_GSIZE_FORMAT " < %d)", map.size,
247             17 + name_length));
248     gst_buffer_unmap (buf, &map);
249     return GST_FLOW_ERROR;
250   }
251 wrong_fixed_word_2:
252   {
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;
258   }
259 wrong_total_length:
260   {
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;
266   }
267 extract_failed:
268   {
269     GST_ELEMENT_ERROR (sub, STREAM, DECODE, (NULL),
270         ("could not extract subtitles"));
271     gst_buffer_unmap (buf, &map);
272     return GST_FLOW_ERROR;
273   }
274 }
275
276 static GstFlowReturn
277 gst_avi_subtitle_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
278 {
279   GstAviSubtitle *sub = GST_AVI_SUBTITLE (parent);
280   GstFlowReturn ret;
281
282   if (sub->subfile != NULL) {
283     GST_WARNING_OBJECT (sub, "Got more buffers than expected, dropping");
284     ret = GST_FLOW_EOS;
285     goto done;
286   }
287
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)
291     goto done;
292
293   /* now push the subtitle data downstream */
294   ret = gst_pad_push (sub->src, gst_buffer_ref (sub->subfile));
295
296 done:
297
298   gst_buffer_unref (buffer);
299   return ret;
300 }
301
302 static gboolean
303 gst_avi_subtitle_send_event (GstElement * element, GstEvent * event)
304 {
305   GstAviSubtitle *avisubtitle = GST_AVI_SUBTITLE (element);
306   gboolean ret = FALSE;
307
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)
312         ret = TRUE;
313     }
314   }
315   gst_event_unref (event);
316   return ret;
317 }
318
319 static void
320 gst_avi_subtitle_class_init (GstAviSubtitleClass * klass)
321 {
322   GstElementClass *gstelement_class = (GstElementClass *) klass;
323
324   GST_DEBUG_CATEGORY_INIT (avisubtitle_debug, "avisubtitle", 0,
325       "parse avi subtitle stream");
326
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);
331
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));
336
337   gst_element_class_set_static_metadata (gstelement_class,
338       "Avi subtitle parser", "Codec/Parser/Subtitle",
339       "Parse avi subtitle stream", "Thijs Vermeir <thijsvermeir@gmail.com>");
340 }
341
342 static void
343 gst_avi_subtitle_init (GstAviSubtitle * self)
344 {
345   GstCaps *caps;
346
347   self->src = gst_pad_new_from_static_template (&src_template, "src");
348   gst_element_add_pad (GST_ELEMENT (self), self->src);
349
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));
353
354   caps = gst_static_pad_template_get_caps (&src_template);
355   gst_pad_set_caps (self->src, caps);
356   gst_caps_unref (caps);
357
358   gst_pad_use_fixed_caps (self->src);
359   gst_element_add_pad (GST_ELEMENT (self), self->sink);
360
361   self->subfile = NULL;
362 }
363
364 static GstStateChangeReturn
365 gst_avi_subtitle_change_state (GstElement * element, GstStateChange transition)
366 {
367   GstStateChangeReturn ret;
368   GstAviSubtitle *sub = GST_AVI_SUBTITLE (element);
369
370   switch (transition) {
371     case GST_STATE_CHANGE_NULL_TO_READY:
372     case GST_STATE_CHANGE_READY_TO_PAUSED:
373     default:
374       break;
375   }
376
377   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
378   if (ret == GST_STATE_CHANGE_FAILURE)
379     return ret;
380
381   switch (transition) {
382     case GST_STATE_CHANGE_PAUSED_TO_READY:
383       if (sub->subfile) {
384         gst_buffer_unref (sub->subfile);
385         sub->subfile = NULL;
386       }
387       break;
388     default:
389       break;
390   }
391
392   return ret;
393 }