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
36 #include "gst_private.h"
37 #include "gstformat.h"
39 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
40 static GList *_gst_formats = NULL;
41 static GHashTable *_nick_to_format = NULL;
42 static GHashTable *_format_to_nick = NULL;
43 static guint32 _n_values = 1; /* we start from 1 because 0 reserved for UNDEFINED */
45 static GstFormatDefinition standard_definitions[] = {
46 {GST_FORMAT_DEFAULT, "default", "Default format for the media type", 0},
47 {GST_FORMAT_BYTES, "bytes", "Bytes", 0},
48 {GST_FORMAT_TIME, "time", "Time", 0},
49 {GST_FORMAT_BUFFERS, "buffers", "Buffers", 0},
50 {GST_FORMAT_PERCENT, "percent", "Percent", 0},
55 _gst_format_initialize (void)
57 GstFormatDefinition *standards = standard_definitions;
59 g_static_mutex_lock (&mutex);
60 if (_nick_to_format == NULL) {
61 _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
62 _format_to_nick = g_hash_table_new (NULL, NULL);
65 while (standards->nick) {
66 standards->quark = g_quark_from_static_string (standards->nick);
67 g_hash_table_insert (_nick_to_format, standards->nick, standards);
68 g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
71 _gst_formats = g_list_append (_gst_formats, standards);
75 g_static_mutex_unlock (&mutex);
79 * gst_format_get_name:
80 * @format: a #GstFormat
82 * Get a printable name for the given format. Do not modify or free.
84 * Returns: a reference to the static name of the format or NULL if
85 * the format is unknown.
88 gst_format_get_name (GstFormat format)
90 const GstFormatDefinition *def;
93 if ((def = gst_format_get_details (format)) != NULL)
102 * gst_format_to_quark:
103 * @format: a #GstFormat
105 * Get the unique quark for the given format.
107 * Returns: the quark associated with the format or 0 if the format
111 gst_format_to_quark (GstFormat format)
113 const GstFormatDefinition *def;
116 if ((def = gst_format_get_details (format)) != NULL)
125 * gst_format_register:
126 * @nick: The nick of the new format
127 * @description: The description of the new format
129 * Create a new GstFormat based on the nick or return an
130 * already registered format with that nick.
132 * Returns: A new GstFormat or an already registered format
133 * with the same nick.
138 gst_format_register (const gchar * nick, const gchar * description)
140 GstFormatDefinition *format;
143 g_return_val_if_fail (nick != NULL, 0);
144 g_return_val_if_fail (description != NULL, 0);
146 query = gst_format_get_by_nick (nick);
147 if (query != GST_FORMAT_UNDEFINED)
150 format = g_new0 (GstFormatDefinition, 1);
151 format->value = _n_values;
152 format->nick = g_strdup (nick);
153 format->description = g_strdup (description);
154 format->quark = g_quark_from_static_string (format->nick);
156 g_static_mutex_lock (&mutex);
157 g_hash_table_insert (_nick_to_format, format->nick, format);
158 g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
160 _gst_formats = g_list_append (_gst_formats, format);
162 g_static_mutex_unlock (&mutex);
164 return format->value;
168 * gst_format_get_by_nick:
169 * @nick: The nick of the format
171 * Return the format registered with the given nick.
173 * Returns: The format with @nick or GST_FORMAT_UNDEFINED
174 * if the format was not registered.
177 gst_format_get_by_nick (const gchar * nick)
179 GstFormatDefinition *format;
181 g_return_val_if_fail (nick != NULL, 0);
183 g_static_mutex_lock (&mutex);
184 format = g_hash_table_lookup (_nick_to_format, nick);
185 g_static_mutex_unlock (&mutex);
188 return format->value;
190 return GST_FORMAT_UNDEFINED;
194 * gst_formats_contains:
195 * @formats: The format array to search
196 * @format: the format to find
198 * See if the given format is inside the format array.
200 * Returns: TRUE if the format is found inside the array
203 gst_formats_contains (const GstFormat * formats, GstFormat format)
209 if (*formats == format)
219 * gst_format_get_details:
220 * @format: The format to get details of
222 * Get details about the given format.
224 * Returns: The #GstFormatDefinition for @format or NULL on failure.
228 const GstFormatDefinition *
229 gst_format_get_details (GstFormat format)
231 const GstFormatDefinition *result;
233 g_static_mutex_lock (&mutex);
234 result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
235 g_static_mutex_unlock (&mutex);
241 * gst_format_iterate_definitions:
243 * Iterate all the registered formats. The format definition is read
246 * Returns: A GstIterator of #GstFormatDefinition.
249 gst_format_iterate_definitions (void)
253 g_static_mutex_lock (&mutex);
254 /* FIXME: register a boxed type for GstFormatDefinition */
255 result = gst_iterator_new_list (G_TYPE_POINTER,
256 g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_formats,
258 g_static_mutex_unlock (&mutex);