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
25 * @see_also: #oggdemux, #oggmux, #kateparse, #GstTagSetter
26 * @short_description: retags kate streams
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
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
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
42 * ## Example pipelines
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
50 * gst-launch-1.0 -v filesrc location=foo.ogg ! oggdemux ! katetag ! oggmux ! filesink location=bar.ogg
53 * This pipeline will set the language and category of the stream to the
56 * gst-launch-1.0 -v filesrc location=foo.ogg ! oggdemux ! katetag language=pt_BR category=subtitles ! oggmux ! filesink location=bar.ogg
67 #include <gst/tag/tag.h>
68 #include <gst/gsttagsetter.h>
70 #include <kate/kate.h>
72 #include "gstkateelements.h"
73 #include "gstkatetag.h"
76 GST_DEBUG_CATEGORY_EXTERN (gst_katetag_debug);
77 #define GST_CAT_DEFAULT gst_katetag_debug
84 ARG_ORIGINAL_CANVAS_WIDTH,
85 ARG_ORIGINAL_CANVAS_HEIGHT,
88 static GstFlowReturn gst_kate_tag_parse_packet (GstKateParse * parse,
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);
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));
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);
107 gst_kate_tag_class_init (GstKateTagClass * klass)
109 GObjectClass *gobject_class;
110 GstElementClass *gstelement_class;
111 GstKateParseClass *gstkateparse_class;
113 gobject_class = (GObjectClass *) klass;
114 gstelement_class = (GstElementClass *) klass;
115 gstkateparse_class = GST_KATE_PARSE_CLASS (klass);
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);
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));
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));
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));
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));
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>");
145 gstkateparse_class->parse_packet =
146 GST_DEBUG_FUNCPTR (gst_kate_tag_parse_packet);
150 gst_kate_tag_init (GstKateTag * kt)
154 kt->original_canvas_width = -1;
155 kt->original_canvas_height = -1;
159 gst_kate_tag_dispose (GObject * object)
161 GstKateTag *kt = GST_KATE_TAG (object);
163 GST_LOG_OBJECT (kt, "disposing");
166 g_free (kt->language);
170 g_free (kt->category);
174 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
178 gst_kate_tag_set_property (GObject * object, guint prop_id,
179 const GValue * value, GParamSpec * pspec)
181 GstKateTag *kt = GST_KATE_TAG (object);
187 g_free (kt->language);
190 str = g_value_get_string (value);
192 kt->language = g_strdup (str);
196 g_free (kt->category);
199 str = g_value_get_string (value);
201 kt->category = g_strdup (str);
203 case ARG_ORIGINAL_CANVAS_WIDTH:
204 kt->original_canvas_width = g_value_get_int (value);
206 case ARG_ORIGINAL_CANVAS_HEIGHT:
207 kt->original_canvas_height = g_value_get_int (value);
210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 gst_kate_tag_get_property (GObject * object, guint prop_id,
217 GValue * value, GParamSpec * pspec)
219 GstKateTag *kt = GST_KATE_TAG (object);
223 g_value_set_string (value, kt->language ? kt->language : "");
226 g_value_set_string (value, kt->category ? kt->category : "");
228 case ARG_ORIGINAL_CANVAS_WIDTH:
229 g_value_set_int (value, kt->original_canvas_width);
231 case ARG_ORIGINAL_CANVAS_HEIGHT:
232 g_value_set_int (value, kt->original_canvas_height);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 encode_canvas_size (size_t size)
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)
254 if (G_UNLIKELY (shift >= 16))
257 /* the size can be represented in our encoding */
258 value = (base << 4) | shift;
260 return (guint16) value;
264 gst_kate_tag_parse_packet (GstKateParse * parse, GstBuffer * buffer)
266 GstTagList *old_tags, *new_tags;
267 const GstTagList *user_tags;
269 gchar *encoder = NULL;
273 kt = GST_KATE_TAG (parse);
275 if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) {
276 GST_ERROR_OBJECT (parse, "Failed to map buffer");
277 return GST_FLOW_ERROR;
280 /* rewrite the language and category */
281 if (info.size >= 64 && info.data[0] == 0x80) {
282 GstBuffer *new_buffer;
284 gst_buffer_unmap (buffer, &info);
285 new_buffer = gst_buffer_copy (buffer);
286 gst_buffer_unref (buffer);
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;
293 /* language is at offset 32, 16 bytes, zero terminated */
295 strncpy ((char *) info.data + 32, kt->language, 15);
298 /* category is at offset 48, 16 bytes, zero terminated */
300 strncpy ((char *) info.data + 48, kt->category, 15);
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;
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;
315 /* rewrite the comments packet */
316 if (info.size >= 9 && info.data[0] == 0x81) {
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);
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);
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);
333 gst_tag_list_unref (new_tags);
335 gst_buffer_unref (buffer);
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);
342 gst_buffer_unmap (buffer, &info);
345 return GST_KATE_PARSE_CLASS (parent_class)->parse_packet (parse, buffer);