aye ladie, no more ugly // comments here, even if Taaz gets upset about it
[platform/upstream/gstreamer.git] / gst / gstcaps.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstcaps.c: Element capabilities subsystem
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 /* #define GST_DEBUG_ENABLED */
24 #include "gst_private.h"
25
26 #include "gstcaps.h"
27 #include "gsttype.h"
28
29 #include "gstpropsprivate.h"
30
31 static GMemChunk *_gst_caps_chunk;
32 static GMutex *_gst_caps_chunk_lock;
33
34 void
35 _gst_caps_initialize (void)
36 {
37   _gst_caps_chunk = g_mem_chunk_new ("GstCaps",
38                   sizeof (GstCaps), sizeof (GstCaps) * 256,
39                   G_ALLOC_AND_FREE);
40   _gst_caps_chunk_lock = g_mutex_new ();
41 }
42
43 static guint16
44 get_type_for_mime (const gchar *mime)
45 {
46   guint16 typeid;
47
48   typeid = gst_type_find_by_mime (mime);
49   if (typeid == 0) {
50      GstTypeDefinition definition;
51      GstTypeFactory *factory;
52
53      definition.name = "capstype";
54      definition.mime = g_strdup (mime);
55      definition.exts = NULL;
56      definition.typefindfunc = NULL;
57
58      factory = gst_typefactory_new (&definition);
59
60      typeid = gst_type_register (factory);
61   }
62   return typeid;
63 }
64
65 /**
66  * gst_caps_new:
67  * @name: the name of this capability
68  * @mime: the mime type to attach to the capability
69  * @props: the properties to add to this capability
70  *
71  * Create a new capability with the given mime typei and properties.
72  *
73  * Returns: a new capability
74  */
75 GstCaps*
76 gst_caps_new (const gchar *name, const gchar *mime, GstProps *props)
77 {
78   GstCaps *caps;
79
80   g_return_val_if_fail (mime != NULL, NULL);
81
82   g_mutex_lock (_gst_caps_chunk_lock);
83   caps = g_mem_chunk_alloc (_gst_caps_chunk);
84   g_mutex_unlock (_gst_caps_chunk_lock);
85
86   caps->name = g_strdup (name);
87   caps->id = get_type_for_mime (mime);
88   caps->properties = props;
89   caps->next = NULL;
90   caps->refcount = 1;
91   caps->lock = g_mutex_new ();
92
93   return caps;
94 }
95
96 /**
97  * gst_caps_destroy:
98  * @caps: the caps to destroy
99  *
100  * Frees the memory used by this caps structure and all
101  * the chained caps and properties.
102  */
103 void
104 gst_caps_destroy (GstCaps *caps)
105 {
106   GstCaps *next;
107
108   g_return_if_fail (caps != NULL);
109
110   GST_CAPS_LOCK (caps);
111   next = caps->next;
112   g_free (caps->name);
113   g_free (caps);
114   GST_CAPS_UNLOCK (caps);
115
116   if (next) 
117     gst_caps_unref (next);
118 }
119
120 /**
121  * gst_caps_unref:
122  * @caps: the caps to unref
123  *
124  * Decrease the refcount of this caps structure, 
125  * destroying it when the refcount is 0
126  *
127  * Returns: caps or NULL if the refcount reached 0
128  */
129 GstCaps*
130 gst_caps_unref (GstCaps *caps)
131 {
132   gboolean zero;
133   GstCaps **next;
134
135   g_return_val_if_fail (caps != NULL, NULL);
136   g_return_val_if_fail (caps->refcount > 0, NULL);
137
138   GST_CAPS_LOCK (caps);
139   caps->refcount--;
140   zero = (caps->refcount == 0);
141   next = &caps->next;
142   GST_CAPS_UNLOCK (caps);
143
144   if (*next)
145     *next = gst_caps_unref (*next);
146
147   if (zero) {
148     gst_caps_destroy (caps);
149     caps = NULL;
150   }
151   return caps;
152 }
153
154 /**
155  * gst_caps_ref:
156  * @caps: the caps to ref
157  *
158  * Increase the refcount of this caps structure
159  *
160  * Returns: the caps with the refcount incremented
161  */
162 GstCaps*
163 gst_caps_ref (GstCaps *caps)
164 {
165   g_return_val_if_fail (caps != NULL, NULL);
166
167   GST_CAPS_LOCK (caps);
168   caps->refcount++;
169   GST_CAPS_UNLOCK (caps);
170
171   return caps;
172 }
173
174 /**
175  * gst_caps_copy:
176  * @caps: the caps to copy
177  *
178  * Copies the caps.
179  *
180  * Returns: a copy of the GstCaps structure.
181  */
182 GstCaps*
183 gst_caps_copy (GstCaps *caps)
184 {
185   GstCaps *new = caps;;
186
187   g_return_val_if_fail (caps != NULL, NULL);
188
189   GST_CAPS_LOCK (caps);
190   new = gst_caps_new (
191                   caps->name,
192                   (gst_type_find_by_id (caps->id))->mime,
193                   gst_props_copy (caps->properties));
194   GST_CAPS_UNLOCK (caps);
195
196   return new;
197 }
198
199 /**
200  * gst_caps_copy_on_write:
201  * @caps: the caps to copy
202  *
203  * Copies the caps if the refcount is greater than 1
204  *
205  * Returns: a pointer to a GstCaps strcuture that can
206  * be safely written to
207  */
208 GstCaps*
209 gst_caps_copy_on_write (GstCaps *caps)
210 {
211   GstCaps *new = caps;
212   gboolean needcopy;
213
214   g_return_val_if_fail (caps != NULL, NULL);
215
216   GST_CAPS_LOCK (caps);
217   needcopy = (caps->refcount > 1);
218   GST_CAPS_UNLOCK (caps);
219
220   if (needcopy) {
221     new = gst_caps_copy (caps);
222     gst_caps_unref (caps);
223   }
224
225   return new;
226 }
227
228 /**
229  * gst_caps_get_name:
230  * @caps: the caps to get the name from
231  *
232  * Get the name of a GstCaps structure.
233  *
234  * Returns: the name of the caps
235  */
236 const gchar*
237 gst_caps_get_name (GstCaps *caps)
238 {
239   g_return_val_if_fail (caps != NULL, NULL);
240
241   return (const gchar *)caps->name;
242 }
243
244 /**
245  * gst_caps_set_name:
246  * @caps: the caps to set the name to
247  * @name: the name to set
248  *
249  * Set the name of a caps.
250  */
251 void
252 gst_caps_set_name (GstCaps *caps, const gchar *name)
253 {
254   g_return_if_fail (caps != NULL);
255
256   if (caps->name)
257     g_free (caps->name);
258
259   caps->name = g_strdup (name);
260 }
261
262 /**
263  * gst_caps_get_mime:
264  * @caps: the caps to get the mime type from
265  *
266  * Get the mime type of the caps as a string.
267  *
268  * Returns: the mime type of the caps
269  */
270 const gchar*
271 gst_caps_get_mime (GstCaps *caps)
272 {
273   GstType *type;
274
275   g_return_val_if_fail (caps != NULL, NULL);
276
277   type = gst_type_find_by_id (caps->id);
278
279   if (type)
280     return type->mime;
281   else
282     return "unknown/unknown";
283 }
284
285 /**
286  * gst_caps_set_mime:
287  * @caps: the caps to set the mime type to
288  * @mime: the mime type to attach to the caps
289  *
290  * Set the mime type of the caps as a string.
291  */
292 void
293 gst_caps_set_mime (GstCaps *caps, const gchar *mime)
294 {
295   g_return_if_fail (caps != NULL);
296   g_return_if_fail (mime != NULL);
297
298   caps->id = get_type_for_mime (mime);
299 }
300
301 /**
302  * gst_caps_get_type_id:
303  * @caps: the caps to get the type id from
304  *
305  * Get the type id of the caps.
306  *
307  * Returns: the type id of the caps
308  */
309 guint16
310 gst_caps_get_type_id (GstCaps *caps)
311 {
312   g_return_val_if_fail (caps != NULL, 0);
313
314   return caps->id;
315 }
316
317 /**
318  * gst_caps_set_type_id:
319  * @caps: the caps to set the type id to
320  * @type_id: the type id to set
321  *
322  * Set the type id of the caps.
323  */
324 void
325 gst_caps_set_type_id (GstCaps *caps, guint16 type_id)
326 {
327   g_return_if_fail (caps != NULL);
328
329   caps->id = type_id;
330 }
331
332 /**
333  * gst_caps_set_props:
334  * @caps: the caps to attach the properties to
335  * @props: the properties to attach
336  *
337  * Set the properties to the given caps.
338  *
339  * Returns: the new caps structure
340  */
341 GstCaps*
342 gst_caps_set_props (GstCaps *caps, GstProps *props)
343 {
344   g_return_val_if_fail (caps != NULL, caps);
345   g_return_val_if_fail (props != NULL, caps);
346   g_return_val_if_fail (caps->properties == NULL, caps);
347
348   caps->properties = props;
349
350   return caps;
351 }
352
353 /**
354  * gst_caps_get_props:
355  * @caps: the caps to get the properties from
356  *
357  * Get the properties of the given caps.
358  *
359  * Returns: the properties of the caps
360  */
361 GstProps*
362 gst_caps_get_props (GstCaps *caps)
363 {
364   g_return_val_if_fail (caps != NULL, NULL);
365
366   return caps->properties;
367 }
368
369 /**
370  * gst_caps_chain:
371  * @caps: a capabilty
372  * @...: more capabilities
373  *
374  * chains the given capabilities
375  *
376  * Returns: the new capability
377  */
378 GstCaps*
379 gst_caps_chain (GstCaps *caps, ...)
380 {
381   GstCaps *orig = caps;
382   va_list var_args;
383
384   va_start (var_args, caps);
385
386   while (caps) {
387     GstCaps *toadd;
388     
389     toadd = va_arg (var_args, GstCaps*);
390     gst_caps_append (caps, toadd);
391     
392     caps = toadd;
393   }
394   va_end (var_args);
395   
396   return orig;
397 }
398
399 /**
400  * gst_caps_append:
401  * @caps: a capabilty
402  * @capstoadd: the capability to append
403  *
404  * Appends a capability to the existing capability.
405  *
406  * Returns: the new capability
407  */
408 GstCaps*
409 gst_caps_append (GstCaps *caps, GstCaps *capstoadd)
410 {
411   GstCaps *orig = caps;
412   
413   g_return_val_if_fail (caps != capstoadd, caps);
414
415   if (caps == NULL)
416     return capstoadd;
417   
418   while (caps->next) {
419     caps = caps->next;
420   }
421   caps->next = capstoadd;
422
423   return orig;
424 }
425
426 /**
427  * gst_caps_prepend:
428  * @caps: a capabilty
429  * @capstoadd: a capabilty to prepend
430  *
431  * prepend the capability to the list of capabilities
432  *
433  * Returns: the new capability
434  */
435 GstCaps*
436 gst_caps_prepend (GstCaps *caps, GstCaps *capstoadd)
437 {
438   GstCaps *orig = capstoadd;
439   
440   g_return_val_if_fail (caps != capstoadd, caps);
441
442   if (capstoadd == NULL)
443     return caps;
444
445   while (capstoadd->next) {
446     capstoadd = capstoadd->next;
447   }
448   capstoadd->next = caps;
449
450   return orig;
451 }
452
453 /**
454  * gst_caps_get_by_name:
455  * @caps: a capabilty
456  * @name: the name of the capability to get
457  *
458  * Get the capability with the given name from this
459  * chain of capabilities.
460  *
461  * Returns: the first capability in the chain with the 
462  * given name
463  */
464 GstCaps*
465 gst_caps_get_by_name (GstCaps *caps, const gchar *name)
466 {
467   g_return_val_if_fail (caps != NULL, NULL);
468   g_return_val_if_fail (name != NULL, NULL);
469    
470   while (caps) {
471     if (!strcmp (caps->name, name)) 
472       return caps;
473     caps = caps->next;
474   }
475
476   return NULL;
477 }
478                                                                                                                    
479 static gboolean
480 gst_caps_check_compatibility_func (GstCaps *fromcaps, GstCaps *tocaps)
481 {
482   if (fromcaps->id != tocaps->id) {
483     GST_DEBUG (GST_CAT_CAPS,"mime types differ (%s to %s)\n",
484                gst_type_find_by_id (fromcaps->id)->mime, 
485                gst_type_find_by_id (tocaps->id)->mime);
486     return FALSE;
487   }
488
489   if (tocaps->properties) {
490     if (fromcaps->properties) {
491       return gst_props_check_compatibility (fromcaps->properties, tocaps->properties);
492     }
493     else {
494       GST_DEBUG (GST_CAT_CAPS,"no source caps\n");
495       return FALSE;
496     }
497   }
498   else {
499     /* assume it accepts everything */
500     GST_DEBUG (GST_CAT_CAPS,"no caps\n");
501     return TRUE;
502   }
503 }
504
505 /**
506  * gst_caps_check_compatibility:
507  * @fromcaps: a capabilty
508  * @tocaps: a capabilty
509  *
510  * Checks whether two capabilities are compatible.
511  *
512  * Returns: TRUE if compatible, FALSE otherwise
513  */
514 gboolean
515 gst_caps_check_compatibility (GstCaps *fromcaps, GstCaps *tocaps)
516 {
517   if (fromcaps == NULL) {
518     if (tocaps == NULL) {
519       GST_DEBUG (GST_CAT_CAPS,"no caps\n");
520       return TRUE;
521     }
522     else {
523       GST_DEBUG (GST_CAT_CAPS,"no source but destination caps\n");
524       return FALSE;
525     }
526   }
527   else {
528     if (tocaps == NULL) {
529       GST_DEBUG (GST_CAT_CAPS,"source caps and no destination caps\n");
530       return TRUE;
531     }
532   }
533
534   while (fromcaps) {
535     GstCaps *destcaps = tocaps;
536
537     while (destcaps) {
538       if (gst_caps_check_compatibility_func (fromcaps, destcaps))
539         return TRUE;
540
541       destcaps =  destcaps->next;
542     }
543     fromcaps =  fromcaps->next;
544   }
545   return FALSE;
546 }
547
548 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
549 /**
550  * gst_caps_save_thyself:
551  * @caps: a capabilty to save
552  * @parent: the parent XML node pointer
553  *
554  * Save the capability into an XML representation.
555  *
556  * Returns: a new XML node pointer
557  */
558 xmlNodePtr
559 gst_caps_save_thyself (GstCaps *caps, xmlNodePtr parent)
560 {
561   xmlNodePtr subtree;
562   xmlNodePtr subsubtree;
563
564   while (caps) {
565     subtree = xmlNewChild (parent, NULL, "capscomp", NULL);
566
567     xmlNewChild (subtree, NULL, "name", caps->name);
568     xmlNewChild (subtree, NULL, "type", gst_type_find_by_id (caps->id)->mime);
569     if (caps->properties) {
570       subsubtree = xmlNewChild (subtree, NULL, "properties", NULL);
571
572       gst_props_save_thyself (caps->properties, subsubtree);
573     }
574
575     caps = caps->next;
576   }
577
578   return parent;
579 }
580
581 /**
582  * gst_caps_load_thyself:
583  * @parent: the parent XML node pointer
584  *
585  * Load a new caps from the XML representation.
586  *
587  * Returns: a new capability
588  */
589 GstCaps*
590 gst_caps_load_thyself (xmlNodePtr parent)
591 {
592   GstCaps *result = NULL;
593   xmlNodePtr field = parent->xmlChildrenNode;
594
595   while (field) {
596     if (!strcmp (field->name, "capscomp")) {
597       xmlNodePtr subfield = field->xmlChildrenNode;
598       GstCaps *caps;
599       gchar *content;
600
601       g_mutex_lock (_gst_caps_chunk_lock);
602       caps = g_mem_chunk_alloc0 (_gst_caps_chunk);
603       g_mutex_unlock (_gst_caps_chunk_lock);
604
605       caps->refcount = 1;
606       caps->lock = g_mutex_new ();
607       caps->next = NULL;
608         
609       while (subfield) {
610         if (!strcmp (subfield->name, "name")) {
611           caps->name = xmlNodeGetContent (subfield);
612         }
613         if (!strcmp (subfield->name, "type")) {
614           content = xmlNodeGetContent (subfield);
615           caps->id = get_type_for_mime (content);
616           g_free (content);
617         }
618         else if (!strcmp (subfield->name, "properties")) {
619           caps->properties = gst_props_load_thyself (subfield);
620         }
621         
622         subfield = subfield->next;
623       }
624       result = gst_caps_append (result, caps);
625     }
626     field = field->next;
627   }
628
629   return result;
630 }
631
632 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */