Merge remote-tracking branch 'origin/master' into symbol-exports
[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 _priv_gst_meta_initialize (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, GstMetaTransformFunction transform_func)
58 {
59   GstMetaInfo *info;
60
61   g_return_val_if_fail (api != NULL, NULL);
62   g_return_val_if_fail (impl != NULL, NULL);
63   g_return_val_if_fail (size != 0, NULL);
64
65   info = g_slice_new (GstMetaInfo);
66   info->api = g_quark_from_string (api);
67   info->type = g_pointer_type_register_static (impl);
68   info->size = size;
69   info->init_func = init_func;
70   info->free_func = free_func;
71   info->copy_func = copy_func;
72   info->transform_func = transform_func;
73
74   GST_DEBUG ("register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT,
75       api, impl, size);
76
77   g_static_rw_lock_writer_lock (&lock);
78   g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
79   g_static_rw_lock_writer_unlock (&lock);
80
81   return info;
82 }
83
84 /**
85  * gst_meta_get_info:
86  * @impl: the name
87  *
88  * Lookup a previously registered meta info structure by its implementor name
89  * @impl.
90  *
91  * Returns: a #GstMetaInfo with @impl or #NULL when no such metainfo
92  * exists.
93  */
94 const GstMetaInfo *
95 gst_meta_get_info (const gchar * impl)
96 {
97   GstMetaInfo *info;
98
99   g_return_val_if_fail (impl != NULL, NULL);
100
101   g_static_rw_lock_reader_lock (&lock);
102   info = g_hash_table_lookup (metainfo, impl);
103   g_static_rw_lock_reader_unlock (&lock);
104
105   return info;
106 }
107
108 /* Timing metadata */
109 static void
110 meta_timing_copy (GstBuffer * copybuf, GstMetaTiming * meta,
111     GstBuffer * buffer, gsize offset, gsize size)
112 {
113   GstMetaTiming *timing;
114
115   GST_DEBUG ("trans called from buffer %p to %p, meta %p,"
116       "offset %" G_GSIZE_FORMAT ", size %" G_GSIZE_FORMAT, buffer,
117       copybuf, meta, offset, size);
118
119   timing = gst_buffer_add_meta_timing (copybuf);
120   if (offset == 0) {
121     /* same offset, copy timestamps */
122     timing->pts = meta->pts;
123     timing->dts = meta->dts;
124     if (size == gst_buffer_get_size (buffer)) {
125       /* same size, copy duration */
126       timing->duration = meta->duration;
127     } else {
128       /* else clear */
129       timing->duration = GST_CLOCK_TIME_NONE;
130     }
131   } else {
132     timing->pts = -1;
133     timing->dts = -1;
134     timing->duration = -1;
135   }
136   timing->clock_rate = meta->clock_rate;
137 }
138
139 const GstMetaInfo *
140 gst_meta_timing_get_info (void)
141 {
142   static const GstMetaInfo *meta_info = NULL;
143
144   if (meta_info == NULL) {
145     meta_info = gst_meta_register ("GstMetaTiming", "GstMetaTiming",
146         sizeof (GstMetaTiming),
147         (GstMetaInitFunction) NULL,
148         (GstMetaFreeFunction) NULL,
149         (GstMetaCopyFunction) meta_timing_copy,
150         (GstMetaTransformFunction) NULL);
151   }
152   return meta_info;
153 }