4aa3a4fa3f8eb8215808e73013bc92a169d4c379
[platform/upstream/gstreamer.git] / ext / kate / gstkatetag.c
1 /*
2  * GStreamer
3  * Copyright (C) 2006 James Livingston <doclivingston@gmail.com>
4  * Copyright (C) 2008 Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:element-katetag
24  * @see_also: #oggdemux, #oggmux, #kateparse, #GstTagSetter
25  * @short_description: retags kate streams
26  *
27  * <refsect2>
28  * <para>
29  * The katetag element can change the tag contained within a raw
30  * kate stream. Specifically, it modifies the comments header packet
31  * of the kate stream, as well as the language and category of the
32  * kate stream.
33  * </para>
34  * <para>
35  * The element will also process the stream as the #kateparse element does
36  * so it can be used when remuxing an Ogg Kate stream, without additional
37  * elements.
38  * </para>
39  * <para>
40  * Applications can set the tags to write using the #GstTagSetter interface.
41  * Tags contained within the kate stream will be picked up
42  * automatically (and merged according to the merge mode set via the tag
43  * setter interface).
44  * </para>
45  * <title>Example pipelines</title>
46  * <para>
47  * This element is only useful with gst-launch for modifying the language
48  * and/or category (which are properties of the stream located in the kate
49  * beginning of stream header), because it does not support setting the tags
50  * on a #GstTagSetter interface. Conceptually, the element will usually be
51  * used like:
52  * <programlisting>
53  * gst-launch -v filesrc location=foo.ogg ! oggdemux ! katetag ! oggmux ! filesink location=bar.ogg
54  * </programlisting>
55  * </para>
56  * <para>
57  * This pipeline will set the language and category of the stream to the
58  * given values:
59  * <programlisting>
60  * gst-launch -v filesrc location=foo.ogg ! oggdemux ! katetag language=pt_BR category=subtitles ! oggmux ! filesink location=bar.ogg
61  * </programlisting>
62  * </para>
63  * </refsect2>
64  */
65
66 #ifdef HAVE_CONFIG_H
67 #  include "config.h"
68 #endif
69
70 #include <string.h>
71 #include <glib.h>
72 #include <gst/tag/tag.h>
73 #include <gst/gsttagsetter.h>
74
75 #include <kate/kate.h>
76
77 #include "gstkatetag.h"
78
79
80 GST_DEBUG_CATEGORY_EXTERN (gst_katetag_debug);
81 #define GST_CAT_DEFAULT gst_katetag_debug
82
83 enum
84 {
85   ARG_0,
86   ARG_LANGUAGE,
87   ARG_CATEGORY,
88   ARG_ORIGINAL_CANVAS_WIDTH,
89   ARG_ORIGINAL_CANVAS_HEIGHT,
90 };
91
92 static GstFlowReturn gst_kate_tag_parse_packet (GstKateParse * parse,
93     GstBuffer * buffer);
94 static void gst_kate_tag_set_property (GObject * object, guint prop_id,
95     const GValue * value, GParamSpec * pspec);
96 static void gst_kate_tag_get_property (GObject * object, guint prop_id,
97     GValue * value, GParamSpec * pspec);
98 static void gst_kate_tag_dispose (GObject * object);
99
100 #define gst_kate_tag_parent_class parent_class
101 G_DEFINE_TYPE_WITH_CODE (GstKateTag, gst_kate_tag, GST_TYPE_KATE_PARSE,
102     G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
103
104 static void
105 gst_kate_tag_class_init (GstKateTagClass * klass)
106 {
107   GObjectClass *gobject_class;
108   GstElementClass *gstelement_class;
109   GstKateParseClass *gstkateparse_class;
110
111   gobject_class = (GObjectClass *) klass;
112   gstelement_class = (GstElementClass *) klass;
113   gstkateparse_class = GST_KATE_PARSE_CLASS (klass);
114
115   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_kate_tag_set_property);
116   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_kate_tag_get_property);
117   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_kate_tag_dispose);
118
119   g_object_class_install_property (gobject_class, ARG_LANGUAGE,
120       g_param_spec_string ("language", "Language",
121           "Set the language of the stream", "",
122           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
123
124   g_object_class_install_property (gobject_class, ARG_CATEGORY,
125       g_param_spec_string ("category", "Category",
126           "Set the category of the stream", "",
127           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128
129   g_object_class_install_property (gobject_class, ARG_ORIGINAL_CANVAS_WIDTH,
130       g_param_spec_int ("original-canvas-width", "Original canvas width",
131           "Set the width of the canvas this stream was authored for (0 is unspecified)",
132           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133
134   g_object_class_install_property (gobject_class, ARG_ORIGINAL_CANVAS_HEIGHT,
135       g_param_spec_int ("original-canvas-height", "Original canvas height",
136           "Set the height of the canvas this stream was authored for (0 is unspecified)",
137           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138
139   gst_element_class_set_static_metadata (gstelement_class, "Kate stream tagger",
140       "Formatter/Metadata",
141       "Retags kate streams", "Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>");
142
143   gstkateparse_class->parse_packet =
144       GST_DEBUG_FUNCPTR (gst_kate_tag_parse_packet);
145 }
146
147 static void
148 gst_kate_tag_init (GstKateTag * kt)
149 {
150   kt->language = NULL;
151   kt->category = NULL;
152   kt->original_canvas_width = -1;
153   kt->original_canvas_height = -1;
154 }
155
156 static void
157 gst_kate_tag_dispose (GObject * object)
158 {
159   GstKateTag *kt = GST_KATE_TAG (object);
160
161   GST_LOG_OBJECT (kt, "disposing");
162
163   if (kt->language) {
164     g_free (kt->language);
165     kt->language = NULL;
166   }
167   if (kt->category) {
168     g_free (kt->category);
169     kt->category = NULL;
170   }
171
172   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
173 }
174
175 static void
176 gst_kate_tag_set_property (GObject * object, guint prop_id,
177     const GValue * value, GParamSpec * pspec)
178 {
179   GstKateTag *kt = GST_KATE_TAG (object);
180   const char *str;
181
182   switch (prop_id) {
183     case ARG_LANGUAGE:
184       if (kt->language) {
185         g_free (kt->language);
186         kt->language = NULL;
187       }
188       str = g_value_get_string (value);
189       if (str)
190         kt->language = g_strdup (str);
191       break;
192     case ARG_CATEGORY:
193       if (kt->category) {
194         g_free (kt->category);
195         kt->category = NULL;
196       }
197       str = g_value_get_string (value);
198       if (str)
199         kt->category = g_strdup (str);
200       break;
201     case ARG_ORIGINAL_CANVAS_WIDTH:
202       kt->original_canvas_width = g_value_get_int (value);
203       break;
204     case ARG_ORIGINAL_CANVAS_HEIGHT:
205       kt->original_canvas_height = g_value_get_int (value);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212
213 static void
214 gst_kate_tag_get_property (GObject * object, guint prop_id,
215     GValue * value, GParamSpec * pspec)
216 {
217   GstKateTag *kt = GST_KATE_TAG (object);
218
219   switch (prop_id) {
220     case ARG_LANGUAGE:
221       g_value_set_string (value, kt->language ? kt->language : "");
222       break;
223     case ARG_CATEGORY:
224       g_value_set_string (value, kt->category ? kt->category : "");
225       break;
226     case ARG_ORIGINAL_CANVAS_WIDTH:
227       g_value_set_int (value, kt->original_canvas_width);
228       break;
229     case ARG_ORIGINAL_CANVAS_HEIGHT:
230       g_value_set_int (value, kt->original_canvas_height);
231       break;
232     default:
233       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234       break;
235   }
236 }
237
238 static guint16
239 encode_canvas_size (size_t size)
240 {
241   size_t base = size;
242   size_t shift = 0;
243   int value;
244
245   while (base & ~((1 << 12) - 1)) {
246     /* we have a high bit we can't fit, increase shift if we wouldn't lose low bits */
247     if ((size >> shift) & 1)
248       return 0;
249     ++shift;
250     base >>= 1;
251   }
252   if (G_UNLIKELY (shift >= 16))
253     return 0;
254
255   /* the size can be represented in our encoding */
256   value = (base << 4) | shift;
257
258   return (guint16) value;
259 }
260
261 static GstFlowReturn
262 gst_kate_tag_parse_packet (GstKateParse * parse, GstBuffer * buffer)
263 {
264   GstTagList *old_tags, *new_tags;
265   const GstTagList *user_tags;
266   GstKateTag *kt;
267   gchar *encoder = NULL;
268   GstBuffer *new_buf;
269   GstMapInfo info;
270
271   kt = GST_KATE_TAG (parse);
272
273   if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
274     GST_ERROR_OBJECT (parse, "Failed to map buffer");
275     return GST_FLOW_ERROR;
276   }
277
278   /* rewrite the language and category */
279   if (info.size >= 64 && info.data[0] == 0x80) {
280     GstBuffer *new_buffer;
281
282     gst_buffer_unmap (buffer, &info);
283     new_buffer = gst_buffer_copy (buffer);
284     buffer = new_buffer;
285
286     if (!gst_buffer_map (buffer, &info, GST_MAP_READWRITE)) {
287       GST_ERROR_OBJECT (parse, "Failed to map copied buffer READWRITE");
288       return GST_FLOW_ERROR;
289     }
290     /* language is at offset 32, 16 bytes, zero terminated */
291     if (kt->language) {
292       strncpy ((char *) info.data + 32, kt->language, 15);
293       info.data[47] = 0;
294     }
295     /* category is at offset 48, 16 bytes, zero terminated */
296     if (kt->category) {
297       strncpy ((char *) info.data + 48, kt->category, 15);
298       info.data[63] = 0;
299     }
300     if (kt->original_canvas_width >= 0) {
301       guint16 v = encode_canvas_size (kt->original_canvas_width);
302       info.data[16] = v & 0xff;
303       info.data[17] = (v >> 8) & 0xff;
304     }
305     if (kt->original_canvas_height >= 0) {
306       guint16 v = encode_canvas_size (kt->original_canvas_height);
307       info.data[18] = v & 0xff;
308       info.data[19] = (v >> 8) & 0xff;
309     }
310   }
311
312   /*  rewrite the comments packet */
313   if (info.size >= 9 && info.data[0] == 0x81) {
314     old_tags =
315         gst_tag_list_from_vorbiscomment (info.data, info.size,
316         (const guint8 *) "\201kate\0\0\0\0", 9, &encoder);
317     user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (kt));
318     gst_buffer_unmap (buffer, &info);
319
320     /* build new tag list */
321     new_tags = gst_tag_list_merge (user_tags, old_tags,
322         gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (kt)));
323     gst_tag_list_unref (old_tags);
324
325     new_buf =
326         gst_tag_list_to_vorbiscomment_buffer (new_tags,
327         (const guint8 *) "\201kate\0\0\0\0", 9, encoder);
328     gst_buffer_copy_into (new_buf, buffer, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
329
330     gst_tag_list_unref (new_tags);
331     g_free (encoder);
332     gst_buffer_unref (buffer);
333
334     /* the buffer will have the framing bit used by Vorbis, but we don't use it */
335     gst_buffer_resize (new_buf, 0, gst_buffer_get_size (new_buf) - 1);
336
337     buffer = new_buf;
338   } else {
339     gst_buffer_unmap (buffer, &info);
340   }
341
342   return GST_KATE_PARSE_CLASS (parent_class)->parse_packet (parse, buffer);
343 }