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.
25 * @short_description: Dynamically register new data formats
26 * @see_also: #GstPad, #GstElement
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.
34 #include "gst_private.h"
35 #include "gstformat.h"
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 */
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"},
53 _gst_format_initialize (void)
55 GstFormatDefinition *standards = standard_definitions;
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);
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),
68 _gst_formats = g_list_append (_gst_formats, standards);
72 g_static_mutex_unlock (&mutex);
76 * gst_format_register:
77 * @nick: The nick of the new format
78 * @description: The description of the new format
80 * Create a new GstFormat based on the nick or return an
81 * already registered format with that nick.
83 * Returns: A new GstFormat or an already registered format
89 gst_format_register (const gchar * nick, const gchar * description)
91 GstFormatDefinition *format;
94 g_return_val_if_fail (nick != NULL, 0);
95 g_return_val_if_fail (description != NULL, 0);
97 query = gst_format_get_by_nick (nick);
98 if (query != GST_FORMAT_UNDEFINED)
101 format = g_new0 (GstFormatDefinition, 1);
102 format->value = _n_values;
103 format->nick = g_strdup (nick);
104 format->description = g_strdup (description);
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),
110 _gst_formats = g_list_append (_gst_formats, format);
112 g_static_mutex_unlock (&mutex);
114 return format->value;
118 * gst_format_get_by_nick:
119 * @nick: The nick of the format
121 * Return the format registered with the given nick.
123 * Returns: The format with @nick or GST_FORMAT_UNDEFINED
124 * if the format was not registered.
127 gst_format_get_by_nick (const gchar * nick)
129 GstFormatDefinition *format;
131 g_return_val_if_fail (nick != NULL, 0);
133 g_static_mutex_lock (&mutex);
134 format = g_hash_table_lookup (_nick_to_format, nick);
135 g_static_mutex_unlock (&mutex);
138 return format->value;
140 return GST_FORMAT_UNDEFINED;
144 * gst_formats_contains:
145 * @formats: The format array to search
146 * @format: the format to find
148 * See if the given format is inside the format array.
150 * Returns: TRUE if the format is found inside the array
153 gst_formats_contains (const GstFormat * formats, GstFormat format)
159 if (*formats == format)
169 * gst_format_get_details:
170 * @format: The format to get details of
172 * Get details about the given format.
174 * Returns: The #GstFormatDefinition for @format or NULL on failure.
178 const GstFormatDefinition *
179 gst_format_get_details (GstFormat format)
181 const GstFormatDefinition *result;
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);
191 * gst_format_iterate_definitions:
193 * Iterate all the registered formats. The format definition is read
196 * Returns: A GstIterator of #GstFormatDefinition.
199 gst_format_iterate_definitions (void)
203 g_static_mutex_lock (&mutex);
204 /* FIXME: register a boxed type for GstFormatDefinition */
205 result = gst_iterator_new_list (G_TYPE_POINTER,
206 g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_formats,
208 g_static_mutex_unlock (&mutex);