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");
60 case GST_CAPS_BOOL_ID:
61 entry->capstype = GST_CAPS_BOOL_ID_NUM;
62 entry->data.bool_data = GPOINTER_TO_INT (factory[i++]);
65 g_print("gstcaps: unknown caps id found\n");
78 caps_compare_func (gconstpointer a,
81 GstCapsEntry *entry1 = (GstCapsEntry *)a;
82 GstCapsEntry *entry2 = (GstCapsEntry *)b;
84 return (entry1->propid - entry2->propid);
89 * @factory: the factory to register
91 * Register the factory.
93 * Returns: The registered capability
96 gst_caps_register (GstCapsFactory factory)
98 GstCapsFactoryEntry tag;
104 g_return_val_if_fail (factory != NULL, NULL);
108 g_return_val_if_fail (tag != NULL, NULL);
110 typeid = gst_type_find_by_mime ((gchar *)tag);
112 GstTypeFactory *factory = g_new0 (GstTypeFactory, 1);
114 factory->mime = g_strdup ((gchar *)tag);
115 factory->exts = NULL;
116 factory->typefindfunc = NULL;
118 typeid = gst_type_register (factory);
121 caps = g_new0 (GstCaps, 1);
122 g_return_val_if_fail (caps != NULL, NULL);
125 caps->properties = NULL;
133 quark = g_quark_from_string ((gchar *)tag);
136 switch (GPOINTER_TO_INT (tag)) {
137 case GST_CAPS_LIST_ID:
139 GstCapsEntry *list_entry;
141 entry = g_new0 (GstCapsEntry, 1);
142 entry->propid = quark;
143 entry->capstype = GST_CAPS_LIST_ID_NUM;
144 entry->data.list_data.entries = NULL;
146 i++; // skip list tag
149 list_entry = gst_caps_create_entry (&factory[i], &skipped);
150 list_entry->propid = quark;
153 entry->data.list_data.entries = g_list_prepend (entry->data.list_data.entries, list_entry);
155 entry->data.list_data.entries = g_list_reverse (entry->data.list_data.entries);
156 i++; //skip NULL (list end)
161 entry = gst_caps_create_entry (&factory[i], &skipped);
162 entry->propid = quark;
167 caps->properties = g_slist_insert_sorted (caps->properties, entry, caps_compare_func);
176 gst_caps_dump_entry_func (GstCapsEntry *entry)
178 switch (entry->capstype) {
179 case GST_CAPS_INT_ID_NUM:
180 g_print("gstcaps: int %d\n", entry->data.int_data);
182 case GST_CAPS_INT_RANGE_ID_NUM:
183 g_print("gstcaps: int range %d %d\n",
184 entry->data.int_range_data.min,
185 entry->data.int_range_data.max);
187 case GST_CAPS_FOURCC_ID_NUM:
188 g_print("gstcaps: fourcc 0x%08x (%4.4s)\n", entry->data.fourcc_data, (gchar *)&entry->data.fourcc_data);
190 case GST_CAPS_BOOL_ID_NUM:
191 g_print("gstcaps: boolean %d\n", entry->data.bool_data);
194 g_print("gstcaps: **illegal entry**\n");
200 gst_caps_dump_list_func (gpointer entry,
203 gst_caps_dump_entry_func ((GstCapsEntry *)entry);
207 gst_caps_dump_func (gpointer data,
212 entry = (GstCapsEntry *)data;
214 g_print("gstcaps: property type \"%s\"\n", g_quark_to_string (entry->propid));
216 switch (entry->capstype) {
217 case GST_CAPS_LIST_ID_NUM:
219 g_print("gstcaps: list type (\n");
220 g_list_foreach (entry->data.list_data.entries, gst_caps_dump_list_func, entry);
221 g_print("gstcaps: )\n");
225 gst_caps_dump_entry_func (entry);
232 * @caps: the capability to dump
234 * Dumps the contents of the capabilty one the console
237 gst_caps_dump (GstCaps *caps)
239 g_return_if_fail (caps != NULL);
241 g_print("gstcaps: {\ngstcaps: mime type \"%d\"\n", caps->id);
243 g_slist_foreach (caps->properties, gst_caps_dump_func, caps);
244 g_print("gstcaps: }\n");
247 /* entry2 is always a list, entry1 never is */
249 gst_caps_entry_check_list_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2)
251 GList *entrylist = entry2->data.list_data.entries;
252 gboolean found = FALSE;
254 while (entrylist && !found) {
255 GstCapsEntry *entry = (GstCapsEntry *) entrylist->data;
257 found |= gst_caps_entry_check_compatibility (entry1, entry);
259 entrylist = g_list_next (entrylist);
266 gst_caps_entry_check_compatibility (GstCapsEntry *entry1, GstCapsEntry *entry2)
268 DEBUG ("compare: %s %s\n", g_quark_to_string (entry1->propid),
269 g_quark_to_string (entry2->propid));
270 switch (entry1->capstype) {
271 case GST_CAPS_LIST_ID_NUM:
273 GList *entrylist = entry1->data.list_data.entries;
274 gboolean valid = TRUE; // innocent until proven guilty
276 while (entrylist && valid) {
277 GstCapsEntry *entry = (GstCapsEntry *) entrylist->data;
279 valid &= gst_caps_entry_check_compatibility (entry, entry2);
281 entrylist = g_list_next (entrylist);
286 case GST_CAPS_INT_RANGE_ID_NUM:
287 switch (entry2->capstype) {
289 case GST_CAPS_INT_RANGE_ID_NUM:
290 return (entry2->data.int_range_data.min <= entry1->data.int_range_data.min &&
291 entry2->data.int_range_data.max >= entry1->data.int_range_data.max);
292 case GST_CAPS_LIST_ID_NUM:
293 return gst_caps_entry_check_list_compatibility (entry1, entry2);
298 case GST_CAPS_FOURCC_ID_NUM:
299 switch (entry2->capstype) {
301 case GST_CAPS_FOURCC_ID_NUM:
302 return (entry2->data.fourcc_data == entry1->data.fourcc_data);
304 case GST_CAPS_LIST_ID_NUM:
305 return gst_caps_entry_check_list_compatibility (entry1, entry2);
310 case GST_CAPS_INT_ID_NUM:
311 switch (entry2->capstype) {
313 case GST_CAPS_INT_RANGE_ID_NUM:
314 return (entry2->data.int_range_data.min <= entry1->data.int_data &&
315 entry2->data.int_range_data.max >= entry1->data.int_data);
317 case GST_CAPS_INT_ID_NUM:
318 return (entry2->data.int_data == entry1->data.int_data);
320 case GST_CAPS_LIST_ID_NUM:
321 return gst_caps_entry_check_list_compatibility (entry1, entry2);
326 case GST_CAPS_BOOL_ID_NUM:
327 switch (entry2->capstype) {
329 case GST_CAPS_BOOL_ID_NUM:
330 return (entry2->data.bool_data == entry1->data.bool_data);
331 case GST_CAPS_LIST_ID_NUM:
332 return gst_caps_entry_check_list_compatibility (entry1, entry2);
344 * gst_caps_check_compatibility:
345 * @fromcaps: a capabilty
346 * @tocaps: a capabilty
348 * Checks whether two capabilities are compatible
350 * Returns: true if compatible, false otherwise
353 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
359 gboolean compatible = TRUE;
361 g_return_val_if_fail (fromcaps != NULL, FALSE);
362 g_return_val_if_fail (tocaps != NULL, FALSE);
364 if (fromcaps->id != tocaps->id)
367 sourcelist = fromcaps->properties;
368 sinklist = tocaps->properties;
370 while (sourcelist && sinklist && compatible) {
371 GstCapsEntry *entry1;
372 GstCapsEntry *entry2;
374 entry1 = (GstCapsEntry *)sourcelist->data;
375 entry2 = (GstCapsEntry *)sinklist->data;
377 while (entry1->propid < entry2->propid) {
378 DEBUG ("source is more specific in \"%s\"\n", g_quark_to_string (entry1->propid));
380 sourcelist = g_slist_next (sourcelist);
381 if (sourcelist) entry1 = (GstCapsEntry *)sourcelist->data;
384 while (entry1->propid > entry2->propid) {
385 DEBUG ("source has missing property \"%s\"\n", g_quark_to_string (entry2->propid));
387 sinklist = g_slist_next (sinklist);
388 if (sinklist) entry2 = (GstCapsEntry *)sinklist->data;
392 compatible &= gst_caps_entry_check_compatibility (entry1, entry2);
394 sourcelist = g_slist_next (sourcelist);
395 sinklist = g_slist_next (sinklist);
406 * gst_caps_register_va:
407 * @factory: the factories to register
409 * Register the given factories.
411 * Returns: A list of the registered factories
414 gst_caps_register_va (GstCapsFactory factory, ...)
417 GstCapsFactoryEntry *current_factory;
419 va_start (var_args, factory);
421 current_factory = (GstCapsFactoryEntry *) factory;
423 while (current_factory) {
424 gst_caps_register (current_factory);
426 current_factory = va_arg (var_args, GstCapsFactoryEntry *);