2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3 * Copyright (C) 2005-2009 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6 * gsttypefindfunctions.c: collection of various typefind functions
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.
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.
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.
29 #include <glib/gprintf.h>
31 /* don't want to add gio xdgmime typefinder if gio was disabled via configure */
43 #include <gst/pbutils/pbutils.h>
44 #include <gst/base/gstbytereader.h>
46 GST_DEBUG_CATEGORY_STATIC (type_find_debug);
47 #define GST_CAT_DEFAULT type_find_debug
49 /* so our code stays ready for 0.11 */
50 #define gst_type_find_peek(tf,off,len) \
51 ((const guint8 *)gst_type_find_peek((tf),(off),(len)))
53 /* DataScanCtx: helper for typefind functions that scan through data
54 * step-by-step, to avoid doing a peek at each and every offset */
56 #define DATA_SCAN_CTX_CHUNK_SIZE 4096
66 data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip)
68 c->offset += bytes_to_skip;
69 if (G_LIKELY (c->size > bytes_to_skip)) {
70 c->size -= bytes_to_skip;
71 c->data += bytes_to_skip;
78 static inline gboolean
79 data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, gint min_len)
83 guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
85 if (G_LIKELY (c->size >= min_len))
88 data = gst_type_find_peek (tf, c->offset, chunk_len);
89 if (G_LIKELY (data != NULL)) {
95 /* if there's less than our chunk size, try to get as much as we can, but
96 * always at least min_len bytes (we might be typefinding the first buffer
97 * of the stream and not have as much data available as we'd like) */
98 len = gst_type_find_get_length (tf);
100 len = CLAMP (len - c->offset, min_len, chunk_len);
105 data = gst_type_find_peek (tf, c->offset, len);
115 static inline gboolean
116 data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset,
117 const gchar * data, guint len)
119 if (!data_scan_ctx_ensure_data (tf, c, offset + len))
122 return (memcmp (c->data + offset, data, len) == 0);
126 static gboolean xml_check_first_element (GstTypeFind * tf,
127 const gchar * element, guint elen, gboolean strict);
128 static gboolean sdp_check_header (GstTypeFind * tf);
130 static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
132 #define UTF8_CAPS gst_static_caps_get(&utf8_caps)
135 utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
136 GstTypeFindProbability * prob)
140 /* randomly decided values */
141 guint min_size = 16; /* minimum size */
142 guint size = 32 * 1024; /* starting size */
143 guint probability = 95; /* starting probability */
144 guint step = 10; /* how much we reduce probability in each
147 while (probability > step && size > min_size) {
148 data = gst_type_find_peek (tf, offset, size);
151 gchar *start = (gchar *) data;
153 if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) { /* allow last char to be cut off */
168 utf8_type_find (GstTypeFind * tf, gpointer unused)
170 GstTypeFindProbability start_prob, mid_prob;
173 /* leave xml to the xml typefinders */
174 if (xml_check_first_element (tf, "", 0, TRUE))
177 /* leave sdp to the sdp typefinders */
178 if (sdp_check_header (tf))
181 /* check beginning of stream */
182 if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob))
185 GST_LOG ("start is plain text with probability of %u", start_prob);
187 /* POSSIBLE is the highest probability we ever return if we can't
188 * probe into the middle of the file and don't know its length */
190 length = gst_type_find_get_length (tf);
191 if (length == 0 || length == (guint64) - 1) {
192 gst_type_find_suggest (tf, MIN (start_prob, GST_TYPE_FIND_POSSIBLE),
197 if (length < 64 * 1024) {
198 gst_type_find_suggest (tf, start_prob, UTF8_CAPS);
202 /* check middle of stream */
203 if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob))
206 GST_LOG ("middle is plain text with probability of %u", mid_prob);
207 gst_type_find_suggest (tf, (start_prob + mid_prob) / 2, UTF8_CAPS);
210 /*** text/utf-16 and text/utf-32} ***/
211 /* While UTF-8 is unicode too, using text/plain for UTF-16 and UTF-32
212 is going to break stuff. */
217 const char *const bom;
218 gboolean (*checker) (const guint8 *, gint, gint);
224 check_utf16 (const guint8 * data, gint len, gint endianness)
234 gst_byte_reader_init (&br, data, len);
236 /* test first for a single 16 bit value in the BMP */
237 if (endianness == G_BIG_ENDIAN)
238 gst_byte_reader_get_uint16_be (&br, &high);
240 gst_byte_reader_get_uint16_le (&br, &high);
241 if (high >= 0xD800 && high <= 0xDBFF) {
242 /* start of a surrogate pair */
246 if (endianness == G_BIG_ENDIAN)
247 gst_byte_reader_get_uint16_be (&br, &low);
249 gst_byte_reader_get_uint16_le (&br, &low);
250 if (low >= 0xDC00 && low <= 0xDFFF) {
251 /* second half of the surrogate pair */
255 if (high >= 0xDC00 && high <= 0xDFFF)
264 check_utf32 (const guint8 * data, gint len, gint endianness)
270 if (endianness == G_BIG_ENDIAN)
271 v = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
273 v = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
283 unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester,
284 guint n_tester, const char *media_type, gboolean require_bom)
288 const guint8 *data = gst_type_find_peek (tf, 0, len);
290 const gint max_scan_size = 256 * 1024;
295 data = gst_type_find_peek (tf, 0, len);
300 /* find a large enough size that works */
301 while (len < max_scan_size) {
302 size_t newlen = len << 1;
303 const guint8 *newdata = gst_type_find_peek (tf, 0, newlen);
310 for (n = 0; n < n_tester; ++n) {
311 int bom_boost = 0, tmpprob;
312 if (len >= tester[n].bomlen) {
313 if (!memcmp (data, tester[n].bom, tester[n].bomlen))
314 bom_boost = tester[n].boost;
316 if (require_bom && bom_boost == 0)
318 if (!(*tester[n].checker) (data, len, tester[n].endianness))
320 tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost;
321 if (tmpprob > prob) {
323 endianness = tester[n].endianness;
328 GST_DEBUG ("This is valid %s %s", media_type,
329 endianness == G_BIG_ENDIAN ? "be" : "le");
330 gst_type_find_suggest_simple (tf, prob, media_type,
331 "endianness", G_TYPE_INT, endianness, NULL);
335 static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16");
337 #define UTF16_CAPS gst_static_caps_get(&utf16_caps)
340 utf16_type_find (GstTypeFind * tf, gpointer unused)
342 static const GstUnicodeTester utf16tester[2] = {
343 {2, "\xff\xfe", check_utf16, 10, G_LITTLE_ENDIAN},
344 {2, "\xfe\xff", check_utf16, 20, G_BIG_ENDIAN},
346 unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester),
347 "text/utf-16", TRUE);
350 static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32");
352 #define UTF32_CAPS gst_static_caps_get(&utf32_caps)
355 utf32_type_find (GstTypeFind * tf, gpointer unused)
357 static const GstUnicodeTester utf32tester[2] = {
358 {4, "\xff\xfe\x00\x00", check_utf32, 10, G_LITTLE_ENDIAN},
359 {4, "\x00\x00\xfe\xff", check_utf32, 20, G_BIG_ENDIAN}
361 unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester),
362 "text/utf-32", TRUE);
365 /*** text/uri-list ***/
367 static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
369 #define URI_CAPS (gst_static_caps_get(&uri_caps))
370 #define BUFFER_SIZE 16 /* If the string is < 16 bytes we're screwed */
371 #define INC_BUFFER { \
373 if (pos == BUFFER_SIZE) { \
375 offset += BUFFER_SIZE; \
376 data = gst_type_find_peek (tf, offset, BUFFER_SIZE); \
377 if (data == NULL) return; \
383 uri_type_find (GstTypeFind * tf, gpointer unused)
385 const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
390 /* Search for # comment lines */
391 while (*data == '#') {
392 /* Goto end of line */
393 while (*data != '\n') {
400 if (!g_ascii_isalpha (*data)) {
401 /* Had a non alpha char - can't be uri-list */
407 while (g_ascii_isalnum (*data)) {
412 /* First non alpha char is not a : */
416 /* Get the next 2 bytes as well */
417 data = gst_type_find_peek (tf, offset + pos, 3);
421 if (data[1] != '/' && data[2] != '/') {
425 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, URI_CAPS);
429 /*** application/x-hls ***/
431 static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls");
432 #define HLS_CAPS (gst_static_caps_get(&hls_caps))
434 /* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */
436 hls_type_find (GstTypeFind * tf, gpointer unused)
438 DataScanCtx c = { 0, NULL, 0 };
440 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 7)))
443 if (memcmp (c.data, "#EXTM3U", 7))
446 data_scan_ctx_advance (tf, &c, 7);
448 /* Check only the first 256 bytes */
449 while (c.offset < 256) {
450 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21)))
453 /* Search for # comment lines */
454 if (c.data[0] == '#' && (memcmp (c.data, "#EXT-X-TARGETDURATION", 21) == 0
455 || memcmp (c.data, "#EXT-X-STREAM-INF", 17) == 0)) {
456 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HLS_CAPS);
460 data_scan_ctx_advance (tf, &c, 1);
465 /*** application/xml **********************************************************/
467 #define XML_BUFFER_SIZE 16
468 #define XML_INC_BUFFER { \
470 if (pos == XML_BUFFER_SIZE) { \
472 offset += XML_BUFFER_SIZE; \
473 data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE); \
474 if (data == NULL) return FALSE; \
480 #define XML_INC_BUFFER_DATA { \
482 if (pos >= length) { \
490 xml_check_first_element_from_data (const guint8 * data, guint length,
491 const gchar * element, guint elen, gboolean strict)
496 g_return_val_if_fail (data != NULL, FALSE);
501 /* look for the XMLDec
502 * see XML spec 2.8, Prolog and Document Type Declaration
503 * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
504 got_xmldec = (memcmp (data, "<?xml", 5) == 0);
506 if (strict && !got_xmldec)
509 /* skip XMLDec in any case if we've got one */
511 if (pos + 5 >= length)
517 /* look for the first element, it has to be the requested element. Bail
518 * out if it is not within the first 4kB. */
519 while (data && pos < MIN (4096, length)) {
520 while (*data != '<' && pos < MIN (4096, length)) {
525 if (!g_ascii_isalpha (*data)) {
526 /* if not alphabetic, it's a PI or an element / attribute declaration
527 * like <?xxx or <!xxx */
532 /* the first normal element, check if it's the one asked for */
533 if (pos + elen + 1 >= length)
535 return (data && element && strncmp ((char *) data, element, elen) == 0);
542 xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
550 data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
554 /* look for the XMLDec
555 * see XML spec 2.8, Prolog and Document Type Declaration
556 * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
557 got_xmldec = (memcmp (data, "<?xml", 5) == 0);
559 if (strict && !got_xmldec)
562 /* skip XMLDec in any case if we've got one */
568 /* look for the first element, it has to be the requested element. Bail
569 * out if it is not within the first 4kB. */
570 while (data && (offset + pos) < 4096) {
571 while (*data != '<' && (offset + pos) < 4096) {
576 if (!g_ascii_isalpha (*data)) {
577 /* if not alphabetic, it's a PI or an element / attribute declaration
578 * like <?xxx or <!xxx */
583 /* the first normal element, check if it's the one asked for */
584 data = gst_type_find_peek (tf, offset + pos, elen + 1);
585 return (data && element && strncmp ((char *) data, element, elen) == 0);
591 static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml");
593 #define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps))
595 xml_type_find (GstTypeFind * tf, gpointer unused)
597 if (xml_check_first_element (tf, "", 0, TRUE)) {
598 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
602 /*** application/sdp *********************************************************/
604 static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp");
606 #define SDP_CAPS (gst_static_caps_get(&sdp_caps))
608 sdp_check_header (GstTypeFind * tf)
612 data = gst_type_find_peek (tf, 0, 5);
616 /* sdp must start with v=0[\r]\n */
617 if (memcmp (data, "v=0", 3))
620 if (data[3] == '\r' && data[4] == '\n')
629 sdp_type_find (GstTypeFind * tf, gpointer unused)
631 if (sdp_check_header (tf))
632 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS);
635 /*** application/smil *********************************************************/
637 static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil");
639 #define SMIL_CAPS (gst_static_caps_get(&smil_caps))
641 smil_type_find (GstTypeFind * tf, gpointer unused)
643 if (xml_check_first_element (tf, "smil", 4, FALSE)) {
644 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
648 #ifdef GST_EXT_SS_TYPE
649 /*** application/x-ss [Microsoft's Smooth Streaming]***********************************/
651 static GstStaticCaps ss_caps = GST_STATIC_CAPS ("application/x-ss");
653 #define SS_CAPS (gst_static_caps_get(&ss_caps))
655 ss_type_find (GstTypeFind * tf, gpointer unused)
658 if (xml_check_first_element (tf, "SmoothStreamingMedia", 20, TRUE)) {
659 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SS_CAPS);
662 gboolean utf16_le, utf16_be;
663 const gchar *convert_from = NULL;
664 guint8 *converted_data;
666 /* try detecting the charset */
667 data = gst_type_find_peek (tf, 0, 2);
672 /* look for a possible BOM */
673 utf16_le = data[0] == 0xFF && data[1] == 0xFE;
674 utf16_be = data[0] == 0xFE && data[1] == 0xFF;
676 convert_from = "UTF-16LE";
677 } else if (utf16_be) {
678 convert_from = "UTF-16BE";
683 guint length = gst_type_find_get_length (tf);
685 /* try a default that should be enough */
688 data = gst_type_find_peek (tf, 0, length);
696 (guint8 *) g_convert ((gchar *) data, length, "UTF-8", convert_from,
697 NULL, &new_size, NULL);
698 if (converted_data) {
699 if (xml_check_first_element_from_data (converted_data, new_size,
700 "SmoothStreamingMedia", 20, TRUE))
701 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
704 g_free (converted_data);
715 static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html");
717 #define HTML_CAPS gst_static_caps_get (&html_caps)
720 html_type_find (GstTypeFind * tf, gpointer unused)
722 const gchar *d, *data;
724 data = (const gchar *) gst_type_find_peek (tf, 0, 16);
728 if (!g_ascii_strncasecmp (data, "<!DOCTYPE HTML", 14)) {
729 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
730 } else if (xml_check_first_element (tf, "html", 4, FALSE)) {
731 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
732 } else if ((d = memchr (data, '<', 16))) {
733 data = (const gchar *) gst_type_find_peek (tf, d - data, 6);
734 if (data && g_ascii_strncasecmp (data, "<html>", 6) == 0) {
735 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
742 static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi");
744 #define MID_CAPS gst_static_caps_get(&mid_caps)
746 mid_type_find (GstTypeFind * tf, gpointer unused)
748 const guint8 *data = gst_type_find_peek (tf, 0, 4);
750 /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
751 if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h'
753 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS);
756 #ifdef GST_EXT_MIME_TYPES
759 static GstStaticCaps xmf_caps = GST_STATIC_CAPS ("audio/xmf");
761 #define XMF_CAPS gst_static_caps_get(&xmf_caps)
763 xmf_type_find (GstTypeFind * tf, gpointer unused)
767 /* Search FileId "XMF_" 4 bytes */
768 data = gst_type_find_peek (tf, 0, 4);
769 if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
771 /* Search Format version "2.00" 4 bytes */
772 data = gst_type_find_peek (tf, 4, 4);
773 if (data && data[0] == '1' && data[1] == '.' && data[2] == '0'
775 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, XMF_CAPS);
781 /*** audio/mobile-xmf ***/
783 static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf");
785 #define MXMF_CAPS gst_static_caps_get(&mxmf_caps)
787 mxmf_type_find (GstTypeFind * tf, gpointer unused)
789 const guint8 *data = NULL;
791 /* Search FileId "XMF_" 4 bytes */
792 data = gst_type_find_peek (tf, 0, 4);
793 if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
795 /* Search Format version "2.00" 4 bytes */
796 data = gst_type_find_peek (tf, 4, 4);
797 if (data && data[0] == '2' && data[1] == '.' && data[2] == '0'
799 /* Search TypeId 2 1 byte */
800 data = gst_type_find_peek (tf, 11, 1);
801 if (data && data[0] == 2) {
802 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS);
808 #ifdef GST_EXT_MIME_TYPES
809 /*** audio/x-smaf ***/
810 static GstStaticCaps mmf_caps = GST_STATIC_CAPS ("audio/x-smaf");
812 #define MMF_CAPS gst_static_caps_get(&mmf_caps)
814 mmf_type_find (GstTypeFind * tf, gpointer unused)
818 /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
819 /* Search FileId "MMMD" 4 bytes */
820 data = gst_type_find_peek (tf, 0, 4);
821 if (data && data[0] == 'M' && data[1] == 'M' && data[2] == 'M'
823 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MMF_CAPS);
828 /*** video/x-fli ***/
830 static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
832 #define FLX_CAPS gst_static_caps_get(&flx_caps)
834 flx_type_find (GstTypeFind * tf, gpointer unused)
836 const guint8 *data = gst_type_find_peek (tf, 0, 134);
839 /* check magic and the frame type of the first frame */
840 if ((data[4] == 0x11 || data[4] == 0x12 ||
841 data[4] == 0x30 || data[4] == 0x44) &&
843 ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
844 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS);
848 data = gst_type_find_peek (tf, 0, 6);
850 /* check magic only */
851 if ((data[4] == 0x11 || data[4] == 0x12 ||
852 data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
853 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
859 /*** application/x-id3 ***/
861 static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
863 #define ID3_CAPS gst_static_caps_get(&id3_caps)
865 id3v2_type_find (GstTypeFind * tf, gpointer unused)
867 const guint8 *data = gst_type_find_peek (tf, 0, 10);
869 if (data && memcmp (data, "ID3", 3) == 0 &&
870 data[3] != 0xFF && data[4] != 0xFF &&
871 (data[6] & 0x80) == 0 && (data[7] & 0x80) == 0 &&
872 (data[8] & 0x80) == 0 && (data[9] & 0x80) == 0) {
873 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
878 id3v1_type_find (GstTypeFind * tf, gpointer unused)
880 const guint8 *data = gst_type_find_peek (tf, -128, 3);
882 if (data && memcmp (data, "TAG", 3) == 0) {
883 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
887 /*** application/x-ape ***/
889 static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag");
891 #define APETAG_CAPS gst_static_caps_get(&apetag_caps)
893 apetag_type_find (GstTypeFind * tf, gpointer unused)
897 /* APEv1/2 at start of file */
898 data = gst_type_find_peek (tf, 0, 8);
899 if (data && !memcmp (data, "APETAGEX", 8)) {
900 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
904 /* APEv1/2 at end of file */
905 data = gst_type_find_peek (tf, -32, 8);
906 if (data && !memcmp (data, "APETAGEX", 8)) {
907 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
912 /*** audio/x-ttafile ***/
914 static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
916 #define TTA_CAPS gst_static_caps_get(&tta_caps)
918 tta_type_find (GstTypeFind * tf, gpointer unused)
920 const guint8 *data = gst_type_find_peek (tf, 0, 3);
923 if (memcmp (data, "TTA", 3) == 0) {
924 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS);
930 /*** audio/x-flac ***/
931 static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");
933 #define FLAC_CAPS (gst_static_caps_get(&flac_caps))
936 flac_type_find (GstTypeFind * tf, gpointer unused)
938 DataScanCtx c = { 0, NULL, 0 };
940 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
943 /* standard flac (also old/broken flac-in-ogg with an initial 4-byte marker
944 * packet and without the usual packet framing) */
945 if (memcmp (c.data, "fLaC", 4) == 0) {
946 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
950 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
953 /* flac-in-ogg, see http://flac.sourceforge.net/ogg_mapping.html */
954 if (memcmp (c.data, "\177FLAC\001", 6) == 0) {
955 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
959 /* disabled because it happily typefinds /dev/urandom as audio/x-flac, and
960 * because I yet have to see header-less flac in the wild */
962 /* flac without headers (subset format) */
963 /* 64K should be enough */
964 while (c.offset < (64 * 1024)) {
965 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
968 /* look for frame header,
969 * http://flac.sourceforge.net/format.html#frame_header
971 if (c.data[0] == 0xff && (c.data[1] >> 2) == 0x3e) {
972 /* bit 15 in the header must be 0 */
973 if (((c.data[1] >> 1) & 0x01) == 0x01)
976 /* blocksize must be != 0x00 */
977 if ((c.data[2] >> 4) == 0x00)
980 /* samplerate must be != 0x0f */
981 if ((c.data[2] & 0x0f) == 0x0f)
983 /* also 0 is invalid, as it means get the info from the header and we
984 * don't have headers if we are here */
985 if ((c.data[2] & 0x0f) == 0x00)
988 /* channel assignment must be < 11 */
989 if ((c.data[3] >> 4) >= 11)
992 /* sample size must be != 0x07 and != 0x05 */
993 if (((c.data[3] >> 1) & 0x07) == 0x07)
995 if (((c.data[3] >> 1) & 0x07) == 0x05)
997 /* also 0 is invalid, as it means get the info from the header and we
998 * don't have headers if we are here */
999 if (((c.data[3] >> 1) & 0x07) == 0x00)
1002 /* next bit must be 0 */
1003 if ((c.data[3] & 0x01) == 0x01)
1006 /* FIXME: shouldn't we include the crc check ? */
1008 GST_DEBUG ("Found flac without headers at %d", (gint) c.offset);
1009 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS);
1013 data_scan_ctx_advance (tf, &c, 1);
1018 /*** audio/mpeg version 2, 4 ***/
1020 static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
1021 "mpegversion = (int) { 2, 4 }, framed = (bool) false");
1022 #define AAC_CAPS (gst_static_caps_get(&aac_caps))
1023 #define AAC_AMOUNT (4096)
1025 aac_type_find (GstTypeFind * tf, gpointer unused)
1027 /* LUT to convert the AudioObjectType from the ADTS header to a string */
1028 DataScanCtx c = { 0, NULL, 0 };
1030 while (c.offset < AAC_AMOUNT) {
1033 /* detect adts header or adif header.
1034 * The ADIF header is 4 bytes, that should be OK. The ADTS header, on
1035 * the other hand, is 14 bits only, so we require one valid frame with
1036 * again a valid syncpoint on the next one (28 bits) for certainty. We
1037 * require 4 kB, which is quite a lot, since frames are generally 200-400
1039 * LOAS has 2 possible syncwords, which are 11 bits and 16 bits long.
1040 * The following stream syntax depends on which one is found.
1042 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
1045 snc = GST_READ_UINT16_BE (c.data);
1046 if (G_UNLIKELY ((snc & 0xfff6) == 0xfff0)) {
1047 /* ADTS header - find frame length */
1048 GST_DEBUG ("Found one ADTS syncpoint at offset 0x%" G_GINT64_MODIFIER
1049 "x, tracing next...", c.offset);
1050 len = ((c.data[3] & 0x03) << 11) |
1051 (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);
1053 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1054 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1058 /* check if there's a second ADTS frame */
1059 snc = GST_READ_UINT16_BE (c.data + len);
1060 if ((snc & 0xfff6) == 0xfff0) {
1062 guint mpegversion, sample_freq_idx, channel_config, profile_idx, rate;
1063 guint8 audio_config[2];
1065 mpegversion = (c.data[1] & 0x08) ? 2 : 4;
1066 profile_idx = c.data[2] >> 6;
1067 sample_freq_idx = ((c.data[2] & 0x3c) >> 2);
1068 channel_config = ((c.data[2] & 0x01) << 2) + (c.data[3] >> 6);
1070 GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%"
1071 G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len);
1073 /* 0xd and 0xe are reserved. 0xf means the sample frequency is directly
1074 * specified in the header, but that's not allowed for ADTS */
1075 if (sample_freq_idx > 0xc) {
1076 GST_DEBUG ("Unexpected sample frequency index %d or wrong sync",
1081 rate = gst_codec_utils_aac_get_sample_rate_from_index (sample_freq_idx);
1082 GST_LOG ("ADTS: profile=%u, rate=%u", profile_idx, rate);
1084 /* The ADTS frame header is slightly different from the
1085 * AudioSpecificConfig defined for the MPEG-4 container, so we just
1086 * construct enough of it for getting the level here. */
1087 /* ADTS counts profiles from 0 instead of 1 to save bits */
1088 audio_config[0] = (profile_idx + 1) << 3;
1089 audio_config[0] |= (sample_freq_idx >> 1) & 0x7;
1090 audio_config[1] = (sample_freq_idx & 0x1) << 7;
1091 audio_config[1] |= (channel_config & 0xf) << 3;
1093 caps = gst_caps_new_simple ("audio/mpeg",
1094 "framed", G_TYPE_BOOLEAN, FALSE,
1095 "mpegversion", G_TYPE_INT, mpegversion,
1096 "stream-format", G_TYPE_STRING, "adts", NULL);
1098 gst_codec_utils_aac_caps_set_level_and_profile (caps, audio_config, 2);
1100 /* add rate and number of channels if we can */
1101 if (channel_config != 0 && channel_config <= 7) {
1102 const guint channels_map[] = { 0, 1, 2, 3, 4, 5, 6, 8 };
1104 gst_caps_set_simple (caps, "channels", G_TYPE_INT,
1105 channels_map[channel_config], "rate", G_TYPE_INT, rate, NULL);
1108 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps);
1109 #ifdef GST_EXT_TYPEFIND_ENHANCEMENT
1110 /* find more aac sync to select correctly */
1112 /* check if there's a third ADTS frame */
1113 len = ((c.data[3] & 0x03) << 11) | (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);
1114 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1115 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1118 snc = GST_READ_UINT16_BE (c.data + len);
1119 if ((snc & 0xfff6) == 0xfff0) {
1120 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 5, caps); /* 85% */
1121 GST_DEBUG ("Find 3rd Sync..probability is %u ", GST_TYPE_FIND_LIKELY + 5);
1124 /* check if there's a fourth ADTS frame */
1125 len = ((c.data[3] & 0x03) << 11) | (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);
1126 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1127 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1130 snc = GST_READ_UINT16_BE (c.data + len);
1131 if ((snc & 0xfff6) == 0xfff0) {
1132 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, caps); /* 90% */
1133 GST_DEBUG ("Find 4th Sync..probability is %u ", GST_TYPE_FIND_LIKELY + 10);
1136 /* check if there's a fifth(last) ADTS frame */
1137 len = ((c.data[3] & 0x03) << 11) | (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);
1138 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1139 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1142 snc = GST_READ_UINT16_BE (c.data + len);
1143 if ((snc & 0xfff6) == 0xfff0) {
1144 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, caps); /* 99% */
1145 GST_DEBUG ("Find 5th Sync..probability is %u ", GST_TYPE_FIND_NEARLY_CERTAIN);
1148 gst_caps_unref (caps);
1152 GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
1153 } else if (G_UNLIKELY (((snc & 0xffe0) == 0x56e0) || (snc == 0x4de1))) {
1156 GST_DEBUG ("Found one LOAS syncword at offset 0x%" G_GINT64_MODIFIER
1157 "x, tracing next...", c.offset);
1159 /* check length of frame for each type of detectable LOAS streams */
1160 if (snc == 0x4de1) {
1161 /* EPAudioSyncStream */
1162 len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
1163 ((c.data[4] & 0x80) >> 7);
1164 /* add size of EP sync stream header */
1167 /* AudioSyncStream */
1168 len = ((c.data[1] & 0x1f) << 8) | c.data[2];
1169 /* add size of sync stream header */
1173 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1174 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1178 /* check if there's a second LOAS frame */
1179 snc = GST_READ_UINT16_BE (c.data + len);
1180 if (((snc & 0xffe0) == 0x56e0) || (snc == 0x4de1)) {
1181 GST_DEBUG ("Found second LOAS syncword at offset 0x%"
1182 G_GINT64_MODIFIER "x, framelen %u", c.offset, len);
1184 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
1185 "framed", G_TYPE_BOOLEAN, FALSE,
1186 "mpegversion", G_TYPE_INT, 4,
1187 "stream-format", G_TYPE_STRING, "loas", NULL);
1191 GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
1192 } else if (!memcmp (c.data, "ADIF", 4)) {
1194 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
1195 "framed", G_TYPE_BOOLEAN, FALSE, "mpegversion", G_TYPE_INT, 4,
1196 "stream-format", G_TYPE_STRING, "adif", NULL);
1202 data_scan_ctx_advance (tf, &c, 1);
1206 /*** audio/mpeg version 1 ***/
1209 * The chance that random data is identified as a valid mp3 header is 63 / 2^18
1210 * (0.024%) per try. This makes the function for calculating false positives
1211 * 1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize)
1212 * This has the following probabilities of false positives:
1213 * datasize MIN_HEADERS
1215 * 4096 62.6% 0.02% 0% 0%
1216 * 16384 98% 0.09% 0% 0%
1217 * 1 MiB 100% 5.88% 0% 0%
1218 * 1 GiB 100% 100% 1.44% 0%
1219 * 1 TiB 100% 100% 100% 0.35%
1220 * This means that the current choice (3 headers by most of the time 4096 byte
1221 * buffers is pretty safe for now.
1223 * The max. size of each frame is 1440 bytes, which means that for N frames to
1224 * be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 bytes of data.
1225 * Assuming we step into the stream right after the frame header, this
1226 * means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes
1227 * of data (5762) to always detect any mp3.
1230 static const guint mp3types_bitrates[2][3][16] =
1231 { {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
1232 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
1233 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
1234 {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
1235 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
1236 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}},
1239 static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
1240 {22050, 24000, 16000},
1241 {44100, 48000, 32000}
1245 mp3_type_frame_length_from_header (guint32 header, guint * put_layer,
1246 guint * put_channels, guint * put_bitrate, guint * put_samplerate,
1247 gboolean * may_be_free_format, gint possible_free_framelen)
1249 guint bitrate, layer, length, mode, samplerate, version, channels;
1251 if ((header & 0xffe00000) != 0xffe00000)
1254 /* we don't need extension, copyright, original or
1255 * emphasis for the frame length */
1259 mode = header & 0x3;
1263 length = header & 0x1;
1266 /* sampling frequency */
1267 samplerate = header & 0x3;
1268 if (samplerate == 3)
1273 bitrate = header & 0xF;
1274 if (bitrate == 0 && possible_free_framelen == -1) {
1275 GST_LOG ("Possibly a free format mp3 - signaling");
1276 *may_be_free_format = TRUE;
1278 if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1))
1281 /* ignore error correction, too */
1285 layer = 4 - (header & 0x3);
1290 /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */
1291 version = header & 0x3;
1296 channels = (mode == 3) ? 1 : 2;
1297 samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
1301 length += possible_free_framelen;
1302 bitrate = length * samplerate / 48000;
1304 length += possible_free_framelen;
1305 bitrate = length * samplerate /
1306 ((layer == 3 && version != 3) ? 72000 : 144000);
1310 bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
1312 length = ((12000 * bitrate / samplerate) + length) * 4;
1314 length += ((layer == 3
1315 && version != 3) ? 72000 : 144000) * bitrate / samplerate;
1319 GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
1321 ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
1322 " - channels = %u", samplerate, bitrate, layer, version, channels);
1327 *put_channels = channels;
1329 *put_bitrate = bitrate;
1331 *put_samplerate = samplerate;
1337 static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, "
1338 "mpegversion = (int) 1, layer = (int) [ 1, 3 ]");
1339 #define MP3_CAPS (gst_static_caps_get(&mp3_caps))
1341 * random values for typefinding
1342 * if no more data is available, we will return a probability of
1343 * (found_headers/TRY_HEADERS) * (MAXIMUM * (TRY_SYNC - bytes_skipped)
1345 * if found_headers >= MIN_HEADERS
1347 #define GST_MP3_TYPEFIND_MIN_HEADERS (2)
1348 #define GST_MP3_TYPEFIND_TRY_HEADERS (5)
1349 #define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */
1350 #define GST_MP3_TYPEFIND_SYNC_SIZE (2048)
1351 #define GST_MP3_WRONG_HEADER (10)
1354 mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
1355 guint * found_layer, GstTypeFindProbability * found_prob)
1357 const guint8 *data = NULL;
1358 const guint8 *data_end = NULL;
1361 gint last_free_offset = -1;
1362 gint last_free_framelen = -1;
1363 gboolean headerstart = TRUE;
1370 while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) {
1372 size = GST_MP3_TYPEFIND_SYNC_SIZE * 2;
1375 data = gst_type_find_peek (tf, skipped + start_off, size);
1376 } while (size > 10 && !data);
1379 data_end = data + size;
1381 if (*data == 0xFF) {
1382 const guint8 *head_data = NULL;
1383 guint layer = 0, bitrate, samplerate, channels;
1384 guint found = 0; /* number of valid headers found */
1385 guint64 offset = skipped;
1386 gboolean changed = FALSE;
1388 while (found < GST_MP3_TYPEFIND_TRY_HEADERS) {
1391 guint prev_layer = 0;
1392 guint prev_channels = 0, prev_samplerate = 0;
1393 gboolean free = FALSE;
1395 if ((gint64) (offset - skipped + 4) >= 0 &&
1396 data + offset - skipped + 4 < data_end) {
1397 head_data = data + offset - skipped;
1399 head_data = gst_type_find_peek (tf, offset + start_off, 4);
1403 head = GST_READ_UINT32_BE (head_data);
1404 if (!(length = mp3_type_frame_length_from_header (head, &layer,
1405 &channels, &bitrate, &samplerate, &free,
1406 last_free_framelen))) {
1408 if (last_free_offset == -1)
1409 last_free_offset = offset;
1411 last_free_framelen = offset - last_free_offset;
1412 offset = last_free_offset;
1416 last_free_framelen = -1;
1419 /* Mark the fact that we didn't find a valid header at the beginning */
1421 headerstart = FALSE;
1423 GST_LOG ("%d. header at offset %" G_GUINT64_FORMAT
1424 " (0x%" G_GINT64_MODIFIER "x) was not an mp3 header "
1425 "(possibly-free: %s)", found + 1, start_off + offset,
1426 start_off + offset, free ? "yes" : "no");
1429 if ((prev_layer && prev_layer != layer) ||
1430 /* (prev_bitrate && prev_bitrate != bitrate) || <-- VBR */
1431 (prev_samplerate && prev_samplerate != samplerate) ||
1432 (prev_channels && prev_channels != channels)) {
1433 /* this means an invalid property, or a change, which might mean
1434 * that this is not a mp3 but just a random bytestream. It could
1435 * be a freaking funky encoded mp3 though. We'll just not count
1440 prev_channels = channels;
1441 prev_samplerate = samplerate;
1444 GST_LOG ("found %d. header at offset %" G_GUINT64_FORMAT " (0x%"
1445 G_GINT64_MODIFIER "X)", found, start_off + offset,
1446 start_off + offset);
1450 g_assert (found <= GST_MP3_TYPEFIND_TRY_HEADERS);
1451 if (head_data == NULL &&
1452 gst_type_find_peek (tf, offset + start_off - 1, 1) == NULL)
1453 /* Incomplete last frame - don't count it. */
1455 if (found == GST_MP3_TYPEFIND_TRY_HEADERS ||
1456 (found >= GST_MP3_TYPEFIND_MIN_HEADERS && head_data == NULL)) {
1457 /* we can make a valid guess */
1458 guint probability = found * GST_TYPE_FIND_MAXIMUM *
1459 (GST_MP3_TYPEFIND_TRY_SYNC - skipped) /
1460 GST_MP3_TYPEFIND_TRY_HEADERS / GST_MP3_TYPEFIND_TRY_SYNC;
1463 && probability > (GST_TYPE_FIND_MINIMUM + GST_MP3_WRONG_HEADER))
1464 probability -= GST_MP3_WRONG_HEADER;
1465 if (probability < GST_TYPE_FIND_MINIMUM)
1466 probability = GST_TYPE_FIND_MINIMUM;
1470 probability = (probability + GST_TYPE_FIND_MAXIMUM) / 2;
1473 ("audio/mpeg calculated %u = %u * %u / %u * (%u - %"
1474 G_GUINT64_FORMAT ") / %u", probability, GST_TYPE_FIND_MAXIMUM,
1475 found, GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC,
1476 (guint64) skipped, GST_MP3_TYPEFIND_TRY_SYNC);
1477 /* make sure we're not id3 tagged */
1478 head_data = gst_type_find_peek (tf, -128, 3);
1479 if (head_data && (memcmp (head_data, "TAG", 3) == 0)) {
1482 g_assert (probability <= GST_TYPE_FIND_MAXIMUM);
1484 *found_prob = probability;
1485 if (probability > 0)
1486 *found_layer = layer;
1497 mp3_type_find (GstTypeFind * tf, gpointer unused)
1499 GstTypeFindProbability prob, mid_prob;
1501 guint layer, mid_layer;
1504 mp3_type_find_at_offset (tf, 0, &layer, &prob);
1505 length = gst_type_find_get_length (tf);
1507 if (length == 0 || length == (guint64) - 1) {
1513 /* if we're pretty certain already, skip the additional check */
1514 if (prob >= GST_TYPE_FIND_LIKELY)
1517 mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob);
1521 GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob);
1527 if (layer != mid_layer) {
1528 GST_WARNING ("audio/mpeg layer discrepancy: %u vs. %u", layer, mid_layer);
1529 return; /* FIXME: or should we just go with the one in the middle? */
1532 /* detected mpeg audio both in middle of the file and at the start */
1533 prob = (prob + mid_prob) / 2;
1537 /* let's see if there's a valid header right at the start */
1538 data = gst_type_find_peek (tf, 0, 4); /* use min. frame size? */
1539 if (data && mp3_type_frame_length_from_header (GST_READ_UINT32_BE (data),
1540 &layer, NULL, NULL, NULL, NULL, 0) != 0) {
1542 prob = GST_TYPE_FIND_POSSIBLE - 10;
1544 prob = MAX (GST_TYPE_FIND_POSSIBLE - 10, prob + 10);
1554 g_return_if_fail (layer >= 1 && layer <= 3);
1556 gst_type_find_suggest_simple (tf, prob, "audio/mpeg",
1557 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
1561 /*** audio/x-musepack ***/
1563 static GstStaticCaps musepack_caps =
1564 GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }");
1566 #define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps))
1568 musepack_type_find (GstTypeFind * tf, gpointer unused)
1570 const guint8 *data = gst_type_find_peek (tf, 0, 4);
1571 GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM;
1572 gint streamversion = -1;
1574 if (data && memcmp (data, "MP+", 3) == 0) {
1576 if ((data[3] & 0x7f) == 7) {
1577 prop = GST_TYPE_FIND_MAXIMUM;
1579 prop = GST_TYPE_FIND_LIKELY + 10;
1581 } else if (data && memcmp (data, "MPCK", 4) == 0) {
1583 prop = GST_TYPE_FIND_MAXIMUM;
1586 if (streamversion != -1) {
1587 gst_type_find_suggest_simple (tf, prop, "audio/x-musepack",
1588 "streamversion", G_TYPE_INT, streamversion, NULL);
1592 /*** audio/x-ac3 ***/
1593 /* FIXME 0.11: should be audio/ac3, but isn't for backwards compatibility */
1594 static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3");
1596 #define AC3_CAPS (gst_static_caps_get(&ac3_caps))
1598 static GstStaticCaps eac3_caps = GST_STATIC_CAPS ("audio/x-eac3");
1600 #define EAC3_CAPS (gst_static_caps_get(&eac3_caps))
1604 unsigned short bit_rate;
1605 unsigned short frm_size[3];
1608 static const struct ac3_frmsize ac3_frmsizecod_tbl[] = {
1611 {40, {80, 87, 120}},
1612 {40, {80, 88, 120}},
1613 {48, {96, 104, 144}},
1614 {48, {96, 105, 144}},
1615 {56, {112, 121, 168}},
1616 {56, {112, 122, 168}},
1617 {64, {128, 139, 192}},
1618 {64, {128, 140, 192}},
1619 {80, {160, 174, 240}},
1620 {80, {160, 175, 240}},
1621 {96, {192, 208, 288}},
1622 {96, {192, 209, 288}},
1623 {112, {224, 243, 336}},
1624 {112, {224, 244, 336}},
1625 {128, {256, 278, 384}},
1626 {128, {256, 279, 384}},
1627 {160, {320, 348, 480}},
1628 {160, {320, 349, 480}},
1629 {192, {384, 417, 576}},
1630 {192, {384, 418, 576}},
1631 {224, {448, 487, 672}},
1632 {224, {448, 488, 672}},
1633 {256, {512, 557, 768}},
1634 {256, {512, 558, 768}},
1635 {320, {640, 696, 960}},
1636 {320, {640, 697, 960}},
1637 {384, {768, 835, 1152}},
1638 {384, {768, 836, 1152}},
1639 {448, {896, 975, 1344}},
1640 {448, {896, 976, 1344}},
1641 {512, {1024, 1114, 1536}},
1642 {512, {1024, 1115, 1536}},
1643 {576, {1152, 1253, 1728}},
1644 {576, {1152, 1254, 1728}},
1645 {640, {1280, 1393, 1920}},
1646 {640, {1280, 1394, 1920}}
1650 ac3_type_find (GstTypeFind * tf, gpointer unused)
1652 DataScanCtx c = { 0, NULL, 0 };
1654 /* Search for an ac3 frame; not necessarily right at the start, but give it
1655 * a lower probability if not found right at the start. Check that the
1656 * frame is followed by a second frame at the expected offset.
1657 * We could also check the two ac3 CRCs, but we don't do that right now */
1658 while (c.offset < 1024) {
1659 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5)))
1662 if (c.data[0] == 0x0b && c.data[1] == 0x77) {
1663 guint bsid = c.data[5] >> 3;
1667 guint fscod = c.data[4] >> 6;
1668 guint frmsizecod = c.data[4] & 0x3f;
1670 if (fscod < 3 && frmsizecod < 38) {
1671 DataScanCtx c_next = c;
1674 frame_size = ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod];
1675 GST_LOG ("possible AC3 frame sync at offset %"
1676 G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1677 if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1678 data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1680 if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1681 fscod = c_next.data[4] >> 6;
1682 frmsizecod = c_next.data[4] & 0x3f;
1684 if (fscod < 3 && frmsizecod < 38) {
1685 GstTypeFindProbability prob;
1687 GST_LOG ("found second AC3 frame (size=%u), looks good",
1688 ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]);
1690 prob = GST_TYPE_FIND_MAXIMUM;
1692 prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1694 gst_type_find_suggest (tf, prob, AC3_CAPS);
1698 GST_LOG ("no second AC3 frame found, false sync");
1702 } else if (bsid <= 16 && bsid > 10) {
1704 DataScanCtx c_next = c;
1707 frame_size = (((c.data[2] & 0x07) << 8) + c.data[3]) + 1;
1708 GST_LOG ("possible E-AC3 frame sync at offset %"
1709 G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1710 if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1711 data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1713 if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1714 GstTypeFindProbability prob;
1716 GST_LOG ("found second E-AC3 frame, looks good");
1718 prob = GST_TYPE_FIND_MAXIMUM;
1720 prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1722 gst_type_find_suggest (tf, prob, EAC3_CAPS);
1725 GST_LOG ("no second E-AC3 frame found, false sync");
1729 GST_LOG ("invalid AC3 BSID: %u", bsid);
1732 data_scan_ctx_advance (tf, &c, 1);
1736 /*** audio/x-dts ***/
1737 static GstStaticCaps dts_caps = GST_STATIC_CAPS ("audio/x-dts");
1738 #define DTS_CAPS (gst_static_caps_get (&dts_caps))
1739 #define DTS_MIN_FRAMESIZE 96
1740 #define DTS_MAX_FRAMESIZE 18725 /* 16384*16/14 */
1743 dts_parse_frame_header (DataScanCtx * c, guint * frame_size,
1744 guint * sample_rate, guint * channels, guint * depth, guint * endianness)
1746 static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
1747 22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
1749 static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
1754 guint num_blocks, chans, lfe, i;
1756 marker = GST_READ_UINT32_BE (c->data);
1758 /* raw big endian or 14-bit big endian */
1759 if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
1760 *endianness = G_BIG_ENDIAN;
1761 for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1762 hdr[i] = GST_READ_UINT16_BE (c->data + (i * sizeof (guint16)));
1764 /* raw little endian or 14-bit little endian */
1765 if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
1766 *endianness = G_LITTLE_ENDIAN;
1767 for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1768 hdr[i] = GST_READ_UINT16_LE (c->data + (i * sizeof (guint16)));
1773 GST_LOG ("dts sync marker 0x%08x at offset %u", marker, (guint) c->offset);
1776 if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
1777 if ((hdr[2] & 0xFFF0) != 0x07F0)
1779 /* discard top 2 bits (2 void), shift in 2 */
1780 hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
1781 /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
1782 hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
1783 hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
1784 hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
1785 hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
1786 hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
1787 hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
1788 g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
1794 GST_LOG ("frame header: %04x%04x%04x%04x", hdr[2], hdr[3], hdr[4], hdr[5]);
1796 num_blocks = (hdr[2] >> 2) & 0x7F;
1797 *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
1798 chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
1799 *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
1800 lfe = (hdr[5] >> 9) & 0x03;
1802 if (num_blocks < 5 || *frame_size < 96 || *sample_rate == 0)
1805 if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
1806 *frame_size = (*frame_size * 16) / 14; /* FIXME: round up? */
1808 if (chans < G_N_ELEMENTS (channels_table))
1809 *channels = channels_table[chans] + ((lfe) ? 1 : 0);
1817 dts_type_find (GstTypeFind * tf, gpointer unused)
1819 DataScanCtx c = { 0, NULL, 0 };
1821 /* Search for an dts frame; not necessarily right at the start, but give it
1822 * a lower probability if not found right at the start. Check that the
1823 * frame is followed by a second frame at the expected offset. */
1824 while (c.offset <= DTS_MAX_FRAMESIZE) {
1825 guint frame_size = 0, rate = 0, chans = 0, depth = 0, endianness = 0;
1827 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, DTS_MIN_FRAMESIZE)))
1830 if (G_UNLIKELY (dts_parse_frame_header (&c, &frame_size, &rate, &chans,
1831 &depth, &endianness))) {
1832 GstTypeFindProbability prob;
1835 prob = (c.offset == 0) ? GST_TYPE_FIND_LIKELY : GST_TYPE_FIND_POSSIBLE;
1837 /* check for second frame sync */
1839 data_scan_ctx_advance (tf, &next_c, frame_size);
1840 if (data_scan_ctx_ensure_data (tf, &next_c, 4)) {
1841 GST_LOG ("frame size: %u 0x%04x", frame_size, frame_size);
1842 GST_MEMDUMP ("second frame sync", next_c.data, 4);
1843 if (GST_READ_UINT32_BE (c.data) == GST_READ_UINT32_BE (next_c.data))
1844 prob = GST_TYPE_FIND_MAXIMUM;
1848 gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1849 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
1850 "depth", G_TYPE_INT, depth, "endianness", G_TYPE_INT, endianness,
1851 "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1853 gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1854 "rate", G_TYPE_INT, rate, "depth", G_TYPE_INT, depth,
1855 "endianness", G_TYPE_INT, endianness,
1856 "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1862 data_scan_ctx_advance (tf, &c, 1);
1868 /* can only be detected by using the extension, in which case we use the default
1870 static GstStaticCaps gsm_caps =
1871 GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1");
1873 #define GSM_CAPS (gst_static_caps_get(&gsm_caps))
1877 static GstStaticCaps wavpack_caps =
1878 GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false");
1880 #define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps))
1882 static GstStaticCaps wavpack_correction_caps =
1883 GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");
1885 #define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps))
1888 wavpack_type_find (GstTypeFind * tf, gpointer unused)
1894 data = gst_type_find_peek (tf, 0, 32);
1898 if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k')
1901 /* Note: wavpack blocks can be fairly large (easily 60-110k), possibly
1902 * larger than the max. limits imposed by certain typefinding elements
1903 * like id3demux or apedemux, so typefinding is most likely only going to
1904 * work in pull-mode */
1905 blocksize = GST_READ_UINT32_LE (data + 4);
1906 GST_LOG ("wavpack header, blocksize=0x%04x", blocksize);
1908 while (offset < 32 + blocksize) {
1911 /* get chunk header */
1912 GST_LOG ("peeking at chunk at offset 0x%04x", (guint) offset);
1913 data = gst_type_find_peek (tf, offset, 4);
1916 sublen = ((guint32) data[1]) << 1;
1917 if (data[0] & 0x80) {
1918 sublen |= (((guint32) data[2]) << 9) | (((guint32) data[3]) << 17);
1919 sublen += 1 + 3; /* id + length */
1921 sublen += 1 + 1; /* id + length */
1923 if (sublen > blocksize - offset + 32) {
1924 GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
1925 blocksize - offset);
1928 if ((data[0] & 0x20) == 0) {
1929 switch (data[0] & 0x0f) {
1930 case 0xa: /* ID_WV_BITSTREAM */
1931 case 0xc: /* ID_WVX_BITSTREAM */
1932 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, WAVPACK_CAPS);
1934 case 0xb: /* ID_WVC_BITSTREAM */
1935 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY,
1936 WAVPACK_CORRECTION_CAPS);
1946 /*** application/postscrip ***/
1947 static GstStaticCaps postscript_caps =
1948 GST_STATIC_CAPS ("application/postscript");
1950 #define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps))
1953 postscript_type_find (GstTypeFind * tf, gpointer unused)
1955 const guint8 *data = gst_type_find_peek (tf, 0, 3);
1959 if (data[0] == 0x04)
1961 if (data[0] == '%' && data[1] == '!')
1962 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS);
1966 /*** image/svg+xml ***/
1967 static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml");
1969 #define SVG_CAPS (gst_static_caps_get(&svg_caps))
1972 svg_type_find (GstTypeFind * tf, gpointer unused)
1974 static const gchar svg_doctype[] = "!DOCTYPE svg";
1975 static const gchar svg_tag[] = "<svg";
1976 DataScanCtx c = { 0, NULL, 0 };
1978 while (c.offset <= 1024) {
1979 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
1982 if (memcmp (svg_doctype, c.data, 12) == 0) {
1983 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS);
1985 } else if (memcmp (svg_tag, c.data, 4) == 0) {
1986 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS);
1989 data_scan_ctx_advance (tf, &c, 1);
1993 /*** multipart/x-mixed-replace mimestream ***/
1995 static GstStaticCaps multipart_caps =
1996 GST_STATIC_CAPS ("multipart/x-mixed-replace");
1997 #define MULTIPART_CAPS gst_static_caps_get(&multipart_caps)
1999 /* multipart/x-mixed replace is:
2000 * <maybe some whitespace>--<some ascii chars>[\r]\n
2001 * <more ascii chars>[\r]\nContent-type:<more ascii>[\r]\n */
2003 multipart_type_find (GstTypeFind * tf, gpointer unused)
2008 #define MULTIPART_MAX_BOUNDARY_OFFSET 16
2009 data = gst_type_find_peek (tf, 0, MULTIPART_MAX_BOUNDARY_OFFSET);
2014 x - data < MULTIPART_MAX_BOUNDARY_OFFSET - 2 && g_ascii_isspace (*x);
2016 if (x[0] != '-' || x[1] != '-')
2019 /* Could be okay, peek what should be enough for a complete header */
2020 #define MULTIPART_MAX_HEADER_SIZE 256
2021 data = gst_type_find_peek (tf, 0, MULTIPART_MAX_HEADER_SIZE);
2025 for (x = data; x - data < MULTIPART_MAX_HEADER_SIZE - 14; x++) {
2026 if (!isascii (*x)) {
2030 !g_ascii_strncasecmp ("content-type:", (gchar *) x + 1, 13)) {
2031 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MULTIPART_CAPS);
2037 /*** video/mpeg systemstream ***/
2038 static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, "
2039 "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]");
2041 #define MPEG_SYS_CAPS gst_static_caps_get(&mpeg_sys_caps)
2042 #define IS_MPEG_HEADER(data) (G_UNLIKELY((((guint8 *)(data))[0] == 0x00) && \
2043 (((guint8 *)(data))[1] == 0x00) && \
2044 (((guint8 *)(data))[2] == 0x01)))
2046 #define IS_MPEG_PACK_CODE(b) ((b) == 0xBA)
2047 #define IS_MPEG_SYS_CODE(b) ((b) == 0xBB)
2048 #define IS_MPEG_PACK_HEADER(data) (IS_MPEG_HEADER (data) && \
2049 IS_MPEG_PACK_CODE (((guint8 *)(data))[3]))
2051 #define IS_MPEG_PES_CODE(b) (((b) & 0xF0) == 0xE0 || ((b) & 0xF0) == 0xC0 || \
2053 #define IS_MPEG_PES_HEADER(data) (IS_MPEG_HEADER (data) && \
2054 IS_MPEG_PES_CODE (((guint8 *)(data))[3]))
2056 #define MPEG2_MAX_PROBE_LENGTH (128 * 1024) /* 128kB should be 64 packs of the
2057 * most common 2kB pack size. */
2059 #define MPEG2_MIN_SYS_HEADERS 2
2060 #define MPEG2_MAX_SYS_HEADERS 5
2063 mpeg_sys_is_valid_pack (GstTypeFind * tf, const guint8 * data, guint len,
2066 /* Check the pack header @ offset for validity, assuming that the 4 byte header
2067 * itself has already been checked. */
2073 /* Check marker bits */
2074 if ((data[4] & 0xC4) == 0x44) {
2079 if ((data[6] & 0x04) != 0x04 ||
2080 (data[8] & 0x04) != 0x04 ||
2081 (data[9] & 0x01) != 0x01 || (data[12] & 0x03) != 0x03)
2084 stuff_len = data[13] & 0x07;
2086 /* Check the following header bytes, if we can */
2087 if ((14 + stuff_len + 4) <= len) {
2088 if (!IS_MPEG_HEADER (data + 14 + stuff_len))
2092 *pack_size = 14 + stuff_len;
2094 } else if ((data[4] & 0xF1) == 0x21) {
2096 if ((data[6] & 0x01) != 0x01 ||
2097 (data[8] & 0x01) != 0x01 ||
2098 (data[9] & 0x80) != 0x80 || (data[11] & 0x01) != 0x01)
2101 /* Check the following header bytes, if we can */
2102 if ((12 + 4) <= len) {
2103 if (!IS_MPEG_HEADER (data + 12))
2115 mpeg_sys_is_valid_pes (GstTypeFind * tf, const guint8 * data, guint len,
2118 guint pes_packet_len;
2120 /* Check the PES header at the given position, assuming the header code itself
2121 * was already checked */
2125 /* For MPEG Program streams, unbounded PES is not allowed, so we must have a
2126 * valid length present */
2127 pes_packet_len = GST_READ_UINT16_BE (data + 4);
2128 if (pes_packet_len == 0)
2131 /* Check the following header, if we can */
2132 if (6 + pes_packet_len + 4 <= len) {
2133 if (!IS_MPEG_HEADER (data + 6 + pes_packet_len))
2138 *pack_size = 6 + pes_packet_len;
2143 mpeg_sys_is_valid_sys (GstTypeFind * tf, const guint8 * data, guint len,
2148 /* Check the System header at the given position, assuming the header code itself
2149 * was already checked */
2152 sys_hdr_len = GST_READ_UINT16_BE (data + 4);
2153 if (sys_hdr_len < 6)
2156 /* Check the following header, if we can */
2157 if (6 + sys_hdr_len + 4 <= len) {
2158 if (!IS_MPEG_HEADER (data + 6 + sys_hdr_len))
2163 *pack_size = 6 + sys_hdr_len;
2168 /* calculation of possibility to identify random data as mpeg systemstream:
2169 * bits that must match in header detection: 32 (or more)
2170 * chance that random data is identifed: 1/2^32
2171 * chance that MPEG2_MIN_PACK_HEADERS headers are identified:
2172 * 1/2^(32*MPEG2_MIN_PACK_HEADERS)
2173 * chance that this happens in MPEG2_MAX_PROBE_LENGTH bytes:
2174 * 1-(1+1/2^(32*MPEG2_MIN_PACK_HEADERS)^MPEG2_MAX_PROBE_LENGTH)
2175 * for current values:
2176 * 1-(1+1/2^(32*4)^101024)
2178 * Since we also check marker bits and pes packet lengths, this probability is a
2179 * very coarse upper bound.
2182 mpeg_sys_type_find (GstTypeFind * tf, gpointer unused)
2184 const guint8 *data, *data0, *first_sync, *end;
2185 gint mpegversion = 0;
2186 guint pack_headers = 0;
2187 guint pes_headers = 0;
2189 guint since_last_sync = 0;
2190 guint32 sync_word = 0xffffffff;
2195 len = MPEG2_MAX_PROBE_LENGTH;
2198 data = gst_type_find_peek (tf, 0, 5 + len);
2199 } while (data == NULL && len >= 32);
2211 while (data < end) {
2213 if (sync_word == 0x00000100) {
2214 /* Found potential sync word */
2215 if (first_sync == NULL)
2216 first_sync = data - 3;
2218 if (since_last_sync > 4) {
2219 /* If more than 4 bytes since the last sync word, reset our counters,
2220 * as we're only interested in counting contiguous packets */
2221 pes_headers = pack_headers = 0;
2225 if (IS_MPEG_PACK_CODE (data[0])) {
2226 if ((data[1] & 0xC0) == 0x40) {
2229 } else if ((data[1] & 0xF0) == 0x20) {
2232 if (mpegversion != 0 &&
2233 mpeg_sys_is_valid_pack (tf, data - 3, end - data + 3, &pack_size)) {
2236 } else if (IS_MPEG_PES_CODE (data[0])) {
2238 if (mpeg_sys_is_valid_pes (tf, data - 3, end - data + 3, &pack_size)) {
2240 if (mpegversion == 0)
2243 } else if (IS_MPEG_SYS_CODE (data[0])) {
2244 if (mpeg_sys_is_valid_sys (tf, data - 3, end - data + 3, &pack_size)) {
2249 /* If we found a packet with a known size, skip the bytes in it and loop
2250 * around to check the next packet. */
2251 if (pack_size != 0) {
2252 data += pack_size - 3;
2253 sync_word = 0xffffffff;
2254 since_last_sync = 0;
2259 sync_word |= data[0];
2263 /* If we have found MAX headers, and *some* were pes headers (pack headers
2264 * are optional in an mpeg system stream) then return our high-probability
2266 if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MAX_SYS_HEADERS)
2270 /* If we at least saw MIN headers, and *some* were pes headers (pack headers
2271 * are optional in an mpeg system stream) then return a lower-probability
2273 if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MIN_SYS_HEADERS)
2281 prob = GST_TYPE_FIND_POSSIBLE + (10 * (pack_headers + pes_headers));
2282 prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2284 /* lower probability if the first packet wasn't right at the start */
2285 if (data0 != first_sync && prob >= 10)
2288 GST_LOG ("Suggesting MPEG %d system stream, %d packs, %d pes, prob %u%%\n",
2289 mpegversion, pack_headers, pes_headers, prob);
2291 gst_type_find_suggest_simple (tf, prob, "video/mpeg",
2292 "systemstream", G_TYPE_BOOLEAN, TRUE,
2293 "mpegversion", G_TYPE_INT, mpegversion, NULL);
2297 /*** video/mpegts Transport Stream ***/
2298 static GstStaticCaps mpegts_caps = GST_STATIC_CAPS ("video/mpegts, "
2299 "systemstream = (boolean) true, packetsize = (int) [ 188, 208 ]");
2300 #define MPEGTS_CAPS gst_static_caps_get(&mpegts_caps)
2302 #define GST_MPEGTS_TYPEFIND_MIN_HEADERS 4
2303 #define GST_MPEGTS_TYPEFIND_MAX_HEADERS 10
2304 #define GST_MPEGTS_MAX_PACKET_SIZE 208
2305 #define GST_MPEGTS_TYPEFIND_SYNC_SIZE \
2306 (GST_MPEGTS_TYPEFIND_MIN_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2307 #define GST_MPEGTS_TYPEFIND_MAX_SYNC \
2308 (GST_MPEGTS_TYPEFIND_MAX_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2309 #define GST_MPEGTS_TYPEFIND_SCAN_LENGTH \
2310 (GST_MPEGTS_TYPEFIND_MAX_SYNC * 4)
2312 #define MPEGTS_HDR_SIZE 4
2313 /* Check for sync byte, error_indicator == 0 and packet has payload */
2314 #define IS_MPEGTS_HEADER(data) (((data)[0] == 0x47) && \
2315 (((data)[1] & 0x80) == 0x00) && \
2316 (((data)[3] & 0x30) != 0x00))
2318 /* Helper function to search ahead at intervals of packet_size for mpegts
2321 mpeg_ts_probe_headers (GstTypeFind * tf, guint64 offset, gint packet_size)
2323 /* We always enter this function having found at least one header already */
2325 const guint8 *data = NULL;
2327 GST_LOG ("looking for mpeg-ts packets of size %u", packet_size);
2328 while (found < GST_MPEGTS_TYPEFIND_MAX_HEADERS) {
2329 offset += packet_size;
2331 data = gst_type_find_peek (tf, offset, MPEGTS_HDR_SIZE);
2332 if (data == NULL || !IS_MPEGTS_HEADER (data))
2336 GST_LOG ("mpeg-ts sync #%2d at offset %" G_GUINT64_FORMAT, found, offset);
2342 /* Try and detect at least 4 packets in at most 10 packets worth of
2343 * data. Need to try several possible packet sizes */
2345 mpeg_ts_type_find (GstTypeFind * tf, gpointer unused)
2347 /* TS packet sizes to test: normal, DVHS packet size and
2348 * FEC with 16 or 20 byte codes packet size. */
2349 const gint pack_sizes[] = { 188, 192, 204, 208 };
2350 const guint8 *data = NULL;
2352 guint64 skipped = 0;
2354 while (skipped < GST_MPEGTS_TYPEFIND_SCAN_LENGTH) {
2355 if (size < MPEGTS_HDR_SIZE) {
2356 data = gst_type_find_peek (tf, skipped, GST_MPEGTS_TYPEFIND_SYNC_SIZE);
2359 size = GST_MPEGTS_TYPEFIND_SYNC_SIZE;
2362 /* Have at least MPEGTS_HDR_SIZE bytes at this point */
2363 if (IS_MPEGTS_HEADER (data)) {
2366 GST_LOG ("possible mpeg-ts sync at offset %" G_GUINT64_FORMAT, skipped);
2368 for (p = 0; p < G_N_ELEMENTS (pack_sizes); p++) {
2371 /* Probe ahead at size pack_sizes[p] */
2372 found = mpeg_ts_probe_headers (tf, skipped, pack_sizes[p]);
2373 if (found >= GST_MPEGTS_TYPEFIND_MIN_HEADERS) {
2376 /* found at least 4 headers. 10 headers = MAXIMUM probability.
2377 * Arbitrarily, I assigned 10% probability for each header we
2378 * found, 40% -> 100% */
2379 probability = MIN (10 * found, GST_TYPE_FIND_MAXIMUM);
2381 gst_type_find_suggest_simple (tf, probability, "video/mpegts",
2382 "systemstream", G_TYPE_BOOLEAN, TRUE,
2383 "packetsize", G_TYPE_INT, pack_sizes[p], NULL);
2394 #define GST_MPEGVID_TYPEFIND_TRY_PICTURES 6
2395 #define GST_MPEGVID_TYPEFIND_TRY_SYNC (100 * 1024) /* 100 kB */
2397 /* Scan ahead a maximum of max_extra_offset bytes until the next IS_MPEG_HEADER
2398 * offset. After the call, offset will be after the 0x000001, i.e. at the 4th
2399 * byte of the MPEG header. Returns TRUE if a header was found, FALSE if not.
2402 mpeg_find_next_header (GstTypeFind * tf, DataScanCtx * c,
2403 guint64 max_extra_offset)
2405 guint64 extra_offset;
2407 for (extra_offset = 0; extra_offset <= max_extra_offset; ++extra_offset) {
2408 if (!data_scan_ctx_ensure_data (tf, c, 4))
2410 if (IS_MPEG_HEADER (c->data)) {
2411 data_scan_ctx_advance (tf, c, 3);
2414 data_scan_ctx_advance (tf, c, 1);
2419 /*** video/mpeg MPEG-4 elementary video stream ***/
2421 static GstStaticCaps mpeg4_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2422 "systemstream=(boolean)false, mpegversion=4, parsed=(boolean)false");
2423 #define MPEG4_VIDEO_CAPS gst_static_caps_get(&mpeg4_video_caps)
2426 * This typefind is based on the elementary video header defined in
2427 * http://xhelmboyx.tripod.com/formats/mpeg-layout.txt
2428 * In addition, it allows the visual object sequence header to be
2429 * absent, and even the VOS header to be absent. In the latter case,
2430 * a number of VOPs have to be present.
2433 mpeg4_video_type_find (GstTypeFind * tf, gpointer unused)
2435 DataScanCtx c = { 0, NULL, 0 };
2436 gboolean seen_vios_at_0 = FALSE;
2437 gboolean seen_vios = FALSE;
2438 gboolean seen_vos = FALSE;
2439 gboolean seen_vol = FALSE;
2440 guint num_vop_headers = 0;
2443 while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2444 if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2447 if (!mpeg_find_next_header (tf, &c,
2448 GST_MPEGVID_TYPEFIND_TRY_SYNC - c.offset))
2453 /* visual_object_sequence_start_code */
2456 break; /* Terminate at second vios */
2458 seen_vios_at_0 = TRUE;
2460 data_scan_ctx_advance (tf, &c, 2);
2461 if (!mpeg_find_next_header (tf, &c, 0))
2466 /* Optional metadata */
2468 if (!mpeg_find_next_header (tf, &c, 24))
2472 /* visual_object_start_code (consider it optional) */
2474 data_scan_ctx_advance (tf, &c, 2);
2475 /* may contain ID marker and YUV clamping */
2476 if (!mpeg_find_next_header (tf, &c, 7))
2482 /* video_object_start_code */
2485 break; /* Terminate at second vos */
2487 data_scan_ctx_advance (tf, &c, 2);
2491 /* video_object_layer_start_code */
2492 if (sc >= 0x20 && sc <= 0x2F) {
2494 data_scan_ctx_advance (tf, &c, 5);
2498 /* video_object_plane_start_code */
2501 data_scan_ctx_advance (tf, &c, 2);
2505 /* Unknown start code. */
2508 if (num_vop_headers > 0 || seen_vol) {
2509 GstTypeFindProbability probability = 0;
2511 GST_LOG ("Found %d pictures, vios: %d, vos:%d, vol:%d", num_vop_headers,
2512 seen_vios, seen_vos, seen_vol);
2514 if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios_at_0
2515 && seen_vos && seen_vol)
2516 probability = GST_TYPE_FIND_MAXIMUM - 1;
2517 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios
2518 && seen_vos && seen_vol)
2519 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2520 else if (seen_vios_at_0 && seen_vos && seen_vol)
2521 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2522 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vos
2524 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2525 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vol)
2526 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2527 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2528 probability = GST_TYPE_FIND_LIKELY - 1;
2529 else if (num_vop_headers > 2 && seen_vios && seen_vos && seen_vol)
2530 probability = GST_TYPE_FIND_LIKELY - 9;
2531 else if (seen_vios && seen_vos && seen_vol)
2532 probability = GST_TYPE_FIND_LIKELY - 20;
2533 else if (num_vop_headers > 0 && seen_vos && seen_vol)
2534 probability = GST_TYPE_FIND_POSSIBLE;
2535 else if (num_vop_headers > 0)
2536 probability = GST_TYPE_FIND_POSSIBLE - 10;
2537 else if (seen_vos && seen_vol)
2538 probability = GST_TYPE_FIND_POSSIBLE - 20;
2540 gst_type_find_suggest (tf, probability, MPEG4_VIDEO_CAPS);
2544 /*** video/x-h263 H263 video stream ***/
2545 static GstStaticCaps h263_video_caps = GST_STATIC_CAPS ("video/x-h263");
2547 #define H263_VIDEO_CAPS gst_static_caps_get(&h263_video_caps)
2549 #define H263_MAX_PROBE_LENGTH (128 * 1024)
2552 h263_video_type_find (GstTypeFind * tf, gpointer unused)
2554 DataScanCtx c = { 0, NULL, 0 };
2562 while (c.offset < H263_MAX_PROBE_LENGTH) {
2563 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2566 /* Find the picture start code */
2567 data = (data << 8) + c.data[0];
2568 psc = data & G_GUINT64_CONSTANT (0xfffffc0000);
2569 if (psc == 0x800000) {
2572 tr = (data & 0x3fc) >> 2;
2576 /* Now that we have a Valid PSC, check if we also have a valid PTYPE and
2577 the Source Format, which should range between 1 and 5 */
2578 if (((tr >> 6) == 0x2) && (format > 0 && format < 6))
2583 /* FIXME: maybe bail out early if we get mostly bad syncs ? */
2586 data_scan_ctx_advance (tf, &c, 1);
2589 if (good > 0 && bad == 0)
2590 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H263_VIDEO_CAPS);
2591 else if (good > 2 * bad)
2592 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H263_VIDEO_CAPS);
2597 /*** video/x-h264 H264 elementary video stream ***/
2599 static GstStaticCaps h264_video_caps =
2600 GST_STATIC_CAPS ("video/x-h264,stream-format=byte-stream");
2602 #define H264_VIDEO_CAPS gst_static_caps_get(&h264_video_caps)
2604 #define H264_MAX_PROBE_LENGTH (128 * 1024) /* 128kB for HD should be enough. */
2607 h264_video_type_find (GstTypeFind * tf, gpointer unused)
2609 DataScanCtx c = { 0, NULL, 0 };
2611 /* Stream consists of: a series of sync codes (00 00 00 01) followed
2618 while (c.offset < H264_MAX_PROBE_LENGTH) {
2619 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2622 if (IS_MPEG_HEADER (c.data)) {
2623 nut = c.data[3] & 0x9f; /* forbiden_zero_bit | nal_unit_type */
2624 ref = c.data[3] & 0x60; /* nal_ref_idc */
2626 /* if forbidden bit is different to 0 won't be h264 */
2632 /* collect statistics about the NAL types */
2633 if ((nut >= 1 && nut <= 13) || nut == 19) {
2634 if ((nut == 5 && ref == 0) ||
2635 ((nut == 6 || (nut >= 9 && nut <= 12)) && ref != 0)) {
2640 } else if (nut >= 14 && nut <= 33) {
2642 /* Theoretically these are good, since if they exist in the
2643 stream it merely means that a newer backwards-compatible
2644 h.264 stream. But we should be identifying that separately. */
2647 /* unspecified, application specific */
2648 /* don't consider these bad */
2651 GST_DEBUG ("good %d bad %d", good, bad);
2653 if (good >= 10 && bad < 4) {
2654 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H264_VIDEO_CAPS);
2658 data_scan_ctx_advance (tf, &c, 4);
2660 data_scan_ctx_advance (tf, &c, 1);
2663 if (good >= 2 && bad < 1) {
2664 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H264_VIDEO_CAPS);
2669 /*** video/mpeg video stream ***/
2671 static GstStaticCaps mpeg_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2672 "systemstream = (boolean) false");
2673 #define MPEG_VIDEO_CAPS gst_static_caps_get(&mpeg_video_caps)
2676 * Idea is the same as MPEG system stream typefinding: We check each
2677 * byte of the stream to see if - from that point on - the stream
2678 * matches a predefined set of marker bits as defined in the MPEG
2681 * I'm sure someone will do a chance calculation here too.
2685 mpeg_video_stream_type_find (GstTypeFind * tf, gpointer unused)
2687 DataScanCtx c = { 0, NULL, 0 };
2688 gboolean seen_seq_at_0 = FALSE;
2689 gboolean seen_seq = FALSE;
2690 gboolean seen_gop = FALSE;
2691 guint64 last_pic_offset = 0;
2692 guint num_pic_headers = 0;
2695 while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2696 if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2699 if (!data_scan_ctx_ensure_data (tf, &c, 5))
2702 if (!IS_MPEG_HEADER (c.data))
2705 /* a pack header indicates that this isn't an elementary stream */
2706 if (c.data[3] == 0xBA && mpeg_sys_is_valid_pack (tf, c.data, c.size, NULL))
2709 /* do we have a sequence header? */
2710 if (c.data[3] == 0xB3) {
2711 seen_seq_at_0 = seen_seq_at_0 || (c.offset == 0);
2713 data_scan_ctx_advance (tf, &c, 4 + 8);
2717 /* or a GOP header */
2718 if (c.data[3] == 0xB8) {
2720 data_scan_ctx_advance (tf, &c, 8);
2724 /* but what we'd really like to see is a picture header */
2725 if (c.data[3] == 0x00) {
2727 last_pic_offset = c.offset;
2728 data_scan_ctx_advance (tf, &c, 8);
2732 /* ... each followed by a slice header with slice_vertical_pos=1 that's
2733 * not too far away from the previously seen picture header. */
2734 if (c.data[3] == 0x01 && num_pic_headers > found &&
2735 (c.offset - last_pic_offset) >= 4 &&
2736 (c.offset - last_pic_offset) <= 64) {
2737 data_scan_ctx_advance (tf, &c, 4);
2744 data_scan_ctx_advance (tf, &c, 1);
2747 if (found > 0 || seen_seq) {
2748 GstTypeFindProbability probability = 0;
2750 GST_LOG ("Found %d pictures, seq:%d, gop:%d", found, seen_seq, seen_gop);
2752 if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq && seen_gop)
2753 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2754 else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq)
2755 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2756 else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2757 probability = GST_TYPE_FIND_LIKELY;
2758 else if (seen_seq_at_0 && seen_gop && found > 2)
2759 probability = GST_TYPE_FIND_LIKELY - 10;
2760 else if (seen_seq && seen_gop && found > 2)
2761 probability = GST_TYPE_FIND_LIKELY - 20;
2762 else if (seen_seq_at_0 && found > 0)
2763 probability = GST_TYPE_FIND_POSSIBLE;
2764 else if (seen_seq && found > 0)
2765 probability = GST_TYPE_FIND_POSSIBLE - 5;
2767 probability = GST_TYPE_FIND_POSSIBLE - 10;
2769 probability = GST_TYPE_FIND_POSSIBLE - 20;
2771 gst_type_find_suggest_simple (tf, probability, "video/mpeg",
2772 "systemstream", G_TYPE_BOOLEAN, FALSE,
2773 "mpegversion", G_TYPE_INT, 1, NULL);
2777 /*** audio/x-aiff ***/
2779 static GstStaticCaps aiff_caps = GST_STATIC_CAPS ("audio/x-aiff");
2781 #define AIFF_CAPS gst_static_caps_get(&aiff_caps)
2783 aiff_type_find (GstTypeFind * tf, gpointer unused)
2785 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2787 if (data && memcmp (data, "FORM", 4) == 0) {
2789 if (memcmp (data, "AIFF", 4) == 0 || memcmp (data, "AIFC", 4) == 0)
2790 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AIFF_CAPS);
2794 /*** audio/x-svx ***/
2796 static GstStaticCaps svx_caps = GST_STATIC_CAPS ("audio/x-svx");
2798 #define SVX_CAPS gst_static_caps_get(&svx_caps)
2800 svx_type_find (GstTypeFind * tf, gpointer unused)
2802 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2804 if (data && memcmp (data, "FORM", 4) == 0) {
2806 if (memcmp (data, "8SVX", 4) == 0 || memcmp (data, "16SV", 4) == 0)
2807 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVX_CAPS);
2811 /*** audio/x-shorten ***/
2813 static GstStaticCaps shn_caps = GST_STATIC_CAPS ("audio/x-shorten");
2815 #define SHN_CAPS gst_static_caps_get(&shn_caps)
2817 shn_type_find (GstTypeFind * tf, gpointer unused)
2819 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2821 if (data && memcmp (data, "ajkg", 4) == 0) {
2822 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2824 data = gst_type_find_peek (tf, -8, 8);
2825 if (data && memcmp (data, "SHNAMPSK", 8) == 0) {
2826 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2830 /*** application/x-ape ***/
2832 static GstStaticCaps ape_caps = GST_STATIC_CAPS ("application/x-ape");
2834 #define APE_CAPS gst_static_caps_get(&ape_caps)
2836 ape_type_find (GstTypeFind * tf, gpointer unused)
2838 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2840 if (data && memcmp (data, "MAC ", 4) == 0) {
2841 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, APE_CAPS);
2845 /*** ISO FORMATS ***/
2847 /*** audio/x-m4a ***/
2849 static GstStaticCaps m4a_caps = GST_STATIC_CAPS ("audio/x-m4a");
2851 #define M4A_CAPS (gst_static_caps_get(&m4a_caps))
2853 m4a_type_find (GstTypeFind * tf, gpointer unused)
2855 const guint8 *data = gst_type_find_peek (tf, 4, 8);
2857 if (data && (memcmp (data, "ftypM4A ", 8) == 0)) {
2858 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, M4A_CAPS);
2862 /*** application/x-3gp ***/
2864 /* The Q is there because variables can't start with a number. */
2865 static GstStaticCaps q3gp_caps = GST_STATIC_CAPS ("application/x-3gp");
2866 #define Q3GP_CAPS (gst_static_caps_get(&q3gp_caps))
2868 static const gchar *
2869 q3gp_type_find_get_profile (const guint8 * data)
2871 switch (GST_MAKE_FOURCC (data[0], data[1], data[2], 0)) {
2872 case GST_MAKE_FOURCC ('3', 'g', 'g', 0):
2874 case GST_MAKE_FOURCC ('3', 'g', 'p', 0):
2876 #ifdef GST_EXT_MIME_TYPES /* add extended keyword for differentiating */
2877 case GST_MAKE_FOURCC ('3', 'g', '2', 0):
2879 case GST_MAKE_FOURCC ('i', 's', 'm', 0): {
2880 g_print ("fragment file format...\n\n");
2881 return "fragmented";
2884 case GST_MAKE_FOURCC ('3', 'g', 's', 0):
2885 return "streaming-server";
2886 case GST_MAKE_FOURCC ('3', 'g', 'r', 0):
2887 return "progressive-download";
2892 #ifdef GST_EXT_MIME_TYPES
2893 if (GST_MAKE_FOURCC (data[0], data[1], data[2], data[3]) == GST_MAKE_FOURCC ('p', 'i', 'f', 'f')) {
2894 g_print ("piff fragmented...\n\n");
2895 return "fragmented";
2903 q3gp_type_find (GstTypeFind * tf, gpointer unused)
2905 const gchar *profile;
2906 guint32 ftyp_size = 0;
2908 const guint8 *data = NULL;
2910 if ((data = gst_type_find_peek (tf, 0, 12)) == NULL) {
2915 if (memcmp (data, "ftyp", 4) != 0) {
2919 /* check major brand */
2921 if ((profile = q3gp_type_find_get_profile (data))) {
2922 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
2923 "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
2927 /* check compatible brands */
2928 if ((data = gst_type_find_peek (tf, 0, 4)) != NULL) {
2929 ftyp_size = GST_READ_UINT32_BE (data);
2931 for (offset = 16; offset < ftyp_size; offset += 4) {
2932 if ((data = gst_type_find_peek (tf, offset, 3)) == NULL) {
2935 if ((profile = q3gp_type_find_get_profile (data))) {
2936 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
2937 "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
2946 /*** video/mj2 and image/jp2 ***/
2947 static GstStaticCaps mj2_caps = GST_STATIC_CAPS ("video/mj2");
2949 #define MJ2_CAPS gst_static_caps_get(&mj2_caps)
2951 static GstStaticCaps jp2_caps = GST_STATIC_CAPS ("image/jp2");
2953 #define JP2_CAPS gst_static_caps_get(&jp2_caps)
2956 jp2_type_find (GstTypeFind * tf, gpointer unused)
2960 data = gst_type_find_peek (tf, 0, 24);
2965 if (memcmp (data, "\000\000\000\014jP \015\012\207\012", 12) != 0)
2968 /* check ftyp box */
2970 if (memcmp (data + 4, "ftyp", 4) == 0) {
2971 if (memcmp (data + 8, "jp2 ", 4) == 0)
2972 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, JP2_CAPS);
2973 else if (memcmp (data + 8, "mjp2", 4) == 0)
2974 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MJ2_CAPS);
2978 /*** video/quicktime ***/
2980 static GstStaticCaps qt_caps = GST_STATIC_CAPS ("video/quicktime");
2982 #define QT_CAPS gst_static_caps_get(&qt_caps)
2983 #define STRNCMP(x,y,z) (strncmp ((char*)(x), (char*)(y), z))
2985 /* FIXME 0.11: go through http://www.ftyps.com/ */
2987 qt_type_find (GstTypeFind * tf, gpointer unused)
2993 const gchar *variant = NULL;
2995 while ((data = gst_type_find_peek (tf, offset, 12)) != NULL) {
2998 if (STRNCMP (&data[4], "ftypqt ", 8) == 0) {
2999 tip = GST_TYPE_FIND_MAXIMUM;
3003 if (STRNCMP (&data[4], "ftypisom", 8) == 0 ||
3004 STRNCMP (&data[4], "ftypavc1", 8) == 0 ||
3005 STRNCMP (&data[4], "ftypmp42", 8) == 0 ||
3006 STRNCMP (&data[4], "ftypwmf ", 8) == 0) {
3007 tip = GST_TYPE_FIND_MAXIMUM;
3012 /* box/atom types that are in common with ISO base media file format */
3013 if (STRNCMP (&data[4], "moov", 4) == 0 ||
3014 STRNCMP (&data[4], "mdat", 4) == 0 ||
3015 STRNCMP (&data[4], "ftyp", 4) == 0 ||
3016 STRNCMP (&data[4], "free", 4) == 0 ||
3017 STRNCMP (&data[4], "uuid", 4) == 0 ||
3018 STRNCMP (&data[4], "skip", 4) == 0) {
3020 tip = GST_TYPE_FIND_LIKELY;
3022 tip = GST_TYPE_FIND_NEARLY_CERTAIN;
3025 /* other box/atom types, apparently quicktime specific */
3026 else if (STRNCMP (&data[4], "PICT", 4) == 0 ||
3027 STRNCMP (&data[4], "wide", 4) == 0 ||
3028 STRNCMP (&data[4], "prfl", 4) == 0) {
3029 tip = GST_TYPE_FIND_MAXIMUM;
3036 size = GST_READ_UINT32_BE (data);
3037 /* check compatible brands rather than ever expaning major brands above */
3038 if ((STRNCMP (&data[4], "ftyp", 4) == 0) && (size >= 16)) {
3039 new_offset = offset + 12;
3040 while (new_offset + 4 <= offset + size) {
3041 data = gst_type_find_peek (tf, new_offset, 4);
3044 if (STRNCMP (&data[4], "isom", 4) == 0 ||
3045 STRNCMP (&data[4], "avc1", 4) == 0 ||
3046 STRNCMP (&data[4], "mp41", 4) == 0 ||
3047 STRNCMP (&data[4], "mp42", 4) == 0 ||
3048 STRNCMP (&data[4], "wmf ", 4) == 0) {
3049 tip = GST_TYPE_FIND_MAXIMUM;
3057 const guint8 *sizedata;
3059 sizedata = gst_type_find_peek (tf, offset + 8, 8);
3060 if (sizedata == NULL)
3063 size = GST_READ_UINT64_BE (sizedata);
3068 new_offset = offset + size;
3069 if (new_offset <= offset)
3071 offset = new_offset;
3077 GstCaps *caps = gst_caps_copy (QT_CAPS);
3079 gst_caps_set_simple (caps, "variant", G_TYPE_STRING, variant, NULL);
3080 gst_type_find_suggest (tf, tip, caps);
3081 gst_caps_unref (caps);
3083 gst_type_find_suggest (tf, tip, QT_CAPS);
3089 /*** image/x-quicktime ***/
3091 static GstStaticCaps qtif_caps = GST_STATIC_CAPS ("image/x-quicktime");
3093 #define QTIF_CAPS gst_static_caps_get(&qtif_caps)
3095 /* how many atoms we check before we give up */
3096 #define QTIF_MAXROUNDS 25
3099 qtif_type_find (GstTypeFind * tf, gpointer unused)
3102 gboolean found_idsc = FALSE;
3103 gboolean found_idat = FALSE;
3107 while ((data = gst_type_find_peek (tf, offset, 8)) != NULL) {
3110 size = GST_READ_UINT32_BE (data);
3112 const guint8 *sizedata;
3114 sizedata = gst_type_find_peek (tf, offset + 8, 8);
3115 if (sizedata == NULL)
3118 size = GST_READ_UINT64_BE (sizedata);
3123 if (STRNCMP (data + 4, "idsc", 4) == 0)
3125 if (STRNCMP (data + 4, "idat", 4) == 0)
3128 if (found_idsc && found_idat) {
3129 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, QTIF_CAPS);
3134 if (++rounds > QTIF_MAXROUNDS)
3138 if (found_idsc || found_idat) {
3139 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, QTIF_CAPS);
3144 /*** audio/x-mod ***/
3146 static GstStaticCaps mod_caps = GST_STATIC_CAPS ("audio/x-mod");
3148 #define MOD_CAPS gst_static_caps_get(&mod_caps)
3149 /* FIXME: M15 CheckType to do */
3151 mod_type_find (GstTypeFind * tf, gpointer unused)
3156 if ((data = gst_type_find_peek (tf, 1080, 4)) != NULL) {
3157 /* Protracker and variants */
3158 if ((memcmp (data, "M.K.", 4) == 0) || (memcmp (data, "M!K!", 4) == 0) ||
3160 (memcmp (data, "FLT", 3) == 0 && isdigit (data[3])) ||
3161 (memcmp (data, "EXO", 3) == 0 && isdigit (data[3])) ||
3162 /* Oktalyzer (Amiga) */
3163 (memcmp (data, "OKTA", 4) == 0) ||
3164 /* Oktalyser (Atari) */
3165 (memcmp (data, "CD81", 4) == 0) ||
3167 (memcmp (data + 1, "CHN", 3) == 0 && isdigit (data[0])) ||
3168 /* Fasttracker or Taketracker */
3169 (memcmp (data + 2, "CH", 2) == 0 && isdigit (data[0])
3170 && isdigit (data[1])) || (memcmp (data + 2, "CN", 2) == 0
3171 && isdigit (data[0]) && isdigit (data[1]))) {
3172 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3177 if ((data = gst_type_find_peek (tf, 0, 38)) != NULL) {
3178 if (memcmp (data, "Extended Module: ", 17) == 0 && data[37] == 0x1A) {
3179 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3184 if (data || (data = gst_type_find_peek (tf, 0, 8)) != NULL) {
3185 if (memcmp (data, "OKTASONG", 8) == 0) {
3186 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3190 if (data || (data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3192 if ((memcmp (data, "if", 2) == 0) || (memcmp (data, "JN", 2) == 0)) {
3193 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MOD_CAPS);
3197 if ((memcmp (data, "AMF", 3) == 0 && data[3] > 10 && data[3] < 14) ||
3199 (memcmp (data, "IMPM", 4) == 0) ||
3201 (memcmp (data, "MMD0", 4) == 0) || (memcmp (data, "MMD1", 4) == 0) ||
3203 (memcmp (data, "MTM", 3) == 0)) {
3204 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3208 if (memcmp (data, "RIFF", 4) == 0) {
3209 const guint8 *data2 = gst_type_find_peek (tf, 8, 4);
3212 if (memcmp (data2, "DSMF", 4) == 0) {
3213 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3219 if (memcmp (data, "FAM\xFE", 4) == 0) {
3220 const guint8 *data2 = gst_type_find_peek (tf, 44, 3);
3223 if (memcmp (data2, "compare", 3) == 0) {
3224 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3228 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MOD_CAPS);
3233 if (memcmp (data, "GDM\xFE", 4) == 0) {
3234 const guint8 *data2 = gst_type_find_peek (tf, 71, 4);
3237 if (memcmp (data2, "GMFS", 4) == 0) {
3238 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3242 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MOD_CAPS);
3248 if ((data = gst_type_find_peek (tf, 60, 4)) != NULL) {
3249 if (memcmp (data, "IM10", 4) == 0) {
3250 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3255 if ((data = gst_type_find_peek (tf, 44, 4)) != NULL) {
3256 if (memcmp (data, "SCRM", 4) == 0) {
3257 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3262 if ((data = gst_type_find_peek (tf, 20, 8)) != NULL) {
3263 if (g_ascii_strncasecmp ((gchar *) data, "!Scream!", 8) == 0 ||
3264 g_ascii_strncasecmp ((gchar *) data, "BMOD2STM", 8) == 0) {
3265 const guint8 *id, *stmtype;
3267 if ((id = gst_type_find_peek (tf, 28, 1)) == NULL)
3269 if ((stmtype = gst_type_find_peek (tf, 29, 1)) == NULL)
3271 if (*id == 0x1A && *stmtype == 2)
3272 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3277 if ((data = gst_type_find_peek (tf, 0, 19)) != NULL) {
3278 if (memcmp (data, "ASYLUM Music Format", 19) == 0) {
3279 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3285 /*** application/x-shockwave-flash ***/
3287 static GstStaticCaps swf_caps =
3288 GST_STATIC_CAPS ("application/x-shockwave-flash");
3289 #define SWF_CAPS (gst_static_caps_get(&swf_caps))
3291 swf_type_find (GstTypeFind * tf, gpointer unused)
3293 const guint8 *data = gst_type_find_peek (tf, 0, 4);
3295 if (data && (data[0] == 'F' || data[0] == 'C') &&
3296 data[1] == 'W' && data[2] == 'S') {
3297 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SWF_CAPS);
3301 /*** image/jpeg ***/
3303 #define JPEG_MARKER_IS_START_OF_FRAME(x) \
3304 ((x)>=0xc0 && (x) <= 0xcf && (x)!=0xc4 && (x)!=0xc8 && (x)!=0xcc)
3306 static GstStaticCaps jpeg_caps = GST_STATIC_CAPS ("image/jpeg");
3308 #define JPEG_CAPS (gst_static_caps_get(&jpeg_caps))
3310 jpeg_type_find (GstTypeFind * tf, gpointer unused)
3312 GstTypeFindProbability prob = GST_TYPE_FIND_POSSIBLE;
3313 DataScanCtx c = { 0, NULL, 0 };
3317 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 2)))
3320 if (c.data[0] != 0xff || c.data[1] != 0xd8)
3324 data_scan_ctx_advance (tf, &c, 2);
3326 caps = gst_caps_copy (JPEG_CAPS);
3328 while (data_scan_ctx_ensure_data (tf, &c, 4) && c.offset < (200 * 1024)) {
3332 if (c.data[0] != 0xff)
3336 if (G_UNLIKELY (marker == 0xff)) {
3337 data_scan_ctx_advance (tf, &c, 1);
3341 data_scan_ctx_advance (tf, &c, 2);
3343 /* we assume all markers we'll see before SOF have a payload length; if
3344 * that's not the case we'll just detect a false sync and bail out, but
3345 * still report POSSIBLE probability */
3346 len = GST_READ_UINT16_BE (c.data);
3348 GST_LOG ("possible JPEG marker 0x%02x (@0x%04x), segment length %u",
3349 marker, (guint) c.offset, len);
3351 if (!data_scan_ctx_ensure_data (tf, &c, len))
3354 if (marker == 0xc4 || /* DEFINE_HUFFMAN_TABLES */
3355 marker == 0xcc || /* DEFINE_ARITHMETIC_CONDITIONING */
3356 marker == 0xdb || /* DEFINE_QUANTIZATION_TABLES */
3357 marker == 0xdd || /* DEFINE_RESTART_INTERVAL */
3358 marker == 0xfe) { /* COMMENT */
3359 data_scan_ctx_advance (tf, &c, len);
3361 } else if (marker == 0xe0 && len >= (2 + 4) && /* APP0 */
3362 data_scan_ctx_memcmp (tf, &c, 2, "JFIF", 4)) {
3363 GST_LOG ("found JFIF tag");
3364 prob = GST_TYPE_FIND_MAXIMUM;
3365 data_scan_ctx_advance (tf, &c, len);
3367 /* we continue until we find a start of frame marker */
3368 } else if (marker == 0xe1 && len >= (2 + 4) && /* APP1 */
3369 data_scan_ctx_memcmp (tf, &c, 2, "Exif", 4)) {
3370 GST_LOG ("found Exif tag");
3371 prob = GST_TYPE_FIND_MAXIMUM;
3372 data_scan_ctx_advance (tf, &c, len);
3374 /* we continue until we find a start of frame marker */
3375 } else if (marker >= 0xe0 && marker <= 0xef) { /* APPn */
3376 data_scan_ctx_advance (tf, &c, len);
3378 } else if (JPEG_MARKER_IS_START_OF_FRAME (marker) && len >= (2 + 8)) {
3381 h = GST_READ_UINT16_BE (c.data + 2 + 1);
3382 w = GST_READ_UINT16_BE (c.data + 2 + 1 + 2);
3383 if (h == 0 || w == 0) {
3384 GST_WARNING ("bad width %u and/or height %u in SOF header", w, h);
3388 GST_LOG ("SOF at offset %" G_GUINT64_FORMAT ", num_markers=%d, "
3389 "WxH=%dx%d", c.offset - 2, num_markers, w, h);
3391 if (num_markers >= 5 || prob == GST_TYPE_FIND_MAXIMUM)
3392 prob = GST_TYPE_FIND_MAXIMUM;
3394 prob = GST_TYPE_FIND_LIKELY;
3396 gst_caps_set_simple (caps, "width", G_TYPE_INT, w,
3397 "height", G_TYPE_INT, h, "sof-marker", G_TYPE_INT, marker & 0xf,
3402 GST_WARNING ("bad length or unexpected JPEG marker 0xff 0x%02x", marker);
3407 gst_type_find_suggest (tf, prob, caps);
3408 gst_caps_unref (caps);
3413 static GstStaticCaps bmp_caps = GST_STATIC_CAPS ("image/bmp");
3415 #define BMP_CAPS (gst_static_caps_get(&bmp_caps))
3417 bmp_type_find (GstTypeFind * tf, gpointer unused)
3419 DataScanCtx c = { 0, NULL, 0 };
3420 guint32 struct_size, w, h, planes, bpp;
3422 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 54)))
3425 if (c.data[0] != 'B' || c.data[1] != 'M')
3428 /* skip marker + size */
3429 data_scan_ctx_advance (tf, &c, 2 + 4);
3431 /* reserved, must be 0 */
3432 if (c.data[0] != 0 || c.data[1] != 0 || c.data[2] != 0 || c.data[3] != 0)
3435 data_scan_ctx_advance (tf, &c, 2 + 2);
3437 /* offset to start of image data in bytes (check for sanity) */
3438 GST_LOG ("offset=%u", GST_READ_UINT32_LE (c.data));
3439 if (GST_READ_UINT32_LE (c.data) > (10 * 1024 * 1024))
3442 struct_size = GST_READ_UINT32_LE (c.data + 4);
3443 GST_LOG ("struct_size=%u", struct_size);
3445 data_scan_ctx_advance (tf, &c, 4 + 4);
3447 if (struct_size == 0x0C) {
3448 w = GST_READ_UINT16_LE (c.data);
3449 h = GST_READ_UINT16_LE (c.data + 2);
3450 planes = GST_READ_UINT16_LE (c.data + 2 + 2);
3451 bpp = GST_READ_UINT16_LE (c.data + 2 + 2 + 2);
3452 } else if (struct_size == 40 || struct_size == 64 || struct_size == 108
3453 || struct_size == 124 || struct_size == 0xF0) {
3454 w = GST_READ_UINT32_LE (c.data);
3455 h = GST_READ_UINT32_LE (c.data + 4);
3456 planes = GST_READ_UINT16_LE (c.data + 4 + 4);
3457 bpp = GST_READ_UINT16_LE (c.data + 4 + 4 + 2);
3462 /* image sizes sanity check */
3463 GST_LOG ("w=%u, h=%u, planes=%u, bpp=%u", w, h, planes, bpp);
3464 if (w == 0 || w > 0xfffff || h == 0 || h > 0xfffff || planes != 1 ||
3465 (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32))
3468 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "image/bmp",
3469 "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, "bpp", G_TYPE_INT, bpp,
3473 /*** image/tiff ***/
3474 static GstStaticCaps tiff_caps = GST_STATIC_CAPS ("image/tiff, "
3475 "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }");
3476 #define TIFF_CAPS (gst_static_caps_get(&tiff_caps))
3477 static GstStaticCaps tiff_be_caps = GST_STATIC_CAPS ("image/tiff, "
3478 "endianness = (int) BIG_ENDIAN");
3479 #define TIFF_BE_CAPS (gst_static_caps_get(&tiff_be_caps))
3480 static GstStaticCaps tiff_le_caps = GST_STATIC_CAPS ("image/tiff, "
3481 "endianness = (int) LITTLE_ENDIAN");
3482 #define TIFF_LE_CAPS (gst_static_caps_get(&tiff_le_caps))
3484 tiff_type_find (GstTypeFind * tf, gpointer ununsed)
3486 const guint8 *data = gst_type_find_peek (tf, 0, 8);
3487 guint8 le_header[4] = { 0x49, 0x49, 0x2A, 0x00 };
3488 guint8 be_header[4] = { 0x4D, 0x4D, 0x00, 0x2A };
3491 if (memcmp (data, le_header, 4) == 0) {
3492 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_LE_CAPS);
3493 } else if (memcmp (data, be_header, 4) == 0) {
3494 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_BE_CAPS);
3501 static GstStaticCaps pnm_caps = GST_STATIC_CAPS ("image/x-portable-bitmap; "
3502 "image/x-portable-graymap; image/x-portable-pixmap; "
3503 "image/x-portable-anymap");
3505 #define PNM_CAPS (gst_static_caps_get(&pnm_caps))
3507 #define IS_PNM_WHITESPACE(c) \
3508 ((c) == ' ' || (c) == '\r' || (c) == '\n' || (c) == 't')
3511 pnm_type_find (GstTypeFind * tf, gpointer ununsed)
3513 const gchar *media_type = NULL;
3514 DataScanCtx c = { 0, NULL, 0 };
3517 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 16)))
3520 /* see http://en.wikipedia.org/wiki/Netpbm_format */
3521 if (c.data[0] != 'P' || c.data[1] < '1' || c.data[1] > '7' ||
3522 !IS_PNM_WHITESPACE (c.data[2]) ||
3523 (c.data[3] != '#' && c.data[3] < '0' && c.data[3] > '9'))
3526 switch (c.data[1]) {
3528 media_type = "image/x-portable-bitmap"; /* ASCII */
3531 media_type = "image/x-portable-graymap"; /* ASCII */
3534 media_type = "image/x-portable-pixmap"; /* ASCII */
3537 media_type = "image/x-portable-bitmap"; /* Raw */
3540 media_type = "image/x-portable-graymap"; /* Raw */
3543 media_type = "image/x-portable-pixmap"; /* Raw */
3546 media_type = "image/x-portable-anymap";
3549 g_return_if_reached ();
3552 /* try to extract width and height as well */
3553 if (c.data[1] != '7') {
3554 gchar s[64] = { 0, }
3557 /* need to skip any comment lines first */
3558 data_scan_ctx_advance (tf, &c, 3);
3559 while (c.data[0] == '#') { /* we know there's still data left */
3560 data_scan_ctx_advance (tf, &c, 1);
3561 while (c.data[0] != '\n' && c.data[0] != '\r') {
3562 if (!data_scan_ctx_ensure_data (tf, &c, 4))
3564 data_scan_ctx_advance (tf, &c, 1);
3566 data_scan_ctx_advance (tf, &c, 1);
3567 GST_LOG ("skipped comment line in PNM header");
3570 if (!data_scan_ctx_ensure_data (tf, &c, 32) &&
3571 !data_scan_ctx_ensure_data (tf, &c, 4)) {
3575 /* need to NUL-terminate data for sscanf */
3576 memcpy (s, c.data, MIN (sizeof (s) - 1, c.size));
3577 if (sscanf (s, "%u%c%u%c", &w, &sep1, &h, &sep2) == 4 &&
3578 IS_PNM_WHITESPACE (sep1) && IS_PNM_WHITESPACE (sep2) &&
3579 w > 0 && w < G_MAXINT && h > 0 && h < G_MAXINT) {
3580 GST_LOG ("extracted PNM width and height: %dx%d", w, h);
3586 /* FIXME: extract width + height for anymaps too */
3589 if (w > 0 && h > 0) {
3590 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type,
3591 "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, NULL);
3593 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, media_type, NULL);
3597 static GstStaticCaps sds_caps = GST_STATIC_CAPS ("audio/x-sds");
3599 #define SDS_CAPS (gst_static_caps_get(&sds_caps))
3601 sds_type_find (GstTypeFind * tf, gpointer ununsed)
3603 const guint8 *data = gst_type_find_peek (tf, 0, 4);
3604 guint8 mask[4] = { 0xFF, 0xFF, 0x80, 0xFF };
3605 guint8 match[4] = { 0xF0, 0x7E, 0, 0x01 };
3609 for (x = 0; x < 4; x++) {
3610 if ((data[x] & mask[x]) != match[x]) {
3614 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDS_CAPS);
3618 static GstStaticCaps ircam_caps = GST_STATIC_CAPS ("audio/x-ircam");
3620 #define IRCAM_CAPS (gst_static_caps_get(&ircam_caps))
3622 ircam_type_find (GstTypeFind * tf, gpointer ununsed)
3624 const guint8 *data = gst_type_find_peek (tf, 0, 4);
3625 guint8 mask[4] = { 0xFF, 0xFF, 0xF8, 0xFF };
3626 guint8 match[4] = { 0x64, 0xA3, 0x00, 0x00 };
3628 gboolean matched = TRUE;
3633 for (x = 0; x < 4; x++) {
3634 if ((data[x] & mask[x]) != match[x]) {
3639 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, IRCAM_CAPS);
3642 /* now try the reverse version */
3644 for (x = 0; x < 4; x++) {
3645 if ((data[x] & mask[3 - x]) != match[3 - x]) {
3651 /* EBML typefind helper */
3653 ebml_check_header (GstTypeFind * tf, const gchar * doctype, int doctype_len)
3655 /* 4 bytes for EBML ID, 1 byte for header length identifier */
3656 const guint8 *data = gst_type_find_peek (tf, 0, 4 + 1);
3657 gint len_mask = 0x80, size = 1, n = 1, total;
3663 if (data[0] != 0x1A || data[1] != 0x45 || data[2] != 0xDF || data[3] != 0xA3)
3666 /* length of header */
3668 while (size <= 8 && !(total & len_mask)) {
3674 total &= (len_mask - 1);
3676 total = (total << 8) | data[4 + n++];
3678 /* get new data for full header, 4 bytes for EBML ID,
3679 * EBML length tag and the actual header */
3680 data = gst_type_find_peek (tf, 0, 4 + size + total);
3684 /* only check doctype if asked to do so */
3685 if (doctype == NULL || doctype_len == 0)
3688 /* the header must contain the doctype. For now, we don't parse the
3689 * whole header but simply check for the availability of that array
3690 * of characters inside the header. Not fully fool-proof, but good
3692 for (n = 4 + size; n <= 4 + size + total - doctype_len; n++)
3693 if (!memcmp (&data[n], doctype, doctype_len))
3699 /*** video/x-matroska ***/
3700 static GstStaticCaps matroska_caps = GST_STATIC_CAPS ("video/x-matroska");
3702 #define MATROSKA_CAPS (gst_static_caps_get(&matroska_caps))
3704 matroska_type_find (GstTypeFind * tf, gpointer ununsed)
3706 if (ebml_check_header (tf, "matroska", 8))
3707 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MATROSKA_CAPS);
3708 else if (ebml_check_header (tf, NULL, 0))
3709 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MATROSKA_CAPS);
3712 /*** video/webm ***/
3713 static GstStaticCaps webm_caps = GST_STATIC_CAPS ("video/webm");
3715 #define WEBM_CAPS (gst_static_caps_get(&webm_caps))
3717 webm_type_find (GstTypeFind * tf, gpointer ununsed)
3719 if (ebml_check_header (tf, "webm", 4))
3720 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, WEBM_CAPS);
3723 /*** application/mxf ***/
3724 static GstStaticCaps mxf_caps = GST_STATIC_CAPS ("application/mxf");
3726 #define MXF_MAX_PROBE_LENGTH (1024 * 64)
3727 #define MXF_CAPS (gst_static_caps_get(&mxf_caps))
3730 * MXF files start with a header partition pack key of 16 bytes which is defined
3731 * at SMPTE-377M 6.1. Before this there can be up to 64K of run-in which _must_
3732 * not contain the partition pack key.
3735 mxf_type_find (GstTypeFind * tf, gpointer ununsed)
3737 static const guint8 partition_pack_key[] =
3738 { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01,
3741 DataScanCtx c = { 0, NULL, 0 };
3743 while (c.offset <= MXF_MAX_PROBE_LENGTH) {
3745 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 1024)))
3748 /* look over in chunks of 1kbytes to avoid too much overhead */
3750 for (i = 0; i < 1024 - 16; i++) {
3751 /* Check first byte before calling more expensive memcmp function */
3752 if (G_UNLIKELY (c.data[i] == 0x06
3753 && memcmp (c.data + i, partition_pack_key, 13) == 0)) {
3754 /* Header partition pack? */
3755 if (c.data[i + 13] != 0x02)
3758 /* Partition status */
3759 if (c.data[i + 14] >= 0x05)
3762 /* Reserved, must be 0x00 */
3763 if (c.data[i + 15] != 0x00)
3766 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXF_CAPS);
3772 data_scan_ctx_advance (tf, &c, 1024 - 16);
3776 /*** video/x-dv ***/
3778 static GstStaticCaps dv_caps = GST_STATIC_CAPS ("video/x-dv, "
3779 "systemstream = (boolean) true");
3780 #define DV_CAPS (gst_static_caps_get(&dv_caps))
3782 dv_type_find (GstTypeFind * tf, gpointer private)
3786 data = gst_type_find_peek (tf, 0, 5);
3788 /* check for DIF and DV flag */
3789 if (data && (data[0] == 0x1f) && (data[1] == 0x07) && (data[2] == 0x00)) {
3790 const gchar *format;
3792 if (data[3] & 0x80) {
3798 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "video/x-dv",
3799 "systemstream", G_TYPE_BOOLEAN, TRUE,
3800 "format", G_TYPE_STRING, format, NULL);
3805 /*** application/ogg and application/x-annodex ***/
3806 static GstStaticCaps ogg_caps = GST_STATIC_CAPS ("application/ogg");
3807 static GstStaticCaps annodex_caps = GST_STATIC_CAPS ("application/x-annodex");
3808 static GstStaticCaps ogg_annodex_caps =
3809 GST_STATIC_CAPS ("application/ogg;application/x-annodex");
3811 #define OGGANX_CAPS (gst_static_caps_get(&ogg_annodex_caps))
3814 ogganx_type_find (GstTypeFind * tf, gpointer private)
3816 const guint8 *data = gst_type_find_peek (tf, 0, 4);
3818 if ((data != NULL) && (memcmp (data, "OggS", 4) == 0)) {
3820 /* Check for an annodex fishbone header */
3821 data = gst_type_find_peek (tf, 28, 8);
3822 if (data && memcmp (data, "fishead\0", 8) == 0)
3823 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
3824 gst_static_caps_get (&annodex_caps));
3826 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
3827 gst_static_caps_get (&ogg_caps));
3831 /*** audio/x-vorbis ***/
3832 static GstStaticCaps vorbis_caps = GST_STATIC_CAPS ("audio/x-vorbis");
3834 #define VORBIS_CAPS (gst_static_caps_get(&vorbis_caps))
3836 vorbis_type_find (GstTypeFind * tf, gpointer private)
3838 const guint8 *data = gst_type_find_peek (tf, 0, 30);
3844 /* 1 byte packet type (identification=0x01)
3845 6 byte string "vorbis"
3846 4 byte vorbis version */
3847 if (memcmp (data, "\001vorbis\000\000\000\000", 11) != 0)
3850 /* 1 byte channels must be != 0 */
3854 /* 4 byte samplerate must be != 0 */
3855 if (GST_READ_UINT32_LE (data) == 0)
3858 /* blocksize checks */
3859 blocksize_0 = data[0] & 0x0F;
3860 blocksize_1 = (data[0] & 0xF0) >> 4;
3861 if (blocksize_0 > blocksize_1)
3863 if (blocksize_0 < 6 || blocksize_0 > 13)
3865 if (blocksize_1 < 6 || blocksize_1 > 13)
3869 if ((data[0] & 0x01) != 1)
3871 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VORBIS_CAPS);
3875 /*** video/x-theora ***/
3877 static GstStaticCaps theora_caps = GST_STATIC_CAPS ("video/x-theora");
3879 #define THEORA_CAPS (gst_static_caps_get(&theora_caps))
3881 theora_type_find (GstTypeFind * tf, gpointer private)
3883 const guint8 *data = gst_type_find_peek (tf, 0, 7); //42);
3886 if (data[0] != 0x80)
3888 if (memcmp (&data[1], "theora", 6) != 0)
3890 /* FIXME: make this more reliable when specs are out */
3892 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, THEORA_CAPS);
3898 kate_type_find (GstTypeFind * tf, gpointer private)
3900 const guint8 *data = gst_type_find_peek (tf, 0, 64);
3901 gchar category[16] = { 0, };
3903 if (G_UNLIKELY (data == NULL))
3906 /* see: http://wiki.xiph.org/index.php/OggKate#Format_specification */
3907 if (G_LIKELY (memcmp (data, "\200kate\0\0\0", 8) != 0))
3910 /* make sure we always have a NUL-terminated string */
3911 memcpy (category, data + 48, 15);
3912 GST_LOG ("kate category: %s", category);
3913 /* canonical categories for subtitles: subtitles, spu-subtitles, SUB, K-SPU */
3914 if (strcmp (category, "subtitles") == 0 || strcmp (category, "SUB") == 0 ||
3915 strcmp (category, "spu-subtitles") == 0 ||
3916 strcmp (category, "K-SPU") == 0) {
3917 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3918 "subtitle/x-kate", NULL);
3920 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3921 "application/x-kate", NULL);
3925 /*** application/x-ogm-video or audio***/
3927 static GstStaticCaps ogmvideo_caps =
3928 GST_STATIC_CAPS ("application/x-ogm-video");
3929 #define OGMVIDEO_CAPS (gst_static_caps_get(&ogmvideo_caps))
3931 ogmvideo_type_find (GstTypeFind * tf, gpointer private)
3933 const guint8 *data = gst_type_find_peek (tf, 0, 9);
3936 if (memcmp (data, "\001video\000\000\000", 9) != 0)
3938 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMVIDEO_CAPS);
3942 static GstStaticCaps ogmaudio_caps =
3943 GST_STATIC_CAPS ("application/x-ogm-audio");
3944 #define OGMAUDIO_CAPS (gst_static_caps_get(&ogmaudio_caps))
3946 ogmaudio_type_find (GstTypeFind * tf, gpointer private)
3948 const guint8 *data = gst_type_find_peek (tf, 0, 9);
3951 if (memcmp (data, "\001audio\000\000\000", 9) != 0)
3953 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMAUDIO_CAPS);
3957 static GstStaticCaps ogmtext_caps = GST_STATIC_CAPS ("application/x-ogm-text");
3959 #define OGMTEXT_CAPS (gst_static_caps_get(&ogmtext_caps))
3961 ogmtext_type_find (GstTypeFind * tf, gpointer private)
3963 const guint8 *data = gst_type_find_peek (tf, 0, 9);
3966 if (memcmp (data, "\001text\000\000\000\000", 9) != 0)
3968 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMTEXT_CAPS);
3972 /*** audio/x-speex ***/
3974 static GstStaticCaps speex_caps = GST_STATIC_CAPS ("audio/x-speex");
3976 #define SPEEX_CAPS (gst_static_caps_get(&speex_caps))
3978 speex_type_find (GstTypeFind * tf, gpointer private)
3980 const guint8 *data = gst_type_find_peek (tf, 0, 80);
3983 /* 8 byte string "Speex "
3984 24 byte speex version string + int */
3985 if (memcmp (data, "Speex ", 8) != 0)
3989 /* 4 byte header size >= 80 */
3990 if (GST_READ_UINT32_LE (data) < 80)
3994 /* 4 byte sample rate <= 48000 */
3995 if (GST_READ_UINT32_LE (data) > 48000)
3999 /* currently there are only 3 speex modes. */
4000 if (GST_READ_UINT32_LE (data) > 3)
4004 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SPEEX_CAPS);
4008 /*** audio/x-celt ***/
4010 static GstStaticCaps celt_caps = GST_STATIC_CAPS ("audio/x-celt");
4012 #define CELT_CAPS (gst_static_caps_get(&celt_caps))
4014 celt_type_find (GstTypeFind * tf, gpointer private)
4016 const guint8 *data = gst_type_find_peek (tf, 0, 8);
4019 /* 8 byte string "CELT " */
4020 if (memcmp (data, "CELT ", 8) != 0)
4023 /* TODO: Check other values of the CELT header */
4024 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CELT_CAPS);
4028 /*** application/x-ogg-skeleton ***/
4029 static GstStaticCaps ogg_skeleton_caps =
4030 GST_STATIC_CAPS ("application/x-ogg-skeleton, parsed=(boolean)FALSE");
4031 #define OGG_SKELETON_CAPS (gst_static_caps_get(&ogg_skeleton_caps))
4033 oggskel_type_find (GstTypeFind * tf, gpointer private)
4035 const guint8 *data = gst_type_find_peek (tf, 0, 12);
4038 /* 8 byte string "fishead\0" for the ogg skeleton stream */
4039 if (memcmp (data, "fishead\0", 8) != 0)
4043 /* Require that the header contains version 3.0 */
4044 if (GST_READ_UINT16_LE (data) != 3)
4047 if (GST_READ_UINT16_LE (data) != 0)
4050 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGG_SKELETON_CAPS);
4054 static GstStaticCaps cmml_caps = GST_STATIC_CAPS ("text/x-cmml");
4056 #define CMML_CAPS (gst_static_caps_get(&cmml_caps))
4058 cmml_type_find (GstTypeFind * tf, gpointer private)
4060 /* Header is 12 bytes minimum (though we don't check the minor version */
4061 const guint8 *data = gst_type_find_peek (tf, 0, 12);
4065 /* 8 byte string "CMML\0\0\0\0" for the magic number */
4066 if (memcmp (data, "CMML\0\0\0\0", 8) != 0)
4070 /* Require that the header contains at least version 2.0 */
4071 if (GST_READ_UINT16_LE (data) < 2)
4074 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CMML_CAPS);
4078 /*** application/x-tar ***/
4080 static GstStaticCaps tar_caps = GST_STATIC_CAPS ("application/x-tar");
4082 #define TAR_CAPS (gst_static_caps_get(&tar_caps))
4083 #define OLDGNU_MAGIC "ustar " /* 7 chars and a NUL */
4084 #define NEWGNU_MAGIC "ustar" /* 5 chars and a NUL */
4086 tar_type_find (GstTypeFind * tf, gpointer unused)
4088 const guint8 *data = gst_type_find_peek (tf, 257, 8);
4090 /* of course we are not certain, but we don't want other typefind funcs
4091 * to detect formats of files within the tar archive, e.g. mp3s */
4093 if (memcmp (data, OLDGNU_MAGIC, 8) == 0) { /* sic */
4094 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
4095 } else if (memcmp (data, NEWGNU_MAGIC, 6) == 0 && /* sic */
4096 g_ascii_isdigit (data[6]) && g_ascii_isdigit (data[7])) {
4097 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
4102 /*** application/x-ar ***/
4104 static GstStaticCaps ar_caps = GST_STATIC_CAPS ("application/x-ar");
4106 #define AR_CAPS (gst_static_caps_get(&ar_caps))
4108 ar_type_find (GstTypeFind * tf, gpointer unused)
4110 const guint8 *data = gst_type_find_peek (tf, 0, 24);
4112 if (data && memcmp (data, "!<arch>", 7) == 0) {
4115 for (i = 7; i < 24; ++i) {
4116 if (!g_ascii_isprint (data[i]) && data[i] != '\n') {
4117 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AR_CAPS);
4121 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AR_CAPS);
4125 /*** audio/x-au ***/
4127 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4128 * as it is only possible to register one typefind factory per 'name'
4129 * (which is in this case the caps), and the first one would be replaced by
4130 * the second one. */
4131 static GstStaticCaps au_caps = GST_STATIC_CAPS ("audio/x-au");
4133 #define AU_CAPS (gst_static_caps_get(&au_caps))
4135 au_type_find (GstTypeFind * tf, gpointer unused)
4137 const guint8 *data = gst_type_find_peek (tf, 0, 4);
4140 if (memcmp (data, ".snd", 4) == 0 || memcmp (data, "dns.", 4) == 0) {
4141 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AU_CAPS);
4147 /*** video/x-nuv ***/
4149 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4150 * as it is only possible to register one typefind factory per 'name'
4151 * (which is in this case the caps), and the first one would be replaced by
4152 * the second one. */
4153 static GstStaticCaps nuv_caps = GST_STATIC_CAPS ("video/x-nuv");
4155 #define NUV_CAPS (gst_static_caps_get(&nuv_caps))
4157 nuv_type_find (GstTypeFind * tf, gpointer unused)
4159 const guint8 *data = gst_type_find_peek (tf, 0, 11);
4162 if (memcmp (data, "MythTVVideo", 11) == 0
4163 || memcmp (data, "NuppelVideo", 11) == 0) {
4164 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, NUV_CAPS);
4169 /*** audio/x-paris ***/
4170 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
4171 static GstStaticCaps paris_caps = GST_STATIC_CAPS ("audio/x-paris");
4173 #define PARIS_CAPS (gst_static_caps_get(&paris_caps))
4175 paris_type_find (GstTypeFind * tf, gpointer unused)
4177 const guint8 *data = gst_type_find_peek (tf, 0, 4);
4180 if (memcmp (data, " paf", 4) == 0 || memcmp (data, "fap ", 4) == 0) {
4181 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, PARIS_CAPS);
4186 /*** audio/iLBC-sh ***/
4187 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
4188 static GstStaticCaps ilbc_caps = GST_STATIC_CAPS ("audio/iLBC-sh");
4190 #define ILBC_CAPS (gst_static_caps_get(&ilbc_caps))
4192 ilbc_type_find (GstTypeFind * tf, gpointer unused)
4194 const guint8 *data = gst_type_find_peek (tf, 0, 8);
4197 if (memcmp (data, "#!iLBC30", 8) == 0 || memcmp (data, "#!iLBC20", 8) == 0) {
4198 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, ILBC_CAPS);
4203 /*** application/x-ms-dos-executable ***/
4205 static GstStaticCaps msdos_caps =
4206 GST_STATIC_CAPS ("application/x-ms-dos-executable");
4207 #define MSDOS_CAPS (gst_static_caps_get(&msdos_caps))
4208 /* see http://www.madchat.org/vxdevl/papers/winsys/pefile/pefile.htm */
4210 msdos_type_find (GstTypeFind * tf, gpointer unused)
4212 const guint8 *data = gst_type_find_peek (tf, 0, 64);
4214 if (data && data[0] == 'M' && data[1] == 'Z' &&
4215 GST_READ_UINT16_LE (data + 8) == 4) {
4216 guint32 pe_offset = GST_READ_UINT32_LE (data + 60);
4218 data = gst_type_find_peek (tf, pe_offset, 2);
4219 if (data && data[0] == 'P' && data[1] == 'E') {
4220 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, MSDOS_CAPS);
4225 /*** application/x-mmsh ***/
4227 static GstStaticCaps mmsh_caps = GST_STATIC_CAPS ("application/x-mmsh");
4229 #define MMSH_CAPS gst_static_caps_get(&mmsh_caps)
4231 /* This is to recognise mssh-over-http */
4233 mmsh_type_find (GstTypeFind * tf, gpointer unused)
4235 static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
4236 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
4241 data = gst_type_find_peek (tf, 0, 2 + 2 + 4 + 2 + 2 + 16);
4242 if (data && data[0] == 0x24 && data[1] == 0x48 &&
4243 GST_READ_UINT16_LE (data + 2) > 2 + 2 + 4 + 2 + 2 + 16 &&
4244 memcmp (data + 2 + 2 + 4 + 2 + 2, asf_marker, 16) == 0) {
4245 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MMSH_CAPS);
4249 /*** video/x-dirac ***/
4251 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4252 * as it is only possible to register one typefind factory per 'name'
4253 * (which is in this case the caps), and the first one would be replaced by
4254 * the second one. */
4255 static GstStaticCaps dirac_caps = GST_STATIC_CAPS ("video/x-dirac");
4257 #define DIRAC_CAPS (gst_static_caps_get(&dirac_caps))
4259 dirac_type_find (GstTypeFind * tf, gpointer unused)
4261 const guint8 *data = gst_type_find_peek (tf, 0, 8);
4264 if (memcmp (data, "BBCD", 4) == 0 || memcmp (data, "KW-DIRAC", 8) == 0) {
4265 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DIRAC_CAPS);
4270 /*** video/vivo ***/
4272 static GstStaticCaps vivo_caps = GST_STATIC_CAPS ("video/vivo");
4274 #define VIVO_CAPS gst_static_caps_get(&vivo_caps)
4277 vivo_type_find (GstTypeFind * tf, gpointer unused)
4279 static const guint8 vivo_marker[] = { 'V', 'e', 'r', 's', 'i', 'o', 'n',
4280 ':', 'V', 'i', 'v', 'o', '/'
4285 data = gst_type_find_peek (tf, 0, 1024);
4286 if (data == NULL || data[0] != 0x00)
4289 if ((data[1] & 0x80)) {
4290 if ((data[2] & 0x80))
4292 hdr_len = ((guint) (data[1] & 0x7f)) << 7;
4302 /* 1008 = 1022 - strlen ("Version:Vivo/") - 1 */
4303 while (pos < 1008 && data[pos] == '\r' && data[pos + 1] == '\n')
4306 if (memcmp (data + pos, vivo_marker, sizeof (vivo_marker)) == 0) {
4307 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VIVO_CAPS);
4311 /*** XDG MIME typefinder (to avoid false positives mostly) ***/
4315 xdgmime_typefind (GstTypeFind * find, gpointer user_data)
4318 gsize length = 16384;
4323 if ((tf_length = gst_type_find_get_length (find)) > 0)
4324 length = MIN (length, tf_length);
4326 if ((data = gst_type_find_peek (find, 0, length)) == NULL)
4329 tmp = g_content_type_guess (NULL, data, length, NULL);
4330 if (tmp == NULL || g_content_type_is_unknown (tmp)) {
4335 mimetype = g_content_type_get_mime_type (tmp);
4338 if (mimetype == NULL)
4341 GST_DEBUG ("Got mimetype '%s'", mimetype);
4343 /* Ignore audio/video types:
4344 * - our own typefinders in -base are likely to be better at this
4345 * (and if they're not, we really want to fix them, that's why we don't
4346 * report xdg-detected audio/video types at all, not even with a low
4348 * - we want to detect GStreamer media types and not MIME types
4349 * - the purpose of this xdg mime finder is mainly to prevent false
4350 * positives of non-media formats, not to typefind audio/video formats */
4351 if (g_str_has_prefix (mimetype, "audio/") ||
4352 g_str_has_prefix (mimetype, "video/")) {
4353 GST_LOG ("Ignoring audio/video mime type");
4358 /* Again, we mainly want the xdg typefinding to prevent false-positives on
4359 * non-media formats, so suggest the type with a probability that trumps
4360 * uncertain results of our typefinders, but not more than that. */
4361 GST_LOG ("Suggesting '%s' with probability POSSIBLE", mimetype);
4362 gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE, mimetype, NULL);
4365 #endif /* USE_GIO */
4367 /*** Windows icon typefinder (to avoid false positives mostly) ***/
4370 windows_icon_typefind (GstTypeFind * find, gpointer user_data)
4374 guint16 type, nimages;
4375 gint32 size, offset;
4377 datalen = gst_type_find_get_length (find);
4378 if ((data = gst_type_find_peek (find, 0, 6)) == NULL)
4381 /* header - simple and not enough to rely on it alone */
4382 if (GST_READ_UINT16_LE (data) != 0)
4384 type = GST_READ_UINT16_LE (data + 2);
4385 if (type != 1 && type != 2)
4387 nimages = GST_READ_UINT16_LE (data + 4);
4388 if (nimages == 0) /* we can assume we can't have an empty image file ? */
4392 if (data[6 + 3] != 0)
4395 guint16 planes = GST_READ_UINT16_LE (data + 6 + 4);
4399 size = GST_READ_UINT32_LE (data + 6 + 8);
4400 offset = GST_READ_UINT32_LE (data + 6 + 12);
4401 if (offset < 0 || size <= 0 || size >= datalen || offset >= datalen
4402 || size + offset > datalen)
4405 gst_type_find_suggest_simple (find, GST_TYPE_FIND_NEARLY_CERTAIN,
4406 "image/x-icon", NULL);
4409 /*** WAP WBMP typefinder ***/
4412 wbmp_typefind (GstTypeFind * find, gpointer user_data)
4418 /* http://en.wikipedia.org/wiki/Wireless_Application_Protocol_Bitmap_Format */
4419 datalen = gst_type_find_get_length (find);
4423 data = gst_type_find_peek (find, 0, 5);
4427 /* want 0x00 0x00 at start */
4428 if (*data++ != 0 || *data++ != 0)
4431 /* min header size */
4434 /* let's assume max width/height is 65536 */
4437 w = (w << 8) | *data++;
4441 data = gst_type_find_peek (find, 4, 2);
4447 h = (h << 8) | *data++;
4453 if (w == 0 || h == 0)
4456 /* now add bitmap size */
4457 size += h * (GST_ROUND_UP_8 (w) / 8);
4459 if (datalen == size) {
4460 gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE - 10,
4461 "image/vnd.wap.wbmp", NULL);
4465 /*** DEGAS Atari images (also to avoid false positives, see #625129) ***/
4467 degas_type_find (GstTypeFind * tf, gpointer private)
4469 /* No magic, but it should have a fixed size and a few invalid values */
4470 /* http://www.fileformat.info/format/atari/spec/6ecf9f6eb5be494284a47feb8a214687/view.htm */
4476 len = gst_type_find_get_length (tf);
4477 if (len < 34) /* smallest header of the lot */
4479 data = gst_type_find_peek (tf, 0, 4);
4480 if (G_UNLIKELY (data == NULL))
4482 resolution = GST_READ_UINT16_BE (data);
4484 /* could be DEGAS */
4485 if (resolution <= 2)
4486 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
4487 "image/x-degas", NULL);
4488 } else if (len == 32066) {
4489 /* could be DEGAS Elite */
4490 if (resolution <= 2) {
4491 data = gst_type_find_peek (tf, len - 16, 8);
4492 if (G_UNLIKELY (data == NULL))
4494 for (n = 0; n < 4; n++) {
4495 if (GST_READ_UINT16_BE (data + n * 2) > 2)
4498 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
4499 "image/x-degas", NULL);
4501 } else if (len >= 66 && len < 32066) {
4502 /* could be compressed DEGAS Elite, but it's compressed and so we can't rely on size,
4503 it does have 4 16 bytes values near the end that are 0-2 though. */
4504 if ((resolution & 0x8000) && (resolution & 0x7fff) <= 2) {
4505 data = gst_type_find_peek (tf, len - 16, 8);
4506 if (G_UNLIKELY (data == NULL))
4508 for (n = 0; n < 4; n++) {
4509 if (GST_READ_UINT16_BE (data + n * 2) > 2)
4512 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
4513 "image/x-degas", NULL);
4518 /*** generic typefind for streams that have some data at a specific position***/
4529 start_with_type_find (GstTypeFind * tf, gpointer private)
4531 GstTypeFindData *start_with = (GstTypeFindData *) private;
4534 GST_LOG ("trying to find mime type %s with the first %u bytes of data",
4535 gst_structure_get_name (gst_caps_get_structure (start_with->caps, 0)),
4537 data = gst_type_find_peek (tf, 0, start_with->size);
4538 if (data && memcmp (data, start_with->data, start_with->size) == 0) {
4539 gst_type_find_suggest (tf, start_with->probability, start_with->caps);
4544 sw_data_destroy (GstTypeFindData * sw_data)
4546 if (G_LIKELY (sw_data->caps != NULL))
4547 gst_caps_unref (sw_data->caps);
4551 #define TYPE_FIND_REGISTER_START_WITH(plugin,name,rank,ext,_data,_size,_probability)\
4553 GstTypeFindData *sw_data = g_new (GstTypeFindData, 1); \
4554 sw_data->data = (const guint8 *)_data; \
4555 sw_data->size = _size; \
4556 sw_data->probability = _probability; \
4557 sw_data->caps = gst_caps_new_simple (name, NULL); \
4558 if (!gst_type_find_register (plugin, name, rank, start_with_type_find,\
4559 (char **) ext, sw_data->caps, sw_data, \
4560 (GDestroyNotify) (sw_data_destroy))) { \
4561 gst_caps_unref (sw_data->caps); \
4566 /*** same for riff types ***/
4569 riff_type_find (GstTypeFind * tf, gpointer private)
4571 GstTypeFindData *riff_data = (GstTypeFindData *) private;
4572 const guint8 *data = gst_type_find_peek (tf, 0, 12);
4574 if (data && (memcmp (data, "RIFF", 4) == 0 || memcmp (data, "AVF0", 4) == 0)) {
4576 if (memcmp (data, riff_data->data, 4) == 0)
4577 gst_type_find_suggest (tf, riff_data->probability, riff_data->caps);
4581 #define TYPE_FIND_REGISTER_RIFF(plugin,name,rank,ext,_data) \
4583 GstTypeFindData *sw_data = g_new (GstTypeFindData, 1); \
4584 sw_data->data = (gpointer)_data; \
4585 sw_data->size = 4; \
4586 sw_data->probability = GST_TYPE_FIND_MAXIMUM; \
4587 sw_data->caps = gst_caps_new_simple (name, NULL); \
4588 if (!gst_type_find_register (plugin, name, rank, riff_type_find, \
4589 (char **) ext, sw_data->caps, sw_data, \
4590 (GDestroyNotify) (sw_data_destroy))) { \
4591 gst_caps_unref (sw_data->caps); \
4597 /*** plugin initialization ***/
4599 #define TYPE_FIND_REGISTER(plugin,name,rank,func,ext,caps,priv,notify) \
4601 if (!gst_type_find_register (plugin, name, rank, func, (char **) ext, caps, priv, notify))\
4607 plugin_init (GstPlugin * plugin)
4609 /* can't initialize this via a struct as caps can't be statically initialized */
4611 /* note: asx/wax/wmx are XML files, asf doesn't handle them */
4612 /* FIXME-0.11: these should be const,
4613 this requires gstreamer/gst/gsttypefind::gst_type_find_register()
4614 to have define the parameter as const
4616 static const gchar *asf_exts[] = { "asf", "wm", "wma", "wmv", NULL };
4617 static const gchar *au_exts[] = { "au", "snd", NULL };
4618 static const gchar *avi_exts[] = { "avi", NULL };
4619 static const gchar *qcp_exts[] = { "qcp", NULL };
4620 static const gchar *cdxa_exts[] = { "dat", NULL };
4621 static const gchar *flac_exts[] = { "flac", NULL };
4622 static const gchar *flx_exts[] = { "flc", "fli", NULL };
4623 static const gchar *id3_exts[] =
4624 { "mp3", "mp2", "mp1", "mpga", "ogg", "flac", "tta", NULL };
4625 static const gchar *apetag_exts[] = { "mp3", "ape", "mpc", "wv", NULL };
4626 static const gchar *tta_exts[] = { "tta", NULL };
4627 static const gchar *mod_exts[] = { "669", "amf", "dsm", "gdm", "far", "imf",
4628 "it", "med", "mod", "mtm", "okt", "sam",
4629 "s3m", "stm", "stx", "ult", "xm", NULL
4631 static const gchar *mp3_exts[] = { "mp3", "mp2", "mp1", "mpga", NULL };
4632 static const gchar *ac3_exts[] = { "ac3", "eac3", NULL };
4633 static const gchar *dts_exts[] = { "dts", NULL };
4634 static const gchar *gsm_exts[] = { "gsm", NULL };
4635 static const gchar *musepack_exts[] = { "mpc", "mpp", "mp+", NULL };
4636 static const gchar *mpeg_sys_exts[] = { "mpe", "mpeg", "mpg", NULL };
4637 static const gchar *mpeg_video_exts[] = { "mpv", "mpeg", "mpg", NULL };
4638 static const gchar *mpeg_ts_exts[] = { "ts", "mts", NULL };
4639 static const gchar *ogg_exts[] = { "anx", "ogg", "ogm", NULL };
4640 static const gchar *qt_exts[] = { "mov", NULL };
4641 static const gchar *qtif_exts[] = { "qif", "qtif", "qti", NULL };
4642 static const gchar *mj2_exts[] = { "mj2", NULL };
4643 static const gchar *jp2_exts[] = { "jp2", NULL };
4644 static const gchar *rm_exts[] = { "ra", "ram", "rm", "rmvb", NULL };
4645 static const gchar *swf_exts[] = { "swf", "swfl", NULL };
4646 static const gchar *utf8_exts[] = { "txt", NULL };
4647 static const gchar *unicode_exts[] = { "txt", NULL };
4648 static const gchar *wav_exts[] = { "wav", NULL };
4649 static const gchar *aiff_exts[] = { "aiff", "aif", "aifc", NULL };
4650 static const gchar *svx_exts[] = { "iff", "svx", NULL };
4651 static const gchar *paris_exts[] = { "paf", NULL };
4652 static const gchar *nist_exts[] = { "nist", NULL };
4653 static const gchar *voc_exts[] = { "voc", NULL };
4654 static const gchar *sds_exts[] = { "sds", NULL };
4655 static const gchar *ircam_exts[] = { "sf", NULL };
4656 static const gchar *w64_exts[] = { "w64", NULL };
4657 static const gchar *shn_exts[] = { "shn", NULL };
4658 static const gchar *ape_exts[] = { "ape", NULL };
4659 static const gchar *uri_exts[] = { "ram", NULL };
4660 static const gchar *hls_exts[] = { "m3u8", NULL };
4661 #ifdef GST_EXT_SS_TYPE
4662 static const gchar *ss_exts[] = { "manifest", NULL };
4664 static const gchar *sdp_exts[] = { "sdp", NULL };
4665 static const gchar *smil_exts[] = { "smil", NULL };
4666 static const gchar *html_exts[] = { "htm", "html", NULL };
4667 static const gchar *xml_exts[] = { "xml", NULL };
4668 static const gchar *jpeg_exts[] = { "jpg", "jpe", "jpeg", NULL };
4669 static const gchar *gif_exts[] = { "gif", NULL };
4670 static const gchar *png_exts[] = { "png", NULL };
4671 static const gchar *bmp_exts[] = { "bmp", NULL };
4672 static const gchar *tiff_exts[] = { "tif", "tiff", NULL };
4673 static const gchar *matroska_exts[] = { "mkv", "mka", NULL };
4674 static const gchar *webm_exts[] = { "webm", NULL };
4675 static const gchar *mve_exts[] = { "mve", NULL };
4676 static const gchar *dv_exts[] = { "dv", "dif", NULL };
4677 static const gchar *amr_exts[] = { "amr", NULL };
4678 static const gchar *ilbc_exts[] = { "ilbc", NULL };
4679 static const gchar *sid_exts[] = { "sid", NULL };
4680 static const gchar *xcf_exts[] = { "xcf", NULL };
4681 static const gchar *mng_exts[] = { "mng", NULL };
4682 static const gchar *jng_exts[] = { "jng", NULL };
4683 static const gchar *xpm_exts[] = { "xpm", NULL };
4684 static const gchar *pnm_exts[] = { "pnm", "ppm", "pgm", "pbm", NULL };
4685 static const gchar *ras_exts[] = { "ras", NULL };
4686 static const gchar *bz2_exts[] = { "bz2", NULL };
4687 static const gchar *gz_exts[] = { "gz", NULL };
4688 static const gchar *zip_exts[] = { "zip", NULL };
4689 static const gchar *compress_exts[] = { "Z", NULL };
4690 static const gchar *m4a_exts[] = { "m4a", NULL };
4691 static const gchar *q3gp_exts[] = { "3gp", "ismv", "isma", NULL };
4692 static const gchar *aac_exts[] = { "aac", "adts", "adif", "loas", NULL };
4693 static const gchar *spc_exts[] = { "spc", NULL };
4694 static const gchar *wavpack_exts[] = { "wv", "wvp", NULL };
4695 static const gchar *wavpack_correction_exts[] = { "wvc", NULL };
4696 static const gchar *rar_exts[] = { "rar", NULL };
4697 static const gchar *tar_exts[] = { "tar", NULL };
4698 static const gchar *ar_exts[] = { "a", NULL };
4699 static const gchar *msdos_exts[] = { "dll", "exe", "ocx", "sys", "scr",
4700 "msstyles", "cpl", NULL
4702 static const gchar *flv_exts[] = { "flv", NULL };
4703 static const gchar *m4v_exts[] = { "m4v", NULL };
4704 static const gchar *h263_exts[] = { "h263", "263", NULL };
4705 static const gchar *h264_exts[] = { "h264", "x264", "264", NULL };
4706 static const gchar *nuv_exts[] = { "nuv", NULL };
4707 static const gchar *vivo_exts[] = { "viv", NULL };
4708 static const gchar *nsf_exts[] = { "nsf", NULL };
4709 static const gchar *gym_exts[] = { "gym", NULL };
4710 static const gchar *ay_exts[] = { "ay", NULL };
4711 static const gchar *gbs_exts[] = { "gbs", NULL };
4712 static const gchar *kss_exts[] = { "kss", NULL };
4713 static const gchar *sap_exts[] = { "sap", NULL };
4714 static const gchar *vgm_exts[] = { "vgm", NULL };
4715 static const gchar *mid_exts[] = { "mid", "midi", NULL };
4716 #ifdef GST_EXT_MIME_TYPES
4717 static const gchar *mmf_exts[] = { "mmf", NULL };
4718 static const gchar *xmf_exts[] = { "xmf", NULL };
4720 static const gchar *mxmf_exts[] = { "mxmf", NULL };
4721 static const gchar *imelody_exts[] = { "imy", "ime", "imelody", NULL };
4722 static const gchar *pdf_exts[] = { "pdf", NULL };
4723 static const gchar *ps_exts[] = { "ps", NULL };
4724 static const gchar *svg_exts[] = { "svg", NULL };
4725 static const gchar *mxf_exts[] = { "mxf", NULL };
4726 static const gchar *ivf_exts[] = { "ivf", NULL };
4727 static const gchar *msword_exts[] = { "doc", NULL };
4728 static const gchar *dsstore_exts[] = { "DS_Store", NULL };
4729 static const gchar *psd_exts[] = { "psd", NULL };
4730 static const gchar *y4m_exts[] = { "y4m", NULL };
4732 GST_DEBUG_CATEGORY_INIT (type_find_debug, "typefindfunctions",
4733 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_RED, "generic type find functions");
4735 /* must use strings, macros don't accept initializers */
4736 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ms-asf", GST_RANK_SECONDARY,
4738 "\060\046\262\165\216\146\317\021\246\331\000\252\000\142\316\154", 16,
4739 GST_TYPE_FIND_MAXIMUM);
4740 #ifndef GST_EXT_MIME_TYPES
4741 TYPE_FIND_REGISTER (plugin, "audio/x-musepack", GST_RANK_PRIMARY,
4742 musepack_type_find, musepack_exts, MUSEPACK_CAPS, NULL, NULL);
4743 TYPE_FIND_REGISTER (plugin, "audio/x-au", GST_RANK_MARGINAL,
4744 au_type_find, au_exts, AU_CAPS, NULL, NULL);
4746 TYPE_FIND_REGISTER_RIFF (plugin, "video/x-msvideo", GST_RANK_PRIMARY,
4748 #ifndef GST_EXT_MIME_TYPES
4749 TYPE_FIND_REGISTER_RIFF (plugin, "audio/qcelp", GST_RANK_PRIMARY,
4751 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-vcd", GST_RANK_PRIMARY,
4752 cdxa_exts, "\000\377\377\377\377\377\377\377\377\377\377\000", 12,
4753 GST_TYPE_FIND_MAXIMUM);
4755 TYPE_FIND_REGISTER_RIFF (plugin, "video/x-cdxa", GST_RANK_PRIMARY,
4758 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-imelody", GST_RANK_PRIMARY,
4759 imelody_exts, "BEGIN:IMELODY", 13, GST_TYPE_FIND_MAXIMUM);
4761 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-smoke", GST_RANK_PRIMARY,
4762 NULL, "\x80smoke\x00\x01\x00", 6, GST_TYPE_FIND_MAXIMUM);
4764 TYPE_FIND_REGISTER (plugin, "audio/midi", GST_RANK_PRIMARY, mid_type_find,
4765 mid_exts, MID_CAPS, NULL, NULL);
4766 #ifdef GST_EXT_MIME_TYPES
4767 TYPE_FIND_REGISTER (plugin, "audio/xmf", GST_RANK_PRIMARY,
4768 xmf_type_find, xmf_exts, XMF_CAPS, NULL, NULL);
4769 TYPE_FIND_REGISTER (plugin, "audio/x-smaf", GST_RANK_PRIMARY,
4770 mmf_type_find, mmf_exts, MMF_CAPS, NULL, NULL);
4772 TYPE_FIND_REGISTER_RIFF (plugin, "audio/riff-midi", GST_RANK_PRIMARY,
4774 TYPE_FIND_REGISTER (plugin, "audio/mobile-xmf", GST_RANK_PRIMARY,
4775 mxmf_type_find, mxmf_exts, MXMF_CAPS, NULL, NULL);
4776 #ifndef GST_EXT_MIME_TYPES
4777 TYPE_FIND_REGISTER (plugin, "video/x-fli", GST_RANK_MARGINAL, flx_type_find,
4778 flx_exts, FLX_CAPS, NULL, NULL);
4780 TYPE_FIND_REGISTER (plugin, "application/x-id3v2", GST_RANK_PRIMARY + 103,
4781 id3v2_type_find, id3_exts, ID3_CAPS, NULL, NULL);
4782 TYPE_FIND_REGISTER (plugin, "application/x-id3v1", GST_RANK_PRIMARY + 101,
4783 id3v1_type_find, id3_exts, ID3_CAPS, NULL, NULL);
4784 #ifndef GST_EXT_MIME_TYPES
4785 TYPE_FIND_REGISTER (plugin, "application/x-apetag", GST_RANK_PRIMARY + 102,
4786 apetag_type_find, apetag_exts, APETAG_CAPS, NULL, NULL);
4787 TYPE_FIND_REGISTER (plugin, "audio/x-ttafile", GST_RANK_PRIMARY,
4788 tta_type_find, tta_exts, TTA_CAPS, NULL, NULL);
4789 TYPE_FIND_REGISTER (plugin, "audio/x-mod", GST_RANK_SECONDARY, mod_type_find,
4790 mod_exts, MOD_CAPS, NULL, NULL);
4792 TYPE_FIND_REGISTER (plugin, "audio/mpeg", GST_RANK_PRIMARY, mp3_type_find,
4793 mp3_exts, MP3_CAPS, NULL, NULL);
4794 TYPE_FIND_REGISTER (plugin, "audio/x-ac3", GST_RANK_PRIMARY, ac3_type_find,
4795 ac3_exts, AC3_CAPS, NULL, NULL);
4796 TYPE_FIND_REGISTER (plugin, "audio/x-dts", GST_RANK_SECONDARY, dts_type_find,
4797 dts_exts, DTS_CAPS, NULL, NULL);
4798 #ifndef GST_EXT_MIME_TYPES
4799 TYPE_FIND_REGISTER (plugin, "audio/x-gsm", GST_RANK_PRIMARY, NULL, gsm_exts,
4800 GSM_CAPS, NULL, NULL);
4802 TYPE_FIND_REGISTER (plugin, "video/mpeg-sys", GST_RANK_PRIMARY,
4803 mpeg_sys_type_find, mpeg_sys_exts, MPEG_SYS_CAPS, NULL, NULL);
4804 TYPE_FIND_REGISTER (plugin, "video/mpegts", GST_RANK_PRIMARY,
4805 mpeg_ts_type_find, mpeg_ts_exts, MPEGTS_CAPS, NULL, NULL);
4806 TYPE_FIND_REGISTER (plugin, "application/ogg", GST_RANK_PRIMARY,
4807 ogganx_type_find, ogg_exts, OGGANX_CAPS, NULL, NULL);
4808 TYPE_FIND_REGISTER (plugin, "video/mpeg-elementary", GST_RANK_MARGINAL,
4809 mpeg_video_stream_type_find, mpeg_video_exts, MPEG_VIDEO_CAPS, NULL,
4811 TYPE_FIND_REGISTER (plugin, "video/mpeg4", GST_RANK_PRIMARY,
4812 mpeg4_video_type_find, m4v_exts, MPEG_VIDEO_CAPS, NULL, NULL);
4813 TYPE_FIND_REGISTER (plugin, "video/x-h263", GST_RANK_SECONDARY,
4814 h263_video_type_find, h263_exts, H263_VIDEO_CAPS, NULL, NULL);
4815 TYPE_FIND_REGISTER (plugin, "video/x-h264", GST_RANK_PRIMARY,
4816 h264_video_type_find, h264_exts, H264_VIDEO_CAPS, NULL, NULL);
4817 #ifndef GST_EXT_MIME_TYPES
4818 TYPE_FIND_REGISTER (plugin, "video/x-nuv", GST_RANK_SECONDARY, nuv_type_find,
4819 nuv_exts, NUV_CAPS, NULL, NULL);
4822 TYPE_FIND_REGISTER (plugin, "audio/x-m4a", GST_RANK_PRIMARY, m4a_type_find,
4823 m4a_exts, M4A_CAPS, NULL, NULL);
4824 TYPE_FIND_REGISTER (plugin, "application/x-3gp", GST_RANK_PRIMARY,
4825 q3gp_type_find, q3gp_exts, Q3GP_CAPS, NULL, NULL);
4826 TYPE_FIND_REGISTER (plugin, "video/quicktime", GST_RANK_SECONDARY,
4827 qt_type_find, qt_exts, QT_CAPS, NULL, NULL);
4828 #ifndef GST_EXT_MIME_TYPES
4829 TYPE_FIND_REGISTER (plugin, "image/x-quicktime", GST_RANK_SECONDARY,
4830 qtif_type_find, qtif_exts, QTIF_CAPS, NULL, NULL);
4831 TYPE_FIND_REGISTER (plugin, "image/jp2", GST_RANK_PRIMARY,
4832 jp2_type_find, jp2_exts, JP2_CAPS, NULL, NULL);
4833 TYPE_FIND_REGISTER (plugin, "video/mj2", GST_RANK_PRIMARY,
4834 jp2_type_find, mj2_exts, MJ2_CAPS, NULL, NULL);
4836 TYPE_FIND_REGISTER (plugin, "text/html", GST_RANK_SECONDARY, html_type_find,
4837 html_exts, HTML_CAPS, NULL, NULL);
4838 TYPE_FIND_REGISTER (plugin, "application/x-shockwave-flash",
4839 GST_RANK_SECONDARY, swf_type_find, swf_exts, SWF_CAPS, NULL, NULL);
4841 TYPE_FIND_REGISTER_START_WITH (plugin, "application/vnd.rn-realmedia",
4842 GST_RANK_SECONDARY, rm_exts, ".RMF", 4, GST_TYPE_FIND_MAXIMUM);
4843 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-pn-realaudio",
4844 GST_RANK_SECONDARY, rm_exts, ".ra\375", 4, GST_TYPE_FIND_MAXIMUM);
4845 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-flv", GST_RANK_SECONDARY,
4846 flv_exts, "FLV", 3, GST_TYPE_FIND_MAXIMUM);
4847 TYPE_FIND_REGISTER (plugin, "text/plain", GST_RANK_MARGINAL, utf8_type_find,
4848 utf8_exts, UTF8_CAPS, NULL, NULL);
4849 TYPE_FIND_REGISTER (plugin, "text/utf-16", GST_RANK_MARGINAL, utf16_type_find,
4850 unicode_exts, UTF16_CAPS, NULL, NULL);
4851 TYPE_FIND_REGISTER (plugin, "text/utf-32", GST_RANK_MARGINAL, utf32_type_find,
4852 unicode_exts, UTF32_CAPS, NULL, NULL);
4853 #ifndef GST_EXT_MIME_TYPES
4854 TYPE_FIND_REGISTER (plugin, "text/uri-list", GST_RANK_MARGINAL, uri_type_find,
4855 uri_exts, URI_CAPS, NULL, NULL);
4857 TYPE_FIND_REGISTER (plugin, "application/x-hls", GST_RANK_MARGINAL,
4858 hls_type_find, hls_exts, HLS_CAPS, NULL, NULL);
4859 TYPE_FIND_REGISTER (plugin, "application/sdp", GST_RANK_SECONDARY,
4860 sdp_type_find, sdp_exts, SDP_CAPS, NULL, NULL);
4861 #ifdef GST_EXT_SS_TYPE
4862 TYPE_FIND_REGISTER (plugin, "application/x-ss", GST_RANK_PRIMARY,
4863 ss_type_find, ss_exts, SS_CAPS, NULL, NULL);
4865 #ifndef GST_EXT_MIME_TYPES
4866 TYPE_FIND_REGISTER (plugin, "application/smil", GST_RANK_SECONDARY,
4867 smil_type_find, smil_exts, SMIL_CAPS, NULL, NULL);
4868 TYPE_FIND_REGISTER (plugin, "application/xml", GST_RANK_MARGINAL,
4869 xml_type_find, xml_exts, GENERIC_XML_CAPS, NULL, NULL);
4871 TYPE_FIND_REGISTER_RIFF (plugin, "audio/x-wav", GST_RANK_PRIMARY, wav_exts,
4873 #ifndef GST_EXT_MIME_TYPES
4874 TYPE_FIND_REGISTER (plugin, "audio/x-aiff", GST_RANK_SECONDARY,
4875 aiff_type_find, aiff_exts, AIFF_CAPS, NULL, NULL);
4876 TYPE_FIND_REGISTER (plugin, "audio/x-svx", GST_RANK_SECONDARY, svx_type_find,
4877 svx_exts, SVX_CAPS, NULL, NULL);
4878 TYPE_FIND_REGISTER (plugin, "audio/x-paris", GST_RANK_SECONDARY,
4879 paris_type_find, paris_exts, PARIS_CAPS, NULL, NULL);
4880 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nist", GST_RANK_SECONDARY,
4881 nist_exts, "NIST", 4, GST_TYPE_FIND_MAXIMUM);
4882 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-voc", GST_RANK_SECONDARY,
4883 voc_exts, "Creative", 8, GST_TYPE_FIND_MAXIMUM);
4884 TYPE_FIND_REGISTER (plugin, "audio/x-sds", GST_RANK_SECONDARY, sds_type_find,
4885 sds_exts, SDS_CAPS, NULL, NULL);
4886 TYPE_FIND_REGISTER (plugin, "audio/x-ircam", GST_RANK_SECONDARY,
4887 ircam_type_find, ircam_exts, IRCAM_CAPS, NULL, NULL);
4888 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-w64", GST_RANK_SECONDARY,
4889 w64_exts, "riff", 4, GST_TYPE_FIND_MAXIMUM);
4890 TYPE_FIND_REGISTER (plugin, "audio/x-shorten", GST_RANK_SECONDARY,
4891 shn_type_find, shn_exts, SHN_CAPS, NULL, NULL);
4892 TYPE_FIND_REGISTER (plugin, "application/x-ape", GST_RANK_SECONDARY,
4893 ape_type_find, ape_exts, APE_CAPS, NULL, NULL);
4895 TYPE_FIND_REGISTER (plugin, "image/jpeg", GST_RANK_PRIMARY + 15,
4896 jpeg_type_find, jpeg_exts, JPEG_CAPS, NULL, NULL);
4897 TYPE_FIND_REGISTER_START_WITH (plugin, "image/gif", GST_RANK_PRIMARY,
4898 gif_exts, "GIF8", 4, GST_TYPE_FIND_MAXIMUM);
4899 TYPE_FIND_REGISTER_START_WITH (plugin, "image/png", GST_RANK_PRIMARY + 14,
4900 png_exts, "\211PNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
4901 TYPE_FIND_REGISTER (plugin, "image/bmp", GST_RANK_PRIMARY, bmp_type_find,
4902 bmp_exts, BMP_CAPS, NULL, NULL);
4903 #ifndef GST_EXT_MIME_TYPES
4904 TYPE_FIND_REGISTER (plugin, "image/tiff", GST_RANK_PRIMARY, tiff_type_find,
4905 tiff_exts, TIFF_CAPS, NULL, NULL);
4907 TYPE_FIND_REGISTER (plugin, "image/x-portable-pixmap", GST_RANK_SECONDARY,
4908 pnm_type_find, pnm_exts, PNM_CAPS, NULL, NULL);
4909 TYPE_FIND_REGISTER (plugin, "video/x-matroska", GST_RANK_PRIMARY,
4910 matroska_type_find, matroska_exts, MATROSKA_CAPS, NULL, NULL);
4911 TYPE_FIND_REGISTER (plugin, "video/webm", GST_RANK_PRIMARY,
4912 webm_type_find, webm_exts, WEBM_CAPS, NULL, NULL);
4913 #ifndef GST_EXT_MIME_TYPES
4914 TYPE_FIND_REGISTER (plugin, "application/mxf", GST_RANK_PRIMARY,
4915 mxf_type_find, mxf_exts, MXF_CAPS, NULL, NULL);
4916 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mve", GST_RANK_SECONDARY,
4917 mve_exts, "Interplay MVE File\032\000\032\000\000\001\063\021", 26,
4918 GST_TYPE_FIND_MAXIMUM);
4919 TYPE_FIND_REGISTER (plugin, "video/x-dv", GST_RANK_SECONDARY, dv_type_find,
4920 dv_exts, DV_CAPS, NULL, NULL);
4922 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-nb-sh", GST_RANK_PRIMARY,
4923 amr_exts, "#!AMR", 5, GST_TYPE_FIND_LIKELY);
4924 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-wb-sh", GST_RANK_PRIMARY,
4925 amr_exts, "#!AMR-WB", 7, GST_TYPE_FIND_MAXIMUM);
4926 #ifndef GST_EXT_MIME_TYPES
4927 TYPE_FIND_REGISTER (plugin, "audio/iLBC-sh", GST_RANK_PRIMARY,
4928 ilbc_type_find, ilbc_exts, ILBC_CAPS, NULL, NULL);
4929 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sid", GST_RANK_MARGINAL,
4930 sid_exts, "PSID", 4, GST_TYPE_FIND_MAXIMUM);
4931 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xcf", GST_RANK_SECONDARY,
4932 xcf_exts, "gimp xcf", 8, GST_TYPE_FIND_MAXIMUM);
4933 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mng", GST_RANK_SECONDARY,
4934 mng_exts, "\212MNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
4935 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-jng", GST_RANK_SECONDARY,
4936 jng_exts, "\213JNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
4937 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xpixmap", GST_RANK_SECONDARY,
4938 xpm_exts, "/* XPM */", 9, GST_TYPE_FIND_MAXIMUM);
4939 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-sun-raster",
4940 GST_RANK_SECONDARY, ras_exts, "\131\246\152\225", 4,
4941 GST_TYPE_FIND_MAXIMUM);
4942 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-bzip",
4943 GST_RANK_SECONDARY, bz2_exts, "BZh", 3, GST_TYPE_FIND_LIKELY);
4944 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-gzip",
4945 GST_RANK_SECONDARY, gz_exts, "\037\213", 2, GST_TYPE_FIND_LIKELY);
4946 TYPE_FIND_REGISTER_START_WITH (plugin, "application/zip", GST_RANK_SECONDARY,
4947 zip_exts, "PK\003\004", 4, GST_TYPE_FIND_LIKELY);
4948 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-compress",
4949 GST_RANK_SECONDARY, compress_exts, "\037\235", 2, GST_TYPE_FIND_LIKELY);
4950 TYPE_FIND_REGISTER (plugin, "subtitle/x-kate", GST_RANK_MARGINAL,
4951 kate_type_find, NULL, NULL, NULL, NULL);
4953 TYPE_FIND_REGISTER (plugin, "audio/x-flac", GST_RANK_PRIMARY,
4954 flac_type_find, flac_exts, FLAC_CAPS, NULL, NULL);
4955 TYPE_FIND_REGISTER (plugin, "audio/x-vorbis", GST_RANK_PRIMARY,
4956 vorbis_type_find, NULL, VORBIS_CAPS, NULL, NULL);
4957 TYPE_FIND_REGISTER (plugin, "video/x-theora", GST_RANK_PRIMARY,
4958 theora_type_find, NULL, THEORA_CAPS, NULL, NULL);
4959 #ifndef GST_EXT_MIME_TYPES
4960 TYPE_FIND_REGISTER (plugin, "application/x-ogm-video", GST_RANK_PRIMARY,
4961 ogmvideo_type_find, NULL, OGMVIDEO_CAPS, NULL, NULL);
4962 TYPE_FIND_REGISTER (plugin, "application/x-ogm-audio", GST_RANK_PRIMARY,
4963 ogmaudio_type_find, NULL, OGMAUDIO_CAPS, NULL, NULL);
4964 TYPE_FIND_REGISTER (plugin, "application/x-ogm-text", GST_RANK_PRIMARY,
4965 ogmtext_type_find, NULL, OGMTEXT_CAPS, NULL, NULL);
4966 TYPE_FIND_REGISTER (plugin, "audio/x-speex", GST_RANK_PRIMARY,
4967 speex_type_find, NULL, SPEEX_CAPS, NULL, NULL);
4968 TYPE_FIND_REGISTER (plugin, "audio/x-celt", GST_RANK_PRIMARY,
4969 celt_type_find, NULL, CELT_CAPS, NULL, NULL);
4970 TYPE_FIND_REGISTER (plugin, "application/x-ogg-skeleton", GST_RANK_PRIMARY,
4971 oggskel_type_find, NULL, OGG_SKELETON_CAPS, NULL, NULL);
4972 TYPE_FIND_REGISTER (plugin, "text/x-cmml", GST_RANK_PRIMARY, cmml_type_find,
4973 NULL, CMML_CAPS, NULL, NULL);
4974 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-executable",
4975 GST_RANK_MARGINAL, NULL, "\177ELF", 4, GST_TYPE_FIND_MAXIMUM);
4977 TYPE_FIND_REGISTER (plugin, "audio/aac", GST_RANK_SECONDARY,
4978 aac_type_find, aac_exts, AAC_CAPS, NULL, NULL);
4979 #ifndef GST_EXT_MIME_TYPES
4980 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-spc", GST_RANK_SECONDARY,
4981 spc_exts, "SNES-SPC700 Sound File Data", 27, GST_TYPE_FIND_MAXIMUM);
4982 TYPE_FIND_REGISTER (plugin, "audio/x-wavpack", GST_RANK_SECONDARY,
4983 wavpack_type_find, wavpack_exts, WAVPACK_CAPS, NULL, NULL);
4984 TYPE_FIND_REGISTER (plugin, "audio/x-wavpack-correction", GST_RANK_SECONDARY,
4985 wavpack_type_find, wavpack_correction_exts, WAVPACK_CORRECTION_CAPS, NULL,
4987 TYPE_FIND_REGISTER (plugin, "application/postscript", GST_RANK_SECONDARY,
4988 postscript_type_find, ps_exts, POSTSCRIPT_CAPS, NULL, NULL);
4989 TYPE_FIND_REGISTER (plugin, "image/svg+xml", GST_RANK_SECONDARY,
4990 svg_type_find, svg_exts, SVG_CAPS, NULL, NULL);
4991 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-rar",
4992 GST_RANK_SECONDARY, rar_exts, "Rar!", 4, GST_TYPE_FIND_LIKELY);
4993 TYPE_FIND_REGISTER (plugin, "application/x-tar", GST_RANK_SECONDARY,
4994 tar_type_find, tar_exts, TAR_CAPS, NULL, NULL);
4995 TYPE_FIND_REGISTER (plugin, "application/x-ar", GST_RANK_SECONDARY,
4996 ar_type_find, ar_exts, AR_CAPS, NULL, NULL);
4997 TYPE_FIND_REGISTER (plugin, "application/x-ms-dos-executable",
4998 GST_RANK_SECONDARY, msdos_type_find, msdos_exts, MSDOS_CAPS, NULL, NULL);
4999 TYPE_FIND_REGISTER (plugin, "video/x-dirac", GST_RANK_PRIMARY,
5000 dirac_type_find, NULL, DIRAC_CAPS, NULL, NULL);
5001 TYPE_FIND_REGISTER (plugin, "multipart/x-mixed-replace", GST_RANK_SECONDARY,
5002 multipart_type_find, NULL, MULTIPART_CAPS, NULL, NULL);
5003 TYPE_FIND_REGISTER (plugin, "application/x-mmsh", GST_RANK_SECONDARY,
5004 mmsh_type_find, NULL, MMSH_CAPS, NULL, NULL);
5005 TYPE_FIND_REGISTER (plugin, "video/vivo", GST_RANK_SECONDARY,
5006 vivo_type_find, vivo_exts, VIVO_CAPS, NULL, NULL);
5007 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nsf",
5008 GST_RANK_SECONDARY, nsf_exts, "NESM\x1a", 5, GST_TYPE_FIND_MAXIMUM);
5009 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gym",
5010 GST_RANK_SECONDARY, gym_exts, "GYMX", 4, GST_TYPE_FIND_MAXIMUM);
5011 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-ay",
5012 GST_RANK_SECONDARY, ay_exts, "ZXAYEMUL", 8, GST_TYPE_FIND_MAXIMUM);
5013 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gbs",
5014 GST_RANK_SECONDARY, gbs_exts, "GBS\x01", 4, GST_TYPE_FIND_MAXIMUM);
5015 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-vgm",
5016 GST_RANK_SECONDARY, vgm_exts, "Vgm\x20", 4, GST_TYPE_FIND_MAXIMUM);
5017 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sap",
5018 GST_RANK_SECONDARY, sap_exts, "SAP\x0d\x0a" "AUTHOR\x20", 12,
5019 GST_TYPE_FIND_MAXIMUM);
5020 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ivf", GST_RANK_SECONDARY,
5021 ivf_exts, "DKIF", 4, GST_TYPE_FIND_NEARLY_CERTAIN);
5022 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-kss", GST_RANK_SECONDARY,
5023 kss_exts, "KSSX\0", 5, GST_TYPE_FIND_MAXIMUM);
5024 TYPE_FIND_REGISTER_START_WITH (plugin, "application/pdf", GST_RANK_SECONDARY,
5025 pdf_exts, "%PDF-", 5, GST_TYPE_FIND_LIKELY);
5026 TYPE_FIND_REGISTER_START_WITH (plugin, "application/msword",
5027 GST_RANK_SECONDARY, msword_exts, "\320\317\021\340\241\261\032\341", 8,
5028 GST_TYPE_FIND_LIKELY);
5029 /* Mac OS X .DS_Store files tend to be taken for video/mpeg */
5030 TYPE_FIND_REGISTER_START_WITH (plugin, "application/octet-stream",
5031 GST_RANK_SECONDARY, dsstore_exts, "\000\000\000\001Bud1", 8,
5032 GST_TYPE_FIND_LIKELY);
5033 TYPE_FIND_REGISTER_START_WITH (plugin, "image/vnd.adobe.photoshop",
5034 GST_RANK_SECONDARY, psd_exts, "8BPS\000\001\000\000\000\000", 10,
5035 GST_TYPE_FIND_LIKELY);
5036 TYPE_FIND_REGISTER (plugin, "image/vnd.wap.wbmp", GST_RANK_MARGINAL,
5037 wbmp_typefind, NULL, NULL, NULL, NULL);
5038 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-yuv4mpeg",
5039 GST_RANK_SECONDARY, y4m_exts, "YUV4MPEG2 ", 10, GST_TYPE_FIND_LIKELY);
5040 TYPE_FIND_REGISTER (plugin, "image/x-icon", GST_RANK_MARGINAL,
5041 windows_icon_typefind, NULL, NULL, NULL, NULL);
5044 TYPE_FIND_REGISTER (plugin, "xdgmime-base", GST_RANK_MARGINAL,
5045 xdgmime_typefind, NULL, NULL, NULL, NULL);
5048 TYPE_FIND_REGISTER (plugin, "image/x-degas", GST_RANK_MARGINAL,
5049 degas_type_find, NULL, NULL, NULL, NULL);
5055 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
5057 "typefindfunctions",
5058 "default typefind functions",
5059 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)