2 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
4 * gstmeta.c: metadata operations
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.
25 * @short_description: Buffer metadata
27 * The #GstMeta structure should be included as the first member of a #GstBuffer
28 * metadata structure. The structure defines the API of the metadata and should
29 * be accessible to all elements using the metadata.
31 * A metadata API is registered with gst_meta_api_type_register() which takes a
32 * name for the metadata API and some tags associated with the metadata.
33 * With gst_meta_api_type_has_tag() one can check if a certain metadata API
34 * contains a given tag.
36 * Multiple implementations of a metadata API can be registered.
37 * To implement a metadata API, gst_meta_register() should be used. This
38 * function takes all parameters needed to create, free and transform metadata
39 * along with the size of the metadata. The function returns a #GstMetaInfo
40 * structure that contains the information for the implementation of the API.
42 * A specific implementation can be retrieved by name with gst_meta_get_info().
44 * See #GstBuffer for how the metadata can be added, retrieved and removed from
47 #include "gst_private.h"
49 #include "gstbuffer.h"
55 static GHashTable *metainfo = NULL;
58 GQuark _gst_meta_transform_copy;
59 GQuark _gst_meta_tag_memory;
65 GstStructure *structure;
71 GstCustomMetaTransformFunction custom_transform_func;
72 gpointer custom_transform_user_data;
73 GDestroyNotify custom_transform_destroy_notify;
78 free_info (gpointer data)
80 g_slice_free (GstMetaInfoImpl, data);
84 _priv_gst_meta_initialize (void)
86 g_rw_lock_init (&lock);
87 metainfo = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, free_info);
89 _gst_meta_transform_copy = g_quark_from_static_string ("gst-copy");
90 _gst_meta_tag_memory = g_quark_from_static_string ("memory");
94 notify_custom (gchar * key, GstMetaInfo * info, gpointer unused)
96 GstMetaInfoImpl *impl = (GstMetaInfoImpl *) info;
98 if (impl->is_custom) {
99 if (impl->custom_transform_destroy_notify)
100 impl->custom_transform_destroy_notify (impl->custom_transform_user_data);
106 _priv_gst_meta_cleanup (void)
108 if (metainfo != NULL) {
109 g_hash_table_foreach_remove (metainfo, (GHRFunc) notify_custom, NULL);
110 g_hash_table_unref (metainfo);
116 * gst_meta_api_type_register:
117 * @api: an API to register
118 * @tags: (array zero-terminated=1): tags for @api
120 * Register and return a GType for the @api and associate it with
123 * Returns: a unique GType for @api.
126 gst_meta_api_type_register (const gchar * api, const gchar ** tags)
130 g_return_val_if_fail (api != NULL, 0);
131 g_return_val_if_fail (tags != NULL, 0);
133 GST_CAT_DEBUG (GST_CAT_META, "register API \"%s\"", api);
134 type = g_pointer_type_register_static (api);
139 for (i = 0; tags[i]; i++) {
140 GST_CAT_DEBUG (GST_CAT_META, " adding tag \"%s\"", tags[i]);
141 g_type_set_qdata (type, g_quark_from_string (tags[i]),
142 GINT_TO_POINTER (TRUE));
146 g_type_set_qdata (type, GST_QUARK (TAGS), g_strdupv ((gchar **) tags));
152 custom_init_func (GstMeta * meta, gpointer params, GstBuffer * buffer)
154 GstCustomMetaImpl *cmeta = (GstCustomMetaImpl *) meta;
156 cmeta->structure = gst_structure_new_empty (g_type_name (meta->info->type));
158 gst_structure_set_parent_refcount (cmeta->structure,
159 &GST_MINI_OBJECT_REFCOUNT (buffer));
165 custom_free_func (GstMeta * meta, GstBuffer * buffer)
167 GstCustomMetaImpl *cmeta = (GstCustomMetaImpl *) meta;
169 gst_structure_set_parent_refcount (cmeta->structure, NULL);
170 gst_structure_free (cmeta->structure);
174 custom_transform_func (GstBuffer * transbuf, GstMeta * meta,
175 GstBuffer * buffer, GQuark type, gpointer data)
177 GstCustomMetaImpl *custom, *cmeta = (GstCustomMetaImpl *) meta;
178 GstMetaInfoImpl *info = (GstMetaInfoImpl *) meta->info;
180 if (info->custom_transform_func)
181 return info->custom_transform_func (transbuf, (GstCustomMeta *) meta,
182 buffer, type, data, info->custom_transform_user_data);
184 if (GST_META_TRANSFORM_IS_COPY (type)) {
186 (GstCustomMetaImpl *) gst_buffer_add_meta (transbuf, meta->info, NULL);
187 gst_structure_set_parent_refcount (custom->structure, NULL);
188 gst_structure_take (&custom->structure,
189 gst_structure_copy (cmeta->structure));
190 gst_structure_set_parent_refcount (custom->structure,
191 &GST_MINI_OBJECT_REFCOUNT (buffer));
200 * gst_custom_meta_get_structure:
202 * Retrieve the #GstStructure backing a custom meta, the structure's mutability
203 * is conditioned to the writability of the #GstBuffer @meta is attached to.
205 * Returns: (transfer none): the #GstStructure backing @meta
209 gst_custom_meta_get_structure (GstCustomMeta * meta)
211 g_return_val_if_fail (meta != NULL, NULL);
212 g_return_val_if_fail (gst_meta_info_is_custom (((GstMeta *) meta)->info),
215 return ((GstCustomMetaImpl *) meta)->structure;
219 * gst_custom_meta_has_name:
221 * Checks whether the name of the custom meta is @name
223 * Returns: Whether @name is the name of the custom meta
227 gst_custom_meta_has_name (GstCustomMeta * meta, const gchar * name)
229 g_return_val_if_fail (meta != NULL, FALSE);
230 g_return_val_if_fail (gst_meta_info_is_custom (((GstMeta *) meta)->info),
233 return gst_structure_has_name (((GstCustomMetaImpl *) meta)->structure, name);
237 * gst_meta_register_custom:
238 * @name: the name of the #GstMeta implementation
239 * @tags: (array zero-terminated=1): tags for @api
240 * @transform_func: (scope notified) (nullable): a #GstMetaTransformFunction
241 * @user_data: (closure): user data passed to @transform_func
242 * @destroy_data: #GDestroyNotify for user_data
244 * Register a new custom #GstMeta implementation, backed by an opaque
245 * structure holding a #GstStructure.
247 * The registered info can be retrieved later with gst_meta_get_info() by using
250 * The backing #GstStructure can be retrieved with
251 * gst_custom_meta_get_structure(), its mutability is conditioned by the
252 * writability of the buffer the meta is attached to.
254 * When @transform_func is %NULL, the meta and its backing #GstStructure
255 * will always be copied when the transform operation is copy, other operations
256 * are discarded, copy regions are ignored.
258 * Returns: (transfer none): a #GstMetaInfo that can be used to
263 gst_meta_register_custom (const gchar * name, const gchar ** tags,
264 GstCustomMetaTransformFunction transform_func,
265 gpointer user_data, GDestroyNotify destroy_data)
267 gchar *api_name = g_strdup_printf ("%s-api", name);
269 GstMetaInfoImpl *info;
270 GstMetaInfo *ret = NULL;
272 g_return_val_if_fail (tags != NULL, NULL);
273 g_return_val_if_fail (name != NULL, NULL);
275 api = gst_meta_api_type_register (api_name, tags);
277 if (api == G_TYPE_INVALID)
280 info = (GstMetaInfoImpl *) gst_meta_register (api, name,
281 sizeof (GstCustomMetaImpl),
282 custom_init_func, custom_free_func, custom_transform_func);
287 info->is_custom = TRUE;
288 info->custom_transform_func = transform_func;
289 info->custom_transform_user_data = user_data;
290 info->custom_transform_destroy_notify = destroy_data;
292 ret = (GstMetaInfo *) info;
299 * gst_meta_info_is_custom:
301 * Returns: whether @info was registered as a #GstCustomMeta with
302 * gst_meta_register_custom()
306 gst_meta_info_is_custom (const GstMetaInfo * info)
308 g_return_val_if_fail (info != NULL, FALSE);
310 return ((GstMetaInfoImpl *) info)->is_custom;
314 * gst_meta_api_type_has_tag:
316 * @tag: the tag to check
318 * Check if @api was registered with @tag.
320 * Returns: %TRUE if @api was registered with @tag.
323 gst_meta_api_type_has_tag (GType api, GQuark tag)
325 g_return_val_if_fail (api != 0, FALSE);
326 g_return_val_if_fail (tag != 0, FALSE);
328 return g_type_get_qdata (api, tag) != NULL;
332 * gst_meta_api_type_get_tags:
335 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): an array of tags as strings.
340 gst_meta_api_type_get_tags (GType api)
343 g_return_val_if_fail (api != 0, FALSE);
345 tags = g_type_get_qdata (api, GST_QUARK (TAGS));
350 return (const gchar * const *) tags;
355 * @api: the type of the #GstMeta API
356 * @impl: the name of the #GstMeta implementation
357 * @size: the size of the #GstMeta structure
358 * @init_func: (scope async): a #GstMetaInitFunction
359 * @free_func: (scope async): a #GstMetaFreeFunction
360 * @transform_func: (scope async): a #GstMetaTransformFunction
362 * Register a new #GstMeta implementation.
364 * The same @info can be retrieved later with gst_meta_get_info() by using
367 * Returns: (transfer none): a #GstMetaInfo that can be used to
372 gst_meta_register (GType api, const gchar * impl, gsize size,
373 GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
374 GstMetaTransformFunction transform_func)
379 g_return_val_if_fail (api != 0, NULL);
380 g_return_val_if_fail (impl != NULL, NULL);
381 g_return_val_if_fail (size != 0, NULL);
383 if (init_func == NULL)
384 g_critical ("Registering meta implementation '%s' without init function",
387 /* first try to register the implementation name. It's possible
388 * that this fails because it was already registered. Don't warn,
389 * glib did this for us already. */
390 type = g_pointer_type_register_static (impl);
394 info = (GstMetaInfo *) g_slice_new (GstMetaInfoImpl);
398 info->init_func = init_func;
399 info->free_func = free_func;
400 info->transform_func = transform_func;
401 ((GstMetaInfoImpl *) info)->is_custom = FALSE;
403 GST_CAT_DEBUG (GST_CAT_META,
404 "register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT, impl,
405 g_type_name (api), size);
407 g_rw_lock_writer_lock (&lock);
408 g_hash_table_insert (metainfo, (gpointer) g_intern_string (impl),
410 g_rw_lock_writer_unlock (&lock);
419 * Lookup a previously registered meta info structure by its implementation name
422 * Returns: (transfer none) (nullable): a #GstMetaInfo with @impl, or
423 * %NULL when no such metainfo exists.
426 gst_meta_get_info (const gchar * impl)
430 g_return_val_if_fail (impl != NULL, NULL);
432 g_rw_lock_reader_lock (&lock);
433 info = g_hash_table_lookup (metainfo, impl);
434 g_rw_lock_reader_unlock (&lock);
440 * gst_meta_get_seqnum:
443 * Gets seqnum for this meta.
448 gst_meta_get_seqnum (const GstMeta * meta)
450 GstMetaItem *meta_item;
453 g_return_val_if_fail (meta != NULL, 0);
456 p -= G_STRUCT_OFFSET (GstMetaItem, meta);
457 meta_item = (GstMetaItem *) p;
458 return meta_item->seq_num;
462 * gst_meta_compare_seqnum:
466 * Meta sequence number compare function. Can be used as #GCompareFunc
467 * or a #GCompareDataFunc.
469 * Returns: a negative number if @meta1 comes before @meta2, 0 if both metas
470 * have an equal sequence number, or a positive integer if @meta1 comes
476 gst_meta_compare_seqnum (const GstMeta * meta1, const GstMeta * meta2)
478 guint64 seqnum1 = gst_meta_get_seqnum (meta1);
479 guint64 seqnum2 = gst_meta_get_seqnum (meta2);
481 if (seqnum1 == seqnum2)
484 return (seqnum1 < seqnum2) ? -1 : 1;