Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / vorbis / gstvorbistag.c
1 /*
2  * Copyright (C) 2006 James Livingston <doclivingston@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-vorbistag
22  * @see_also: #oggdemux, #oggmux, #vorbisparse, #GstTagSetter
23  *
24  * The vorbistags element can change the tag contained within a raw
25  * vorbis stream. Specifically, it modifies the comments header packet
26  * of the vorbis stream.
27  *
28  * The element will also process the stream as the #vorbisparse element does
29  * so it can be used when remuxing an Ogg Vorbis stream, without additional
30  * elements.
31  *
32  * Applications can set the tags to write using the #GstTagSetter interface.
33  * Tags contained withing the vorbis bitstream will be picked up
34  * automatically (and merged according to the merge mode set via the tag
35  * setter interface).
36  *
37  * <refsect2>
38  * <title>Example pipelines</title>
39  * |[
40  * gst-launch -v filesrc location=foo.ogg ! oggdemux ! vorbistag ! oggmux ! filesink location=bar.ogg
41  * ]| This element is not useful with gst-launch, because it does not support
42  * setting the tags on a #GstTagSetter interface. Conceptually, the element
43  * will usually be used in this order though.
44  * </refsect2>
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #  include "config.h"
49 #endif
50
51 #include <glib.h>
52 #include <gst/tag/tag.h>
53 #include <gst/gsttagsetter.h>
54
55 #include <vorbis/codec.h>
56
57 #include "gstvorbistag.h"
58
59
60 GST_DEBUG_CATEGORY_EXTERN (vorbisparse_debug);
61 #define GST_CAT_DEFAULT vorbisparse_debug
62
63 static GstFlowReturn gst_vorbis_tag_parse_packet (GstVorbisParse * parse,
64     GstBuffer * buffer);
65
66 #define _do_init(type)                                                          \
67   G_STMT_START{                                                                 \
68     static const GInterfaceInfo tag_setter_info = {                             \
69       NULL,                                                                     \
70       NULL,                                                                     \
71       NULL                                                                      \
72     };                                                                          \
73     g_type_add_interface_static (type, GST_TYPE_TAG_SETTER,                     \
74                                  &tag_setter_info);                             \
75   }G_STMT_END
76
77 GST_BOILERPLATE_FULL (GstVorbisTag, gst_vorbis_tag, GstVorbisParse,
78     GST_TYPE_VORBIS_PARSE, _do_init);
79
80
81 static void
82 gst_vorbis_tag_base_init (gpointer g_class)
83 {
84   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
85
86   gst_element_class_set_details_simple (element_class,
87       "VorbisTag", "Formatter/Metadata",
88       "Retags vorbis streams", "James Livingston <doclivingston@gmail.com>");
89 }
90
91 static void
92 gst_vorbis_tag_class_init (GstVorbisTagClass * klass)
93 {
94   GstVorbisParseClass *vorbisparse_class = GST_VORBIS_PARSE_CLASS (klass);
95
96   vorbisparse_class->parse_packet = gst_vorbis_tag_parse_packet;
97 }
98
99 static void
100 gst_vorbis_tag_init (GstVorbisTag * tagger, GstVorbisTagClass * g_class)
101 {
102   /* nothing to do */
103 }
104
105
106 static GstFlowReturn
107 gst_vorbis_tag_parse_packet (GstVorbisParse * parse, GstBuffer * buffer)
108 {
109   GstTagList *old_tags, *new_tags;
110   const GstTagList *user_tags;
111   GstVorbisTag *tagger;
112   gchar *encoder = NULL;
113   GstBuffer *new_buf;
114
115   /* just pass everything except the comments packet */
116   if (GST_BUFFER_SIZE (buffer) >= 1 && GST_BUFFER_DATA (buffer)[0] != 0x03) {
117     return GST_VORBIS_PARSE_CLASS (parent_class)->parse_packet (parse, buffer);
118   }
119
120   tagger = GST_VORBIS_TAG (parse);
121
122   old_tags =
123       gst_tag_list_from_vorbiscomment_buffer (buffer, (guint8 *) "\003vorbis",
124       7, &encoder);
125   user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (tagger));
126
127   /* build new tag list */
128   new_tags = gst_tag_list_merge (user_tags, old_tags,
129       gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (tagger)));
130   gst_tag_list_free (old_tags);
131
132   new_buf =
133       gst_tag_list_to_vorbiscomment_buffer (new_tags, (guint8 *) "\003vorbis",
134       7, encoder);
135   gst_buffer_copy_metadata (new_buf, buffer, GST_BUFFER_COPY_TIMESTAMPS);
136
137   gst_tag_list_free (new_tags);
138   g_free (encoder);
139   gst_buffer_unref (buffer);
140
141   return GST_VORBIS_PARSE_CLASS (parent_class)->parse_packet (parse, new_buf);
142 }