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);
111 /* Memory metadata */
118 } GstMetaMemoryParams;
122 GstMetaMemory memory;
123 GstMetaMemoryParams params;
127 meta_memory_mmap (GstMetaMemory * meta, gsize offset, gsize * size,
128 GstMetaMapFlags flags)
130 GstMetaMemoryImpl *impl = (GstMetaMemoryImpl *) meta;
132 *size = impl->params.size - offset;
133 return impl->params.data + offset;
137 meta_memory_munmap (GstMetaMemory * meta, gpointer data, gsize size)
143 meta_memory_init (GstMetaMemoryImpl * meta, GstMetaMemoryParams * params,
146 meta->memory.mmap_func = meta_memory_mmap;
147 meta->memory.munmap_func = meta_memory_munmap;
148 meta->params = *params;
153 meta_memory_free (GstMetaMemoryImpl * meta, GstBuffer * buffer)
155 if (meta->params.free_func)
156 meta->params.free_func (meta->params.data);
160 meta_memory_copy (GstBuffer * copy, GstMetaMemoryImpl * meta,
161 const GstBuffer * buffer)
163 gst_buffer_add_meta_memory (copy,
164 g_memdup (meta->params.data, meta->params.size),
165 g_free, meta->params.size, meta->params.offset);
169 meta_memory_sub (GstBuffer * subbuf, GstMetaMemoryImpl * meta,
170 GstBuffer * buffer, guint offset, guint size)
172 gst_buffer_add_meta_memory (subbuf,
173 meta->params.data, NULL, size, meta->params.offset + offset);
177 gst_meta_memory_get_info (void)
179 static const GstMetaInfo *meta_info = NULL;
181 if (meta_info == NULL) {
182 meta_info = gst_meta_register ("GstMetaMemory", "GstMetaMemoryImpl",
183 sizeof (GstMetaMemoryImpl),
184 (GstMetaInitFunction) meta_memory_init,
185 (GstMetaFreeFunction) meta_memory_free,
186 (GstMetaCopyFunction) meta_memory_copy,
187 (GstMetaSubFunction) meta_memory_sub,
188 (GstMetaSerializeFunction) NULL, (GstMetaDeserializeFunction) NULL);
194 gst_buffer_add_meta_memory (GstBuffer * buffer, gpointer data,
195 GFreeFunc free_func, gsize size, gsize offset)
198 GstMetaMemoryParams params;
201 params.free_func = free_func;
203 params.offset = offset;
205 meta = gst_buffer_add_meta (buffer, GST_META_MEMORY_INFO, ¶ms);
207 return (GstMetaMemory *) meta;
210 /* Timing metadata */
212 meta_timing_copy (GstBuffer * copy, GstMetaTiming * meta, GstBuffer * buffer)
214 GstMetaTiming *timing;
216 GST_DEBUG ("copy called from buffer %p to %p, meta %p", buffer, copy, meta);
218 timing = gst_buffer_add_meta_timing (copy);
219 timing->pts = meta->pts;
220 timing->dts = meta->dts;
221 timing->duration = meta->duration;
222 timing->clock_rate = meta->clock_rate;
226 meta_timing_sub (GstBuffer * sub, GstMetaTiming * meta, GstBuffer * buffer,
227 guint offset, guint size)
229 GstMetaTiming *timing;
231 GST_DEBUG ("sub called from buffer %p to %p, meta %p, %u-%u", buffer, sub,
234 timing = gst_buffer_add_meta_timing (sub);
236 /* same offset, copy timestamps */
237 timing->pts = meta->pts;
238 timing->dts = meta->dts;
239 if (size == GST_BUFFER_SIZE (buffer)) {
240 /* same size, copy duration */
241 timing->duration = meta->duration;
244 timing->duration = GST_CLOCK_TIME_NONE;
249 timing->duration = -1;
251 timing->clock_rate = meta->clock_rate;
255 gst_meta_timing_get_info (void)
257 static const GstMetaInfo *meta_info = NULL;
259 if (meta_info == NULL) {
260 meta_info = gst_meta_register ("GstMetaTiming", "GstMetaTiming",
261 sizeof (GstMetaTiming),
262 (GstMetaInitFunction) NULL,
263 (GstMetaFreeFunction) NULL,
264 (GstMetaCopyFunction) meta_timing_copy,
265 (GstMetaSubFunction) meta_timing_sub,
266 (GstMetaSerializeFunction) NULL, (GstMetaDeserializeFunction) NULL);