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