Merge branch 'master' into 0.11
[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 GStaticRWLock lock = G_STATIC_RW_LOCK_INIT;
37
38 void
39 _gst_meta_init (void)
40 {
41   metainfo = g_hash_table_new (g_str_hash, g_str_equal);
42 }
43
44 /**
45  * gst_meta_register_info:
46  * @info: a #GstMetaInfo
47  *
48  * Register a #GstMetaInfo. The same @info can be retrieved later with
49  * gst_meta_get_info() by using @impl as the key.
50  *
51  * Returns: a #GstMetaInfo that can be used to access metadata.
52  */
53
54 const GstMetaInfo *
55 gst_meta_register (const gchar * api, const gchar * impl, gsize size,
56     GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
57     GstMetaCopyFunction copy_func,
58     GstMetaTransformFunction transform_func,
59     GstMetaSerializeFunction serialize_func,
60     GstMetaDeserializeFunction deserialize_func)
61 {
62   GstMetaInfo *info;
63
64   g_return_val_if_fail (api != NULL, NULL);
65   g_return_val_if_fail (impl != NULL, NULL);
66   g_return_val_if_fail (size != 0, NULL);
67
68   info = g_slice_new (GstMetaInfo);
69   info->api = g_quark_from_string (api);
70   info->impl = g_quark_from_string (impl);
71   info->size = size;
72   info->init_func = init_func;
73   info->free_func = free_func;
74   info->copy_func = copy_func;
75   info->transform_func = transform_func;
76   info->serialize_func = serialize_func;
77   info->deserialize_func = deserialize_func;
78
79   GST_DEBUG ("register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT,
80       api, impl, size);
81
82   g_static_rw_lock_writer_lock (&lock);
83   g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
84   g_static_rw_lock_writer_unlock (&lock);
85
86   return info;
87 }
88
89 /**
90  * gst_meta_get_info:
91  * @impl: the name
92  *
93  * Lookup a previously registered meta info structure by its implementor name
94  * @impl.
95  *
96  * Returns: a #GstMetaInfo with @impl or #NULL when no such metainfo
97  * exists.
98  */
99 const GstMetaInfo *
100 gst_meta_get_info (const gchar * impl)
101 {
102   GstMetaInfo *info;
103
104   g_return_val_if_fail (impl != NULL, NULL);
105
106   g_static_rw_lock_reader_lock (&lock);
107   info = g_hash_table_lookup (metainfo, impl);
108   g_static_rw_lock_reader_unlock (&lock);
109
110   return info;
111 }
112
113 /* Timing metadata */
114 static void
115 meta_timing_copy (GstBuffer * copybuf, GstMetaTiming * meta,
116     GstBuffer * buffer, gsize offset, gsize size)
117 {
118   GstMetaTiming *timing;
119
120   GST_DEBUG ("trans called from buffer %p to %p, meta %p, %u-%u", buffer,
121       copybuf, meta, offset, size);
122
123   timing = gst_buffer_add_meta_timing (copybuf);
124   if (offset == 0) {
125     /* same offset, copy timestamps */
126     timing->pts = meta->pts;
127     timing->dts = meta->dts;
128     if (size == gst_buffer_get_size (buffer)) {
129       /* same size, copy duration */
130       timing->duration = meta->duration;
131     } else {
132       /* else clear */
133       timing->duration = GST_CLOCK_TIME_NONE;
134     }
135   } else {
136     timing->pts = -1;
137     timing->dts = -1;
138     timing->duration = -1;
139   }
140   timing->clock_rate = meta->clock_rate;
141 }
142
143 const GstMetaInfo *
144 gst_meta_timing_get_info (void)
145 {
146   static const GstMetaInfo *meta_info = NULL;
147
148   if (meta_info == NULL) {
149     meta_info = gst_meta_register ("GstMetaTiming", "GstMetaTiming",
150         sizeof (GstMetaTiming),
151         (GstMetaInitFunction) NULL,
152         (GstMetaFreeFunction) NULL,
153         (GstMetaCopyFunction) meta_timing_copy,
154         (GstMetaTransformFunction) NULL,
155         (GstMetaSerializeFunction) NULL, (GstMetaDeserializeFunction) NULL);
156   }
157   return meta_info;
158 }