Debian Codegraph @

libs/gst/base/gsttypefindhelper.c

raw
1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <[email protected]>
3 * Copyright (C) 2000,2005 Wim Taymans <[email protected]>
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 GstFlowReturn flow_ret;
67 } GstTypeFindHelper;
68
69 /*
70 * helper_find_peek:
71 * @data: helper data struct
72 * @off: stream offset
73 * @size: block size
74 *
75 * Get data pointer within a stream. Keeps a cache of read buffers (partly
76 * for performance reasons, but mostly because pointers returned by us need
77 * to stay valid until typefinding has finished)
78 *
79 * Returns: (nullable): address of the data or %NULL if buffer does not cover
80 * the requested range.
81 */
82 static const guint8 *
83 helper_find_peek (gpointer data, gint64 offset, guint size)
84 {
85 GstTypeFindHelper *helper;
86 GstBuffer *buffer;
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, "Typefind factory called peek (%" G_GINT64_FORMAT
98 ", %u)", 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 helper->flow_ret =
144 helper->func (helper->obj, helper->parent, offset, MAX (size, 4096),
145 &buffer);
146
147 if (helper->flow_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_size < size) {
171 GST_DEBUG ("dropping short buffer of size %" G_GSIZE_FORMAT ","
172 "requested size was %u", buf_size, size);
173 gst_buffer_unref (buffer);
174 return NULL;
175 }
176
177 if (buf_offset != -1 && buf_offset != offset) {
178 GST_DEBUG ("dropping buffer with unexpected offset %" G_GUINT64_FORMAT ", "
179 "expected offset was %" G_GUINT64_FORMAT, buf_offset, offset);
180 gst_buffer_unref (buffer);
181 return NULL;
182 }
183
184 bmap = g_new0 (GstMappedBuffer, 1);
185
186 if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ))
187 goto map_failed;
188
189 bmap->buffer = buffer;
190
191 if (insert_pos) {
192 helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
193 } else {
194 /* if insert_pos is not set, our offset is bigger than the largest offset
195 * we have so far; since we keep the list sorted with highest offsets
196 * first, we need to prepend the buffer to the list */
197 helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size;
198 helper->buffers = g_slist_prepend (helper->buffers, bmap);
199 }
200
201 return bmap->map.data;
202
203 error:
204 {
205 GST_INFO ("typefind function returned: %s",
206 gst_flow_get_name (helper->flow_ret));
207 return NULL;
208 }
209 map_failed:
210 {
211 GST_ERROR ("map failed");
212 gst_buffer_unref (buffer);
213 g_free (bmap);
214 return NULL;
215 }
216 }
217
218 /*
219 * helper_find_suggest:
220 * @data: helper data struct
221 * @probability: probability of the match
222 * @caps: caps of the type
223 *
224 * If given @probability is higher, replace previously store caps.
225 */
226 static void
227 helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
228 {
229 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
230
231 GST_LOG_OBJECT (helper->obj,
232 "Typefind factory called suggest (%u, %" GST_PTR_FORMAT ")",
233 probability, caps);
234
235 if (probability > helper->best_probability) {
236 gst_caps_replace (&helper->caps, caps);
237 helper->best_probability = probability;
238 }
239 }
240
241 static guint64
242 helper_find_get_length (gpointer data)
243 {
244 GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
245
246 GST_LOG_OBJECT (helper->obj, "Typefind factory called get_length, returning %"
247 G_GUINT64_FORMAT, helper->size);
248
249 return helper->size;
250 }
251
252 static GList *
253 prioritize_extension (GstObject * obj, GList * type_list,
254 const gchar * extension)
255 {
256 gint pos = 0;
257 GList *next, *l;
258
259 if (!extension)
260 return type_list;
261
262 /* move the typefinders for the extension first in the list. The idea is that
263 * when one of them returns MAX we don't need to search further as there is a
264 * very high chance we got the right type. */
265
266 GST_LOG_OBJECT (obj, "sorting typefind for extension %s to head", extension);
267
268 for (l = type_list; l; l = next) {
269 const gchar *const *ext;
270 GstTypeFindFactory *factory;
271
272 next = l->next;
273
274 factory = GST_TYPE_FIND_FACTORY (l->data);
275
276 ext = gst_type_find_factory_get_extensions (factory);
277 if (ext == NULL)
278 continue;
279
280 GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
281 GST_OBJECT_NAME (factory), extension);
282
283 while (*ext != NULL) {
284 if (strcmp (*ext, extension) == 0) {
285 /* found extension, move in front */
286 GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
287 extension);
288 /* remove entry from list */
289 type_list = g_list_delete_link (type_list, l);
290 /* insert at the position */
291 type_list = g_list_insert (type_list, factory, pos);
292 /* next element will be inserted after this one */
293 pos++;
294 break;
295 }
296 ++ext;
297 }
298 }
299
300 return type_list;
301 }
302
303 /**
304 * gst_type_find_helper_get_range:
305 * @obj: A #GstObject that will be passed as first argument to @func
306 * @parent: (nullable): the parent of @obj or %NULL
307 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
308 * be used to access data at random offsets when doing the typefinding
309 * @size: The length in bytes
310 * @extension: (nullable): extension of the media, or %NULL
311 * @prob: (out) (optional): location to store the probability of the found
312 * caps, or %NULL
313 *
314 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
315 * however, this function will use the specified function @func to obtain the
316 * data needed by the typefind functions, rather than operating on a given
317 * source pad. This is useful mostly for elements like tag demuxers which
318 * strip off data at the beginning and/or end of a file and want to typefind
319 * the stripped data stream before adding their own source pad (the specified
320 * callback can then call the upstream peer pad with offsets adjusted for the
321 * tag size, for example).
322 *
323 * When @extension is not %NULL, this function will first try the typefind
324 * functions for the given extension, which might speed up the typefinding
325 * in many cases.
326 *
327 * Free-function: gst_caps_unref
328 *
329 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
330 * stream. Returns %NULL if no #GstCaps matches the data stream.
331 */
332 GstCaps *
333 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
334 GstTypeFindHelperGetRangeFunction func, guint64 size,
335 const gchar * extension, GstTypeFindProbability * prob)
336 {
337 GstCaps *caps = NULL;
338
339 gst_type_find_helper_get_range_full (obj, parent, func, size, extension,
340 &caps, prob);
341
342 return caps;
343 }
344
345 /**
346 * gst_type_find_helper_get_range_full:
347 * @obj: A #GstObject that will be passed as first argument to @func
348 * @parent: (nullable): the parent of @obj or %NULL
349 * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
350 * be used to access data at random offsets when doing the typefinding
351 * @size: The length in bytes
352 * @extension: (nullable): extension of the media, or %NULL
353 * @caps: (out) (transfer full): returned caps
354 * @prob: (out) (optional): location to store the probability of the found
355 * caps, or %NULL
356 *
357 * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
358 * however, this function will use the specified function @func to obtain the
359 * data needed by the typefind functions, rather than operating on a given
360 * source pad. This is useful mostly for elements like tag demuxers which
361 * strip off data at the beginning and/or end of a file and want to typefind
362 * the stripped data stream before adding their own source pad (the specified
363 * callback can then call the upstream peer pad with offsets adjusted for the
364 * tag size, for example).
365 *
366 * When @extension is not %NULL, this function will first try the typefind
367 * functions for the given extension, which might speed up the typefinding
368 * in many cases.
369 *
370 * Returns: the last %GstFlowReturn from pulling a buffer or %GST_FLOW_OK if
371 * typefinding was successful.
372 *
373 * Since: 1.14.3
374 */
375 GstFlowReturn
376 gst_type_find_helper_get_range_full (GstObject * obj, GstObject * parent,
377 GstTypeFindHelperGetRangeFunction func, guint64 size,
378 const gchar * extension, GstCaps ** caps, GstTypeFindProbability * prob)
379 {
380 GstTypeFindHelper helper;
381 GstTypeFind find;
382 GSList *walk;
383 GList *l, *type_list;
384 GstCaps *result = NULL;
385
386 g_return_val_if_fail (GST_IS_OBJECT (obj), GST_FLOW_ERROR);
387 g_return_val_if_fail (func != NULL, GST_FLOW_ERROR);
388 g_return_val_if_fail (caps != NULL, GST_FLOW_ERROR);
389
390 *caps = NULL;
391
392 helper.buffers = NULL;
393 helper.size = size;
394 helper.last_offset = 0;
395 helper.func = func;
396 helper.best_probability = GST_TYPE_FIND_NONE;
397 helper.caps = NULL;
398 helper.obj = obj;
399 helper.parent = parent;
400 helper.flow_ret = GST_FLOW_OK;
401
402 find.data = &helper;
403 find.peek = helper_find_peek;
404 find.suggest = helper_find_suggest;
405
406 if (size == 0 || size == (guint64) - 1) {
407 find.get_length = NULL;
408 } else {
409 find.get_length = helper_find_get_length;
410 }
411
412 type_list = gst_type_find_factory_get_list ();
413 type_list = prioritize_extension (obj, type_list, extension);
414
415 for (l = type_list; l; l = l->next) {
416 helper.factory = GST_TYPE_FIND_FACTORY (l->data);
417 gst_type_find_factory_call_function (helper.factory, &find);
418 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM) {
419 /* Any other flow return can be ignored here, we found
420 * something before any error with highest probability */
421 helper.flow_ret = GST_FLOW_OK;
422 break;
423 } else if (helper.flow_ret != GST_FLOW_OK
424 && helper.flow_ret != GST_FLOW_EOS) {
425 /* We had less than maximum probability and an error, don't return
426 * any caps as they might be with a lower probability than what
427 * we would've gotten when continuing if there was no error */
428 gst_caps_replace (&helper.caps, NULL);
429 break;
430 }
431 }
432 gst_plugin_feature_list_free (type_list);
433
434 for (walk = helper.buffers; walk; walk = walk->next) {
435 GstMappedBuffer *bmap = (GstMappedBuffer *) walk->data;
436
437 gst_buffer_unmap (bmap->buffer, &bmap->map);
438 gst_buffer_unref (bmap->buffer);
439 g_free (bmap);
440 }
441 g_slist_free (helper.buffers);
442
443 if (helper.best_probability > 0)
444 result = helper.caps;
445
446 if (prob)
447 *prob = helper.best_probability;
448
449 *caps = result;
450 if (helper.flow_ret == GST_FLOW_EOS) {
451 /* Some typefinder might've tried to read too much, especially if
452 * dealing with small sources or with limited num-buffers. In
453 * this case, EOS is received, and it is converted to ERROR
454 * because the downstream peer is not yet connected to send it. */
455 helper.flow_ret = GST_FLOW_ERROR;
456 }
457
458 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
459 result, (guint) helper.best_probability);
460
461 return helper.flow_ret;
462 }
463
464 /**
465 * gst_type_find_helper:
466 * @src: A source #GstPad
467 * @size: The length in bytes
468 *
469 * Tries to find what type of data is flowing from the given source #GstPad.
470 *
471 * Free-function: gst_caps_unref
472 *
473 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
474 * stream. Returns %NULL if no #GstCaps matches the data stream.
475 */
476
477 GstCaps *
478 gst_type_find_helper (GstPad * src, guint64 size)
479 {
480 GstTypeFindHelperGetRangeFunction func;
481
482 g_return_val_if_fail (GST_IS_OBJECT (src), NULL);
483 g_return_val_if_fail (GST_PAD_GETRANGEFUNC (src) != NULL, NULL);
484
485 func = (GstTypeFindHelperGetRangeFunction) (GST_PAD_GETRANGEFUNC (src));
486
487 return gst_type_find_helper_get_range (GST_OBJECT (src),
488 GST_OBJECT_PARENT (src), func, size, NULL, NULL);
489 }
490
491 /* ********************** typefinding for buffers ************************* */
492
493 typedef struct
494 {
495 const guint8 *data; /* buffer data */
496 gsize size;
497 GstTypeFindProbability best_probability;
498 GstCaps *caps;
499 GstObject *obj; /* for logging */
500 } GstTypeFindBufHelper;
501
502 /**
503 * GstTypeFindData:
504 *
505 * The opaque #GstTypeFindData structure.
506 *
507 * Since: 1.22
508 *
509 */
510 struct _GstTypeFindData
511 {
512 GstTypeFind find;
513 GstTypeFindBufHelper helper;
514 };
515
516 /*
517 * buf_helper_find_peek:
518 * @data: helper data struct
519 * @off: stream offset
520 * @size: block size
521 *
522 * Get data pointer within a buffer.
523 *
524 * Returns: (nullable): address inside the buffer or %NULL if buffer does not
525 * cover the requested range.
526 */
527 static const guint8 *
528 buf_helper_find_peek (gpointer data, gint64 off, guint size)
529 {
530 GstTypeFindBufHelper *helper;
531
532 helper = (GstTypeFindBufHelper *) data;
533 GST_LOG_OBJECT (helper->obj,
534 "Typefind factory called peek (%" G_GINT64_FORMAT ", %u)", off, size);
535
536 if (size == 0)
537 return NULL;
538
539 if (off < 0) {
540 GST_LOG_OBJECT (helper->obj,
541 "Typefind factory wanted to peek at end; not supported");
542 return NULL;
543 }
544
545 /* If we request beyond the available size, we're sure we can't return
546 * anything regardless of the requested offset */
547 if (size > helper->size)
548 return NULL;
549
550 /* Only return data if there's enough room left for the given offset.
551 * This is the same as "if (off + size <= helper->size)" except that
552 * it doesn't exceed type limits */
553 if (off <= helper->size - size)
554 return helper->data + off;
555
556 return NULL;
557 }
558
559 /*
560 * buf_helper_find_suggest:
561 * @data: helper data struct
562 * @probability: probability of the match
563 * @caps: caps of the type
564 *
565 * If given @probability is higher, replace previously store caps.
566 */
567 static void
568 buf_helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
569 {
570 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
571
572 GST_LOG_OBJECT (helper->obj,
573 "Typefind factory called suggest (%u, %" GST_PTR_FORMAT ")",
574 probability, caps);
575
576 /* Note: not >= as we call typefinders in order of rank, highest first */
577 if (probability > helper->best_probability) {
578 gst_caps_replace (&helper->caps, caps);
579 helper->best_probability = probability;
580 }
581 }
582
583 static guint64
584 buf_helper_get_length (gpointer data)
585 {
586 GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
587
588 return helper->size;
589 }
590
591 /**
592 * gst_type_find_helper_for_data:
593 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
594 * @data: (transfer none) (array length=size): * a pointer with data to typefind
595 * @size: the size of @data
596 * @prob: (out) (optional): location to store the probability of the found
597 * caps, or %NULL
598 *
599 * Tries to find what type of data is contained in the given @data, the
600 * assumption being that the data represents the beginning of the stream or
601 * file.
602 *
603 * All available typefinders will be called on the data in order of rank. If
604 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
605 * typefinding is stopped immediately and the found caps will be returned
606 * right away. Otherwise, all available typefind functions will the tried,
607 * and the caps with the highest probability will be returned, or %NULL if
608 * the content of @data could not be identified.
609 *
610 * Free-function: gst_caps_unref
611 *
612 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
613 * or %NULL if no type could be found. The caller should free the caps
614 * returned with gst_caps_unref().
615 */
616 GstCaps *
617 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
618 GstTypeFindProbability * prob)
619 {
620 return gst_type_find_helper_for_data_with_extension (obj, data, size, NULL,
621 prob);
622 }
623
624 /**
625 * gst_type_find_helper_for_data_with_extension:
626 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
627 * @data: (transfer none) (array length=size): * a pointer with data to typefind
628 * @size: the size of @data
629 * @extension: (nullable): extension of the media, or %NULL
630 * @prob: (out) (optional): location to store the probability of the found
631 * caps, or %NULL
632 *
633 * Tries to find what type of data is contained in the given @data, the
634 * assumption being that the data represents the beginning of the stream or
635 * file.
636 *
637 * All available typefinders will be called on the data in order of rank. If
638 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
639 * typefinding is stopped immediately and the found caps will be returned
640 * right away. Otherwise, all available typefind functions will the tried,
641 * and the caps with the highest probability will be returned, or %NULL if
642 * the content of @data could not be identified.
643 *
644 * When @extension is not %NULL, this function will first try the typefind
645 * functions for the given extension, which might speed up the typefinding
646 * in many cases.
647 *
648 * Free-function: gst_caps_unref
649 *
650 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
651 * or %NULL if no type could be found. The caller should free the caps
652 * returned with gst_caps_unref().
653 *
654 * Since: 1.16
655 *
656 */
657 GstCaps *
658 gst_type_find_helper_for_data_with_extension (GstObject * obj,
659 const guint8 * data, gsize size, const gchar * extension,
660 GstTypeFindProbability * prob)
661 {
662 GstTypeFindBufHelper helper;
663 GstTypeFindFactory *factory;
664 GstTypeFind find;
665 GList *l, *type_list;
666 GstCaps *result = NULL;
667
668 g_return_val_if_fail (data != NULL, NULL);
669
670 helper.data = data;
671 helper.size = size;
672 helper.best_probability = GST_TYPE_FIND_NONE;
673 helper.caps = NULL;
674 helper.obj = obj;
675
676 if (helper.data == NULL || helper.size == 0)
677 return NULL;
678
679 find.data = &helper;
680 find.peek = buf_helper_find_peek;
681 find.suggest = buf_helper_find_suggest;
682 find.get_length = buf_helper_get_length;
683
684 type_list = gst_type_find_factory_get_list ();
685 type_list = prioritize_extension (obj, type_list, extension);
686
687 for (l = type_list; l; l = l->next) {
688 factory = GST_TYPE_FIND_FACTORY (l->data);
689 gst_type_find_factory_call_function (factory, &find);
690 if (helper.best_probability >= GST_TYPE_FIND_MAXIMUM)
691 break;
692 }
693 gst_plugin_feature_list_free (type_list);
694
695 if (helper.best_probability > 0)
696 result = helper.caps;
697
698 if (prob)
699 *prob = helper.best_probability;
700
701 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
702 result, (guint) helper.best_probability);
703
704 return result;
705 }
706
707 /**
708 * gst_type_find_helper_for_data_with_caps:
709 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
710 * @data: (transfer none) (array length=size): a pointer with data to typefind
711 * @size: the size of @data
712 * @caps: caps of the media
713 * @prob: (out) (optional): location to store the probability of the found
714 * caps, or %NULL
715 *
716 * Tries to find if type of media contained in the given @data, matches the
717 * @caps specified, assumption being that the data represents the beginning
718 * of the stream or file.
719 *
720 * Only the typefinder matching the given caps will be called, if found. The
721 * caps with the highest probability will be returned, or %NULL if the content
722 * of the @data could not be identified.
723 *
724 * Free-function: gst_caps_unref
725 *
726 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
727 * or %NULL if no type could be found. The caller should free the caps
728 * returned with gst_caps_unref().
729 *
730 * Since: 1.22
731 *
732 */
733 GstCaps *
734 gst_type_find_helper_for_data_with_caps (GstObject * obj,
735 const guint8 * data, gsize size, GstCaps * caps,
736 GstTypeFindProbability * prob)
737 {
738 GstTypeFind *find;
739 GstTypeFindData *find_data;
740 GList *l, *factories = NULL;
741 GstCaps *result = NULL;
742 GstTypeFindProbability last_found_probability;
743
744 g_return_val_if_fail (data != NULL, NULL);
745 g_return_val_if_fail (caps != NULL, NULL);
746 g_return_val_if_fail (size != 0, NULL);
747
748 find_data = gst_type_find_data_new (obj, data, size);
749 find = gst_type_find_data_get_typefind (find_data);
750
751 factories = gst_type_find_list_factories_for_caps (obj, caps);
752 if (!factories) {
753 GST_INFO_OBJECT (obj, "Failed to typefind for caps: %" GST_PTR_FORMAT,
754 caps);
755 goto out;
756 }
757
758 last_found_probability = GST_TYPE_FIND_NONE;
759
760 for (l = factories; l; l = l->next) {
761 GstTypeFindProbability found_probability;
762 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (l->data);
763
764 gst_type_find_factory_call_function (factory, find);
765
766 found_probability = gst_type_find_data_get_probability (find_data);
767
768 if (found_probability > last_found_probability) {
769 last_found_probability = found_probability;
770 result = gst_type_find_data_get_caps (find_data);
771
772 GST_DEBUG_OBJECT (obj, "Found %" GST_PTR_FORMAT " (probability = %u)",
773 result, (guint) last_found_probability);
774 if (last_found_probability >= GST_TYPE_FIND_MAXIMUM)
775 break;
776 }
777 }
778
779 if (prob)
780 *prob = last_found_probability;
781
782 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT " (probability = %u)",
783 result, (guint) last_found_probability);
784
785 out:
786 g_list_free_full (factories, (GDestroyNotify) gst_object_unref);
787
788 gst_type_find_data_free (find_data);
789
790 return result;
791 }
792
793 /**
794 * gst_type_find_helper_for_buffer:
795 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
796 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
797 * @prob: (out) (optional): location to store the probability of the found
798 * caps, or %NULL
799 *
800 * Tries to find what type of data is contained in the given #GstBuffer, the
801 * assumption being that the buffer represents the beginning of the stream or
802 * file.
803 *
804 * All available typefinders will be called on the data in order of rank. If
805 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
806 * typefinding is stopped immediately and the found caps will be returned
807 * right away. Otherwise, all available typefind functions will the tried,
808 * and the caps with the highest probability will be returned, or %NULL if
809 * the content of the buffer could not be identified.
810 *
811 * Free-function: gst_caps_unref
812 *
813 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
814 * or %NULL if no type could be found. The caller should free the caps
815 * returned with gst_caps_unref().
816 */
817 GstCaps *
818 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
819 GstTypeFindProbability * prob)
820 {
821 return gst_type_find_helper_for_buffer_with_extension (obj, buf, NULL, prob);
822 }
823
824 /**
825 * gst_type_find_helper_for_buffer_with_extension:
826 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
827 * @buf: (in) (transfer none): a #GstBuffer with data to typefind
828 * @extension: (nullable): extension of the media, or %NULL
829 * @prob: (out) (optional): location to store the probability of the found
830 * caps, or %NULL
831 *
832 * Tries to find what type of data is contained in the given #GstBuffer, the
833 * assumption being that the buffer represents the beginning of the stream or
834 * file.
835 *
836 * All available typefinders will be called on the data in order of rank. If
837 * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
838 * typefinding is stopped immediately and the found caps will be returned
839 * right away. Otherwise, all available typefind functions will the tried,
840 * and the caps with the highest probability will be returned, or %NULL if
841 * the content of the buffer could not be identified.
842 *
843 * When @extension is not %NULL, this function will first try the typefind
844 * functions for the given extension, which might speed up the typefinding
845 * in many cases.
846 *
847 * Free-function: gst_caps_unref
848 *
849 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
850 * or %NULL if no type could be found. The caller should free the caps
851 * returned with gst_caps_unref().
852 *
853 * Since: 1.16
854 *
855 */
856 GstCaps *
857 gst_type_find_helper_for_buffer_with_extension (GstObject * obj,
858 GstBuffer * buf, const gchar * extension, GstTypeFindProbability * prob)
859 {
860 GstCaps *result;
861 GstMapInfo info;
862
863 g_return_val_if_fail (buf != NULL, NULL);
864 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
865 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
866 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
867
868 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
869 return NULL;
870 result =
871 gst_type_find_helper_for_data_with_extension (obj, info.data, info.size,
872 extension, prob);
873 gst_buffer_unmap (buf, &info);
874
875 return result;
876 }
877
878 /**
879 * gst_type_find_helper_for_buffer_with_caps:
880 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
881 * @buf: (transfer none): a #GstBuffer with data to typefind
882 * @caps: caps of the media
883 * @prob: (out) (optional): location to store the probability of the found
884 * caps, or %NULL
885 *
886 * Tries to find if type of media contained in the given #GstBuffer, matches
887 * @caps specified, assumption being that the buffer represents the beginning
888 * of the stream or file.
889 *
890 * Tries to find what type of data is contained in the given @data, the
891 * assumption being that the data represents the beginning of the stream or
892 * file.
893 *
894 * Only the typefinder matching the given caps will be called, if found. The
895 * caps with the highest probability will be returned, or %NULL if the content
896 * of the @data could not be identified.
897 *
898 * Free-function: gst_caps_unref
899 *
900 * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
901 * or %NULL if no type could be found. The caller should free the caps
902 * returned with gst_caps_unref().
903 *
904 * Since: 1.22
905 *
906 */
907 GstCaps *
908 gst_type_find_helper_for_buffer_with_caps (GstObject * obj,
909 GstBuffer * buf, GstCaps * caps, GstTypeFindProbability * prob)
910 {
911 GstCaps *result;
912 GstMapInfo info;
913
914 g_return_val_if_fail (caps != NULL, NULL);
915 g_return_val_if_fail (buf != NULL, NULL);
916 g_return_val_if_fail (GST_IS_BUFFER (buf), NULL);
917 g_return_val_if_fail (GST_BUFFER_OFFSET (buf) == 0 ||
918 GST_BUFFER_OFFSET (buf) == GST_BUFFER_OFFSET_NONE, NULL);
919
920 if (!gst_buffer_map (buf, &info, GST_MAP_READ))
921 return NULL;
922
923 result =
924 gst_type_find_helper_for_data_with_caps (obj, info.data, info.size,
925 caps, prob);
926
927 gst_buffer_unmap (buf, &info);
928
929 return result;
930 }
931
932 /**
933 * gst_type_find_helper_for_extension:
934 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
935 * @extension: an extension
936 *
937 * Tries to find the best #GstCaps associated with @extension.
938 *
939 * All available typefinders will be checked against the extension in order
940 * of rank. The caps of the first typefinder that can handle @extension will be
941 * returned.
942 *
943 * Free-function: gst_caps_unref
944 *
945 * Returns: (transfer full) (nullable): the #GstCaps corresponding to
946 * @extension, or %NULL if no type could be found. The caller should free
947 * the caps returned with gst_caps_unref().
948 */
949 GstCaps *
950 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
951 {
952 GList *l, *type_list;
953 GstCaps *result = NULL;
954
955 g_return_val_if_fail (extension != NULL, NULL);
956
957 GST_LOG_OBJECT (obj, "finding caps for extension %s", extension);
958
959 type_list = gst_type_find_factory_get_list ();
960
961 for (l = type_list; l; l = g_list_next (l)) {
962 GstTypeFindFactory *factory;
963 const gchar *const *ext;
964
965 factory = GST_TYPE_FIND_FACTORY (l->data);
966
967 /* we only want to check those factories without a function */
968 if (gst_type_find_factory_has_function (factory))
969 continue;
970
971 /* get the extension that this typefind factory can handle */
972 ext = gst_type_find_factory_get_extensions (factory);
973 if (ext == NULL)
974 continue;
975
976 /* there are extension, see if one of them matches the requested
977 * extension */
978 while (*ext != NULL) {
979 if (strcmp (*ext, extension) == 0) {
980 /* we found a matching extension, take the caps */
981 if ((result = gst_type_find_factory_get_caps (factory))) {
982 gst_caps_ref (result);
983 goto done;
984 }
985 }
986 ++ext;
987 }
988 }
989 done:
990 gst_plugin_feature_list_free (type_list);
991
992 GST_LOG_OBJECT (obj, "Returning %" GST_PTR_FORMAT, result);
993
994 return result;
995 }
996
997 /**
998 * gst_type_find_list_factories_for_caps:
999 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
1000 * @caps: caps of the media
1001 *
1002 * Tries to find the best #GstTypeFindFactory associated with @caps.
1003 *
1004 * The typefinder that can handle @caps will be returned.
1005 *
1006 * Free-function: g_list_free
1007 *
1008 * Returns: (transfer full) (nullable) (element-type Gst.TypeFindFactory): the list of #GstTypeFindFactory
1009 * corresponding to @caps, or %NULL if no typefinder could be
1010 * found. Caller should free the returned list with g_list_free()
1011 * and list elements with gst_object_unref().
1012 *
1013 * Since: 1.22
1014 *
1015 */
1016 GList *
1017 gst_type_find_list_factories_for_caps (GstObject * obj, GstCaps * caps)
1018 {
1019 GList *l, *type_list, *factories = NULL;
1020
1021 g_return_val_if_fail (caps != NULL, NULL);
1022
1023 GST_LOG_OBJECT (obj, "finding factory for caps %" GST_PTR_FORMAT, caps);
1024
1025 type_list = gst_type_find_factory_get_list ();
1026
1027 for (l = type_list; l; l = g_list_next (l)) {
1028 GstTypeFindFactory *factory;
1029 GstCaps *factory_caps;
1030
1031 factory = GST_TYPE_FIND_FACTORY (l->data);
1032
1033 /* We only want to check those factories without a function */
1034 if (gst_type_find_factory_has_function (factory))
1035 continue;
1036
1037 /* Get the caps that this typefind factory can handle */
1038 factory_caps = gst_type_find_factory_get_caps (factory);
1039 if (!factory_caps)
1040 continue;
1041
1042 if (gst_caps_can_intersect (factory_caps, caps)) {
1043 factory = gst_object_ref (factory);
1044 factories = g_list_prepend (factories, factory);
1045 }
1046 }
1047
1048 gst_plugin_feature_list_free (type_list);
1049
1050 return g_list_reverse (factories);
1051 }
1052
1053 /**
1054 * gst_type_find_data_new: (skip)
1055 * @obj: (nullable): object doing the typefinding, or %NULL (used for logging)
1056 * @data: (transfer none) (array length=size): a pointer with data to typefind
1057 * @size: the size of @data
1058 *
1059 * Free-function: gst_type_find_data_free
1060 *
1061 * Returns: (transfer full): the #GstTypeFindData. The caller should free
1062 * the returned #GstTypeFindData with gst_type_find_data_free().
1063 *
1064 * Since: 1.22
1065 *
1066 */
1067 GstTypeFindData *
1068 gst_type_find_data_new (GstObject * obj, const guint8 * data, gsize size)
1069 {
1070 GstTypeFindData *find_data;
1071
1072 g_return_val_if_fail (data != NULL, NULL);
1073 g_return_val_if_fail (size != 0, NULL);
1074
1075 find_data = g_new0 (GstTypeFindData, 1);
1076
1077 find_data->helper.data = data;
1078 find_data->helper.size = size;
1079 find_data->helper.best_probability = GST_TYPE_FIND_NONE;
1080 find_data->helper.caps = NULL;
1081 find_data->helper.obj = obj;
1082
1083 find_data->find.data = (gpointer) (&find_data->helper);
1084 find_data->find.peek = buf_helper_find_peek;
1085 find_data->find.suggest = buf_helper_find_suggest;
1086 find_data->find.get_length = NULL;
1087
1088 return find_data;
1089 }
1090
1091 /**
1092 * gst_type_find_data_get_caps: (skip)
1093 * @data: GstTypeFindData *
1094 *
1095 * Returns #GstCaps associated with #GstTypeFindData
1096 *
1097 * Returns: (transfer full) (nullable): #GstCaps.
1098 *
1099 * Since: 1.22
1100 *
1101 */
1102 GstCaps *
1103 gst_type_find_data_get_caps (GstTypeFindData * data)
1104 {
1105 g_return_val_if_fail (data != NULL, NULL);
1106
1107 return gst_caps_ref (data->helper.caps);
1108 }
1109
1110 /**
1111 * gst_type_find_data_get_probability: (skip)
1112 * @data: GstTypeFindData *
1113 *
1114 * Returns #GstTypeFindProbability associated with #GstTypeFindData
1115 *
1116 * Returns: #GstTypeFindProbability.
1117 *
1118 * Since: 1.22
1119 *
1120 */
1121 GstTypeFindProbability
1122 gst_type_find_data_get_probability (GstTypeFindData * data)
1123 {
1124 g_return_val_if_fail (data != NULL, GST_TYPE_FIND_NONE);
1125
1126 return data->helper.best_probability;
1127 }
1128
1129 /**
1130 * gst_type_find_data_get_typefind: (skip)
1131 * @data: GstTypeFindData *
1132 *
1133 * Returns #GstTypeFind associated with #GstTypeFindData
1134 *
1135 * Returns: #GstTypeFind.
1136 *
1137 * Since: 1.22
1138 *
1139 */
1140 GstTypeFind *
1141 gst_type_find_data_get_typefind (GstTypeFindData * data)
1142 {
1143 g_return_val_if_fail (data != NULL, NULL);
1144
1145 return &data->find;
1146 }
1147
1148 /**
1149 * gst_type_find_data_free: (skip)
1150 * @data: GstTypeFindData * to free
1151 *
1152 * Since: 1.22
1153 *
1154 */
1155 void
1156 gst_type_find_data_free (GstTypeFindData * data)
1157 {
1158 if (data && data->helper.caps)
1159 gst_caps_unref (data->helper.caps);
1160
1161 g_free (data);
1162 }