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>
6 * gstformat.c: GstFormat registration
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.
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.
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.
26 * @short_description: Dynamically register new data formats
27 * @see_also: #GstPad, #GstElement
29 * GstFormats functions are used to register a new format to the gstreamer
30 * core. Formats can be used to perform seeking or conversions/query
35 #include "gst_private.h"
37 #include "gstformat.h"
38 #include "gstenumtypes.h"
40 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
41 static GList *_gst_formats = NULL;
42 static GHashTable *_nick_to_format = NULL;
43 static GHashTable *_format_to_nick = NULL;
44 static guint32 _n_values = 1; /* we start from 1 because 0 reserved for UNDEFINED */
46 static GstFormatDefinition standard_definitions[] = {
47 {GST_FORMAT_DEFAULT, "default", "Default format for the media type", 0},
48 {GST_FORMAT_BYTES, "bytes", "Bytes", 0},
49 {GST_FORMAT_TIME, "time", "Time", 0},
50 {GST_FORMAT_BUFFERS, "buffers", "Buffers", 0},
51 {GST_FORMAT_PERCENT, "percent", "Percent", 0},
52 {GST_FORMAT_UNDEFINED, NULL, NULL, 0}
56 _priv_gst_format_initialize (void)
58 GstFormatDefinition *standards = standard_definitions;
60 g_static_mutex_lock (&mutex);
61 if (_nick_to_format == NULL) {
62 _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
63 _format_to_nick = g_hash_table_new (NULL, NULL);
66 while (standards->nick) {
67 standards->quark = g_quark_from_static_string (standards->nick);
68 g_hash_table_insert (_nick_to_format, (gpointer) standards->nick,
70 g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
73 _gst_formats = g_list_append (_gst_formats, standards);
77 /* getting the type registers the enum */
78 g_type_class_ref (gst_format_get_type ());
79 g_static_mutex_unlock (&mutex);
83 * gst_format_get_name:
84 * @format: a #GstFormat
86 * Get a printable name for the given format. Do not modify or free.
88 * Returns: a reference to the static name of the format or NULL if
89 * the format is unknown.
92 gst_format_get_name (GstFormat format)
94 const GstFormatDefinition *def;
97 if ((def = gst_format_get_details (format)) != NULL)
106 * gst_format_to_quark:
107 * @format: a #GstFormat
109 * Get the unique quark for the given format.
111 * Returns: the quark associated with the format or 0 if the format
115 gst_format_to_quark (GstFormat format)
117 const GstFormatDefinition *def;
120 if ((def = gst_format_get_details (format)) != NULL)
129 * gst_format_register:
130 * @nick: The nick of the new format
131 * @description: The description of the new format
133 * Create a new GstFormat based on the nick or return an
134 * already registered format with that nick.
136 * Returns: A new GstFormat or an already registered format
137 * with the same nick.
142 gst_format_register (const gchar * nick, const gchar * description)
144 GstFormatDefinition *format;
147 g_return_val_if_fail (nick != NULL, GST_FORMAT_UNDEFINED);
148 g_return_val_if_fail (description != NULL, GST_FORMAT_UNDEFINED);
150 query = gst_format_get_by_nick (nick);
151 if (query != GST_FORMAT_UNDEFINED)
154 g_static_mutex_lock (&mutex);
155 format = g_slice_new (GstFormatDefinition);
156 format->value = (GstFormat) _n_values;
157 format->nick = g_strdup (nick);
158 format->description = g_strdup (description);
159 format->quark = g_quark_from_static_string (format->nick);
161 g_hash_table_insert (_nick_to_format, (gpointer) format->nick, format);
162 g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
164 _gst_formats = g_list_append (_gst_formats, format);
166 g_static_mutex_unlock (&mutex);
168 return format->value;
172 * gst_format_get_by_nick:
173 * @nick: The nick of the format
175 * Return the format registered with the given nick.
177 * Returns: The format with @nick or GST_FORMAT_UNDEFINED
178 * if the format was not registered.
181 gst_format_get_by_nick (const gchar * nick)
183 GstFormatDefinition *format;
185 g_return_val_if_fail (nick != NULL, GST_FORMAT_UNDEFINED);
187 g_static_mutex_lock (&mutex);
188 format = g_hash_table_lookup (_nick_to_format, nick);
189 g_static_mutex_unlock (&mutex);
192 return format->value;
194 return GST_FORMAT_UNDEFINED;
198 * gst_formats_contains:
199 * @formats: The format array to search
200 * @format: the format to find
202 * See if the given format is inside the format array.
204 * Returns: TRUE if the format is found inside the array
207 gst_formats_contains (const GstFormat * formats, GstFormat format)
213 if (*formats == format)
223 * gst_format_get_details:
224 * @format: The format to get details of
226 * Get details about the given format.
228 * Returns: The #GstFormatDefinition for @format or NULL on failure.
232 const GstFormatDefinition *
233 gst_format_get_details (GstFormat format)
235 const GstFormatDefinition *result;
237 g_static_mutex_lock (&mutex);
238 result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
239 g_static_mutex_unlock (&mutex);
245 * gst_format_iterate_definitions:
247 * Iterate all the registered formats. The format definition is read
250 * Returns: (transfer full): a GstIterator of #GstFormatDefinition.
253 gst_format_iterate_definitions (void)
257 g_static_mutex_lock (&mutex);
258 /* FIXME: register a boxed type for GstFormatDefinition */
259 result = gst_iterator_new_list (G_TYPE_POINTER,
260 g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_formats, NULL, NULL);
261 g_static_mutex_unlock (&mutex);