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