Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / libs / gst / base / gsttypefindhelper.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2000,2005 Wim Taymans <wim@fluendo.com>
4  * Copyright (C) 2006      Tim-Philipp Müller <tim centricular net>
5  *
6  * gsttypefindhelper.c:
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:gsttypefindhelper
26  * @short_description: Utility functions for typefinding 
27  *
28  * Utility functions for elements doing typefinding:
29  * gst_type_find_helper() does typefinding in pull mode, while
30  * gst_type_find_helper_for_buffer() is useful for elements needing to do
31  * typefinding in push mode from a chain function.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #  include "config.h"
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "gsttypefindhelper.h"
42
43 /* ********************** typefinding in pull mode ************************ */
44
45 static void
46 helper_find_suggest (gpointer data, guint probability, const GstCaps * caps);
47
48 typedef struct
49 {
50   GSList *buffers;              /* buffer cache */
51   guint64 size;
52   guint64 last_offset;
53   GstTypeFindHelperGetRangeFunction func;
54   guint best_probability;
55   GstCaps *caps;
56   GstTypeFindFactory *factory;  /* for logging */
57   GstObject *obj;               /* for logging */
58 } GstTypeFindHelper;
59
60 /*
61  * helper_find_peek:
62  * @data: helper data struct
63  * @off: stream offset
64  * @size: block size
65  *
66  * Get data pointer within a stream. Keeps a cache of read buffers (partly
67  * for performance reasons, but mostly because pointers returned by us need
68  * to stay valid until typefinding has finished)
69  *
70  * Returns: address of the data or %NULL if buffer does not cover the
71  * requested range.
72  */
73 static const guint8 *
74 helper_find_peek (gpointer data, gint64 offset, guint size)
75 {
76   GstTypeFindHelper *helper;
77   GstBuffer *buffer;
78   GstFlowReturn ret;
79   GSList *insert_pos = NULL;
80   gsize buf_size;
81   guint64 buf_offset;
82 #if 0
83   GstCaps *caps;
84 #endif
85
86   helper = (GstTypeFindHelper *) data;
87
88   GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT
89       ", %u)", GST_OBJECT_NAME (helper->factory), offset, size);
90
91   if (size == 0)
92     return NULL;
93
94   if (offset < 0) {
95     if (helper->size == -1 || helper->size < -offset)
96       return NULL;
97
98     offset += helper->size;
99   }
100
101   /* see if we have a matching buffer already in our list */
102   if (size > 0 && offset <= helper->last_offset) {
103     GSList *walk;
104
105     for (walk = helper->buffers; walk; walk = walk->next) {
106       GstBuffer *buf = GST_BUFFER_CAST (walk->data);
107       guint64 buf_offset = GST_BUFFER_OFFSET (buf);
108       guint buf_size = gst_buffer_get_size (buf);
109
110       /* buffers are kept sorted by end offset (highest first) in the list, so
111        * at this point we save the current position and stop searching if 
112        * we're after the searched end offset */
113       if (buf_offset <= offset) {
114         if ((offset + size) < (buf_offset + buf_size)) {
115           guint8 *data;
116
117           /* FIXME, unmap after usage */
118           data = gst_buffer_map (buf, NULL, NULL, GST_MAP_READ);
119
120           return data + (offset - buf_offset);
121         }
122       } else if (offset + size >= buf_offset + buf_size) {
123         insert_pos = walk;
124         break;
125       }
126     }
127   }
128
129   buffer = NULL;
130   /* some typefinders go in 1 byte steps over 1k of data and request
131    * small buffers. It is really inefficient to pull each time, and pulling
132    * a larger chunk is almost free. Trying to pull a larger chunk at the end
133    * of the file is also not a problem here, we'll just get a truncated buffer
134    * in that case (and we'll have to double-check the size we actually get
135    * anyway, see below) */
136   ret = helper->func (helper->obj, offset, MAX (size, 4096), &buffer);
137
138   if (ret != GST_FLOW_OK)
139     goto error;
140
141 #if 0
142   caps = GST_BUFFER_CAPS (buffer);
143
144   if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) {
145     GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability",
146         caps);
147
148     gst_caps_replace (&helper->caps, caps);
149     helper->best_probability = GST_TYPE_FIND_MAXIMUM;
150
151     gst_buffer_unref (buffer);
152     return NULL;
153   }
154 #endif
155
156   /* getrange might silently return shortened buffers at the end of a file,
157    * we must, however, always return either the full requested data or NULL */
158   buf_offset = GST_BUFFER_OFFSET (buffer);
159   buf_size = gst_buffer_get_size (buffer);
160
161   if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) {
162     GST_DEBUG ("droping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
163         " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT,
164         buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1);
165     gst_buffer_unref (buffer);
166     return NULL;
167   }
168
169   if (insert_pos) {
170     helper->buffers =
171         g_slist_insert_before (helper->buffers, insert_pos, buffer);
172   } else {
173     /* if insert_pos is not set, our offset is bigger than the largest offset
174      * we have so far; since we keep the list sorted with highest offsets
175      * first, we need to prepend the buffer to the list */
176     helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
177     helper->buffers = g_slist_prepend (helper->buffers, buffer);
178   }
179
180   /* FIXME, unmap */
181   return gst_buffer_map (buffer, NULL, NULL, GST_MAP_READ);
182
183 error:
184   {
185     GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
186     return NULL;
187   }
188 }
189
190 /*
191  * helper_find_suggest:
192  * @data: helper data struct
193  * @probability: probability of the match
194  * @caps: caps of the type
195  *
196  * If given @probability is higher, replace previously store caps.
197  */
198 static void
199 helper_find_suggest (gpointer data, guint probability, const GstCaps * caps)
200 {
201   GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
202
203   GST_LOG_OBJECT (helper->obj,
204       "'%s' called called suggest (%u, %" GST_PTR_FORMAT ")",
205       GST_OBJECT_NAME (helper->factory), probability, caps);
206
207   if (probability > helper->best_probability) {
208     GstCaps *copy = gst_caps_copy (caps);
209
210     gst_caps_replace (&helper->caps, copy);
211     gst_caps_unref (copy);
212     helper->best_probability = probability;
213   }
214 }
215
216 static guint64
217 helper_find_get_length (gpointer data)
218 {
219   GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
220
221   GST_LOG_OBJECT (helper->obj, "'%s' called called get_length, returning %"
222       G_GUINT64_FORMAT, GST_OBJECT_NAME (helper->factory), helper->size);
223
224   return helper->size;
225 }
226
227 /**
228  * gst_type_find_helper_get_range_ext:
229  * @obj: A #GstObject that will be passed as first argument to @func
230  * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
231  *        be used to access data at random offsets when doing the typefinding
232  * @size: The length in bytes
233  * @extension: extension of the media
234  * @prob: (out) (allow-none): location to store the probability of the found
235  *     caps, or #NULL
236  *
237  * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
238  * however, this function will use the specified function @func to obtain the
239  * data needed by the typefind functions, rather than operating on a given
240  * source pad. This is useful mostly for elements like tag demuxers which
241  * strip off data at the beginning and/or end of a file and want to typefind
242  * the stripped data stream before adding their own source pad (the specified
243  * callback can then call the upstream peer pad with offsets adjusted for the
244  * tag size, for example).
245  *
246  * When @extension is not NULL, this function will first try the typefind
247  * functions for the given extension, which might speed up the typefinding
248  * in many cases.
249  *
250  * Free-function: gst_caps_unref
251  *
252  * Returns: (transfer full): the #GstCaps corresponding to the data stream.
253  *     Returns #NULL if no #GstCaps matches the data stream.
254  *
255  * Since: 0.10.26
256  */
257 GstCaps *
258 gst_type_find_helper_get_range_ext (GstObject * obj,
259     GstTypeFindHelperGetRangeFunction func, guint64 size,
260     const gchar * extension, GstTypeFindProbability * prob)
261 {
262   GstTypeFindHelper helper;
263   GstTypeFind find;
264   GSList *walk;
265   GList *l, *type_list;
266   GstCaps *result = NULL;
267   gint pos = 0;
268
269   g_return_val_if_fail (GST_IS_OBJECT (obj), NULL);
270   g_return_val_if_fail (func != NULL, NULL);
271
272   helper.buffers = NULL;
273   helper.size = size;
274   helper.last_offset = 0;
275   helper.func = func;
276   helper.best_probability = 0;
277   helper.caps = NULL;
278   helper.obj = obj;
279
280   find.data = &helper;
281   find.peek = helper_find_peek;
282   find.suggest = helper_find_suggest;
283
284   if (size == 0 || size == (guint64) - 1) {
285     find.get_length = NULL;
286   } else {
287     find.get_length = helper_find_get_length;
288   }
289
290   type_list = gst_type_find_factory_get_list ();
291
292   /* move the typefinders for the extension first in the list. The idea is that
293    * when one of them returns MAX we don't need to search further as there is a
294    * very high chance we got the right type. */
295   if (extension) {
296     GList *next;
297
298     GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head",
299         extension);
300
301     for (l = type_list; l; l = next) {
302       GstTypeFindFactory *factory;
303       gint i;
304       gchar **ext;
305
306       next = l->next;
307
308       factory = GST_TYPE_FIND_FACTORY (l->data);
309
310       ext = gst_type_find_factory_get_extensions (factory);
311       if (ext == NULL)
312         continue;
313
314       GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
315           GST_OBJECT_NAME (factory), extension);
316
317       for (i = 0; ext[i]; i++) {
318         if (strcmp (ext[i], extension) == 0) {
319           /* found extension, move in front */
320           GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
321               extension);
322           /* remove entry from list */
323           type_list = g_list_delete_link (type_list, l);
324           /* insert at the position */
325           type_list = g_list_insert (type_list, factory, pos);
326           /* next element will be inserted after this one */
327           pos++;
328           break;
329         }
330       }
331     }
332   }
333
334   for (l = type_list; l; l = l->next) {
335     helper.factory = GST_TYPE_FIND_FACTORY (l->data);
336     gst_type_find_factory_call_function (helper.factory, &find);
337     if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
338       break;
339   }
340   gst_plugin_feature_list_free (type_list);
341
342   for (walk = helper.buffers; walk; walk = walk->next)
343     gst_buffer_unref (GST_BUFFER_CAST (walk->data));
344   g_slist_free (helper.buffers);
345
346   if (helper.best_probability > 0)
347     result = helper.caps;
348
349   if (prob)
350     *prob = helper.best_probability;
351
352   GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
353       result, (guint) helper.best_probability);
354
355   return result;
356 }
357
358 /**
359  * gst_type_find_helper_get_range:
360  * @obj: A #GstObject that will be passed as first argument to @func
361  * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
362  *        be used to access data at random offsets when doing the typefinding
363  * @size: The length in bytes
364  * @prob: (out) (allow-none): location to store the probability of the found
365  *     caps, or #NULL
366  *
367  * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
368  * however, this function will use the specified function @func to obtain the
369  * data needed by the typefind functions, rather than operating on a given
370  * source pad. This is useful mostly for elements like tag demuxers which
371  * strip off data at the beginning and/or end of a file and want to typefind
372  * the stripped data stream before adding their own source pad (the specified
373  * callback can then call the upstream peer pad with offsets adjusted for the
374  * tag size, for example).
375  *
376  * Free-function: gst_caps_unref
377  *
378  * Returns: (transfer full): the #GstCaps corresponding to the data stream.
379  *     Returns #NULL if no #GstCaps matches the data stream.
380  */
381 GstCaps *
382 gst_type_find_helper_get_range (GstObject * obj,
383     GstTypeFindHelperGetRangeFunction func, guint64 size,
384     GstTypeFindProbability * prob)
385 {
386   return gst_type_find_helper_get_range_ext (obj, func, size, NULL, prob);
387 }
388
389 /**
390  * gst_type_find_helper:
391  * @src: A source #GstPad
392  * @size: The length in bytes
393  *
394  * Tries to find what type of data is flowing from the given source #GstPad.
395  *
396  * Free-function: gst_caps_unref
397  *
398  * Returns: (transfer full): the #GstCaps corresponding to the data stream.
399  *     Returns #NULL if no #GstCaps matches the data stream.
400  */
401
402 GstCaps *
403 gst_type_find_helper (GstPad * src, guint64 size)
404 {
405   GstTypeFindHelperGetRangeFunction func;
406
407   g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
408   g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
409
410   func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
411
412   return gst_type_find_helper_get_range (GST_OBJECT (src), func, size, NULL);
413 }
414
415 /* ********************** typefinding for buffers ************************* */
416
417 typedef struct
418 {
419   const guint8 *data;           /* buffer data */
420   gsize size;
421   guint best_probability;
422   GstCaps *caps;
423   GstTypeFindFactory *factory;  /* for logging */
424   GstObject *obj;               /* for logging */
425 } GstTypeFindBufHelper;
426
427 /*
428  * buf_helper_find_peek:
429  * @data: helper data struct
430  * @off: stream offset
431  * @size: block size
432  *
433  * Get data pointer within a buffer.
434  *
435  * Returns: address inside the buffer or %NULL if buffer does not cover the
436  * requested range.
437  */
438 static const guint8 *
439 buf_helper_find_peek (gpointer data, gint64 off, guint size)
440 {
441   GstTypeFindBufHelper *helper;
442
443   helper = (GstTypeFindBufHelper *) data;
444   GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)",
445       GST_OBJECT_NAME (helper->factory), off, size);
446
447   if (size == 0)
448     return NULL;
449
450   if (off < 0) {
451     GST_LOG_OBJECT (helper->obj, "'%s' wanted to peek at end; not supported",
452         GST_OBJECT_NAME (helper->factory));
453     return NULL;
454   }
455
456   if ((off + size) <= helper->size)
457     return helper->data + off;
458
459   return NULL;
460 }
461
462 /*
463  * buf_helper_find_suggest:
464  * @data: helper data struct
465  * @probability: probability of the match
466  * @caps: caps of the type
467  *
468  * If given @probability is higher, replace previously store caps.
469  */
470 static void
471 buf_helper_find_suggest (gpointer data, guint probability, const GstCaps * caps)
472 {
473   GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
474
475   GST_LOG_OBJECT (helper->obj,
476       "'%s' called called suggest (%u, %" GST_PTR_FORMAT ")",
477       GST_OBJECT_NAME (helper->factory), probability, caps);
478
479   /* Note: not >= as we call typefinders in order of rank, highest first */
480   if (probability > helper->best_probability) {
481     GstCaps *copy = gst_caps_copy (caps);
482
483     gst_caps_replace (&helper->caps, copy);
484     gst_caps_unref (copy);
485     helper->best_probability = probability;
486   }
487 }
488
489 /**
490  * gst_type_find_helper_for_data:
491  * @obj: object doing the typefinding, or NULL (used for logging)
492  * @data: (in) (transfer none): a pointer with data to typefind
493  * @size: (in) (transfer none): the size of @data
494  * @prob: (out) (allow-none): location to store the probability of the found
495  *     caps, or #NULL
496  *
497  * Tries to find what type of data is contained in the given @data, the
498  * assumption being that the data represents the beginning of the stream or
499  * file.
500  *
501  * All available typefinders will be called on the data in order of rank. If
502  * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
503  * typefinding is stopped immediately and the found caps will be returned
504  * right away. Otherwise, all available typefind functions will the tried,
505  * and the caps with the highest probability will be returned, or #NULL if
506  * the content of @data could not be identified.
507  *
508  * Free-function: gst_caps_unref
509  *
510  * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
511  *     if no type could be found. The caller should free the caps returned
512  *     with gst_caps_unref().
513  */
514 GstCaps *
515 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
516     GstTypeFindProbability * prob)
517 {
518   GstTypeFindBufHelper helper;
519   GstTypeFind find;
520   GList *l, *type_list;
521   GstCaps *result = NULL;
522
523   g_return_val_if_fail (data != NULL, NULL);
524
525   helper.data = data;
526   helper.size = size;
527   helper.best_probability = 0;
528   helper.caps = NULL;
529   helper.obj = obj;
530
531   if (helper.data == NULL || helper.size == 0)
532     return NULL;
533
534   find.data = &helper;
535   find.peek = buf_helper_find_peek;
536   find.suggest = buf_helper_find_suggest;
537   find.get_length = NULL;
538
539   type_list = gst_type_find_factory_get_list ();
540
541   for (l = type_list; l; l = l->next) {
542     helper.factory = GST_TYPE_FIND_FACTORY (l->data);
543     gst_type_find_factory_call_function (helper.factory, &find);
544     if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
545       break;
546   }
547   gst_plugin_feature_list_free (type_list);
548
549   if (helper.best_probability > 0)
550     result = helper.caps;
551
552   if (prob)
553     *prob = helper.best_probability;
554
555   GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
556       result, (guint) helper.best_probability);
557
558   return result;
559 }
560
561 /**
562  * gst_type_find_helper_for_buffer:
563  * @obj: object doing the typefinding, or NULL (used for logging)
564  * @buf: (in) (transfer none): a #GstBuffer with data to typefind
565  * @prob: (out) (allow-none): location to store the probability of the found
566  *     caps, or #NULL
567  *
568  * Tries to find what type of data is contained in the given #GstBuffer, the
569  * assumption being that the buffer represents the beginning of the stream or
570  * file.
571  *
572  * All available typefinders will be called on the data in order of rank. If
573  * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
574  * typefinding is stopped immediately and the found caps will be returned
575  * right away. Otherwise, all available typefind functions will the tried,
576  * and the caps with the highest probability will be returned, or #NULL if
577  * the content of the buffer could not be identified.
578  *
579  * Free-function: gst_caps_unref
580  *
581  * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
582  *     if no type could be found. The caller should free the caps returned
583  *     with gst_caps_unref().
584  */
585 GstCaps *
586 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
587     GstTypeFindProbability * prob)
588 {
589   GstCaps *result;
590   guint8 *data;
591   gsize size;
592
593   g_return_val_if_fail (buf != NULL, NULL);
594   g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
595   g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
596       GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
597
598   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
599   result = gst_type_find_helper_for_data (obj, data, size, prob);
600   gst_buffer_unmap (buf, data, size);
601
602   return result;
603 }
604
605 /**
606  * gst_type_find_helper_for_extension:
607  * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
608  * @extension: an extension
609  *
610  * Tries to find the best #GstCaps associated with @extension.
611  *
612  * All available typefinders will be checked against the extension in order
613  * of rank. The caps of the first typefinder that can handle @extension will be
614  * returned.
615  *
616  * Free-function: gst_caps_unref
617  *
618  * Returns: (transfer full): the #GstCaps corresponding to @extension, or
619  *     #NULL if no type could be found. The caller should free the caps
620  *     returned with gst_caps_unref().
621  * 
622  * Since: 0.10.23
623  */
624 GstCaps *
625 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
626 {
627   GList *l, *type_list;
628   GstCaps *result = NULL;
629
630   g_return_val_if_fail (extension != NULL, NULL);
631
632   GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
633
634   type_list = gst_type_find_factory_get_list ();
635
636   for (l = type_list; l; l = g_list_next (l)) {
637     GstTypeFindFactory *factory;
638     gchar **ext;
639     gint i;
640
641     factory = GST_TYPE_FIND_FACTORY (l->data);
642
643     /* we only want to check those factories without a function */
644     if (factory->function != NULL)
645       continue;
646
647     /* get the extension that this typefind factory can handle */
648     ext = gst_type_find_factory_get_extensions (factory);
649     if (ext == NULL)
650       continue;
651
652     /* there are extension, see if one of them matches the requested
653      * extension */
654     for (i = 0; ext[i]; i++) {
655       if (strcmp (ext[i], extension) == 0) {
656         /* we found a matching extension, take the caps */
657         if ((result = gst_type_find_factory_get_caps (factory))) {
658           gst_caps_ref (result);
659           goto done;
660         }
661       }
662     }
663   }
664 done:
665   gst_plugin_feature_list_free (type_list);
666
667   GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);
668
669   return result;
670 }