caps: _do_simplify() -> _simplify()
[platform/upstream/gstreamer.git] / gst / gstmeta.c
1 /* GStreamer
2  * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstmeta.c: metadata operations
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstmeta
24  * @short_description: Buffer metadata
25  *
26  * Last reviewed on December 17th, 2009 (0.10.26)
27  */
28 #include "gst_private.h"
29
30 #include "gstbuffer.h"
31 #include "gstmeta.h"
32 #include "gstinfo.h"
33 #include "gstutils.h"
34
35 static GHashTable *metainfo = NULL;
36 static GRWLock lock;
37
38 GQuark _gst_meta_transform_copy;
39 GQuark _gst_meta_tag_memory;
40
41 void
42 _priv_gst_meta_initialize (void)
43 {
44   g_rw_lock_init (&lock);
45   metainfo = g_hash_table_new (g_str_hash, g_str_equal);
46
47   _gst_meta_transform_copy = g_quark_from_static_string ("gst-copy");
48   _gst_meta_tag_memory = g_quark_from_static_string ("memory");
49 }
50
51 /**
52  * gst_meta_api_type_register:
53  * @api: an API to register
54  * @tags: tags for @api
55  *
56  * Register and return a GType for the @api and associate it with
57  * @tags.
58  *
59  * Returns: a unique GType for @api.
60  */
61 GType
62 gst_meta_api_type_register (const gchar * api, const gchar ** tags)
63 {
64   GType type;
65
66   g_return_val_if_fail (api != NULL, 0);
67   g_return_val_if_fail (tags != NULL, 0);
68
69   GST_CAT_DEBUG (GST_CAT_META, "register API \"%s\"", api);
70   type = g_pointer_type_register_static (api);
71
72   if (type != 0) {
73     gint i;
74
75     for (i = 0; tags[i]; i++) {
76       GST_CAT_DEBUG (GST_CAT_META, "  adding tag \"%s\"", tags[i]);
77       g_type_set_qdata (type, g_quark_from_string (tags[i]),
78           GINT_TO_POINTER (TRUE));
79     }
80   }
81   return type;
82 }
83
84 /**
85  * gst_meta_api_type_has_tag:
86  * @api: an API
87  * @tag: the tag to check
88  *
89  * Check if @api was registered with @tag.
90  *
91  * Returns: %TRUE if @api was registered with @tag.
92  */
93 gboolean
94 gst_meta_api_type_has_tag (GType api, GQuark tag)
95 {
96   g_return_val_if_fail (api != 0, FALSE);
97   g_return_val_if_fail (tag != 0, FALSE);
98
99   return g_type_get_qdata (api, tag) != NULL;
100 }
101
102 /**
103  * gst_meta_register:
104  * @api: the type of the #GstMeta API
105  * @impl: the name of the #GstMeta implementation
106  * @size: the size of the #GstMeta structure
107  * @init_func: a #GstMetaInitFunction
108  * @free_func: a #GstMetaFreeFunction
109  * @transform_func: a #GstMetaTransformFunction
110  *
111  * Register a new #GstMeta implementation.
112  *
113  * The same @info can be retrieved later with gst_meta_get_info() by using
114  * @impl as the key.
115  *
116  * Returns: (transfer none): a #GstMetaInfo that can be used to access metadata.
117  */
118
119 const GstMetaInfo *
120 gst_meta_register (GType api, const gchar * impl, gsize size,
121     GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
122     GstMetaTransformFunction transform_func)
123 {
124   GstMetaInfo *info;
125
126   g_return_val_if_fail (api != 0, NULL);
127   g_return_val_if_fail (impl != NULL, NULL);
128   g_return_val_if_fail (size != 0, NULL);
129
130   info = g_slice_new (GstMetaInfo);
131   info->api = api;
132   info->type = g_pointer_type_register_static (impl);
133   info->size = size;
134   info->init_func = init_func;
135   info->free_func = free_func;
136   info->transform_func = transform_func;
137
138   GST_CAT_DEBUG (GST_CAT_META,
139       "register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT, impl,
140       g_type_name (api), size);
141
142   g_rw_lock_writer_lock (&lock);
143   g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
144   g_rw_lock_writer_unlock (&lock);
145
146   return info;
147 }
148
149 /**
150  * gst_meta_get_info:
151  * @impl: the name
152  *
153  * Lookup a previously registered meta info structure by its implementation name
154  * @impl.
155  *
156  * Returns: (transfer none): a #GstMetaInfo with @impl, or #NULL when no such
157  * metainfo exists.
158  */
159 const GstMetaInfo *
160 gst_meta_get_info (const gchar * impl)
161 {
162   GstMetaInfo *info;
163
164   g_return_val_if_fail (impl != NULL, NULL);
165
166   g_rw_lock_reader_lock (&lock);
167   info = g_hash_table_lookup (metainfo, impl);
168   g_rw_lock_reader_unlock (&lock);
169
170   return info;
171 }