3 * Copyright (C) 2006 James Livingston <doclivingston@gmail.com>
4 * Copyright (C) 2008 Vincent Penquerc'h <ogg.k.ogg.k@googlemail.com>
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.
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.
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.
23 * SECTION:element-katetag
24 * @see_also: #oggdemux, #oggmux, #kateparse, #GstTagSetter
25 * @short_description: retags kate streams
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
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
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
45 * <title>Example pipelines</title>
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
53 * gst-launch -v filesrc location=foo.ogg ! oggdemux ! katetag ! oggmux ! filesink location=bar.ogg
57 * This pipeline will set the language and category of the stream to the
60 * gst-launch -v filesrc location=foo.ogg ! oggdemux ! katetag language=pt_BR category=subtitles ! oggmux ! filesink location=bar.ogg
72 #include <gst/tag/tag.h>
73 #include <gst/gsttagsetter.h>
75 #include <kate/kate.h>
77 #include "gstkatetag.h"
80 GST_DEBUG_CATEGORY_EXTERN (gst_katetag_debug);
81 #define GST_CAT_DEFAULT gst_katetag_debug
88 ARG_ORIGINAL_CANVAS_WIDTH,
89 ARG_ORIGINAL_CANVAS_HEIGHT,
92 static GstFlowReturn gst_kate_tag_parse_packet (GstKateParse * parse,
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);
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));
105 gst_kate_tag_class_init (GstKateTagClass * klass)
107 GObjectClass *gobject_class;
108 GstElementClass *gstelement_class;
109 GstKateParseClass *gstkateparse_class;
111 gobject_class = (GObjectClass *) klass;
112 gstelement_class = (GstElementClass *) klass;
113 gstkateparse_class = GST_KATE_PARSE_CLASS (klass);
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);
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));
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));
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));
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));
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>");
143 gstkateparse_class->parse_packet =
144 GST_DEBUG_FUNCPTR (gst_kate_tag_parse_packet);
148 gst_kate_tag_init (GstKateTag * kt)
152 kt->original_canvas_width = -1;
153 kt->original_canvas_height = -1;
157 gst_kate_tag_dispose (GObject * object)
159 GstKateTag *kt = GST_KATE_TAG (object);
161 GST_LOG_OBJECT (kt, "disposing");
164 g_free (kt->language);
168 g_free (kt->category);
172 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
176 gst_kate_tag_set_property (GObject * object, guint prop_id,
177 const GValue * value, GParamSpec * pspec)
179 GstKateTag *kt = GST_KATE_TAG (object);
185 g_free (kt->language);
188 str = g_value_get_string (value);
190 kt->language = g_strdup (str);
194 g_free (kt->category);
197 str = g_value_get_string (value);
199 kt->category = g_strdup (str);
201 case ARG_ORIGINAL_CANVAS_WIDTH:
202 kt->original_canvas_width = g_value_get_int (value);
204 case ARG_ORIGINAL_CANVAS_HEIGHT:
205 kt->original_canvas_height = g_value_get_int (value);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gst_kate_tag_get_property (GObject * object, guint prop_id,
215 GValue * value, GParamSpec * pspec)
217 GstKateTag *kt = GST_KATE_TAG (object);
221 g_value_set_string (value, kt->language ? kt->language : "");
224 g_value_set_string (value, kt->category ? kt->category : "");
226 case ARG_ORIGINAL_CANVAS_WIDTH:
227 g_value_set_int (value, kt->original_canvas_width);
229 case ARG_ORIGINAL_CANVAS_HEIGHT:
230 g_value_set_int (value, kt->original_canvas_height);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 encode_canvas_size (size_t size)
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)
252 if (G_UNLIKELY (shift >= 16))
255 /* the size can be represented in our encoding */
256 value = (base << 4) | shift;
258 return (guint16) value;
262 gst_kate_tag_parse_packet (GstKateParse * parse, GstBuffer * buffer)
264 GstTagList *old_tags, *new_tags;
265 const GstTagList *user_tags;
267 gchar *encoder = NULL;
271 kt = GST_KATE_TAG (parse);
273 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
274 GST_ERROR_OBJECT (parse, "Failed to map buffer");
275 return GST_FLOW_ERROR;
278 /* rewrite the language and category */
279 if (info.size >= 64 && info.data[0] == 0x80) {
280 GstBuffer *new_buffer;
282 gst_buffer_unmap (buffer, &info);
283 new_buffer = gst_buffer_copy (buffer);
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;
290 /* language is at offset 32, 16 bytes, zero terminated */
292 strncpy ((char *) info.data + 32, kt->language, 15);
295 /* category is at offset 48, 16 bytes, zero terminated */
297 strncpy ((char *) info.data + 48, kt->category, 15);
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;
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;
312 /* rewrite the comments packet */
313 if (info.size >= 9 && info.data[0] == 0x81) {
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);
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);
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);
330 gst_tag_list_unref (new_tags);
332 gst_buffer_unref (buffer);
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);
339 gst_buffer_unmap (buffer, &info);
342 return GST_KATE_PARSE_CLASS (parent_class)->parse_packet (parse, buffer);