2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 //#define DEBUG_ENABLED
23 #include <gst/gstcapsprivate.h>
25 static gboolean gst_caps_entry_check_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2);
29 _gst_caps_initialize (void)
34 gst_caps_create_entry (GstCapsFactory factory, gint *skipped)
36 GstCapsFactoryEntry tag;
40 entry = g_new0 (GstCapsEntry, 1);
43 switch (GPOINTER_TO_INT (tag)) {
45 entry->capstype = GST_CAPS_INT_ID_NUM;
46 entry->data.int_data = GPOINTER_TO_INT (factory[i++]);
48 case GST_CAPS_INT_RANGE_ID:
49 entry->capstype = GST_CAPS_INT_RANGE_ID_NUM;
50 entry->data.int_range_data.min = GPOINTER_TO_INT (factory[i++]);
51 entry->data.int_range_data.max = GPOINTER_TO_INT (factory[i++]);
53 case GST_CAPS_FOURCC_ID:
54 entry->capstype = GST_CAPS_FOURCC_ID_NUM;
55 entry->data.fourcc_data = GPOINTER_TO_INT (factory[i++]);
57 case GST_CAPS_LIST_ID:
58 g_print("gstcaps: list not allowed in list\n");
61 g_print("gstcaps: unknown caps id found\n");
74 caps_compare_func (gconstpointer a,
77 GstCapsEntry *entry1 = (GstCapsEntry *)a;
78 GstCapsEntry *entry2 = (GstCapsEntry *)b;
80 return (entry1->propid - entry2->propid);
85 * @factory: the factory to register
87 * Register the factory.
89 * Returns: The registered capability
92 gst_caps_register (GstCapsFactory factory)
94 GstCapsFactoryEntry tag;
100 g_return_val_if_fail (factory != NULL, NULL);
104 g_return_val_if_fail (tag != NULL, NULL);
106 typeid = gst_type_find_by_mime ((gchar *)tag);
108 GstTypeFactory *factory = g_new0 (GstTypeFactory, 1);
110 factory->mime = g_strdup ((gchar *)tag);
111 factory->exts = NULL;
112 factory->typefindfunc = NULL;
114 typeid = gst_type_register (factory);
117 caps = g_new0 (GstCaps, 1);
118 g_return_val_if_fail (caps != NULL, NULL);
121 caps->properties = NULL;
129 quark = g_quark_from_string ((gchar *)tag);
132 switch (GPOINTER_TO_INT (tag)) {
133 case GST_CAPS_LIST_ID:
135 GstCapsEntry *list_entry;
137 entry = g_new0 (GstCapsEntry, 1);
138 entry->propid = quark;
139 entry->capstype = GST_CAPS_LIST_ID_NUM;
140 entry->data.list_data.entries = NULL;
142 i++; // skip list tag
145 list_entry = gst_caps_create_entry (&factory[i], &skipped);
146 list_entry->propid = quark;
149 entry->data.list_data.entries = g_list_prepend (entry->data.list_data.entries, list_entry);
151 entry->data.list_data.entries = g_list_reverse (entry->data.list_data.entries);
152 i++; //skip NULL (list end)
157 entry = gst_caps_create_entry (&factory[i], &skipped);
158 entry->propid = quark;
163 caps->properties = g_slist_insert_sorted (caps->properties, entry, caps_compare_func);
172 gst_caps_dump_entry_func (GstCapsEntry *entry)
174 switch (entry->capstype) {
175 case GST_CAPS_INT_ID_NUM:
176 g_print("gstcaps: int %d\n", entry->data.int_data);
178 case GST_CAPS_INT_RANGE_ID_NUM:
179 g_print("gstcaps: int range %d %d\n",
180 entry->data.int_range_data.min,
181 entry->data.int_range_data.max);
183 case GST_CAPS_FOURCC_ID_NUM:
184 g_print("gstcaps: fourcc 0x%08x (%4.4s)\n", entry->data.fourcc_data, &entry->data.fourcc_data);
186 case GST_CAPS_BOOL_ID_NUM:
187 g_print("gstcaps: boolean %d\n", entry->data.bool_data);
190 g_print("gstcaps: **illegal entry**\n");
196 gst_caps_dump_list_func (gpointer entry,
199 gst_caps_dump_entry_func ((GstCapsEntry *)entry);
203 gst_caps_dump_func (gpointer data,
208 entry = (GstCapsEntry *)data;
210 g_print("gstcaps: property type \"%s\"\n", g_quark_to_string (entry->propid));
212 switch (entry->capstype) {
213 case GST_CAPS_LIST_ID_NUM:
215 g_print("gstcaps: list type (\n");
216 g_list_foreach (entry->data.list_data.entries, gst_caps_dump_list_func, entry);
217 g_print("gstcaps: )\n");
221 gst_caps_dump_entry_func (entry);
228 * @caps: the capability to dump
230 * Dumps the contents of the capabilty one the console
233 gst_caps_dump (GstCaps *caps)
235 g_return_if_fail (caps != NULL);
237 g_print("gstcaps: {\ngstcaps: mime type \"%d\"\n", caps->id);
239 g_slist_foreach (caps->properties, gst_caps_dump_func, caps);
240 g_print("gstcaps: }\n");
243 /* entry2 is always a list, entry1 never is */
245 gst_caps_entry_check_list_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2)
247 GList *entrylist = entry2->data.list_data.entries;
248 gboolean found = FALSE;
250 while (entrylist && !found) {
251 GstCapsEntry *entry = (GstCapsEntry *) entrylist->data;
253 found |= gst_caps_entry_check_compatibility (entry1, entry);
255 entrylist = g_list_next (entrylist);
262 gst_caps_entry_check_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2)
264 DEBUG ("compare: %s %s\n", g_quark_to_string (entry1->propid),
265 g_quark_to_string (entry2->propid));
266 switch (entry1->capstype) {
267 case GST_CAPS_LIST_ID_NUM:
269 GList *entrylist = entry1->data.list_data.entries;
270 gboolean valid = TRUE; // innocent until proven guilty
272 while (entrylist && valid) {
273 GstCapsEntry *entry = (GstCapsEntry *) entrylist->data;
275 valid &= gst_caps_entry_check_compatibility (entry, entry2);
277 entrylist = g_list_next (entrylist);
282 case GST_CAPS_INT_RANGE_ID_NUM:
283 switch (entry2->capstype) {
285 case GST_CAPS_INT_RANGE_ID_NUM:
286 return (entry2->data.int_range_data.min <= entry1->data.int_range_data.min &&
287 entry2->data.int_range_data.max >= entry1->data.int_range_data.max);
288 case GST_CAPS_LIST_ID_NUM:
289 return gst_caps_entry_check_list_compatibility (entry1, entry2);
294 case GST_CAPS_FOURCC_ID_NUM:
295 switch (entry2->capstype) {
297 case GST_CAPS_FOURCC_ID_NUM:
298 return (entry2->data.fourcc_data == entry1->data.fourcc_data);
300 case GST_CAPS_LIST_ID_NUM:
301 return gst_caps_entry_check_list_compatibility (entry1, entry2);
306 case GST_CAPS_INT_ID_NUM:
307 switch (entry2->capstype) {
309 case GST_CAPS_INT_RANGE_ID_NUM:
310 return (entry2->data.int_range_data.min <= entry1->data.int_data &&
311 entry2->data.int_range_data.max >= entry1->data.int_data);
313 case GST_CAPS_INT_ID_NUM:
314 return (entry2->data.int_data == entry1->data.int_data);
316 case GST_CAPS_LIST_ID_NUM:
317 return gst_caps_entry_check_list_compatibility (entry1, entry2);
322 case GST_CAPS_BOOL_ID_NUM:
323 switch (entry2->capstype) {
325 case GST_CAPS_BOOL_ID_NUM:
326 return (entry2->data.bool_data == entry1->data.bool_data);
327 case GST_CAPS_LIST_ID_NUM:
328 return gst_caps_entry_check_list_compatibility (entry1, entry2);
340 * gst_caps_check_compatibility:
341 * @fromcaps: a capabilty
342 * @tocaps: a capabilty
344 * Checks whether two capabilities are compatible
346 * Returns: true if compatible, false otherwise
349 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
355 gboolean compatible = TRUE;
357 g_return_val_if_fail (fromcaps != NULL, FALSE);
358 g_return_val_if_fail (tocaps != NULL, FALSE);
360 if (fromcaps->id != tocaps->id)
363 sourcelist = fromcaps->properties;
364 sinklist = tocaps->properties;
366 while (sourcelist && sinklist && compatible) {
367 GstCapsEntry *entry1;
368 GstCapsEntry *entry2;
370 entry1 = (GstCapsEntry *)sourcelist->data;
371 entry2 = (GstCapsEntry *)sinklist->data;
373 while (entry1->propid < entry2->propid) {
374 DEBUG ("source is more specific in \"%s\"\n", g_quark_to_string (entry1->propid));
376 sourcelist = g_slist_next (sourcelist);
377 if (sourcelist) entry1 = (GstCapsEntry *)sourcelist->data;
380 while (entry1->propid > entry2->propid) {
381 DEBUG ("source has missing property \"%s\"\n", g_quark_to_string (entry2->propid));
383 sinklist = g_slist_next (sinklist);
384 if (sinklist) entry2 = (GstCapsEntry *)sinklist->data;
388 compatible &= gst_caps_entry_check_compatibility (entry1, entry2);
390 sourcelist = g_slist_next (sourcelist);
391 sinklist = g_slist_next (sinklist);
402 * gst_caps_register_va:
403 * @factory: the factories to register
405 * Register the given factories.
407 * Returns: A list of the registered factories
410 gst_caps_register_va (GstCapsFactory factory, ...)
413 GstCapsFactoryEntry *current_factory;
415 va_start (var_args, factory);
417 current_factory = (GstCapsFactoryEntry *) factory;
419 while (current_factory) {
420 gst_caps_register (current_factory);
422 current_factory = va_arg (var_args, GstCapsFactoryEntry *);