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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Buffer metadata
26 * Last reviewed on December 17th, 2009 (0.10.26)
28 #include "gst_private.h"
30 #include "gstbuffer.h"
35 static GHashTable *metainfo = NULL;
36 static GStaticRWLock lock = G_STATIC_RW_LOCK_INIT;
41 metainfo = g_hash_table_new (g_str_hash, g_str_equal);
45 * gst_meta_register_info:
46 * @info: a #GstMetaInfo
48 * Register a #GstMetaInfo. The same @info can be retrieved later with
49 * gst_meta_get_info() by using @impl as the key.
51 * Returns: a #GstMetaInfo that can be used to access metadata.
55 gst_meta_register (const gchar * api, const gchar * impl, gsize size,
56 GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
57 GstMetaCopyFunction copy_func, GstMetaSubFunction sub_func,
58 GstMetaSerializeFunction serialize_func,
59 GstMetaDeserializeFunction deserialize_func)
63 g_return_val_if_fail (api != NULL, NULL);
64 g_return_val_if_fail (impl != NULL, NULL);
65 g_return_val_if_fail (size != 0, NULL);
67 info = g_slice_new (GstMetaInfo);
68 info->api = g_quark_from_string (api);
69 info->impl = g_quark_from_string (impl);
71 info->init_func = init_func;
72 info->free_func = free_func;
73 info->copy_func = copy_func;
74 info->sub_func = sub_func;
75 info->serialize_func = serialize_func;
76 info->deserialize_func = deserialize_func;
78 GST_DEBUG ("register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT,
81 g_static_rw_lock_writer_lock (&lock);
82 g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
83 g_static_rw_lock_writer_unlock (&lock);
92 * Lookup a previously registered meta info structure by its @implementor name.
94 * Returns: a #GstMetaInfo with @name or #NULL when no such metainfo
98 gst_meta_get_info (const gchar * impl)
102 g_return_val_if_fail (impl != NULL, NULL);
104 g_static_rw_lock_reader_lock (&lock);
105 info = g_hash_table_lookup (metainfo, impl);
106 g_static_rw_lock_reader_unlock (&lock);
117 } GstMetaMemoryParams;
121 GstMetaMemory memory;
122 GstMetaMemoryParams params;
126 meta_memory_mmap (GstMetaMemory * meta, gsize offset, gsize * size,
127 GstMetaMapFlags flags)
129 GstMetaMemoryImpl *impl = (GstMetaMemoryImpl *) meta;
131 *size = impl->params.size - offset;
132 return impl->params.data + offset;
136 meta_memory_munmap (GstMetaMemory * meta, gpointer data, gsize size)
142 meta_memory_init (GstMetaMemoryImpl * meta, GstMetaMemoryParams * params,
145 meta->memory.mmap_func = meta_memory_mmap;
146 meta->memory.munmap_func = meta_memory_munmap;
147 meta->params = *params;
152 meta_memory_free (GstMetaMemoryImpl * meta, GstBuffer * buffer)
154 if (meta->params.free_func)
155 meta->params.free_func (meta->params.data);
159 meta_memory_copy (GstBuffer * copy, GstMetaMemoryImpl * meta,
160 const GstBuffer * buffer)
162 gst_buffer_add_meta_memory (copy,
163 g_memdup (meta->params.data, meta->params.size),
164 g_free, meta->params.size, meta->params.offset);
168 meta_memory_sub (GstBuffer * subbuf, GstMetaMemoryImpl * meta,
169 GstBuffer * buffer, guint offset, guint size)
171 gst_buffer_add_meta_memory (subbuf,
172 meta->params.data, NULL, size, meta->params.offset + offset);
176 gst_meta_memory_get_info (void)
178 static const GstMetaInfo *meta_info = NULL;
180 if (meta_info == NULL) {
181 meta_info = gst_meta_register ("GstMetaMemory", "GstMetaMemoryImpl",
182 sizeof (GstMetaMemoryImpl),
183 (GstMetaInitFunction) meta_memory_init,
184 (GstMetaFreeFunction) meta_memory_free,
185 (GstMetaCopyFunction) meta_memory_copy,
186 (GstMetaSubFunction) meta_memory_sub,
187 (GstMetaSerializeFunction) NULL, (GstMetaDeserializeFunction) NULL);
193 gst_buffer_add_meta_memory (GstBuffer * buffer, gpointer data,
194 GFreeFunc free_func, gsize size, gsize offset)
197 GstMetaMemoryParams params;
200 params.free_func = free_func;
202 params.offset = offset;
204 meta = gst_buffer_add_meta (buffer, GST_META_MEMORY_INFO, ¶ms);
206 return (GstMetaMemory *) meta;