don't break docs build
[platform/upstream/gstreamer.git] / gst / gstformat.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstformat.c: GstFormat registration
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:gstformat
25  * @short_description: Dynamically register new data formats
26  * @see_also: #GstPad, #GstElement
27  *
28  * GstFormats functions are used to register a new format to the gstreamer core.
29  * Formats can be used to perform seeking or conversions/query operations.
30  */
31
32 #include <string.h>
33
34 #include "gst_private.h"
35 #include "gstformat.h"
36
37 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
38 static GList *_gst_formats = NULL;
39 static GHashTable *_nick_to_format = NULL;
40 static GHashTable *_format_to_nick = NULL;
41 static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for UNDEFINED */
42
43 static GstFormatDefinition standard_definitions[] = {
44   {GST_FORMAT_DEFAULT, "default", "Default format for the media type"},
45   {GST_FORMAT_BYTES, "bytes", "Bytes"},
46   {GST_FORMAT_TIME, "time", "Time"},
47   {GST_FORMAT_BUFFERS, "buffers", "Buffers"},
48   {GST_FORMAT_PERCENT, "percent", "Percent"},
49   {0, NULL, NULL}
50 };
51
52 void
53 _gst_format_initialize (void)
54 {
55   GstFormatDefinition *standards = standard_definitions;
56
57   g_static_mutex_lock (&mutex);
58   if (_nick_to_format == NULL) {
59     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
60     _format_to_nick = g_hash_table_new (NULL, NULL);
61   }
62
63   while (standards->nick) {
64     g_hash_table_insert (_nick_to_format, standards->nick, standards);
65     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
66         standards);
67
68     _gst_formats = g_list_append (_gst_formats, standards);
69     standards++;
70     _n_values++;
71   }
72   g_static_mutex_unlock (&mutex);
73 }
74
75 /**
76  * gst_format_register:
77  * @nick: The nick of the new format
78  * @description: The description of the new format
79  *
80  * Create a new GstFormat based on the nick or return an
81  * already registered format with that nick.
82  *
83  * Returns: A new GstFormat or an already registered format
84  * with the same nick.
85  *
86  * MT safe.
87  */
88 GstFormat
89 gst_format_register (const gchar * nick, const gchar * description)
90 {
91   GstFormatDefinition *format;
92   GstFormat query;
93
94   g_return_val_if_fail (nick != NULL, 0);
95   g_return_val_if_fail (description != NULL, 0);
96
97   query = gst_format_get_by_nick (nick);
98   if (query != GST_FORMAT_UNDEFINED)
99     return query;
100
101   format = g_new0 (GstFormatDefinition, 1);
102   format->value = _n_values;
103   format->nick = g_strdup (nick);
104   format->description = g_strdup (description);
105
106   g_static_mutex_lock (&mutex);
107   g_hash_table_insert (_nick_to_format, format->nick, format);
108   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
109       format);
110   _gst_formats = g_list_append (_gst_formats, format);
111   _n_values++;
112   g_static_mutex_unlock (&mutex);
113
114   return format->value;
115 }
116
117 /**
118  * gst_format_get_by_nick:
119  * @nick: The nick of the format
120  *
121  * Return the format registered with the given nick. 
122  *
123  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
124  * if the format was not registered.
125  */
126 GstFormat
127 gst_format_get_by_nick (const gchar * nick)
128 {
129   GstFormatDefinition *format;
130
131   g_return_val_if_fail (nick != NULL, 0);
132
133   g_static_mutex_lock (&mutex);
134   format = g_hash_table_lookup (_nick_to_format, nick);
135   g_static_mutex_unlock (&mutex);
136
137   if (format != NULL)
138     return format->value;
139   else
140     return GST_FORMAT_UNDEFINED;
141 }
142
143 /**
144  * gst_formats_contains:
145  * @formats: The format array to search
146  * @format: the format to find
147  *
148  * See if the given format is inside the format array.
149  *
150  * Returns: TRUE if the format is found inside the array
151  */
152 gboolean
153 gst_formats_contains (const GstFormat * formats, GstFormat format)
154 {
155   if (!formats)
156     return FALSE;
157
158   while (*formats) {
159     if (*formats == format)
160       return TRUE;
161
162     formats++;
163   }
164   return FALSE;
165 }
166
167
168 /**
169  * gst_format_get_details:
170  * @format: The format to get details of
171  *
172  * Get details about the given format.
173  *
174  * Returns: The #GstFormatDefinition for @format or NULL on failure.
175  *
176  * MT safe.
177  */
178 const GstFormatDefinition *
179 gst_format_get_details (GstFormat format)
180 {
181   const GstFormatDefinition *result;
182
183   g_static_mutex_lock (&mutex);
184   result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
185   g_static_mutex_unlock (&mutex);
186
187   return result;
188 }
189
190 /**
191  * gst_format_iterate_definitions:
192  *
193  * Iterate all the registered formats. The format definition is read 
194  * only.
195  *
196  * Returns: A GstIterator of #GstFormatDefinition.
197  */
198 GstIterator *
199 gst_format_iterate_definitions (void)
200 {
201   GstIterator *result;
202
203   g_static_mutex_lock (&mutex);
204   result = gst_iterator_new_list (g_static_mutex_get_mutex (&mutex),
205       &_n_values, &_gst_formats, NULL, NULL, NULL);
206   g_static_mutex_unlock (&mutex);
207
208   return result;
209 }