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