- moved functionality of GST_FORMAT_UNITS to GST_FORMAT_DEFAULT, the old _DEFAULT...
[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  *
5  * gstformat.c: GstFormat registration
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <string.h>
24
25 #include "gstlog.h"
26
27 #include "gstformat.h"
28
29 static GList *_gst_formats = NULL;
30 static GHashTable *_nick_to_format = NULL;
31 static GHashTable *_format_to_nick = NULL;
32 static gint  _n_values = 1; /* we start from 1 because 0 reserved for UNDEFINED */
33
34 static GstFormatDefinition standard_definitions[] = {
35   { GST_FORMAT_DEFAULT, "default", "Default format for the media type" },
36   { GST_FORMAT_BYTES,   "bytes",   "Bytes" },
37   { GST_FORMAT_TIME,    "time",    "Time" }, 
38   { GST_FORMAT_BUFFERS, "buffers", "Buffers" },
39   { GST_FORMAT_PERCENT, "percent", "Percent" },
40   { 0, NULL, NULL }
41 };
42
43 void            
44 _gst_format_initialize (void)
45 {
46   GstFormatDefinition *standards = standard_definitions;
47
48   if (_nick_to_format == NULL) {
49     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
50     _format_to_nick = g_hash_table_new (NULL, NULL);
51   }
52   
53   while (standards->nick) {
54     g_hash_table_insert (_nick_to_format, standards->nick, standards);
55     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value), standards);
56
57     _gst_formats = g_list_append (_gst_formats, standards);
58     standards++;
59     _n_values++;
60   }
61 }
62
63 /**
64  * gst_format_register:
65  * @nick: The nick of the new format
66  * @description: The description of the new format
67  *
68  * Create a new GstFormat based on the nick or return an
69  * allrady registered format with that nick
70  *
71  * Returns: A new GstFormat or an already registered format
72  * with the same nick.
73  */
74 GstFormat
75 gst_format_register (const gchar *nick, const gchar *description)
76 {
77   GstFormatDefinition *format;
78   GstFormat query;
79   
80   g_return_val_if_fail (nick != NULL, 0);
81   g_return_val_if_fail (description != NULL, 0);
82
83   query = gst_format_get_by_nick (nick);
84   if (query != GST_FORMAT_UNDEFINED)
85     return query;
86   
87   format = g_new0 (GstFormatDefinition, 1);
88   format->value = _n_values;
89   format->nick = g_strdup (nick);
90   format->description = g_strdup (description);
91
92   g_hash_table_insert (_nick_to_format, format->nick, format);
93   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value), format);
94   _gst_formats = g_list_append (_gst_formats, format);
95   _n_values++;
96   
97   return format->value;
98 }
99
100 /**
101  * gst_format_get_by_nick:
102  * @nick: The nick of the format
103  *
104  * Return the format registered with the given nick. 
105  *
106  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
107  * if the format was not registered.
108  */
109 GstFormat
110 gst_format_get_by_nick (const gchar *nick)
111 {
112   GstFormatDefinition *format;
113   
114   g_return_val_if_fail (nick != NULL, 0);
115
116   format = g_hash_table_lookup (_nick_to_format, nick);
117   
118   if (format != NULL)
119     return format->value;
120   else
121     return GST_FORMAT_UNDEFINED;
122 }
123
124 /**
125  * gst_formats_contains:
126  * @formats: The format array to search
127  * @format: the format to find
128  *
129  * See if the given format is inside the format array.
130  *
131  * Returns: TRUE if the format is found inside the array
132  */
133 gboolean
134 gst_formats_contains (const GstFormat *formats, GstFormat format)
135 {
136   if (!formats)
137     return FALSE;
138
139   while (*formats) {
140     if (*formats == format)
141       return TRUE;
142
143     formats++;
144   }
145   return FALSE;
146 }
147
148
149 /**
150  * gst_format_get_details:
151  * @format: The format to get details of
152  *
153  * Get details about the given format.
154  *
155  * Returns: The #GstFormatDefinition for @format or NULL on failure.
156  */
157 const GstFormatDefinition*
158 gst_format_get_details (GstFormat format)
159 {
160   return g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
161 }
162
163 /**
164  * gst_format_get_definitions:
165  *
166  * Get a list of all the registered formats.
167  *
168  * Returns: A GList of #GstFormatDefinition.
169  */
170 const GList*
171 gst_format_get_definitions (void)
172 {
173   return _gst_formats;
174 }