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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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 /* DataScanCtx: helper for typefind functions that scan through data
50 * step-by-step, to avoid doing a peek at each and every offset */
52 #define DATA_SCAN_CTX_CHUNK_SIZE 4096
62 data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip)
64 c->offset += bytes_to_skip;
65 if (G_LIKELY (c->size > bytes_to_skip)) {
66 c->size -= bytes_to_skip;
67 c->data += bytes_to_skip;
74 static inline gboolean
75 data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, gint min_len)
79 guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
81 if (G_LIKELY (c->size >= min_len))
84 data = gst_type_find_peek (tf, c->offset, chunk_len);
85 if (G_LIKELY (data != NULL)) {
91 /* if there's less than our chunk size, try to get as much as we can, but
92 * always at least min_len bytes (we might be typefinding the first buffer
93 * of the stream and not have as much data available as we'd like) */
94 len = gst_type_find_get_length (tf);
96 len = CLAMP (len - c->offset, min_len, chunk_len);
101 data = gst_type_find_peek (tf, c->offset, len);
111 static inline gboolean
112 data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset,
113 const gchar * data, guint len)
115 if (!data_scan_ctx_ensure_data (tf, c, offset + len))
118 return (memcmp (c->data + offset, data, len) == 0);
122 static gboolean xml_check_first_element (GstTypeFind * tf,
123 const gchar * element, guint elen, gboolean strict);
124 static gboolean sdp_check_header (GstTypeFind * tf);
126 static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
128 #define UTF8_CAPS gst_static_caps_get(&utf8_caps)
131 utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
132 GstTypeFindProbability * prob)
136 /* randomly decided values */
137 guint min_size = 16; /* minimum size */
138 guint size = 32 * 1024; /* starting size */
139 guint probability = 95; /* starting probability */
140 guint step = 10; /* how much we reduce probability in each
143 while (probability > step && size > min_size) {
144 data = gst_type_find_peek (tf, offset, size);
147 gchar *start = (gchar *) data;
149 if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) { /* allow last char to be cut off */
164 utf8_type_find (GstTypeFind * tf, gpointer unused)
166 GstTypeFindProbability start_prob, mid_prob;
169 /* leave xml to the xml typefinders */
170 if (xml_check_first_element (tf, "", 0, TRUE))
173 /* leave sdp to the sdp typefinders */
174 if (sdp_check_header (tf))
177 /* check beginning of stream */
178 if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob))
181 GST_LOG ("start is plain text with probability of %u", start_prob);
183 /* POSSIBLE is the highest probability we ever return if we can't
184 * probe into the middle of the file and don't know its length */
186 length = gst_type_find_get_length (tf);
187 if (length == 0 || length == (guint64) - 1) {
188 gst_type_find_suggest (tf, MIN (start_prob, GST_TYPE_FIND_POSSIBLE),
193 if (length < 64 * 1024) {
194 gst_type_find_suggest (tf, start_prob, UTF8_CAPS);
198 /* check middle of stream */
199 if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob))
202 GST_LOG ("middle is plain text with probability of %u", mid_prob);
203 gst_type_find_suggest (tf, (start_prob + mid_prob) / 2, UTF8_CAPS);
206 /*** text/utf-16 and text/utf-32} ***/
207 /* While UTF-8 is unicode too, using text/plain for UTF-16 and UTF-32
208 is going to break stuff. */
213 const char *const bom;
214 gboolean (*checker) (const guint8 *, gint, gint);
220 check_utf16 (const guint8 * data, gint len, gint endianness)
230 gst_byte_reader_init (&br, data, len);
232 /* test first for a single 16 bit value in the BMP */
233 if (endianness == G_BIG_ENDIAN)
234 high = gst_byte_reader_get_uint16_be_unchecked (&br);
236 high = gst_byte_reader_get_uint16_le_unchecked (&br);
237 if (high >= 0xD800 && high <= 0xDBFF) {
238 /* start of a surrogate pair */
242 if (endianness == G_BIG_ENDIAN)
243 low = gst_byte_reader_get_uint16_be_unchecked (&br);
245 low = gst_byte_reader_get_uint16_le_unchecked (&br);
246 if (low >= 0xDC00 && low <= 0xDFFF) {
247 /* second half of the surrogate pair */
251 if (high >= 0xDC00 && high <= 0xDFFF)
260 check_utf32 (const guint8 * data, gint len, gint endianness)
266 if (endianness == G_BIG_ENDIAN)
267 v = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
269 v = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
279 unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester,
280 guint n_tester, const char *media_type, gboolean require_bom)
284 const guint8 *data = gst_type_find_peek (tf, 0, len);
286 const gint max_scan_size = 256 * 1024;
291 data = gst_type_find_peek (tf, 0, len);
296 /* find a large enough size that works */
297 while (len < max_scan_size) {
298 size_t newlen = len << 1;
299 const guint8 *newdata = gst_type_find_peek (tf, 0, newlen);
306 for (n = 0; n < n_tester; ++n) {
307 int bom_boost = 0, tmpprob;
308 if (len >= tester[n].bomlen) {
309 if (!memcmp (data, tester[n].bom, tester[n].bomlen))
310 bom_boost = tester[n].boost;
312 if (require_bom && bom_boost == 0)
314 if (!(*tester[n].checker) (data, len, tester[n].endianness))
316 tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost;
317 if (tmpprob > prob) {
319 endianness = tester[n].endianness;
324 GST_DEBUG ("This is valid %s %s", media_type,
325 endianness == G_BIG_ENDIAN ? "be" : "le");
326 gst_type_find_suggest_simple (tf, prob, media_type,
327 "endianness", G_TYPE_INT, endianness, NULL);
331 static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16");
333 #define UTF16_CAPS gst_static_caps_get(&utf16_caps)
336 utf16_type_find (GstTypeFind * tf, gpointer unused)
338 static const GstUnicodeTester utf16tester[2] = {
339 {2, "\xff\xfe", check_utf16, 10, G_LITTLE_ENDIAN},
340 {2, "\xfe\xff", check_utf16, 20, G_BIG_ENDIAN},
342 unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester),
343 "text/utf-16", TRUE);
346 static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32");
348 #define UTF32_CAPS gst_static_caps_get(&utf32_caps)
351 utf32_type_find (GstTypeFind * tf, gpointer unused)
353 static const GstUnicodeTester utf32tester[2] = {
354 {4, "\xff\xfe\x00\x00", check_utf32, 10, G_LITTLE_ENDIAN},
355 {4, "\x00\x00\xfe\xff", check_utf32, 20, G_BIG_ENDIAN}
357 unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester),
358 "text/utf-32", TRUE);
361 /*** text/uri-list ***/
363 static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
365 #define URI_CAPS (gst_static_caps_get(&uri_caps))
366 #define BUFFER_SIZE 16 /* If the string is < 16 bytes we're screwed */
367 #define INC_BUFFER { \
369 if (pos == BUFFER_SIZE) { \
371 offset += BUFFER_SIZE; \
372 data = gst_type_find_peek (tf, offset, BUFFER_SIZE); \
373 if (data == NULL) return; \
379 uri_type_find (GstTypeFind * tf, gpointer unused)
381 const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
386 /* Search for # comment lines */
387 while (*data == '#') {
388 /* Goto end of line */
389 while (*data != '\n') {
396 if (!g_ascii_isalpha (*data)) {
397 /* Had a non alpha char - can't be uri-list */
403 while (g_ascii_isalnum (*data)) {
408 /* First non alpha char is not a : */
412 /* Get the next 2 bytes as well */
413 data = gst_type_find_peek (tf, offset + pos, 3);
417 if (data[1] != '/' && data[2] != '/') {
421 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, URI_CAPS);
425 /*** application/x-hls ***/
427 static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls");
428 #define HLS_CAPS (gst_static_caps_get(&hls_caps))
430 /* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */
432 hls_type_find (GstTypeFind * tf, gpointer unused)
434 DataScanCtx c = { 0, NULL, 0 };
436 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 7)))
439 if (memcmp (c.data, "#EXTM3U", 7))
442 data_scan_ctx_advance (tf, &c, 7);
444 /* Check only the first 256 bytes */
445 while (c.offset < 256) {
446 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21)))
449 /* Search for # comment lines */
450 if (c.data[0] == '#' && (memcmp (c.data, "#EXT-X-TARGETDURATION", 21) == 0
451 || memcmp (c.data, "#EXT-X-STREAM-INF", 17) == 0
452 || memcmp (c.data, "#EXT-X-MEDIA", 12) == 0)) {
453 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HLS_CAPS);
457 data_scan_ctx_advance (tf, &c, 1);
462 /*** application/xml **********************************************************/
464 #define XML_BUFFER_SIZE 16
465 #define XML_INC_BUFFER { \
467 if (pos == XML_BUFFER_SIZE) { \
469 offset += XML_BUFFER_SIZE; \
470 data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE); \
471 if (data == NULL) return FALSE; \
477 #define XML_INC_BUFFER_DATA { \
479 if (pos >= length) { \
487 xml_check_first_element_from_data (const guint8 * data, guint length,
488 const gchar * element, guint elen, gboolean strict)
493 g_return_val_if_fail (data != NULL, FALSE);
498 /* look for the XMLDec
499 * see XML spec 2.8, Prolog and Document Type Declaration
500 * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
501 got_xmldec = (memcmp (data, "<?xml", 5) == 0);
503 if (strict && !got_xmldec)
506 /* skip XMLDec in any case if we've got one */
512 /* look for the first element, it has to be the requested element. Bail
513 * out if it is not within the first 4kB. */
514 while (pos < MIN (4096, length)) {
515 while (*data != '<' && pos < MIN (4096, length)) {
520 if (!g_ascii_isalpha (*data)) {
521 /* if not alphabetic, it's a PI or an element / attribute declaration
522 * like <?xxx or <!xxx */
527 /* the first normal element, check if it's the one asked for */
528 if (pos + elen + 1 >= length)
530 return (element && strncmp ((const char *) data, element, elen) == 0);
537 xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
545 data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
549 /* look for the XMLDec
550 * see XML spec 2.8, Prolog and Document Type Declaration
551 * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
552 got_xmldec = (memcmp (data, "<?xml", 5) == 0);
554 if (strict && !got_xmldec)
557 /* skip XMLDec in any case if we've got one */
563 /* look for the first element, it has to be the requested element. Bail
564 * out if it is not within the first 4kB. */
565 while (data && (offset + pos) < 4096) {
566 while (*data != '<' && (offset + pos) < 4096) {
571 if (!g_ascii_isalpha (*data)) {
572 /* if not alphabetic, it's a PI or an element / attribute declaration
573 * like <?xxx or <!xxx */
578 /* the first normal element, check if it's the one asked for */
579 data = gst_type_find_peek (tf, offset + pos, elen + 1);
580 return (data && element && strncmp ((char *) data, element, elen) == 0);
586 static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml");
588 #define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps))
590 xml_type_find (GstTypeFind * tf, gpointer unused)
592 if (xml_check_first_element (tf, "", 0, TRUE)) {
593 gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
597 /*** application/dash+xml ****************************************************/
599 static GstStaticCaps dash_caps = GST_STATIC_CAPS ("application/dash+xml");
601 #define DASH_CAPS gst_static_caps_get (&dash_caps)
604 dash_mpd_type_find (GstTypeFind * tf, gpointer unused)
606 if (xml_check_first_element (tf, "MPD", 3, FALSE) ||
607 xml_check_first_element (tf, "mpd", 3, FALSE)) {
608 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DASH_CAPS);
612 /*** application/sdp *********************************************************/
614 static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp");
616 #define SDP_CAPS (gst_static_caps_get(&sdp_caps))
618 sdp_check_header (GstTypeFind * tf)
622 data = gst_type_find_peek (tf, 0, 5);
626 /* sdp must start with v=0[\r]\n */
627 if (memcmp (data, "v=0", 3))
630 if (data[3] == '\r' && data[4] == '\n')
639 sdp_type_find (GstTypeFind * tf, gpointer unused)
641 if (sdp_check_header (tf))
642 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS);
645 /*** application/smil *********************************************************/
647 static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil");
649 #define SMIL_CAPS (gst_static_caps_get(&smil_caps))
651 smil_type_find (GstTypeFind * tf, gpointer unused)
653 if (xml_check_first_element (tf, "smil", 4, FALSE)) {
654 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
658 /*** application/ttml+xml *****************************************************/
660 static GstStaticCaps ttml_xml_caps = GST_STATIC_CAPS ("application/ttml+xml");
662 #define TTML_XML_CAPS (gst_static_caps_get(&ttml_xml_caps))
664 ttml_xml_type_find (GstTypeFind * tf, gpointer unused)
666 if (xml_check_first_element (tf, "tt", 2, FALSE)) {
667 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTML_XML_CAPS);
673 static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html");
675 #define HTML_CAPS gst_static_caps_get (&html_caps)
678 html_type_find (GstTypeFind * tf, gpointer unused)
680 const gchar *d, *data;
682 data = (const gchar *) gst_type_find_peek (tf, 0, 16);
686 if (!g_ascii_strncasecmp (data, "<!DOCTYPE HTML", 14)) {
687 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
688 } else if (xml_check_first_element (tf, "html", 4, FALSE)) {
689 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
690 } else if ((d = memchr (data, '<', 16))) {
691 data = (const gchar *) gst_type_find_peek (tf, d - data, 6);
692 if (data && g_ascii_strncasecmp (data, "<html>", 6) == 0) {
693 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
700 static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi");
702 #define MID_CAPS gst_static_caps_get(&mid_caps)
704 mid_type_find (GstTypeFind * tf, gpointer unused)
706 const guint8 *data = gst_type_find_peek (tf, 0, 4);
708 /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
709 if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h'
711 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS);
714 /*** audio/mobile-xmf ***/
716 static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf");
718 #define MXMF_CAPS gst_static_caps_get(&mxmf_caps)
720 mxmf_type_find (GstTypeFind * tf, gpointer unused)
722 const guint8 *data = NULL;
724 /* Search FileId "XMF_" 4 bytes */
725 data = gst_type_find_peek (tf, 0, 4);
726 if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
728 /* Search Format version "2.00" 4 bytes */
729 data = gst_type_find_peek (tf, 4, 4);
730 if (data && data[0] == '2' && data[1] == '.' && data[2] == '0'
732 /* Search TypeId 2 1 byte */
733 data = gst_type_find_peek (tf, 11, 1);
734 if (data && data[0] == 2) {
735 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS);
742 /*** video/x-fli ***/
744 static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
746 #define FLX_CAPS gst_static_caps_get(&flx_caps)
748 flx_type_find (GstTypeFind * tf, gpointer unused)
750 const guint8 *data = gst_type_find_peek (tf, 0, 134);
753 /* check magic and the frame type of the first frame */
754 if ((data[4] == 0x11 || data[4] == 0x12 ||
755 data[4] == 0x30 || data[4] == 0x44) &&
757 ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
758 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS);
762 data = gst_type_find_peek (tf, 0, 6);
764 /* check magic only */
765 if ((data[4] == 0x11 || data[4] == 0x12 ||
766 data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
767 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
773 /*** application/x-id3 ***/
775 static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
777 #define ID3_CAPS gst_static_caps_get(&id3_caps)
779 id3v2_type_find (GstTypeFind * tf, gpointer unused)
781 const guint8 *data = gst_type_find_peek (tf, 0, 10);
783 if (data && memcmp (data, "ID3", 3) == 0 &&
784 data[3] != 0xFF && data[4] != 0xFF &&
785 (data[6] & 0x80) == 0 && (data[7] & 0x80) == 0 &&
786 (data[8] & 0x80) == 0 && (data[9] & 0x80) == 0) {
787 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
792 id3v1_type_find (GstTypeFind * tf, gpointer unused)
794 const guint8 *data = gst_type_find_peek (tf, -128, 3);
796 if (data && memcmp (data, "TAG", 3) == 0) {
797 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
801 /*** application/x-ape ***/
803 static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag");
805 #define APETAG_CAPS gst_static_caps_get(&apetag_caps)
807 apetag_type_find (GstTypeFind * tf, gpointer unused)
811 /* APEv1/2 at start of file */
812 data = gst_type_find_peek (tf, 0, 8);
813 if (data && !memcmp (data, "APETAGEX", 8)) {
814 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
818 /* APEv1/2 at end of file */
819 data = gst_type_find_peek (tf, -32, 8);
820 if (data && !memcmp (data, "APETAGEX", 8)) {
821 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
826 /*** audio/x-ttafile ***/
828 static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
830 #define TTA_CAPS gst_static_caps_get(&tta_caps)
832 tta_type_find (GstTypeFind * tf, gpointer unused)
834 const guint8 *data = gst_type_find_peek (tf, 0, 3);
837 if (memcmp (data, "TTA", 3) == 0) {
838 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS);
844 /*** audio/x-flac ***/
845 static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");
847 #define FLAC_CAPS (gst_static_caps_get(&flac_caps))
850 flac_type_find (GstTypeFind * tf, gpointer unused)
852 DataScanCtx c = { 0, NULL, 0 };
854 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
857 /* standard flac (also old/broken flac-in-ogg with an initial 4-byte marker
858 * packet and without the usual packet framing) */
859 if (memcmp (c.data, "fLaC", 4) == 0) {
860 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
864 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
867 /* flac-in-ogg, see http://flac.sourceforge.net/ogg_mapping.html */
868 if (memcmp (c.data, "\177FLAC\001", 6) == 0) {
869 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
873 /* disabled because it happily typefinds /dev/urandom as audio/x-flac, and
874 * because I yet have to see header-less flac in the wild */
876 /* flac without headers (subset format) */
877 /* 64K should be enough */
878 while (c.offset < (64 * 1024)) {
879 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
882 /* look for frame header,
883 * http://flac.sourceforge.net/format.html#frame_header
885 if (c.data[0] == 0xff && (c.data[1] >> 2) == 0x3e) {
886 /* bit 15 in the header must be 0 */
887 if (((c.data[1] >> 1) & 0x01) == 0x01)
890 /* blocksize must be != 0x00 */
891 if ((c.data[2] >> 4) == 0x00)
894 /* samplerate must be != 0x0f */
895 if ((c.data[2] & 0x0f) == 0x0f)
897 /* also 0 is invalid, as it means get the info from the header and we
898 * don't have headers if we are here */
899 if ((c.data[2] & 0x0f) == 0x00)
902 /* channel assignment must be < 11 */
903 if ((c.data[3] >> 4) >= 11)
906 /* sample size must be != 0x07 and != 0x05 */
907 if (((c.data[3] >> 1) & 0x07) == 0x07)
909 if (((c.data[3] >> 1) & 0x07) == 0x05)
911 /* also 0 is invalid, as it means get the info from the header and we
912 * don't have headers if we are here */
913 if (((c.data[3] >> 1) & 0x07) == 0x00)
916 /* next bit must be 0 */
917 if ((c.data[3] & 0x01) == 0x01)
920 /* FIXME: shouldn't we include the crc check ? */
922 GST_DEBUG ("Found flac without headers at %d", (gint) c.offset);
923 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS);
927 data_scan_ctx_advance (tf, &c, 1);
932 /* TODO: we could probably make a generic function for this.. */
934 aac_type_find_scan_loas_frames_ep (GstTypeFind * tf, DataScanCtx * scan_ctx,
937 DataScanCtx c = *scan_ctx;
943 if (!data_scan_ctx_ensure_data (tf, &c, 5))
946 /* EPAudioSyncStream */
947 len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
948 ((c.data[4] & 0x80) >> 7);
950 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
951 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
955 /* check length of frame */
956 snc = GST_READ_UINT16_BE (c.data + len);
958 GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
964 GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
965 "framelen %u", count, c.offset, len);
967 data_scan_ctx_advance (tf, &c, len);
968 } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
970 GST_DEBUG ("found %d consecutive frames", count);
975 aac_type_find_scan_loas_frames (GstTypeFind * tf, DataScanCtx * scan_ctx,
978 DataScanCtx c = *scan_ctx;
984 if (!data_scan_ctx_ensure_data (tf, &c, 3))
987 /* AudioSyncStream */
988 len = ((c.data[1] & 0x1f) << 8) | c.data[2];
989 /* add size of sync stream header */
992 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
993 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
997 /* check length of frame */
998 snc = GST_READ_UINT16_BE (c.data + len);
999 if ((snc & 0xffe0) != 0x56e0) {
1000 GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
1006 GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
1007 "framelen %u", count, c.offset, len);
1009 data_scan_ctx_advance (tf, &c, len);
1010 } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
1012 GST_DEBUG ("found %d consecutive frames", count);
1016 /*** audio/mpeg version 2, 4 ***/
1018 static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
1019 "mpegversion = (int) { 2, 4 }, framed = (bool) false");
1020 #define AAC_CAPS (gst_static_caps_get(&aac_caps))
1021 #define AAC_AMOUNT (4096)
1023 aac_type_find (GstTypeFind * tf, gpointer unused)
1025 DataScanCtx c = { 0, NULL, 0 };
1026 GstTypeFindProbability best_probability = GST_TYPE_FIND_NONE;
1027 GstCaps *best_caps = NULL;
1028 guint best_count = 0;
1030 while (c.offset < AAC_AMOUNT) {
1031 guint snc, len, offset, i;
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);
1059 /* check if there's a second ADTS frame */
1060 snc = GST_READ_UINT16_BE (c.data + offset);
1061 if ((snc & 0xfff6) == 0xfff0) {
1063 guint mpegversion, sample_freq_idx, channel_config, profile_idx, rate;
1064 guint8 audio_config[2];
1066 mpegversion = (c.data[1] & 0x08) ? 2 : 4;
1067 profile_idx = c.data[2] >> 6;
1068 sample_freq_idx = ((c.data[2] & 0x3c) >> 2);
1069 channel_config = ((c.data[2] & 0x01) << 2) + (c.data[3] >> 6);
1071 GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%"
1072 G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len);
1074 /* 0xd and 0xe are reserved. 0xf means the sample frequency is directly
1075 * specified in the header, but that's not allowed for ADTS */
1076 if (sample_freq_idx > 0xc) {
1077 GST_DEBUG ("Unexpected sample frequency index %d or wrong sync",
1082 rate = gst_codec_utils_aac_get_sample_rate_from_index (sample_freq_idx);
1083 GST_LOG ("ADTS: profile=%u, rate=%u", profile_idx, rate);
1085 /* The ADTS frame header is slightly different from the
1086 * AudioSpecificConfig defined for the MPEG-4 container, so we just
1087 * construct enough of it for getting the level here. */
1088 /* ADTS counts profiles from 0 instead of 1 to save bits */
1089 audio_config[0] = (profile_idx + 1) << 3;
1090 audio_config[0] |= (sample_freq_idx >> 1) & 0x7;
1091 audio_config[1] = (sample_freq_idx & 0x1) << 7;
1092 audio_config[1] |= (channel_config & 0xf) << 3;
1094 caps = gst_caps_new_simple ("audio/mpeg",
1095 "framed", G_TYPE_BOOLEAN, FALSE,
1096 "mpegversion", G_TYPE_INT, mpegversion,
1097 "stream-format", G_TYPE_STRING, "adts", NULL);
1099 gst_codec_utils_aac_caps_set_level_and_profile (caps, audio_config, 2);
1101 /* add rate and number of channels if we can */
1102 if (channel_config != 0 && channel_config <= 7) {
1103 const guint channels_map[] = { 0, 1, 2, 3, 4, 5, 6, 8 };
1105 gst_caps_set_simple (caps, "channels", G_TYPE_INT,
1106 channels_map[channel_config], "rate", G_TYPE_INT, rate, NULL);
1109 /* length of the second ADTS frame */
1110 len = ((c.data[offset + 3] & 0x03) << 11) |
1111 (c.data[offset + 4] << 3) | ((c.data[offset + 5] & 0xe0) >> 5);
1113 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1114 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1115 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps);
1118 /* find more aac sync to select correctly */
1119 /* check if there's a third/fourth/fifth/sixth ADTS frame, if there is a sixth frame, set probability to maximum:100% */
1120 for (i = 3; i <= 6; i++) {
1121 len = ((c.data[offset + 3] & 0x03) << 11) |
1122 (c.data[offset + 4] << 3) | ((c.data[offset + 5] & 0xe0) >> 5);
1123 if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1124 GST_DEBUG ("Wrong sync or next frame not within reach, len=%u",
1128 snc = GST_READ_UINT16_BE (c.data + offset);
1129 if ((snc & 0xfff6) == 0xfff0) {
1130 GST_DEBUG ("Find %und Sync..probability is %u ", i,
1131 GST_TYPE_FIND_LIKELY + 5 * (i - 2));
1137 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 5 * (i - 3), caps);
1140 gst_caps_unref (caps);
1144 GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
1145 } else if (G_UNLIKELY ((snc & 0xffe0) == 0x56e0 || snc == 0x4de1)) {
1149 GST_INFO ("Possible LOAS syncword at offset 0x%" G_GINT64_MODIFIER
1150 "x, scanning for more frames...", c.offset);
1153 count = aac_type_find_scan_loas_frames_ep (tf, &c, 20);
1155 count = aac_type_find_scan_loas_frames (tf, &c, 20);
1157 if (count >= 3 && count > best_count) {
1158 gst_caps_replace (&best_caps, NULL);
1159 best_caps = gst_caps_new_simple ("audio/mpeg",
1160 "framed", G_TYPE_BOOLEAN, FALSE,
1161 "mpegversion", G_TYPE_INT, 4,
1162 "stream-format", G_TYPE_STRING, "loas", NULL);
1164 best_probability = GST_TYPE_FIND_POSSIBLE - 10 + count * 3;
1165 if (best_probability >= GST_TYPE_FIND_LIKELY)
1168 } else if (!memcmp (c.data, "ADIF", 4)) {
1170 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
1171 "framed", G_TYPE_BOOLEAN, FALSE, "mpegversion", G_TYPE_INT, 4,
1172 "stream-format", G_TYPE_STRING, "adif", NULL);
1178 data_scan_ctx_advance (tf, &c, 1);
1181 if (best_probability > GST_TYPE_FIND_NONE) {
1182 gst_type_find_suggest (tf, best_probability, best_caps);
1183 gst_caps_unref (best_caps);
1187 /*** audio/mpeg version 1 ***/
1190 * The chance that random data is identified as a valid mp3 header is 63 / 2^18
1191 * (0.024%) per try. This makes the function for calculating false positives
1192 * 1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize)
1193 * This has the following probabilities of false positives:
1194 * datasize MIN_HEADERS
1196 * 4096 62.6% 0.02% 0% 0%
1197 * 16384 98% 0.09% 0% 0%
1198 * 1 MiB 100% 5.88% 0% 0%
1199 * 1 GiB 100% 100% 1.44% 0%
1200 * 1 TiB 100% 100% 100% 0.35%
1201 * This means that the current choice (3 headers by most of the time 4096 byte
1202 * buffers is pretty safe for now.
1204 * The max. size of each frame is 1440 bytes, which means that for N frames to
1205 * be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 bytes of data.
1206 * Assuming we step into the stream right after the frame header, this
1207 * means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes
1208 * of data (5762) to always detect any mp3.
1211 static const guint mp3types_bitrates[2][3][16] =
1212 { {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
1213 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
1214 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
1215 {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
1216 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
1217 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}},
1220 static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
1221 {22050, 24000, 16000},
1222 {44100, 48000, 32000}
1226 mp3_type_frame_length_from_header (guint32 header, guint * put_layer,
1227 guint * put_channels, guint * put_bitrate, guint * put_samplerate,
1228 gboolean * may_be_free_format, gint possible_free_framelen)
1230 guint bitrate, layer, length, mode, samplerate, version, channels;
1232 if ((header & 0xffe00000) != 0xffe00000)
1235 /* we don't need extension, copyright, original or
1236 * emphasis for the frame length */
1240 mode = header & 0x3;
1244 length = header & 0x1;
1247 /* sampling frequency */
1248 samplerate = header & 0x3;
1249 if (samplerate == 3)
1254 bitrate = header & 0xF;
1255 if (bitrate == 0 && possible_free_framelen == -1) {
1256 GST_LOG ("Possibly a free format mp3 - signaling");
1257 *may_be_free_format = TRUE;
1259 if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1))
1262 /* ignore error correction, too */
1266 layer = 4 - (header & 0x3);
1271 /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */
1272 version = header & 0x3;
1277 channels = (mode == 3) ? 1 : 2;
1278 samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
1280 /* possible freeform mp3 */
1283 length += possible_free_framelen;
1284 bitrate = length * samplerate / 48000;
1286 length += possible_free_framelen;
1287 bitrate = length * samplerate /
1288 ((layer == 3 && version != 3) ? 72000 : 144000);
1290 /* freeform mp3 should have a higher-than-usually-allowed bitrate */
1291 GST_LOG ("calculated bitrate: %u, max usually: %u", bitrate,
1292 mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][14]);
1293 if (bitrate < mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][14])
1297 bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
1299 length = ((12000 * bitrate / samplerate) + length) * 4;
1301 length += ((layer == 3
1302 && version != 3) ? 72000 : 144000) * bitrate / samplerate;
1306 GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
1308 ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
1309 " - channels = %u", samplerate, bitrate, layer, version, channels);
1314 *put_channels = channels;
1316 *put_bitrate = bitrate;
1318 *put_samplerate = samplerate;
1324 static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, "
1325 "mpegversion = (int) 1, layer = (int) [ 1, 3 ]");
1326 #define MP3_CAPS (gst_static_caps_get(&mp3_caps))
1328 * random values for typefinding
1329 * if no more data is available, we will return a probability of
1330 * (found_headers/TRY_HEADERS) * (MAXIMUM * (TRY_SYNC - bytes_skipped)
1332 * if found_headers >= MIN_HEADERS
1334 #define GST_MP3_TYPEFIND_MIN_HEADERS (2)
1335 #define GST_MP3_TYPEFIND_TRY_HEADERS (5)
1336 #define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */
1337 #define GST_MP3_TYPEFIND_SYNC_SIZE (2048)
1338 #define GST_MP3_WRONG_HEADER (10)
1341 mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
1342 guint * found_layer, GstTypeFindProbability * found_prob)
1344 const guint8 *data = NULL;
1345 const guint8 *data_end = NULL;
1348 gint last_free_offset = -1;
1349 gint last_free_framelen = -1;
1350 gboolean headerstart = TRUE;
1357 while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) {
1359 size = GST_MP3_TYPEFIND_SYNC_SIZE * 2;
1362 data = gst_type_find_peek (tf, skipped + start_off, size);
1363 } while (size > 10 && !data);
1366 data_end = data + size;
1368 if (*data == 0xFF) {
1369 const guint8 *head_data = NULL;
1370 guint layer = 0, bitrate, samplerate, channels;
1371 guint found = 0; /* number of valid headers found */
1372 guint64 offset = skipped;
1373 gboolean changed = FALSE;
1375 while (found < GST_MP3_TYPEFIND_TRY_HEADERS) {
1378 guint prev_layer = 0;
1379 guint prev_channels = 0, prev_samplerate = 0;
1380 gboolean free = FALSE;
1382 if ((gint64) (offset - skipped + 4) >= 0 &&
1383 data + offset - skipped + 4 < data_end) {
1384 head_data = data + offset - skipped;
1386 head_data = gst_type_find_peek (tf, offset + start_off, 4);
1390 head = GST_READ_UINT32_BE (head_data);
1391 if (!(length = mp3_type_frame_length_from_header (head, &layer,
1392 &channels, &bitrate, &samplerate, &free,
1393 last_free_framelen))) {
1395 if (last_free_offset == -1)
1396 last_free_offset = offset;
1398 last_free_framelen = offset - last_free_offset;
1399 offset = last_free_offset;
1403 last_free_framelen = -1;
1406 /* Mark the fact that we didn't find a valid header at the beginning */
1408 headerstart = FALSE;
1410 GST_LOG ("%d. header at offset %" G_GUINT64_FORMAT
1411 " (0x%" G_GINT64_MODIFIER "x) was not an mp3 header "
1412 "(possibly-free: %s)", found + 1, start_off + offset,
1413 start_off + offset, free ? "yes" : "no");
1416 if ((prev_layer && prev_layer != layer) ||
1417 /* (prev_bitrate && prev_bitrate != bitrate) || <-- VBR */
1418 (prev_samplerate && prev_samplerate != samplerate) ||
1419 (prev_channels && prev_channels != channels)) {
1420 /* this means an invalid property, or a change, which might mean
1421 * that this is not a mp3 but just a random bytestream. It could
1422 * be a freaking funky encoded mp3 though. We'll just not count
1427 prev_channels = channels;
1428 prev_samplerate = samplerate;
1431 GST_LOG ("found %d. header at offset %" G_GUINT64_FORMAT " (0x%"
1432 G_GINT64_MODIFIER "X)", found, start_off + offset,
1433 start_off + offset);
1437 g_assert (found <= GST_MP3_TYPEFIND_TRY_HEADERS);
1438 if (head_data == NULL &&
1439 gst_type_find_peek (tf, offset + start_off - 1, 1) == NULL)
1440 /* Incomplete last frame - don't count it. */
1442 if (found == GST_MP3_TYPEFIND_TRY_HEADERS ||
1443 (found >= GST_MP3_TYPEFIND_MIN_HEADERS && head_data == NULL)) {
1444 /* we can make a valid guess */
1445 guint probability = found * GST_TYPE_FIND_MAXIMUM *
1446 (GST_MP3_TYPEFIND_TRY_SYNC - skipped) /
1447 GST_MP3_TYPEFIND_TRY_HEADERS / GST_MP3_TYPEFIND_TRY_SYNC;
1450 && probability > (GST_TYPE_FIND_MINIMUM + GST_MP3_WRONG_HEADER))
1451 probability -= GST_MP3_WRONG_HEADER;
1452 if (probability < GST_TYPE_FIND_MINIMUM)
1453 probability = GST_TYPE_FIND_MINIMUM;
1457 probability = (probability + GST_TYPE_FIND_MAXIMUM) / 2;
1460 ("audio/mpeg calculated %u = %u * %u / %u * (%u - %"
1461 G_GUINT64_FORMAT ") / %u", probability, GST_TYPE_FIND_MAXIMUM,
1462 found, GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC,
1463 (guint64) skipped, GST_MP3_TYPEFIND_TRY_SYNC);
1464 /* make sure we're not id3 tagged */
1465 head_data = gst_type_find_peek (tf, -128, 3);
1466 if (head_data && (memcmp (head_data, "TAG", 3) == 0)) {
1469 g_assert (probability <= GST_TYPE_FIND_MAXIMUM);
1471 *found_prob = probability;
1472 if (probability > 0)
1473 *found_layer = layer;
1484 mp3_type_find (GstTypeFind * tf, gpointer unused)
1486 GstTypeFindProbability prob, mid_prob;
1488 guint layer, mid_layer;
1491 mp3_type_find_at_offset (tf, 0, &layer, &prob);
1492 length = gst_type_find_get_length (tf);
1494 if (length == 0 || length == (guint64) - 1) {
1500 /* if we're pretty certain already, skip the additional check */
1501 if (prob >= GST_TYPE_FIND_LIKELY)
1504 mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob);
1508 GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob);
1514 if (layer != mid_layer) {
1515 GST_WARNING ("audio/mpeg layer discrepancy: %u vs. %u", layer, mid_layer);
1516 return; /* FIXME: or should we just go with the one in the middle? */
1519 /* detected mpeg audio both in middle of the file and at the start */
1520 prob = (prob + mid_prob) / 2;
1524 /* a valid header right at the start makes it more likely
1525 * that this is actually plain mpeg-1 audio */
1527 data = gst_type_find_peek (tf, 0, 4); /* use min. frame size? */
1528 if (data && mp3_type_frame_length_from_header (GST_READ_UINT32_BE (data),
1529 &layer, NULL, NULL, NULL, NULL, 0) != 0) {
1530 prob = MIN (prob + 10, GST_TYPE_FIND_MAXIMUM);
1541 g_return_if_fail (layer >= 1 && layer <= 3);
1543 gst_type_find_suggest_simple (tf, prob, "audio/mpeg",
1544 "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer,
1545 "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
1549 /*** audio/x-musepack ***/
1551 static GstStaticCaps musepack_caps =
1552 GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }");
1554 #define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps))
1556 musepack_type_find (GstTypeFind * tf, gpointer unused)
1558 const guint8 *data = gst_type_find_peek (tf, 0, 4);
1559 GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM;
1560 gint streamversion = -1;
1562 if (data && memcmp (data, "MP+", 3) == 0) {
1564 if ((data[3] & 0x7f) == 7) {
1565 prop = GST_TYPE_FIND_MAXIMUM;
1567 prop = GST_TYPE_FIND_LIKELY + 10;
1569 } else if (data && memcmp (data, "MPCK", 4) == 0) {
1571 prop = GST_TYPE_FIND_MAXIMUM;
1574 if (streamversion != -1) {
1575 gst_type_find_suggest_simple (tf, prop, "audio/x-musepack",
1576 "streamversion", G_TYPE_INT, streamversion, NULL);
1580 /*** audio/x-ac3 ***/
1581 /* FIXME 0.11: should be audio/ac3, but isn't for backwards compatibility */
1582 static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3");
1584 #define AC3_CAPS (gst_static_caps_get(&ac3_caps))
1586 static GstStaticCaps eac3_caps = GST_STATIC_CAPS ("audio/x-eac3");
1588 #define EAC3_CAPS (gst_static_caps_get(&eac3_caps))
1592 unsigned short bit_rate;
1593 unsigned short frm_size[3];
1596 static const struct ac3_frmsize ac3_frmsizecod_tbl[] = {
1599 {40, {80, 87, 120}},
1600 {40, {80, 88, 120}},
1601 {48, {96, 104, 144}},
1602 {48, {96, 105, 144}},
1603 {56, {112, 121, 168}},
1604 {56, {112, 122, 168}},
1605 {64, {128, 139, 192}},
1606 {64, {128, 140, 192}},
1607 {80, {160, 174, 240}},
1608 {80, {160, 175, 240}},
1609 {96, {192, 208, 288}},
1610 {96, {192, 209, 288}},
1611 {112, {224, 243, 336}},
1612 {112, {224, 244, 336}},
1613 {128, {256, 278, 384}},
1614 {128, {256, 279, 384}},
1615 {160, {320, 348, 480}},
1616 {160, {320, 349, 480}},
1617 {192, {384, 417, 576}},
1618 {192, {384, 418, 576}},
1619 {224, {448, 487, 672}},
1620 {224, {448, 488, 672}},
1621 {256, {512, 557, 768}},
1622 {256, {512, 558, 768}},
1623 {320, {640, 696, 960}},
1624 {320, {640, 697, 960}},
1625 {384, {768, 835, 1152}},
1626 {384, {768, 836, 1152}},
1627 {448, {896, 975, 1344}},
1628 {448, {896, 976, 1344}},
1629 {512, {1024, 1114, 1536}},
1630 {512, {1024, 1115, 1536}},
1631 {576, {1152, 1253, 1728}},
1632 {576, {1152, 1254, 1728}},
1633 {640, {1280, 1393, 1920}},
1634 {640, {1280, 1394, 1920}}
1638 ac3_type_find (GstTypeFind * tf, gpointer unused)
1640 DataScanCtx c = { 0, NULL, 0 };
1642 /* Search for an ac3 frame; not necessarily right at the start, but give it
1643 * a lower probability if not found right at the start. Check that the
1644 * frame is followed by a second frame at the expected offset.
1645 * We could also check the two ac3 CRCs, but we don't do that right now */
1646 while (c.offset < 1024) {
1647 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5)))
1650 if (c.data[0] == 0x0b && c.data[1] == 0x77) {
1651 guint bsid = c.data[5] >> 3;
1655 guint fscod = c.data[4] >> 6;
1656 guint frmsizecod = c.data[4] & 0x3f;
1658 if (fscod < 3 && frmsizecod < 38) {
1659 DataScanCtx c_next = c;
1662 frame_size = ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod];
1663 GST_LOG ("possible AC3 frame sync at offset %"
1664 G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1665 if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1666 data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1668 if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1669 fscod = c_next.data[4] >> 6;
1670 frmsizecod = c_next.data[4] & 0x3f;
1672 if (fscod < 3 && frmsizecod < 38) {
1673 GstTypeFindProbability prob;
1675 GST_LOG ("found second AC3 frame (size=%u), looks good",
1676 ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]);
1678 prob = GST_TYPE_FIND_MAXIMUM;
1680 prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1682 gst_type_find_suggest (tf, prob, AC3_CAPS);
1686 GST_LOG ("no second AC3 frame found, false sync");
1690 } else if (bsid <= 16 && bsid > 10) {
1692 DataScanCtx c_next = c;
1695 frame_size = (((c.data[2] & 0x07) << 8) + c.data[3]) + 1;
1696 GST_LOG ("possible E-AC3 frame sync at offset %"
1697 G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1698 if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1699 data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1701 if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1702 GstTypeFindProbability prob;
1704 GST_LOG ("found second E-AC3 frame, looks good");
1706 prob = GST_TYPE_FIND_MAXIMUM;
1708 prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1710 gst_type_find_suggest (tf, prob, EAC3_CAPS);
1713 GST_LOG ("no second E-AC3 frame found, false sync");
1717 GST_LOG ("invalid AC3 BSID: %u", bsid);
1720 data_scan_ctx_advance (tf, &c, 1);
1724 /*** audio/x-dts ***/
1725 static GstStaticCaps dts_caps = GST_STATIC_CAPS ("audio/x-dts");
1726 #define DTS_CAPS (gst_static_caps_get (&dts_caps))
1727 #define DTS_MIN_FRAMESIZE 96
1728 #define DTS_MAX_FRAMESIZE 18725 /* 16384*16/14 */
1731 dts_parse_frame_header (DataScanCtx * c, guint * frame_size,
1732 guint * sample_rate, guint * channels, guint * depth, guint * endianness)
1734 static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
1735 22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
1737 static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
1742 guint num_blocks, chans, lfe, i;
1744 marker = GST_READ_UINT32_BE (c->data);
1746 /* raw big endian or 14-bit big endian */
1747 if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
1748 *endianness = G_BIG_ENDIAN;
1749 for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1750 hdr[i] = GST_READ_UINT16_BE (c->data + (i * sizeof (guint16)));
1752 /* raw little endian or 14-bit little endian */
1753 if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
1754 *endianness = G_LITTLE_ENDIAN;
1755 for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1756 hdr[i] = GST_READ_UINT16_LE (c->data + (i * sizeof (guint16)));
1761 GST_LOG ("dts sync marker 0x%08x at offset %u", marker, (guint) c->offset);
1764 if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
1765 if ((hdr[2] & 0xFFF0) != 0x07F0)
1767 /* discard top 2 bits (2 void), shift in 2 */
1768 hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
1769 /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
1770 hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
1771 hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
1772 hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
1773 hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
1774 hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
1775 hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
1776 g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
1782 GST_LOG ("frame header: %04x%04x%04x%04x", hdr[2], hdr[3], hdr[4], hdr[5]);
1784 num_blocks = (hdr[2] >> 2) & 0x7F;
1785 *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
1786 chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
1787 *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
1788 lfe = (hdr[5] >> 9) & 0x03;
1790 if (num_blocks < 5 || *frame_size < 96 || *sample_rate == 0)
1793 if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
1794 *frame_size = (*frame_size * 16) / 14; /* FIXME: round up? */
1796 if (chans < G_N_ELEMENTS (channels_table))
1797 *channels = channels_table[chans] + ((lfe) ? 1 : 0);
1805 dts_type_find (GstTypeFind * tf, gpointer unused)
1807 DataScanCtx c = { 0, NULL, 0 };
1809 /* Search for an dts frame; not necessarily right at the start, but give it
1810 * a lower probability if not found right at the start. Check that the
1811 * frame is followed by a second frame at the expected offset. */
1812 while (c.offset <= DTS_MAX_FRAMESIZE) {
1813 guint frame_size = 0, rate = 0, chans = 0, depth = 0, endianness = 0;
1815 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, DTS_MIN_FRAMESIZE)))
1818 if (G_UNLIKELY (dts_parse_frame_header (&c, &frame_size, &rate, &chans,
1819 &depth, &endianness))) {
1820 GstTypeFindProbability prob;
1823 prob = (c.offset == 0) ? GST_TYPE_FIND_LIKELY : GST_TYPE_FIND_POSSIBLE;
1825 /* check for second frame sync */
1827 data_scan_ctx_advance (tf, &next_c, frame_size);
1828 if (data_scan_ctx_ensure_data (tf, &next_c, 4)) {
1829 GST_LOG ("frame size: %u 0x%04x", frame_size, frame_size);
1830 GST_MEMDUMP ("second frame sync", next_c.data, 4);
1831 if (GST_READ_UINT32_BE (c.data) == GST_READ_UINT32_BE (next_c.data))
1832 prob = GST_TYPE_FIND_MAXIMUM;
1836 gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1837 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
1838 "depth", G_TYPE_INT, depth, "endianness", G_TYPE_INT, endianness,
1839 "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1841 gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1842 "rate", G_TYPE_INT, rate, "depth", G_TYPE_INT, depth,
1843 "endianness", G_TYPE_INT, endianness,
1844 "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1850 data_scan_ctx_advance (tf, &c, 1);
1856 /* can only be detected by using the extension, in which case we use the default
1858 static GstStaticCaps gsm_caps =
1859 GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1");
1861 #define GSM_CAPS (gst_static_caps_get(&gsm_caps))
1865 static GstStaticCaps wavpack_caps =
1866 GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false");
1868 #define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps))
1870 static GstStaticCaps wavpack_correction_caps =
1871 GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");
1873 #define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps))
1876 wavpack_type_find (GstTypeFind * tf, gpointer unused)
1878 GstTypeFindProbability base_prob = GST_TYPE_FIND_POSSIBLE;
1882 guint count_wv, count_wvc;
1884 data = gst_type_find_peek (tf, 0, 32);
1888 if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k')
1891 /* Note: wavpack blocks can be fairly large (easily 60-110k), possibly
1892 * larger than the max. limits imposed by certain typefinding elements
1893 * like id3demux or apedemux, so typefinding is most likely only going to
1894 * work in pull-mode */
1895 blocksize = GST_READ_UINT32_LE (data + 4);
1896 GST_LOG ("wavpack header, blocksize=0x%04x", blocksize);
1900 while (offset < 8 + blocksize) {
1903 /* get chunk header */
1904 GST_LOG ("peeking at chunk at offset 0x%04x", (guint) offset);
1905 data = gst_type_find_peek (tf, offset, 4);
1908 sublen = ((guint32) data[1]) << 1;
1909 if (data[0] & 0x80) {
1910 sublen |= (((guint32) data[2]) << 9) | (((guint32) data[3]) << 17);
1911 sublen += 1 + 3; /* id + length */
1913 sublen += 1 + 1; /* id + length */
1915 if (offset + sublen > 8 + blocksize) {
1916 GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
1917 blocksize - offset);
1920 if ((data[0] & 0x20) == 0) {
1921 switch (data[0] & 0x0f) {
1922 case 0xa: /* ID_WV_BITSTREAM */
1923 case 0xc: /* ID_WVX_BITSTREAM */
1926 case 0xb: /* ID_WVC_BITSTREAM */
1932 if (count_wv >= 5 || count_wvc >= 5)
1938 /* check for second block header */
1939 data = gst_type_find_peek (tf, 8 + blocksize, 4);
1940 if (data != NULL && memcmp (data, "wvpk", 4) == 0) {
1941 GST_DEBUG ("found second block sync");
1942 base_prob = GST_TYPE_FIND_LIKELY;
1945 GST_DEBUG ("wvc=%d, wv=%d", count_wvc, count_wv);
1947 if (count_wvc > 0 && count_wvc > count_wv) {
1948 gst_type_find_suggest (tf,
1949 MIN (base_prob + 5 * count_wvc, GST_TYPE_FIND_NEARLY_CERTAIN),
1950 WAVPACK_CORRECTION_CAPS);
1951 } else if (count_wv > 0) {
1952 gst_type_find_suggest (tf,
1953 MIN (base_prob + 5 * count_wv, GST_TYPE_FIND_NEARLY_CERTAIN),
1958 /*** application/postscrip ***/
1959 static GstStaticCaps postscript_caps =
1960 GST_STATIC_CAPS ("application/postscript");
1962 #define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps))
1965 postscript_type_find (GstTypeFind * tf, gpointer unused)
1967 const guint8 *data = gst_type_find_peek (tf, 0, 3);
1971 if (data[0] == 0x04)
1973 if (data[0] == '%' && data[1] == '!')
1974 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS);
1978 /*** image/svg+xml ***/
1979 static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml");
1981 #define SVG_CAPS (gst_static_caps_get(&svg_caps))
1984 svg_type_find (GstTypeFind * tf, gpointer unused)
1986 static const gchar svg_doctype[] = "!DOCTYPE svg";
1987 static const gchar svg_tag[] = "<svg";
1988 DataScanCtx c = { 0, NULL, 0 };
1990 while (c.offset <= 1024) {
1991 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
1994 if (memcmp (svg_doctype, c.data, 12) == 0) {
1995 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS);
1997 } else if (memcmp (svg_tag, c.data, 4) == 0) {
1998 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS);
2001 data_scan_ctx_advance (tf, &c, 1);
2005 /*** multipart/x-mixed-replace mimestream ***/
2007 static GstStaticCaps multipart_caps =
2008 GST_STATIC_CAPS ("multipart/x-mixed-replace");
2009 #define MULTIPART_CAPS gst_static_caps_get(&multipart_caps)
2011 /* multipart/x-mixed replace is:
2012 * <maybe some whitespace>--<some ascii chars>[\r]\n
2013 * <more ascii chars>[\r]\nContent-type:<more ascii>[\r]\n */
2015 multipart_type_find (GstTypeFind * tf, gpointer unused)
2020 #define MULTIPART_MAX_BOUNDARY_OFFSET 16
2021 data = gst_type_find_peek (tf, 0, MULTIPART_MAX_BOUNDARY_OFFSET);
2026 x - data < MULTIPART_MAX_BOUNDARY_OFFSET - 2 && g_ascii_isspace (*x);
2028 if (x[0] != '-' || x[1] != '-')
2031 /* Could be okay, peek what should be enough for a complete header */
2032 #define MULTIPART_MAX_HEADER_SIZE 256
2033 data = gst_type_find_peek (tf, 0, MULTIPART_MAX_HEADER_SIZE);
2037 for (x = data; x - data < MULTIPART_MAX_HEADER_SIZE - 14; x++) {
2038 if (!isascii (*x)) {
2042 !g_ascii_strncasecmp ("content-type:", (gchar *) x + 1, 13)) {
2043 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MULTIPART_CAPS);
2049 /*** video/mpeg systemstream ***/
2050 static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, "
2051 "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]");
2053 #define MPEG_SYS_CAPS gst_static_caps_get(&mpeg_sys_caps)
2054 #define IS_MPEG_HEADER(data) (G_UNLIKELY((((guint8 *)(data))[0] == 0x00) && \
2055 (((guint8 *)(data))[1] == 0x00) && \
2056 (((guint8 *)(data))[2] == 0x01)))
2058 #define IS_MPEG_PACK_CODE(b) ((b) == 0xBA)
2059 #define IS_MPEG_SYS_CODE(b) ((b) == 0xBB)
2060 #define IS_MPEG_PACK_HEADER(data) (IS_MPEG_HEADER (data) && \
2061 IS_MPEG_PACK_CODE (((guint8 *)(data))[3]))
2063 #define IS_MPEG_PES_CODE(b) (((b) & 0xF0) == 0xE0 || ((b) & 0xF0) == 0xC0 || \
2065 #define IS_MPEG_PES_HEADER(data) (IS_MPEG_HEADER (data) && \
2066 IS_MPEG_PES_CODE (((guint8 *)(data))[3]))
2068 #define MPEG2_MAX_PROBE_LENGTH (128 * 1024) /* 128kB should be 64 packs of the
2069 * most common 2kB pack size. */
2071 #define MPEG2_MIN_SYS_HEADERS 2
2072 #define MPEG2_MAX_SYS_HEADERS 5
2075 mpeg_sys_is_valid_pack (GstTypeFind * tf, const guint8 * data, guint len,
2078 /* Check the pack header @ offset for validity, assuming that the 4 byte header
2079 * itself has already been checked. */
2085 /* Check marker bits */
2086 if ((data[4] & 0xC4) == 0x44) {
2091 if ((data[6] & 0x04) != 0x04 ||
2092 (data[8] & 0x04) != 0x04 ||
2093 (data[9] & 0x01) != 0x01 || (data[12] & 0x03) != 0x03)
2096 stuff_len = data[13] & 0x07;
2098 /* Check the following header bytes, if we can */
2099 if ((14 + stuff_len + 4) <= len) {
2100 if (!IS_MPEG_HEADER (data + 14 + stuff_len))
2104 *pack_size = 14 + stuff_len;
2106 } else if ((data[4] & 0xF1) == 0x21) {
2108 if ((data[6] & 0x01) != 0x01 ||
2109 (data[8] & 0x01) != 0x01 ||
2110 (data[9] & 0x80) != 0x80 || (data[11] & 0x01) != 0x01)
2113 /* Check the following header bytes, if we can */
2114 if ((12 + 4) <= len) {
2115 if (!IS_MPEG_HEADER (data + 12))
2127 mpeg_sys_is_valid_pes (GstTypeFind * tf, const guint8 * data, guint len,
2130 guint pes_packet_len;
2132 /* Check the PES header at the given position, assuming the header code itself
2133 * was already checked */
2137 /* For MPEG Program streams, unbounded PES is not allowed, so we must have a
2138 * valid length present */
2139 pes_packet_len = GST_READ_UINT16_BE (data + 4);
2140 if (pes_packet_len == 0)
2143 /* Check the following header, if we can */
2144 if (6 + pes_packet_len + 4 <= len) {
2145 if (!IS_MPEG_HEADER (data + 6 + pes_packet_len))
2150 *pack_size = 6 + pes_packet_len;
2155 mpeg_sys_is_valid_sys (GstTypeFind * tf, const guint8 * data, guint len,
2160 /* Check the System header at the given position, assuming the header code itself
2161 * was already checked */
2164 sys_hdr_len = GST_READ_UINT16_BE (data + 4);
2165 if (sys_hdr_len < 6)
2168 /* Check the following header, if we can */
2169 if (6 + sys_hdr_len + 4 <= len) {
2170 if (!IS_MPEG_HEADER (data + 6 + sys_hdr_len))
2175 *pack_size = 6 + sys_hdr_len;
2180 /* calculation of possibility to identify random data as mpeg systemstream:
2181 * bits that must match in header detection: 32 (or more)
2182 * chance that random data is identifed: 1/2^32
2183 * chance that MPEG2_MIN_PACK_HEADERS headers are identified:
2184 * 1/2^(32*MPEG2_MIN_PACK_HEADERS)
2185 * chance that this happens in MPEG2_MAX_PROBE_LENGTH bytes:
2186 * 1-(1+1/2^(32*MPEG2_MIN_PACK_HEADERS)^MPEG2_MAX_PROBE_LENGTH)
2187 * for current values:
2188 * 1-(1+1/2^(32*4)^101024)
2190 * Since we also check marker bits and pes packet lengths, this probability is a
2191 * very coarse upper bound.
2194 mpeg_sys_type_find (GstTypeFind * tf, gpointer unused)
2196 const guint8 *data, *data0, *first_sync, *end;
2197 gint mpegversion = 0;
2198 guint pack_headers = 0;
2199 guint pes_headers = 0;
2201 guint since_last_sync = 0;
2202 guint32 sync_word = 0xffffffff;
2203 guint potential_headers = 0;
2208 len = MPEG2_MAX_PROBE_LENGTH;
2211 data = gst_type_find_peek (tf, 0, 5 + len);
2227 while (data < end) {
2229 if (sync_word == 0x00000100) {
2230 /* Found potential sync word */
2231 if (first_sync == NULL)
2232 first_sync = data - 3;
2234 if (since_last_sync > 4) {
2235 /* If more than 4 bytes since the last sync word, reset our counters,
2236 * as we're only interested in counting contiguous packets */
2237 pes_headers = pack_headers = 0;
2241 potential_headers++;
2242 if (IS_MPEG_PACK_CODE (data[0])) {
2243 if ((data[1] & 0xC0) == 0x40) {
2246 } else if ((data[1] & 0xF0) == 0x20) {
2249 if (mpegversion != 0 &&
2250 mpeg_sys_is_valid_pack (tf, data - 3, end - data + 3, &pack_size)) {
2253 } else if (IS_MPEG_PES_CODE (data[0])) {
2255 if (mpeg_sys_is_valid_pes (tf, data - 3, end - data + 3, &pack_size)) {
2257 if (mpegversion == 0)
2260 } else if (IS_MPEG_SYS_CODE (data[0])) {
2261 if (mpeg_sys_is_valid_sys (tf, data - 3, end - data + 3, &pack_size)) {
2266 /* If we found a packet with a known size, skip the bytes in it and loop
2267 * around to check the next packet. */
2268 if (pack_size != 0) {
2269 data += pack_size - 3;
2270 sync_word = 0xffffffff;
2271 since_last_sync = 0;
2276 sync_word |= data[0];
2280 /* If we have found MAX headers, and *some* were pes headers (pack headers
2281 * are optional in an mpeg system stream) then return our high-probability
2283 if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MAX_SYS_HEADERS)
2287 /* If we at least saw MIN headers, and *some* were pes headers (pack headers
2288 * are optional in an mpeg system stream) then return a lower-probability
2290 if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MIN_SYS_HEADERS)
2298 prob = GST_TYPE_FIND_POSSIBLE + (10 * (pack_headers + pes_headers));
2299 prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2301 /* With the above test, we get into problems when we try to typefind
2302 a MPEG stream from a small amount of data, which can happen when
2303 we get data pushed from a HTTP source. We thus make a second test
2304 to give higher probability if all the potential headers were either
2305 pack or pes headers (ie, no potential header was unrecognized). */
2306 if (potential_headers == pack_headers + pes_headers) {
2307 GST_LOG ("Only %u headers, but all were recognized", potential_headers);
2309 prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2312 /* lower probability if the first packet wasn't right at the start */
2313 if (data0 != first_sync && prob >= 10)
2316 GST_LOG ("Suggesting MPEG %d system stream, %d packs, %d pes, prob %u%%\n",
2317 mpegversion, pack_headers, pes_headers, prob);
2319 gst_type_find_suggest_simple (tf, prob, "video/mpeg",
2320 "systemstream", G_TYPE_BOOLEAN, TRUE,
2321 "mpegversion", G_TYPE_INT, mpegversion, NULL);
2325 /*** video/mpegts Transport Stream ***/
2326 static GstStaticCaps mpegts_caps = GST_STATIC_CAPS ("video/mpegts, "
2327 "systemstream = (boolean) true, packetsize = (int) [ 188, 208 ]");
2328 #define MPEGTS_CAPS gst_static_caps_get(&mpegts_caps)
2330 #define GST_MPEGTS_TYPEFIND_MIN_HEADERS 4
2331 #define GST_MPEGTS_TYPEFIND_MAX_HEADERS 10
2332 #define GST_MPEGTS_MAX_PACKET_SIZE 208
2333 #define GST_MPEGTS_TYPEFIND_SYNC_SIZE \
2334 (GST_MPEGTS_TYPEFIND_MIN_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2335 #define GST_MPEGTS_TYPEFIND_MAX_SYNC \
2336 (GST_MPEGTS_TYPEFIND_MAX_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2337 #define GST_MPEGTS_TYPEFIND_SCAN_LENGTH \
2338 (GST_MPEGTS_TYPEFIND_MAX_SYNC * 4)
2340 #define MPEGTS_HDR_SIZE 4
2341 /* Check for sync byte, error_indicator == 0 and packet has payload */
2342 #define IS_MPEGTS_HEADER(data) (((data)[0] == 0x47) && \
2343 (((data)[1] & 0x80) == 0x00) && \
2344 (((data)[3] & 0x30) != 0x00))
2346 /* Helper function to search ahead at intervals of packet_size for mpegts
2349 mpeg_ts_probe_headers (GstTypeFind * tf, guint64 offset, gint packet_size)
2351 /* We always enter this function having found at least one header already */
2353 const guint8 *data = NULL;
2355 GST_LOG ("looking for mpeg-ts packets of size %u", packet_size);
2356 while (found < GST_MPEGTS_TYPEFIND_MAX_HEADERS) {
2357 offset += packet_size;
2359 data = gst_type_find_peek (tf, offset, MPEGTS_HDR_SIZE);
2360 if (data == NULL || !IS_MPEGTS_HEADER (data))
2364 GST_LOG ("mpeg-ts sync #%2d at offset %" G_GUINT64_FORMAT, found, offset);
2370 /* Try and detect at least 4 packets in at most 10 packets worth of
2371 * data. Need to try several possible packet sizes */
2373 mpeg_ts_type_find (GstTypeFind * tf, gpointer unused)
2375 /* TS packet sizes to test: normal, DVHS packet size and
2376 * FEC with 16 or 20 byte codes packet size. */
2377 const gint pack_sizes[] = { 188, 192, 204, 208 };
2378 const guint8 *data = NULL;
2380 guint64 skipped = 0;
2382 while (skipped < GST_MPEGTS_TYPEFIND_SCAN_LENGTH) {
2383 if (size < MPEGTS_HDR_SIZE) {
2384 data = gst_type_find_peek (tf, skipped, GST_MPEGTS_TYPEFIND_SYNC_SIZE);
2387 size = GST_MPEGTS_TYPEFIND_SYNC_SIZE;
2390 /* Have at least MPEGTS_HDR_SIZE bytes at this point */
2391 if (IS_MPEGTS_HEADER (data)) {
2394 GST_LOG ("possible mpeg-ts sync at offset %" G_GUINT64_FORMAT, skipped);
2396 for (p = 0; p < G_N_ELEMENTS (pack_sizes); p++) {
2399 /* Probe ahead at size pack_sizes[p] */
2400 found = mpeg_ts_probe_headers (tf, skipped, pack_sizes[p]);
2401 if (found >= GST_MPEGTS_TYPEFIND_MIN_HEADERS) {
2404 /* found at least 4 headers. 10 headers = MAXIMUM probability.
2405 * Arbitrarily, I assigned 10% probability for each header we
2406 * found, 40% -> 100% */
2407 probability = MIN (10 * found, GST_TYPE_FIND_MAXIMUM);
2409 gst_type_find_suggest_simple (tf, probability, "video/mpegts",
2410 "systemstream", G_TYPE_BOOLEAN, TRUE,
2411 "packetsize", G_TYPE_INT, pack_sizes[p], NULL);
2422 #define GST_MPEGVID_TYPEFIND_TRY_PICTURES 6
2423 #define GST_MPEGVID_TYPEFIND_TRY_SYNC (100 * 1024) /* 100 kB */
2425 /* Scan ahead a maximum of max_extra_offset bytes until the next IS_MPEG_HEADER
2426 * offset. After the call, offset will be after the 0x000001, i.e. at the 4th
2427 * byte of the MPEG header. Returns TRUE if a header was found, FALSE if not.
2430 mpeg_find_next_header (GstTypeFind * tf, DataScanCtx * c,
2431 guint64 max_extra_offset)
2433 guint64 extra_offset;
2435 for (extra_offset = 0; extra_offset <= max_extra_offset; ++extra_offset) {
2436 if (!data_scan_ctx_ensure_data (tf, c, 4))
2438 if (IS_MPEG_HEADER (c->data)) {
2439 data_scan_ctx_advance (tf, c, 3);
2442 data_scan_ctx_advance (tf, c, 1);
2447 /*** video/mpeg MPEG-4 elementary video stream ***/
2449 static GstStaticCaps mpeg4_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2450 "systemstream=(boolean)false, mpegversion=4, parsed=(boolean)false");
2451 #define MPEG4_VIDEO_CAPS gst_static_caps_get(&mpeg4_video_caps)
2454 * This typefind is based on the elementary video header defined in
2455 * http://xhelmboyx.tripod.com/formats/mpeg-layout.txt
2456 * In addition, it allows the visual object sequence header to be
2457 * absent, and even the VOS header to be absent. In the latter case,
2458 * a number of VOPs have to be present.
2461 mpeg4_video_type_find (GstTypeFind * tf, gpointer unused)
2463 DataScanCtx c = { 0, NULL, 0 };
2464 gboolean seen_vios_at_0 = FALSE;
2465 gboolean seen_vios = FALSE;
2466 gboolean seen_vos = FALSE;
2467 gboolean seen_vol = FALSE;
2468 guint num_vop_headers = 0;
2471 while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2472 if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2475 if (!mpeg_find_next_header (tf, &c,
2476 GST_MPEGVID_TYPEFIND_TRY_SYNC - c.offset))
2481 /* visual_object_sequence_start_code */
2484 break; /* Terminate at second vios */
2486 seen_vios_at_0 = TRUE;
2488 data_scan_ctx_advance (tf, &c, 2);
2489 if (!mpeg_find_next_header (tf, &c, 0))
2494 /* Optional metadata */
2496 if (!mpeg_find_next_header (tf, &c, 24))
2500 /* visual_object_start_code (consider it optional) */
2502 data_scan_ctx_advance (tf, &c, 2);
2503 /* may contain ID marker and YUV clamping */
2504 if (!mpeg_find_next_header (tf, &c, 7))
2510 /* video_object_start_code */
2513 break; /* Terminate at second vos */
2515 data_scan_ctx_advance (tf, &c, 2);
2519 /* video_object_layer_start_code */
2520 if (sc >= 0x20 && sc <= 0x2F) {
2522 data_scan_ctx_advance (tf, &c, 5);
2526 /* video_object_plane_start_code */
2529 data_scan_ctx_advance (tf, &c, 2);
2533 /* Unknown start code. */
2536 if (num_vop_headers > 0 || seen_vol) {
2537 GstTypeFindProbability probability = 0;
2539 GST_LOG ("Found %d pictures, vios: %d, vos:%d, vol:%d", num_vop_headers,
2540 seen_vios, seen_vos, seen_vol);
2542 if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios_at_0
2543 && seen_vos && seen_vol)
2544 probability = GST_TYPE_FIND_MAXIMUM - 1;
2545 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios
2546 && seen_vos && seen_vol)
2547 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2548 else if (seen_vios_at_0 && seen_vos && seen_vol)
2549 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2550 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vos
2552 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2553 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vol)
2554 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2555 else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2556 probability = GST_TYPE_FIND_LIKELY - 1;
2557 else if (num_vop_headers > 2 && seen_vios && seen_vos && seen_vol)
2558 probability = GST_TYPE_FIND_LIKELY - 9;
2559 else if (seen_vios && seen_vos && seen_vol)
2560 probability = GST_TYPE_FIND_LIKELY - 20;
2561 else if (num_vop_headers > 0 && seen_vos && seen_vol)
2562 probability = GST_TYPE_FIND_POSSIBLE;
2563 else if (num_vop_headers > 0)
2564 probability = GST_TYPE_FIND_POSSIBLE - 10;
2565 else if (seen_vos && seen_vol)
2566 probability = GST_TYPE_FIND_POSSIBLE - 20;
2568 gst_type_find_suggest (tf, probability, MPEG4_VIDEO_CAPS);
2572 /*** video/x-h263 H263 video stream ***/
2573 static GstStaticCaps h263_video_caps =
2574 GST_STATIC_CAPS ("video/x-h263, variant=(string)itu");
2576 #define H263_VIDEO_CAPS gst_static_caps_get(&h263_video_caps)
2578 #define H263_MAX_PROBE_LENGTH (128 * 1024)
2581 h263_video_type_find (GstTypeFind * tf, gpointer unused)
2583 DataScanCtx c = { 0, NULL, 0 };
2591 while (c.offset < H263_MAX_PROBE_LENGTH) {
2592 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2595 /* Find the picture start code */
2596 data = (data << 8) + c.data[0];
2597 psc = data & G_GUINT64_CONSTANT (0xfffffc0000);
2598 if (psc == 0x800000) {
2601 tr = (data & 0x3fc) >> 2;
2605 /* Now that we have a Valid PSC, check if we also have a valid PTYPE and
2606 the Source Format, which should range between 1 and 5 */
2607 if (((tr >> 6) == 0x2) && (format > 0 && format < 6))
2612 /* FIXME: maybe bail out early if we get mostly bad syncs ? */
2615 data_scan_ctx_advance (tf, &c, 1);
2618 GST_LOG ("good: %d, bad: %d", good, bad);
2621 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H263_VIDEO_CAPS);
2626 /*** video/x-h264 H264 elementary video stream ***/
2628 static GstStaticCaps h264_video_caps =
2629 GST_STATIC_CAPS ("video/x-h264,stream-format=byte-stream");
2631 #define H264_VIDEO_CAPS gst_static_caps_get(&h264_video_caps)
2633 #define H264_MAX_PROBE_LENGTH (128 * 1024) /* 128kB for HD should be enough. */
2636 h264_video_type_find (GstTypeFind * tf, gpointer unused)
2638 DataScanCtx c = { 0, NULL, 0 };
2640 /* Stream consists of: a series of sync codes (00 00 00 01) followed
2643 gboolean seen_idr = FALSE;
2644 gboolean seen_sps = FALSE;
2645 gboolean seen_pps = FALSE;
2646 gboolean seen_ssps = FALSE;
2651 while (c.offset < H264_MAX_PROBE_LENGTH) {
2652 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2655 if (IS_MPEG_HEADER (c.data)) {
2656 nut = c.data[3] & 0x9f; /* forbiden_zero_bit | nal_unit_type */
2657 ref = c.data[3] & 0x60; /* nal_ref_idc */
2659 /* if forbidden bit is different to 0 won't be h264 */
2665 /* collect statistics about the NAL types */
2666 if ((nut >= 1 && nut <= 13) || nut == 19) {
2667 if ((nut == 5 && ref == 0) ||
2668 ((nut == 6 || (nut >= 9 && nut <= 12)) && ref != 0)) {
2680 } else if (nut >= 14 && nut <= 33) {
2684 } else if (nut == 14 || nut == 20) {
2685 /* Sometimes we see NAL 14 or 20 without SSPS
2686 * if dropped into the middle of a stream -
2687 * just ignore those (don't add to bad count) */
2692 /* Theoretically these are good, since if they exist in the
2693 stream it merely means that a newer backwards-compatible
2694 h.264 stream. But we should be identifying that separately. */
2698 /* unspecified, application specific */
2699 /* don't consider these bad */
2702 GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, idr:%d ssps:%d", good, bad,
2703 seen_pps, seen_sps, seen_idr, seen_ssps);
2705 if (seen_sps && seen_pps && seen_idr && good >= 10 && bad < 4) {
2706 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H264_VIDEO_CAPS);
2710 data_scan_ctx_advance (tf, &c, 4);
2712 data_scan_ctx_advance (tf, &c, 1);
2715 GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, idr:%d ssps=%d", good, bad,
2716 seen_pps, seen_sps, seen_idr, seen_ssps);
2718 if (good >= 2 && bad == 0) {
2719 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H264_VIDEO_CAPS);
2723 /*** video/x-h265 H265 elementary video stream ***/
2725 static GstStaticCaps h265_video_caps =
2726 GST_STATIC_CAPS ("video/x-h265,stream-format=byte-stream");
2728 #define H265_VIDEO_CAPS gst_static_caps_get(&h265_video_caps)
2730 #define H265_MAX_PROBE_LENGTH (128 * 1024) /* 128kB for HD should be enough. */
2733 h265_video_type_find (GstTypeFind * tf, gpointer unused)
2735 DataScanCtx c = { 0, NULL, 0 };
2737 /* Stream consists of: a series of sync codes (00 00 00 01) followed
2740 gboolean seen_irap = FALSE;
2741 gboolean seen_vps = FALSE;
2742 gboolean seen_sps = FALSE;
2743 gboolean seen_pps = FALSE;
2748 while (c.offset < H265_MAX_PROBE_LENGTH) {
2749 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5)))
2752 if (IS_MPEG_HEADER (c.data)) {
2753 /* forbiden_zero_bit | nal_unit_type */
2754 nut = c.data[3] & 0xfe;
2756 /* if forbidden bit is different to 0 won't be h265 */
2763 /* if nuh_layer_id is not zero or nuh_temporal_id_plus1 is zero then
2764 * it won't be h265 */
2765 if ((c.data[3] & 0x01) || (c.data[4] & 0xf8) || !(c.data[4] & 0x07)) {
2770 /* collect statistics about the NAL types */
2771 if ((nut >= 0 && nut <= 9) || (nut >= 16 && nut <= 21) || (nut >= 32
2779 else if (nut >= 16 && nut <= 21) {
2780 /* BLA, IDR and CRA pictures are belongs to be IRAP picture */
2781 /* we are not counting the reserved IRAP pictures (22 and 23) to good */
2786 } else if ((nut >= 10 && nut <= 15) || (nut >= 22 && nut <= 31)
2787 || (nut >= 41 && nut <= 47)) {
2788 /* reserved values are counting as bad */
2791 /* unspecified (48..63), application specific */
2792 /* don't consider these as bad */
2795 GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, vps:%d, irap:%d", good, bad,
2796 seen_pps, seen_sps, seen_vps, seen_irap);
2798 if (seen_sps && seen_pps && seen_irap && good >= 10 && bad < 4) {
2799 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H265_VIDEO_CAPS);
2803 data_scan_ctx_advance (tf, &c, 5);
2805 data_scan_ctx_advance (tf, &c, 1);
2808 GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, vps:%d, irap:%d", good, bad,
2809 seen_pps, seen_sps, seen_vps, seen_irap);
2811 if (good >= 2 && bad == 0) {
2812 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H265_VIDEO_CAPS);
2816 /*** video/mpeg video stream ***/
2818 static GstStaticCaps mpeg_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2819 "systemstream = (boolean) false");
2820 #define MPEG_VIDEO_CAPS gst_static_caps_get(&mpeg_video_caps)
2823 * Idea is the same as MPEG system stream typefinding: We check each
2824 * byte of the stream to see if - from that point on - the stream
2825 * matches a predefined set of marker bits as defined in the MPEG
2828 * I'm sure someone will do a chance calculation here too.
2832 mpeg_video_stream_type_find (GstTypeFind * tf, gpointer unused)
2834 DataScanCtx c = { 0, NULL, 0 };
2835 gboolean seen_seq_at_0 = FALSE;
2836 gboolean seen_seq = FALSE;
2837 gboolean seen_gop = FALSE;
2838 guint64 last_pic_offset = 0;
2839 guint num_pic_headers = 0;
2842 while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2843 if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2846 if (!data_scan_ctx_ensure_data (tf, &c, 5))
2849 if (!IS_MPEG_HEADER (c.data))
2852 /* a pack header indicates that this isn't an elementary stream */
2853 if (c.data[3] == 0xBA && mpeg_sys_is_valid_pack (tf, c.data, c.size, NULL))
2856 /* do we have a sequence header? */
2857 if (c.data[3] == 0xB3) {
2858 seen_seq_at_0 = seen_seq_at_0 || (c.offset == 0);
2860 data_scan_ctx_advance (tf, &c, 4 + 8);
2864 /* or a GOP header */
2865 if (c.data[3] == 0xB8) {
2867 data_scan_ctx_advance (tf, &c, 8);
2871 /* but what we'd really like to see is a picture header */
2872 if (c.data[3] == 0x00) {
2874 last_pic_offset = c.offset;
2875 data_scan_ctx_advance (tf, &c, 8);
2879 /* ... each followed by a slice header with slice_vertical_pos=1 that's
2880 * not too far away from the previously seen picture header. */
2881 if (c.data[3] == 0x01 && num_pic_headers > found &&
2882 (c.offset - last_pic_offset) >= 4 &&
2883 (c.offset - last_pic_offset) <= 64) {
2884 data_scan_ctx_advance (tf, &c, 4);
2891 data_scan_ctx_advance (tf, &c, 1);
2894 if (found > 0 || seen_seq) {
2895 GstTypeFindProbability probability = 0;
2897 GST_LOG ("Found %d pictures, seq:%d, gop:%d", found, seen_seq, seen_gop);
2899 if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq && seen_gop)
2900 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2901 else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq)
2902 probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2903 else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2904 probability = GST_TYPE_FIND_LIKELY;
2905 else if (seen_seq_at_0 && seen_gop && found > 2)
2906 probability = GST_TYPE_FIND_LIKELY - 10;
2907 else if (seen_seq && seen_gop && found > 2)
2908 probability = GST_TYPE_FIND_LIKELY - 20;
2909 else if (seen_seq_at_0 && found > 0)
2910 probability = GST_TYPE_FIND_POSSIBLE;
2911 else if (seen_seq && found > 0)
2912 probability = GST_TYPE_FIND_POSSIBLE - 5;
2914 probability = GST_TYPE_FIND_POSSIBLE - 10;
2916 probability = GST_TYPE_FIND_POSSIBLE - 20;
2918 gst_type_find_suggest_simple (tf, probability, "video/mpeg",
2919 "systemstream", G_TYPE_BOOLEAN, FALSE,
2920 "mpegversion", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
2924 /*** audio/x-aiff ***/
2926 static GstStaticCaps aiff_caps = GST_STATIC_CAPS ("audio/x-aiff");
2928 #define AIFF_CAPS gst_static_caps_get(&aiff_caps)
2930 aiff_type_find (GstTypeFind * tf, gpointer unused)
2932 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2934 if (data && memcmp (data, "FORM", 4) == 0) {
2936 if (memcmp (data, "AIFF", 4) == 0 || memcmp (data, "AIFC", 4) == 0)
2937 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AIFF_CAPS);
2941 /*** audio/x-svx ***/
2943 static GstStaticCaps svx_caps = GST_STATIC_CAPS ("audio/x-svx");
2945 #define SVX_CAPS gst_static_caps_get(&svx_caps)
2947 svx_type_find (GstTypeFind * tf, gpointer unused)
2949 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2951 if (data && memcmp (data, "FORM", 4) == 0) {
2953 if (memcmp (data, "8SVX", 4) == 0 || memcmp (data, "16SV", 4) == 0)
2954 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVX_CAPS);
2958 /*** audio/x-shorten ***/
2960 static GstStaticCaps shn_caps = GST_STATIC_CAPS ("audio/x-shorten");
2962 #define SHN_CAPS gst_static_caps_get(&shn_caps)
2964 shn_type_find (GstTypeFind * tf, gpointer unused)
2966 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2968 if (data && memcmp (data, "ajkg", 4) == 0) {
2969 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2971 data = gst_type_find_peek (tf, -8, 8);
2972 if (data && memcmp (data, "SHNAMPSK", 8) == 0) {
2973 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2977 /*** application/x-ape ***/
2979 static GstStaticCaps ape_caps = GST_STATIC_CAPS ("application/x-ape");
2981 #define APE_CAPS gst_static_caps_get(&ape_caps)
2983 ape_type_find (GstTypeFind * tf, gpointer unused)
2985 const guint8 *data = gst_type_find_peek (tf, 0, 4);
2987 if (data && memcmp (data, "MAC ", 4) == 0) {
2988 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, APE_CAPS);
2992 /*** ISO FORMATS ***/
2994 /*** audio/x-m4a ***/
2996 static GstStaticCaps m4a_caps = GST_STATIC_CAPS ("audio/x-m4a");
2998 #define M4A_CAPS (gst_static_caps_get(&m4a_caps))
3000 m4a_type_find (GstTypeFind * tf, gpointer unused)
3002 const guint8 *data = gst_type_find_peek (tf, 4, 8);
3004 if (data && (memcmp (data, "ftypM4A ", 8) == 0)) {
3005 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, M4A_CAPS);
3009 /*** application/x-3gp ***/
3011 /* The Q is there because variables can't start with a number. */
3012 static GstStaticCaps q3gp_caps = GST_STATIC_CAPS ("application/x-3gp");
3013 #define Q3GP_CAPS (gst_static_caps_get(&q3gp_caps))
3015 static const gchar *
3016 q3gp_type_find_get_profile (const guint8 * data)
3018 switch (GST_MAKE_FOURCC (data[0], data[1], data[2], 0)) {
3019 case GST_MAKE_FOURCC ('3', 'g', 'g', 0):
3021 case GST_MAKE_FOURCC ('3', 'g', 'p', 0):
3023 case GST_MAKE_FOURCC ('3', 'g', 's', 0):
3024 return "streaming-server";
3025 case GST_MAKE_FOURCC ('3', 'g', 'r', 0):
3026 return "progressive-download";
3034 q3gp_type_find (GstTypeFind * tf, gpointer unused)
3036 const gchar *profile;
3037 guint32 ftyp_size = 0;
3039 const guint8 *data = NULL;
3041 if ((data = gst_type_find_peek (tf, 0, 12)) == NULL) {
3046 if (memcmp (data, "ftyp", 4) != 0) {
3050 /* check major brand */
3052 if ((profile = q3gp_type_find_get_profile (data))) {
3053 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3054 "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
3058 /* check compatible brands */
3059 if ((data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3060 ftyp_size = GST_READ_UINT32_BE (data);
3062 for (offset = 16; offset < ftyp_size; offset += 4) {
3063 if ((data = gst_type_find_peek (tf, offset, 3)) == NULL) {
3066 if ((profile = q3gp_type_find_get_profile (data))) {
3067 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3068 "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
3077 /*** video/mj2 and image/jp2 ***/
3078 static GstStaticCaps mj2_caps = GST_STATIC_CAPS ("video/mj2");
3080 #define MJ2_CAPS gst_static_caps_get(&mj2_caps)
3082 static GstStaticCaps jp2_caps = GST_STATIC_CAPS ("image/jp2");
3084 #define JP2_CAPS gst_static_caps_get(&jp2_caps)
3087 jp2_type_find (GstTypeFind * tf, gpointer unused)
3091 data = gst_type_find_peek (tf, 0, 24);
3096 if (memcmp (data, "\000\000\000\014jP \015\012\207\012", 12) != 0)
3099 /* check ftyp box */
3101 if (memcmp (data + 4, "ftyp", 4) == 0) {
3102 if (memcmp (data + 8, "jp2 ", 4) == 0)
3103 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, JP2_CAPS);
3104 else if (memcmp (data + 8, "mjp2", 4) == 0)
3105 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MJ2_CAPS);
3109 /*** video/quicktime ***/
3111 static GstStaticCaps qt_caps = GST_STATIC_CAPS ("video/quicktime");
3113 #define QT_CAPS gst_static_caps_get(&qt_caps)
3114 #define STRNCMP(x,y,z) (strncmp ((char*)(x), (char*)(y), z))
3116 /* FIXME 0.11: go through http://www.ftyps.com/ */
3118 qt_type_find (GstTypeFind * tf, gpointer unused)
3124 const gchar *variant = NULL;
3126 while ((data = gst_type_find_peek (tf, offset, 12)) != NULL) {
3129 if (STRNCMP (&data[4], "ftypqt ", 8) == 0) {
3130 tip = GST_TYPE_FIND_MAXIMUM;
3134 if (STRNCMP (&data[4], "ftypisom", 8) == 0 ||
3135 STRNCMP (&data[4], "ftypavc1", 8) == 0 ||
3136 STRNCMP (&data[4], "ftypmp42", 8) == 0) {
3137 tip = GST_TYPE_FIND_MAXIMUM;
3142 if (STRNCMP (&data[4], "ftypisml", 8) == 0 ||
3143 STRNCMP (&data[4], "ftypavc3", 8) == 0) {
3144 tip = GST_TYPE_FIND_MAXIMUM;
3145 variant = "iso-fragmented";
3149 if (STRNCMP (&data[4], "ftypccff", 8) == 0) {
3150 tip = GST_TYPE_FIND_MAXIMUM;
3155 /* box/atom types that are in common with ISO base media file format */
3156 if (STRNCMP (&data[4], "moov", 4) == 0 ||
3157 STRNCMP (&data[4], "mdat", 4) == 0 ||
3158 STRNCMP (&data[4], "ftyp", 4) == 0 ||
3159 STRNCMP (&data[4], "free", 4) == 0 ||
3160 STRNCMP (&data[4], "uuid", 4) == 0 ||
3161 STRNCMP (&data[4], "moof", 4) == 0 ||
3162 STRNCMP (&data[4], "skip", 4) == 0) {
3164 tip = GST_TYPE_FIND_LIKELY;
3166 tip = GST_TYPE_FIND_NEARLY_CERTAIN;
3169 /* other box/atom types, apparently quicktime specific */
3170 else if (STRNCMP (&data[4], "pnot", 4) == 0 ||
3171 STRNCMP (&data[4], "PICT", 4) == 0 ||
3172 STRNCMP (&data[4], "wide", 4) == 0 ||
3173 STRNCMP (&data[4], "prfl", 4) == 0) {
3174 tip = GST_TYPE_FIND_MAXIMUM;
3181 size = GST_READ_UINT32_BE (data);
3182 /* check compatible brands rather than ever expaning major brands above */
3183 if ((STRNCMP (&data[4], "ftyp", 4) == 0) && (size >= 16)) {
3184 new_offset = offset + 12;
3185 while (new_offset + 4 <= offset + size) {
3186 data = gst_type_find_peek (tf, new_offset, 4);
3189 if (STRNCMP (&data[4], "isom", 4) == 0 ||
3190 STRNCMP (&data[4], "dash", 4) == 0 ||
3191 STRNCMP (&data[4], "avc1", 4) == 0 ||
3192 STRNCMP (&data[4], "avc3", 4) == 0 ||
3193 STRNCMP (&data[4], "mp41", 4) == 0 ||
3194 STRNCMP (&data[4], "mp42", 4) == 0) {
3195 tip = GST_TYPE_FIND_MAXIMUM;
3203 const guint8 *sizedata;
3205 sizedata = gst_type_find_peek (tf, offset + 8, 8);
3206 if (sizedata == NULL)
3209 size = GST_READ_UINT64_BE (sizedata);
3214 new_offset = offset + size;
3215 if (new_offset <= offset)
3217 offset = new_offset;
3223 GstCaps *caps = gst_caps_copy (QT_CAPS);
3225 gst_caps_set_simple (caps, "variant", G_TYPE_STRING, variant, NULL);
3226 gst_type_find_suggest (tf, tip, caps);
3227 gst_caps_unref (caps);
3229 gst_type_find_suggest (tf, tip, QT_CAPS);
3235 /*** image/x-quicktime ***/
3237 static GstStaticCaps qtif_caps = GST_STATIC_CAPS ("image/x-quicktime");
3239 #define QTIF_CAPS gst_static_caps_get(&qtif_caps)
3241 /* how many atoms we check before we give up */
3242 #define QTIF_MAXROUNDS 25
3245 qtif_type_find (GstTypeFind * tf, gpointer unused)
3248 gboolean found_idsc = FALSE;
3249 gboolean found_idat = FALSE;
3253 while ((data = gst_type_find_peek (tf, offset, 8)) != NULL) {
3256 size = GST_READ_UINT32_BE (data);
3258 const guint8 *sizedata;
3260 sizedata = gst_type_find_peek (tf, offset + 8, 8);
3261 if (sizedata == NULL)
3264 size = GST_READ_UINT64_BE (sizedata);
3269 if (STRNCMP (data + 4, "idsc", 4) == 0)
3271 if (STRNCMP (data + 4, "idat", 4) == 0)
3274 if (found_idsc && found_idat) {
3275 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, QTIF_CAPS);
3280 if (++rounds > QTIF_MAXROUNDS)
3284 if (found_idsc || found_idat) {
3285 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, QTIF_CAPS);
3290 /*** audio/x-mod ***/
3292 static GstStaticCaps mod_caps = GST_STATIC_CAPS ("audio/x-mod");
3294 #define MOD_CAPS gst_static_caps_get(&mod_caps)
3295 /* FIXME: M15 CheckType to do */
3297 mod_type_find (GstTypeFind * tf, gpointer unused)
3300 GstTypeFindProbability probability;
3301 const char *mod_type = NULL;
3304 if ((data = gst_type_find_peek (tf, 1080, 4)) != NULL) {
3305 /* Protracker and variants */
3306 if ((memcmp (data, "M.K.", 4) == 0) ||
3307 (memcmp (data, "M!K!", 4) == 0) ||
3308 (memcmp (data, "M&K!", 4) == 0) || (memcmp (data, "N.T.", 4) == 0) ||
3310 (memcmp (data, "FLT", 3) == 0 && isdigit (data[3])) ||
3311 (memcmp (data, "EXO", 3) == 0 && isdigit (data[3])) ||
3312 /* Oktalyzer (Amiga) */
3313 (memcmp (data, "OKTA", 4) == 0) || (memcmp (data, "OCTA", 4) == 0) ||
3314 /* Oktalyser (Atari) */
3315 (memcmp (data, "CD81", 4) == 0) ||
3317 (memcmp (data, "TDZ", 3) == 0 && isdigit (data[3])) ||
3319 (memcmp (data + 1, "CHN", 3) == 0 && isdigit (data[0])) ||
3320 /* Fasttracker or Taketracker */
3321 (memcmp (data + 2, "CH", 2) == 0 && isdigit (data[0])
3322 && isdigit (data[1])) || (memcmp (data + 2, "CN", 2) == 0
3323 && isdigit (data[0]) && isdigit (data[1]))) {
3325 probability = GST_TYPE_FIND_MAXIMUM;
3326 goto suggest_audio_mod_caps;
3329 /* J2B (Jazz Jackrabbit 2) */
3330 if ((data = gst_type_find_peek (tf, 0, 8)) != NULL) {
3331 if ((memcmp (data, "MUSE\xDE\xAD", 4) == 0) &&
3332 ((memcmp (data + 6, "\xBE\xEF", 2) == 0) ||
3333 (memcmp (data + 6, "\xBA\xBE", 2) == 0))) {
3335 probability = GST_TYPE_FIND_MAXIMUM;
3336 goto suggest_audio_mod_caps;
3339 /* AMS (Velvet Studio) */
3340 if ((data = gst_type_find_peek (tf, 0, 7)) != NULL) {
3341 if (memcmp (data, "AMShdr\x1A", 7) == 0) {
3342 mod_type = "velvet-ams";
3343 probability = GST_TYPE_FIND_MAXIMUM;
3344 goto suggest_audio_mod_caps;
3347 /* AMS (Extreme Tracker) */
3348 if ((data = gst_type_find_peek (tf, 0, 9)) != NULL) {
3349 if ((memcmp (data, "Extreme", 7) == 0) && (data[8] == 1)) {
3350 mod_type = "extreme-ams";
3351 probability = GST_TYPE_FIND_LIKELY;
3352 goto suggest_audio_mod_caps;
3355 /* ULT (Ultratracker) */
3356 if ((data = gst_type_find_peek (tf, 0, 14)) != NULL) {
3357 if (memcmp (data, "MAS_UTrack_V00", 14) == 0) {
3359 probability = GST_TYPE_FIND_MAXIMUM;
3360 goto suggest_audio_mod_caps;
3363 /* DIGI (DigiBooster) */
3364 if ((data = gst_type_find_peek (tf, 0, 20)) != NULL) {
3365 if (memcmp (data, "DIGI Booster module\0", 20) == 0) {
3367 probability = GST_TYPE_FIND_MAXIMUM;
3368 goto suggest_audio_mod_caps;
3371 /* PTM (PolyTracker) */
3372 if ((data = gst_type_find_peek (tf, 0x2C, 4)) != NULL) {
3373 if (memcmp (data, "PTMF", 4) == 0) {
3375 probability = GST_TYPE_FIND_LIKELY;
3376 goto suggest_audio_mod_caps;
3380 if ((data = gst_type_find_peek (tf, 0, 38)) != NULL) {
3381 if ((memcmp (data, "Extended Module: ", 17) == 0) && (data[37] == 0x1A)) {
3383 probability = GST_TYPE_FIND_MAXIMUM;
3384 goto suggest_audio_mod_caps;
3388 if (data || (data = gst_type_find_peek (tf, 0, 8)) != NULL) {
3389 if (memcmp (data, "OKTASONG", 8) == 0) {
3391 probability = GST_TYPE_FIND_MAXIMUM;
3392 goto suggest_audio_mod_caps;
3395 /* Various formats with a 4-byte magic ID at the beginning of the file */
3396 if (data || (data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3397 /* PSM (Protracker Studio PSM) */
3398 if (memcmp (data, "PSM", 3) == 0) {
3399 unsigned char fbyte = data[3];
3400 if ((fbyte == ' ') || (fbyte == 254)) {
3402 probability = GST_TYPE_FIND_MAXIMUM;
3403 goto suggest_audio_mod_caps;
3407 if ((memcmp (data, "if", 2) == 0) || (memcmp (data, "JN", 2) == 0)) {
3409 probability = GST_TYPE_FIND_LIKELY;
3410 goto suggest_audio_mod_caps;
3413 if ((memcmp (data, "AMF", 3) == 0) && (data[3] > 10) && (data[3] < 14)) {
3414 mod_type = "dsmi-amf";
3415 probability = GST_TYPE_FIND_MAXIMUM;
3416 goto suggest_audio_mod_caps;
3419 if (memcmp (data, "IMPM", 4) == 0) {
3421 probability = GST_TYPE_FIND_MAXIMUM;
3422 goto suggest_audio_mod_caps;
3424 /* DBM (DigiBooster Pro) */
3425 if (memcmp (data, "DBM0", 4) == 0) {
3427 probability = GST_TYPE_FIND_MAXIMUM;
3428 goto suggest_audio_mod_caps;
3430 /* MDL (DigiTrakker) */
3431 if (memcmp (data, "DMDL", 4) == 0) {
3433 probability = GST_TYPE_FIND_MAXIMUM;
3434 goto suggest_audio_mod_caps;
3436 /* MT2 (MadTracker 2.0) */
3437 if (memcmp (data, "MT20", 4) == 0) {
3439 probability = GST_TYPE_FIND_MAXIMUM;
3440 goto suggest_audio_mod_caps;
3442 /* DMF (X-Tracker) */
3443 if (memcmp (data, "DDMF", 4) == 0) {
3445 probability = GST_TYPE_FIND_MAXIMUM;
3446 goto suggest_audio_mod_caps;
3449 if ((memcmp (data, "MMD0", 4) == 0) || (memcmp (data, "MMD1", 4) == 0)) {
3451 probability = GST_TYPE_FIND_MAXIMUM;
3452 goto suggest_audio_mod_caps;
3455 if (memcmp (data, "MTM", 3) == 0) {
3457 probability = GST_TYPE_FIND_MAXIMUM;
3458 goto suggest_audio_mod_caps;
3461 if (memcmp (data, "RIFF", 4) == 0) {
3462 const guint8 *data2 = gst_type_find_peek (tf, 8, 4);
3465 if (memcmp (data2, "DSMF", 4) == 0) {
3467 probability = GST_TYPE_FIND_MAXIMUM;
3468 goto suggest_audio_mod_caps;
3472 /* FAR (Farandole) */
3473 if (memcmp (data, "FAR\xFE", 4) == 0) {
3475 probability = GST_TYPE_FIND_MAXIMUM;
3476 goto suggest_audio_mod_caps;
3479 if (memcmp (data, "FAM\xFE", 4) == 0) {
3480 const guint8 *data2 = gst_type_find_peek (tf, 44, 3);
3483 if (memcmp (data2, "compare", 3) == 0) {
3485 probability = GST_TYPE_FIND_MAXIMUM;
3486 goto suggest_audio_mod_caps;
3488 /* otherwise do not suggest anything */
3491 probability = GST_TYPE_FIND_LIKELY;
3492 goto suggest_audio_mod_caps;
3496 if (memcmp (data, "GDM\xFE", 4) == 0) {
3497 const guint8 *data2 = gst_type_find_peek (tf, 71, 4);
3500 if (memcmp (data2, "GMFS", 4) == 0) {
3502 probability = GST_TYPE_FIND_MAXIMUM;
3503 goto suggest_audio_mod_caps;
3505 /* otherwise do not suggest anything */
3508 probability = GST_TYPE_FIND_LIKELY;
3509 goto suggest_audio_mod_caps;
3513 if (memcmp (data, "\xC1\x83\x2A\x9E", 4) == 0) {
3515 probability = GST_TYPE_FIND_POSSIBLE;
3516 goto suggest_audio_mod_caps;
3519 /* FAR (Farandole) (secondary detection) */
3520 if ((data = gst_type_find_peek (tf, 44, 3)) != NULL) {
3521 if (memcmp (data, "\x0D\x0A\x1A", 3) == 0) {
3523 probability = GST_TYPE_FIND_POSSIBLE;
3524 goto suggest_audio_mod_caps;
3528 if ((data = gst_type_find_peek (tf, 60, 4)) != NULL) {
3529 if (memcmp (data, "IM10", 4) == 0) {
3531 probability = GST_TYPE_FIND_MAXIMUM;
3532 goto suggest_audio_mod_caps;
3536 if ((data = gst_type_find_peek (tf, 44, 4)) != NULL) {
3537 if (memcmp (data, "SCRM", 4) == 0) {
3539 probability = GST_TYPE_FIND_MAXIMUM;
3540 goto suggest_audio_mod_caps;
3544 if ((data = gst_type_find_peek (tf, 20, 8)) != NULL) {
3545 if (g_ascii_strncasecmp ((gchar *) data, "!Scream!", 8) == 0 ||
3546 g_ascii_strncasecmp ((gchar *) data, "BMOD2STM", 8) == 0) {
3547 const guint8 *id, *stmtype;
3549 if ((id = gst_type_find_peek (tf, 28, 1)) == NULL)
3551 if ((stmtype = gst_type_find_peek (tf, 29, 1)) == NULL)
3553 if (*id == 0x1A && *stmtype == 2) {
3555 probability = GST_TYPE_FIND_MAXIMUM;
3556 goto suggest_audio_mod_caps;
3561 if ((data = gst_type_find_peek (tf, 0, 19)) != NULL) {
3562 if (memcmp (data, "ASYLUM Music Format", 19) == 0) {
3563 mod_type = "asylum-amf";
3564 probability = GST_TYPE_FIND_MAXIMUM;
3565 goto suggest_audio_mod_caps;
3569 suggest_audio_mod_caps:
3570 if (mod_type != NULL) {
3571 GstCaps *caps = gst_caps_new_simple ("audio/x-mod",
3572 "type", G_TYPE_STRING, mod_type, NULL);
3574 gst_type_find_suggest (tf, probability, caps);
3575 gst_caps_unref (caps);
3579 /*** application/x-shockwave-flash ***/
3581 static GstStaticCaps swf_caps =
3582 GST_STATIC_CAPS ("application/x-shockwave-flash");
3583 #define SWF_CAPS (gst_static_caps_get(&swf_caps))
3585 swf_type_find (GstTypeFind * tf, gpointer unused)
3587 const guint8 *data = gst_type_find_peek (tf, 0, 4);
3589 if (data && (data[0] == 'F' || data[0] == 'C') &&
3590 data[1] == 'W' && data[2] == 'S') {
3591 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SWF_CAPS);
3595 /*** application/vnd.ms-sstr+xml ***/
3598 mss_manifest_load_utf16 (gunichar2 * utf16_ne, const guint8 * utf16_data,
3599 gsize data_size, guint data_endianness)
3601 memcpy (utf16_ne, utf16_data, data_size);
3602 if (data_endianness != G_BYTE_ORDER) {
3605 for (i = 0; i < data_size / 2; ++i)
3606 utf16_ne[i] = GUINT16_SWAP_LE_BE (utf16_ne[i]);
3610 static GstStaticCaps mss_manifest_caps =
3611 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml");
3612 #define MSS_MANIFEST_CAPS (gst_static_caps_get(&mss_manifest_caps))
3614 mss_manifest_type_find (GstTypeFind * tf, gpointer unused)
3616 gunichar2 utf16_ne[512];
3618 guint data_endianness = 0;
3619 glong n_read = 0, size = 0;
3622 gboolean utf8_bom_detected = FALSE;
3624 if (xml_check_first_element (tf, "SmoothStreamingMedia", 20, TRUE)) {
3625 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3629 length = gst_type_find_get_length (tf);
3631 /* try detecting the charset */
3632 data = gst_type_find_peek (tf, 0, 3);
3637 /* look for a possible BOM */
3638 if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
3639 utf8_bom_detected = TRUE;
3640 else if (data[0] == 0xFF && data[1] == 0xFE)
3641 data_endianness = G_LITTLE_ENDIAN;
3642 else if (data[0] == 0xFE && data[1] == 0xFF)
3643 data_endianness = G_BIG_ENDIAN;
3647 /* try a default that should be enough */
3650 else if (length < 64)
3652 else /* the first few bytes should be enough */
3653 length = MIN (1024, length);
3655 data = gst_type_find_peek (tf, 0, length);
3664 if (utf8_bom_detected) {
3665 /* skip last byte of the BOM */
3669 if (xml_check_first_element_from_data (data, length,
3670 "SmoothStreamingMedia", 20, TRUE))
3671 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3673 length = GST_ROUND_DOWN_2 (length);
3675 /* convert to native endian UTF-16 */
3676 mss_manifest_load_utf16 (utf16_ne, data, length, data_endianness);
3678 /* and now convert to UTF-8 */
3679 utf8 = g_utf16_to_utf8 (utf16_ne, length / 2, &n_read, &size, NULL);
3680 if (utf8 != NULL && n_read > 0) {
3681 if (xml_check_first_element_from_data ((const guint8 *) utf8, size,
3682 "SmoothStreamingMedia", 20, TRUE))
3683 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3689 /*** image/jpeg ***/
3691 #define JPEG_MARKER_IS_START_OF_FRAME(x) \
3692 ((x)>=0xc0 && (x) <= 0xcf && (x)!=0xc4 && (x)!=0xc8 && (x)!=0xcc)
3694 static GstStaticCaps jpeg_caps = GST_STATIC_CAPS ("image/jpeg");
3696 #define JPEG_CAPS (gst_static_caps_get(&jpeg_caps))
3698 jpeg_type_find (GstTypeFind * tf, gpointer unused)
3700 GstTypeFindProbability prob = GST_TYPE_FIND_POSSIBLE;
3701 DataScanCtx c = { 0, NULL, 0 };
3705 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 2)))
3708 if (c.data[0] != 0xff || c.data[1] != 0xd8)
3712 data_scan_ctx_advance (tf, &c, 2);
3714 caps = gst_caps_copy (JPEG_CAPS);
3716 while (data_scan_ctx_ensure_data (tf, &c, 4) && c.offset < (200 * 1024)) {
3720 if (c.data[0] != 0xff)
3724 if (G_UNLIKELY (marker == 0xff)) {
3725 data_scan_ctx_advance (tf, &c, 1);
3729 data_scan_ctx_advance (tf, &c, 2);
3731 /* we assume all markers we'll see before SOF have a payload length; if
3732 * that's not the case we'll just detect a false sync and bail out, but
3733 * still report POSSIBLE probability */
3734 len = GST_READ_UINT16_BE (c.data);
3736 GST_LOG ("possible JPEG marker 0x%02x (@0x%04x), segment length %u",
3737 marker, (guint) c.offset, len);
3739 if (!data_scan_ctx_ensure_data (tf, &c, len))
3742 if (marker == 0xc4 || /* DEFINE_HUFFMAN_TABLES */
3743 marker == 0xcc || /* DEFINE_ARITHMETIC_CONDITIONING */
3744 marker == 0xdb || /* DEFINE_QUANTIZATION_TABLES */
3745 marker == 0xdd || /* DEFINE_RESTART_INTERVAL */
3746 marker == 0xfe) { /* COMMENT */
3747 data_scan_ctx_advance (tf, &c, len);
3749 } else if (marker == 0xe0 && len >= (2 + 4) && /* APP0 */
3750 data_scan_ctx_memcmp (tf, &c, 2, "JFIF", 4)) {
3751 GST_LOG ("found JFIF tag");
3752 prob = GST_TYPE_FIND_MAXIMUM;
3753 data_scan_ctx_advance (tf, &c, len);
3755 /* we continue until we find a start of frame marker */
3756 } else if (marker == 0xe1 && len >= (2 + 4) && /* APP1 */
3757 data_scan_ctx_memcmp (tf, &c, 2, "Exif", 4)) {
3758 GST_LOG ("found Exif tag");
3759 prob = GST_TYPE_FIND_MAXIMUM;
3760 data_scan_ctx_advance (tf, &c, len);
3762 /* we continue until we find a start of frame marker */
3763 } else if (marker >= 0xe0 && marker <= 0xef) { /* APPn */
3764 data_scan_ctx_advance (tf, &c, len);
3766 } else if (JPEG_MARKER_IS_START_OF_FRAME (marker) && len >= (2 + 8)) {
3769 h = GST_READ_UINT16_BE (c.data + 2 + 1);
3770 w = GST_READ_UINT16_BE (c.data + 2 + 1 + 2);
3771 if (h == 0 || w == 0) {
3772 GST_WARNING ("bad width %u and/or height %u in SOF header", w, h);
3776 GST_LOG ("SOF at offset %" G_GUINT64_FORMAT ", num_markers=%d, "
3777 "WxH=%dx%d", c.offset - 2, num_markers, w, h);
3779 if (num_markers >= 5 || prob == GST_TYPE_FIND_MAXIMUM)
3780 prob = GST_TYPE_FIND_MAXIMUM;
3782 prob = GST_TYPE_FIND_LIKELY;
3784 gst_caps_set_simple (caps, "width", G_TYPE_INT, w,
3785 "height", G_TYPE_INT, h, "sof-marker", G_TYPE_INT, marker & 0xf,
3790 GST_WARNING ("bad length or unexpected JPEG marker 0xff 0x%02x", marker);
3795 gst_type_find_suggest (tf, prob, caps);
3796 gst_caps_unref (caps);
3801 static GstStaticCaps bmp_caps = GST_STATIC_CAPS ("image/bmp");
3803 #define BMP_CAPS (gst_static_caps_get(&bmp_caps))
3805 bmp_type_find (GstTypeFind * tf, gpointer unused)
3807 DataScanCtx c = { 0, NULL, 0 };
3808 guint32 struct_size, w, h, planes, bpp;
3810 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 54)))
3813 if (c.data[0] != 'B' || c.data[1] != 'M')
3816 /* skip marker + size */
3817 data_scan_ctx_advance (tf, &c, 2 + 4);
3819 /* reserved, must be 0 */
3820 if (c.data[0] != 0 || c.data[1] != 0 || c.data[2] != 0 || c.data[3] != 0)
3823 data_scan_ctx_advance (tf, &c, 2 + 2);
3825 /* offset to start of image data in bytes (check for sanity) */
3826 GST_LOG ("offset=%u", GST_READ_UINT32_LE (c.data));
3827 if (GST_READ_UINT32_LE (c.data) > (10 * 1024 * 1024))
3830 struct_size = GST_READ_UINT32_LE (c.data + 4);
3831 GST_LOG ("struct_size=%u", struct_size);
3833 data_scan_ctx_advance (tf, &c, 4 + 4);
3835 if (struct_size == 0x0C) {
3836 w = GST_READ_UINT16_LE (c.data);
3837 h = GST_READ_UINT16_LE (c.data + 2);
3838 planes = GST_READ_UINT16_LE (c.data + 2 + 2);
3839 bpp = GST_READ_UINT16_LE (c.data + 2 + 2 + 2);
3840 } else if (struct_size == 40 || struct_size == 64 || struct_size == 108
3841 || struct_size == 124 || struct_size == 0xF0) {
3842 w = GST_READ_UINT32_LE (c.data);
3843 h = GST_READ_UINT32_LE (c.data + 4);
3844 planes = GST_READ_UINT16_LE (c.data + 4 + 4);
3845 bpp = GST_READ_UINT16_LE (c.data + 4 + 4 + 2);
3850 /* image sizes sanity check */
3851 GST_LOG ("w=%u, h=%u, planes=%u, bpp=%u", w, h, planes, bpp);
3852 if (w == 0 || w > 0xfffff || h == 0 || h > 0xfffff || planes != 1 ||
3853 (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32))
3856 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "image/bmp",
3857 "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, "bpp", G_TYPE_INT, bpp,
3861 /*** image/tiff ***/
3862 static GstStaticCaps tiff_caps = GST_STATIC_CAPS ("image/tiff, "
3863 "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }");
3864 #define TIFF_CAPS (gst_static_caps_get(&tiff_caps))
3865 static GstStaticCaps tiff_be_caps = GST_STATIC_CAPS ("image/tiff, "
3866 "endianness = (int) BIG_ENDIAN");
3867 #define TIFF_BE_CAPS (gst_static_caps_get(&tiff_be_caps))
3868 static GstStaticCaps tiff_le_caps = GST_STATIC_CAPS ("image/tiff, "
3869 "endianness = (int) LITTLE_ENDIAN");
3870 #define TIFF_LE_CAPS (gst_static_caps_get(&tiff_le_caps))
3872 tiff_type_find (GstTypeFind * tf, gpointer ununsed)
3874 const guint8 *data = gst_type_find_peek (tf, 0, 8);
3875 guint8 le_header[4] = { 0x49, 0x49, 0x2A, 0x00 };
3876 guint8 be_header[4] = { 0x4D, 0x4D, 0x00, 0x2A };
3879 if (memcmp (data, le_header, 4) == 0) {
3880 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_LE_CAPS);
3881 } else if (memcmp (data, be_header, 4) == 0) {
3882 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_BE_CAPS);
3887 /*** image/x-exr ***/
3888 static GstStaticCaps exr_caps = GST_STATIC_CAPS ("image/x-exr");
3889 #define EXR_CAPS (gst_static_caps_get(&exr_caps))
3891 exr_type_find (GstTypeFind * tf, gpointer ununsed)
3893 const guint8 *data = gst_type_find_peek (tf, 0, 8);
3898 if (GST_READ_UINT32_LE (data) != 0x01312f76)
3901 flags = GST_READ_UINT32_LE (data + 4);
3902 if ((flags & 0xff) != 1 && (flags & 0xff) != 2)
3905 /* If bit 9 is set, bit 11 and 12 must be 0 */
3906 if ((flags & 0x200) && (flags & 0x1800))
3909 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, EXR_CAPS);
3916 static GstStaticCaps pnm_caps = GST_STATIC_CAPS ("image/x-portable-bitmap; "
3917 "image/x-portable-graymap; image/x-portable-pixmap; "
3918 "image/x-portable-anymap");
3920 #define PNM_CAPS (gst_static_caps_get(&pnm_caps))
3922 #define IS_PNM_WHITESPACE(c) \
3923 ((c) == ' ' || (c) == '\r' || (c) == '\n' || (c) == 't')
3926 pnm_type_find (GstTypeFind * tf, gpointer ununsed)
3928 const gchar *media_type = NULL;
3929 DataScanCtx c = { 0, NULL, 0 };
3932 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 16)))
3935 /* see http://en.wikipedia.org/wiki/Netpbm_format */
3936 if (c.data[0] != 'P' || c.data[1] < '1' || c.data[1] > '7' ||
3937 !IS_PNM_WHITESPACE (c.data[2]) ||
3938 (c.data[3] != '#' && c.data[3] < '0' && c.data[3] > '9'))
3941 switch (c.data[1]) {
3943 media_type = "image/x-portable-bitmap"; /* ASCII */
3946 media_type = "image/x-portable-graymap"; /* ASCII */
3949 media_type = "image/x-portable-pixmap"; /* ASCII */
3952 media_type = "image/x-portable-bitmap"; /* Raw */
3955 media_type = "image/x-portable-graymap"; /* Raw */
3958 media_type = "image/x-portable-pixmap"; /* Raw */
3961 media_type = "image/x-portable-anymap";
3964 g_return_if_reached ();
3967 /* try to extract width and height as well */
3968 if (c.data[1] != '7') {
3969 gchar s[64] = { 0, }
3972 /* need to skip any comment lines first */
3973 data_scan_ctx_advance (tf, &c, 3);
3974 while (c.data[0] == '#') { /* we know there's still data left */
3975 data_scan_ctx_advance (tf, &c, 1);
3976 while (c.data[0] != '\n' && c.data[0] != '\r') {
3977 if (!data_scan_ctx_ensure_data (tf, &c, 4))
3979 data_scan_ctx_advance (tf, &c, 1);
3981 data_scan_ctx_advance (tf, &c, 1);
3982 GST_LOG ("skipped comment line in PNM header");
3985 if (!data_scan_ctx_ensure_data (tf, &c, 32) &&
3986 !data_scan_ctx_ensure_data (tf, &c, 4)) {
3990 /* need to NUL-terminate data for sscanf */
3991 memcpy (s, c.data, MIN (sizeof (s) - 1, c.size));
3992 if (sscanf (s, "%u%c%u%c", &w, &sep1, &h, &sep2) == 4 &&
3993 IS_PNM_WHITESPACE (sep1) && IS_PNM_WHITESPACE (sep2) &&
3994 w > 0 && w < G_MAXINT && h > 0 && h < G_MAXINT) {
3995 GST_LOG ("extracted PNM width and height: %dx%d", w, h);
4001 /* FIXME: extract width + height for anymaps too */
4004 if (w > 0 && h > 0) {
4005 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type,
4006 "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, NULL);
4008 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, media_type, NULL);
4012 static GstStaticCaps sds_caps = GST_STATIC_CAPS ("audio/x-sds");
4014 #define SDS_CAPS (gst_static_caps_get(&sds_caps))
4016 sds_type_find (GstTypeFind * tf, gpointer ununsed)
4018 const guint8 *data = gst_type_find_peek (tf, 0, 4);
4019 guint8 mask[4] = { 0xFF, 0xFF, 0x80, 0xFF };
4020 guint8 match[4] = { 0xF0, 0x7E, 0, 0x01 };
4024 for (x = 0; x < 4; x++) {
4025 if ((data[x] & mask[x]) != match[x]) {
4029 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDS_CAPS);
4033 static GstStaticCaps ircam_caps = GST_STATIC_CAPS ("audio/x-ircam");
4035 #define IRCAM_CAPS (gst_static_caps_get(&ircam_caps))
4037 ircam_type_find (GstTypeFind * tf, gpointer ununsed)
4039 const guint8 *data = gst_type_find_peek (tf, 0, 4);
4040 guint8 mask[4] = { 0xFF, 0xFF, 0xF8, 0xFF };
4041 guint8 match[4] = { 0x64, 0xA3, 0x00, 0x00 };
4043 gboolean matched = TRUE;
4048 for (x = 0; x < 4; x++) {
4049 if ((data[x] & mask[x]) != match[x]) {
4054 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, IRCAM_CAPS);
4057 /* now try the reverse version */
4059 for (x = 0; x < 4; x++) {
4060 if ((data[x] & mask[3 - x]) != match[3 - x]) {
4066 /*** Matroska/WebM ***/
4068 #define EBML_HEADER 0x1A45DFA3
4069 #define EBML_VERSION 0x4286
4070 #define EBML_DOCTYPE 0x4282
4071 #define EBML_DOCTYPE_VERSION 0x4287
4072 #define MATROSKA_SEGMENT 0x18538067
4073 #define MATROSKA_CLUSTER 0x1F43B675
4074 #define MATROSKA_TRACKS 0x1654AE6B
4075 #define MATROSKA_TRACK_ENTRY 0xAE
4076 #define MATROSKA_TRACK_TYPE 0x83
4077 #define MATROSKA_STEREO_MODE 0x53B8
4079 #define EBML_MAX_LEN (2 * 1024 * 1024)
4083 EBML_DOCTYPE_UNKNOWN = 0,
4084 EBML_DOCTYPE_MATROSKA,
4090 GstEbmlDocType doctype;
4096 guint tracks_ok; /* if we've seen and fully parsed the TRACKS element */
4100 ebml_read_chunk_header (GstTypeFind * tf, DataScanCtx * c, guint max_size,
4101 guint32 * id, guint64 * size)
4104 guint msbit_set, i, len, id_len;
4106 if (c->size < 12 || max_size < 1)
4111 if ((c->data[0] & 0x80) == 0x80) {
4113 } else if ((c->data[0] & 0xC0) == 0x40) {
4115 } else if ((c->data[0] & 0xE0) == 0x20) {
4117 } else if ((c->data[0] & 0xF0) == 0x10) {
4123 if (max_size < id_len)
4126 for (i = 1; i < id_len; ++i) {
4127 *id = (*id << 8) | c->data[i];
4130 data_scan_ctx_advance (tf, c, id_len);
4134 if (max_size < 1 || c->data[0] == 0)
4137 msbit_set = g_bit_nth_msf (c->data[0], 8);
4138 mask = ((1 << msbit_set) - 1);
4139 *size = c->data[0] & mask;
4140 len = 7 - msbit_set;
4142 if (max_size < 1 + len)
4144 for (i = 0; i < len; ++i) {
4145 mask = (mask << 8) | 0xff;
4146 *size = (*size << 8) | c->data[1 + i];
4149 data_scan_ctx_advance (tf, c, 1 + len);
4151 /* undefined/unknown size? (all bits 1) */
4152 if (*size == mask) {
4153 /* allow unknown size for SEGMENT chunk, bail out otherwise */
4154 if (*id == MATROSKA_SEGMENT)
4155 *size = G_MAXUINT64;
4160 return id_len + (1 + len);
4164 ebml_parse_chunk (GstTypeFind * tf, DataScanCtx * ctx, guint32 chunk_id,
4165 guint chunk_size, GstMatroskaInfo * info, guint depth)
4166 { /* FIXME: make sure input size is clipped to 32 bit */
4167 static const gchar SPACES[] = " ";
4168 DataScanCtx c = *ctx;
4169 guint64 element_size;
4170 guint32 id, hdr_len;
4172 if (depth >= 8) /* keep SPACES large enough for depth */
4175 while (chunk_size > 0) {
4176 if (c.offset > EBML_MAX_LEN || !data_scan_ctx_ensure_data (tf, &c, 64))
4179 hdr_len = ebml_read_chunk_header (tf, &c, chunk_size, &id, &element_size);
4183 g_assert (hdr_len <= chunk_size);
4184 chunk_size -= hdr_len;
4186 if (element_size > chunk_size)
4189 GST_DEBUG ("%s %08x, size %" G_GUINT64_FORMAT " / %" G_GUINT64_FORMAT,
4190 SPACES + sizeof (SPACES) - 1 - (2 * depth), id, element_size,
4191 hdr_len + element_size);
4193 if (!data_scan_ctx_ensure_data (tf, &c, element_size)) {
4194 GST_DEBUG ("not enough data");
4200 if (element_size >= 8 && memcmp (c.data, "matroska", 8) == 0)
4201 info->doctype = EBML_DOCTYPE_MATROSKA;
4202 else if (element_size >= 4 && memcmp (c.data, "webm", 4) == 0)
4203 info->doctype = EBML_DOCTYPE_WEBM;
4205 case MATROSKA_SEGMENT:
4206 GST_LOG ("parsing segment");
4207 ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1);
4208 GST_LOG ("parsed segment, done");
4210 case MATROSKA_TRACKS:
4211 GST_LOG ("parsing tracks");
4213 ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1);
4214 GST_LOG ("parsed tracks: %s, done (after %" G_GUINT64_FORMAT " bytes)",
4215 info->tracks_ok ? "ok" : "FAIL", c.offset + element_size);
4217 case MATROSKA_TRACK_ENTRY:
4218 GST_LOG ("parsing track entry");
4219 if (!ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1))
4222 case MATROSKA_TRACK_TYPE:{
4225 /* is supposed to always be 1-byte, but not everyone's following that */
4226 for (i = 0; i < element_size; ++i)
4227 type = (type << 8) | c.data[i];
4229 GST_DEBUG ("%s track type %u",
4230 SPACES + sizeof (SPACES) - 1 - (2 * depth), type);
4234 else if (c.data[0] == 2)
4240 case MATROSKA_STEREO_MODE:
4241 ++info->video_stereo;
4243 case MATROSKA_CLUSTER:
4244 GST_WARNING ("cluster, bailing out (should've found tracks by now)");
4249 data_scan_ctx_advance (tf, &c, element_size);
4250 chunk_size -= element_size;
4257 static GstStaticCaps matroska_caps = GST_STATIC_CAPS ("video/x-matroska");
4259 #define MATROSKA_CAPS (gst_static_caps_get(&matroska_caps))
4261 matroska_type_find (GstTypeFind * tf, gpointer ununsed)
4263 GstTypeFindProbability prob;
4264 GstMatroskaInfo info = { 0, };
4265 const gchar *type_name;
4266 DataScanCtx c = { 0, NULL, 0 };
4269 guint32 id, hdr_len;
4271 if (!data_scan_ctx_ensure_data (tf, &c, 64))
4274 if (GST_READ_UINT32_BE (c.data) != EBML_HEADER)
4277 while (c.offset < EBML_MAX_LEN && data_scan_ctx_ensure_data (tf, &c, 64)) {
4278 hdr_len = ebml_read_chunk_header (tf, &c, c.size, &id, &size);
4282 GST_INFO ("=== top-level chunk %08x, size %" G_GUINT64_FORMAT
4283 " / %" G_GUINT64_FORMAT, id, size, size + hdr_len);
4285 if (!ebml_parse_chunk (tf, &c, id, size, &info, 0))
4287 data_scan_ctx_advance (tf, &c, size);
4288 GST_INFO ("=== done with chunk %08x", id);
4289 if (id == MATROSKA_SEGMENT)
4293 GST_INFO ("audio=%u video=%u other=%u chunks=%u doctype=%d all_tracks=%d",
4294 info.audio, info.video, info.other, info.chunks, info.doctype,
4297 /* perhaps we should bail out if tracks_ok is FALSE and wait for more data?
4298 * (we would need new API to signal this properly and prevent other
4299 * typefinders from taking over the decision then) */
4300 is_audio = (info.audio > 0 && info.video == 0 && info.other == 0);
4302 if (info.doctype == EBML_DOCTYPE_WEBM) {
4303 type_name = (is_audio) ? "audio/webm" : "video/webm";
4304 } else if (info.video > 0 && info.video_stereo) {
4305 type_name = "video/x-matroska-3d";
4307 type_name = (is_audio) ? "audio/x-matroska" : "video/x-matroska";
4310 if (info.doctype == EBML_DOCTYPE_UNKNOWN)
4311 prob = GST_TYPE_FIND_LIKELY;
4313 prob = GST_TYPE_FIND_MAXIMUM;
4315 gst_type_find_suggest_simple (tf, prob, type_name, NULL);
4318 /*** application/mxf ***/
4319 static GstStaticCaps mxf_caps = GST_STATIC_CAPS ("application/mxf");
4321 #define MXF_MAX_PROBE_LENGTH (1024 * 64)
4322 #define MXF_CAPS (gst_static_caps_get(&mxf_caps))
4325 * MXF files start with a header partition pack key of 16 bytes which is defined
4326 * at SMPTE-377M 6.1. Before this there can be up to 64K of run-in which _must_
4327 * not contain the partition pack key.
4330 mxf_type_find (GstTypeFind * tf, gpointer ununsed)
4332 static const guint8 partition_pack_key[] =
4333 { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01,
4336 DataScanCtx c = { 0, NULL, 0 };
4338 while (c.offset <= MXF_MAX_PROBE_LENGTH) {
4340 if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 1024)))
4343 /* look over in chunks of 1kbytes to avoid too much overhead */
4345 for (i = 0; i < 1024 - 16; i++) {
4346 /* Check first byte before calling more expensive memcmp function */
4347 if (G_UNLIKELY (c.data[i] == 0x06
4348 && memcmp (c.data + i, partition_pack_key, 13) == 0)) {
4349 /* Header partition pack? */
4350 if (c.data[i + 13] != 0x02)
4353 /* Partition status */
4354 if (c.data[i + 14] >= 0x05)
4357 /* Reserved, must be 0x00 */
4358 if (c.data[i + 15] != 0x00)
4361 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXF_CAPS);
4367 data_scan_ctx_advance (tf, &c, 1024 - 16);
4371 /*** video/x-dv ***/
4373 static GstStaticCaps dv_caps = GST_STATIC_CAPS ("video/x-dv, "
4374 "systemstream = (boolean) true");
4375 #define DV_CAPS (gst_static_caps_get(&dv_caps))
4377 dv_type_find (GstTypeFind * tf, gpointer private)
4381 data = gst_type_find_peek (tf, 0, 5);
4383 /* check for DIF and DV flag */
4384 if (data && (data[0] == 0x1f) && (data[1] == 0x07) && (data[2] == 0x00)) {
4385 const gchar *format;
4387 if (data[3] & 0x80) {
4393 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "video/x-dv",
4394 "systemstream", G_TYPE_BOOLEAN, TRUE,
4395 "format", G_TYPE_STRING, format, NULL);
4400 /*** Ogg variants ***/
4401 static GstStaticCaps ogg_caps =
4402 GST_STATIC_CAPS ("application/ogg;video/ogg;audio/ogg;application/kate");
4404 #define OGG_CAPS (gst_static_caps_get(&ogg_caps))
4418 ogganx_type_find (GstTypeFind * tf, gpointer private)
4420 const gchar *media_type;
4421 DataScanCtx c = { 0, NULL, 0 };
4422 guint ogg_syncs = 0;
4423 guint hdr_count[OGG_NUM] = { 0, };
4426 const gchar marker[10];
4428 GstOggStreamType stream_type;
4431 "\001vorbis", 7, OGG_AUDIO}, {
4432 "\200theora", 7, OGG_VIDEO}, {
4433 "fLaC", 4, OGG_AUDIO}, {
4434 "\177FLAC", 5, OGG_AUDIO}, {
4435 "Speex", 5, OGG_AUDIO}, {
4436 "CMML\0\0\0\0", 8, OGG_OTHER}, {
4437 "PCM ", 8, OGG_AUDIO}, {
4438 "Annodex", 7, OGG_ANNODEX}, {
4439 "fishead", 7, OGG_SKELETON}, {
4440 "AnxData", 7, OGG_ANNODEX}, {
4441 "CELT ", 8, OGG_AUDIO}, {
4442 "\200kate\0\0\0", 8, OGG_KATE}, {
4443 "BBCD\0", 5, OGG_VIDEO}, {
4444 "OVP80\1\1", 7, OGG_VIDEO}, {
4445 "OpusHead", 8, OGG_AUDIO}, {
4446 "\001audio\0\0\0", 9, OGG_AUDIO}, {
4447 "\001video\0\0\0", 9, OGG_VIDEO}, {
4448 "\001text\0\0\0", 9, OGG_OTHER}
4451 while (c.offset < 4096 && data_scan_ctx_ensure_data (tf, &c, 64)) {
4454 if (memcmp (c.data, "OggS", 5) != 0)
4460 if (c.data[5] != 0x02)
4463 /* headers should only have one segment */
4464 if (c.data[26] != 1)
4471 data_scan_ctx_advance (tf, &c, 28);
4473 if (!data_scan_ctx_ensure_data (tf, &c, MAX (size, 8)))
4476 for (i = 0; i < G_N_ELEMENTS (markers); ++i) {
4477 if (memcmp (c.data, markers[i].marker, markers[i].marker_size) == 0) {
4478 ++hdr_count[markers[i].stream_type];
4483 if (i == G_N_ELEMENTS (markers)) {
4484 GST_MEMDUMP ("unknown Ogg stream marker", c.data, size);
4485 ++hdr_count[OGG_OTHER];
4488 data_scan_ctx_advance (tf, &c, size);
4494 /* We don't bother with annodex types. FIXME: what about XSPF? */
4495 if (hdr_count[OGG_VIDEO] > 0) {
4496 media_type = "video/ogg";
4497 } else if (hdr_count[OGG_AUDIO] > 0) {
4498 media_type = "audio/ogg";
4499 } else if (hdr_count[OGG_KATE] > 0 && hdr_count[OGG_OTHER] == 0) {
4500 media_type = "application/kate";
4502 media_type = "application/ogg";
4505 GST_INFO ("found %s (audio:%u, video:%u, annodex:%u, skeleton:%u, other:%u)",
4506 media_type, hdr_count[OGG_AUDIO], hdr_count[OGG_VIDEO],
4507 hdr_count[OGG_ANNODEX], hdr_count[OGG_SKELETON], hdr_count[OGG_OTHER]);
4509 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type, NULL);
4512 /*** audio/x-vorbis ***/
4513 static GstStaticCaps vorbis_caps = GST_STATIC_CAPS ("audio/x-vorbis");
4515 #define VORBIS_CAPS (gst_static_caps_get(&vorbis_caps))
4517 vorbis_type_find (GstTypeFind * tf, gpointer private)
4519 const guint8 *data = gst_type_find_peek (tf, 0, 30);
4525 /* 1 byte packet type (identification=0x01)
4526 6 byte string "vorbis"
4527 4 byte vorbis version */
4528 if (memcmp (data, "\001vorbis\000\000\000\000", 11) != 0)
4531 /* 1 byte channels must be != 0 */
4535 /* 4 byte samplerate must be != 0 */
4536 if (GST_READ_UINT32_LE (data) == 0)
4539 /* blocksize checks */
4540 blocksize_0 = data[0] & 0x0F;
4541 blocksize_1 = (data[0] & 0xF0) >> 4;
4542 if (blocksize_0 > blocksize_1)
4544 if (blocksize_0 < 6 || blocksize_0 > 13)
4546 if (blocksize_1 < 6 || blocksize_1 > 13)
4550 if ((data[0] & 0x01) != 1)
4552 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VORBIS_CAPS);
4556 /*** video/x-theora ***/
4558 static GstStaticCaps theora_caps = GST_STATIC_CAPS ("video/x-theora");
4560 #define THEORA_CAPS (gst_static_caps_get(&theora_caps))
4562 theora_type_find (GstTypeFind * tf, gpointer private)
4564 const guint8 *data = gst_type_find_peek (tf, 0, 7); //42);
4567 if (data[0] != 0x80)
4569 if (memcmp (&data[1], "theora", 6) != 0)
4571 /* FIXME: make this more reliable when specs are out */
4573 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, THEORA_CAPS);
4579 kate_type_find (GstTypeFind * tf, gpointer private)
4581 const guint8 *data = gst_type_find_peek (tf, 0, 64);
4582 gchar category[16] = { 0, };
4584 if (G_UNLIKELY (data == NULL))
4587 /* see: http://wiki.xiph.org/index.php/OggKate#Format_specification */
4588 if (G_LIKELY (memcmp (data, "\200kate\0\0\0", 8) != 0))
4591 /* make sure we always have a NUL-terminated string */
4592 memcpy (category, data + 48, 15);
4593 GST_LOG ("kate category: %s", category);
4594 /* canonical categories for subtitles: subtitles, spu-subtitles, SUB, K-SPU */
4595 if (strcmp (category, "subtitles") == 0 || strcmp (category, "SUB") == 0 ||
4596 strcmp (category, "spu-subtitles") == 0 ||
4597 strcmp (category, "K-SPU") == 0) {
4598 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
4599 "subtitle/x-kate", NULL);
4601 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
4602 "application/x-kate", NULL);
4606 /*** application/x-ogm-video or audio***/
4608 static GstStaticCaps ogmvideo_caps =
4609 GST_STATIC_CAPS ("application/x-ogm-video");
4610 #define OGMVIDEO_CAPS (gst_static_caps_get(&ogmvideo_caps))
4612 ogmvideo_type_find (GstTypeFind * tf, gpointer private)
4614 const guint8 *data = gst_type_find_peek (tf, 0, 9);
4617 if (memcmp (data, "\001video\000\000\000", 9) != 0)
4619 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMVIDEO_CAPS);
4623 static GstStaticCaps ogmaudio_caps =
4624 GST_STATIC_CAPS ("application/x-ogm-audio");
4625 #define OGMAUDIO_CAPS (gst_static_caps_get(&ogmaudio_caps))
4627 ogmaudio_type_find (GstTypeFind * tf, gpointer private)
4629 const guint8 *data = gst_type_find_peek (tf, 0, 9);
4632 if (memcmp (data, "\001audio\000\000\000", 9) != 0)
4634 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMAUDIO_CAPS);
4638 static GstStaticCaps ogmtext_caps = GST_STATIC_CAPS ("application/x-ogm-text");
4640 #define OGMTEXT_CAPS (gst_static_caps_get(&ogmtext_caps))
4642 ogmtext_type_find (GstTypeFind * tf, gpointer private)
4644 const guint8 *data = gst_type_find_peek (tf, 0, 9);
4647 if (memcmp (data, "\001text\000\000\000\000", 9) != 0)
4649 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMTEXT_CAPS);
4653 /*** audio/x-speex ***/
4655 static GstStaticCaps speex_caps = GST_STATIC_CAPS ("audio/x-speex");
4657 #define SPEEX_CAPS (gst_static_caps_get(&speex_caps))
4659 speex_type_find (GstTypeFind * tf, gpointer private)
4661 const guint8 *data = gst_type_find_peek (tf, 0, 80);
4664 /* 8 byte string "Speex "
4665 24 byte speex version string + int */
4666 if (memcmp (data, "Speex ", 8) != 0)
4670 /* 4 byte header size >= 80 */
4671 if (GST_READ_UINT32_LE (data) < 80)
4675 /* 4 byte sample rate <= 48000 */
4676 if (GST_READ_UINT32_LE (data) > 48000)
4680 /* currently there are only 3 speex modes. */
4681 if (GST_READ_UINT32_LE (data) > 3)
4685 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SPEEX_CAPS);
4689 /*** audio/x-celt ***/
4691 static GstStaticCaps celt_caps = GST_STATIC_CAPS ("audio/x-celt");
4693 #define CELT_CAPS (gst_static_caps_get(&celt_caps))
4695 celt_type_find (GstTypeFind * tf, gpointer private)
4697 const guint8 *data = gst_type_find_peek (tf, 0, 8);
4700 /* 8 byte string "CELT " */
4701 if (memcmp (data, "CELT ", 8) != 0)
4704 /* TODO: Check other values of the CELT header */
4705 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CELT_CAPS);
4709 /*** application/x-ogg-skeleton ***/
4710 static GstStaticCaps ogg_skeleton_caps =
4711 GST_STATIC_CAPS ("application/x-ogg-skeleton, parsed=(boolean)FALSE");
4712 #define OGG_SKELETON_CAPS (gst_static_caps_get(&ogg_skeleton_caps))
4714 oggskel_type_find (GstTypeFind * tf, gpointer private)
4716 const guint8 *data = gst_type_find_peek (tf, 0, 12);
4719 /* 8 byte string "fishead\0" for the ogg skeleton stream */
4720 if (memcmp (data, "fishead\0", 8) != 0)
4724 /* Require that the header contains version 3.0 */
4725 if (GST_READ_UINT16_LE (data) != 3)
4728 if (GST_READ_UINT16_LE (data) != 0)
4731 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGG_SKELETON_CAPS);
4735 static GstStaticCaps cmml_caps = GST_STATIC_CAPS ("text/x-cmml");
4737 #define CMML_CAPS (gst_static_caps_get(&cmml_caps))
4739 cmml_type_find (GstTypeFind * tf, gpointer private)
4741 /* Header is 12 bytes minimum (though we don't check the minor version */
4742 const guint8 *data = gst_type_find_peek (tf, 0, 12);
4746 /* 8 byte string "CMML\0\0\0\0" for the magic number */
4747 if (memcmp (data, "CMML\0\0\0\0", 8) != 0)
4751 /* Require that the header contains at least version 2.0 */
4752 if (GST_READ_UINT16_LE (data) < 2)
4755 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CMML_CAPS);
4759 /*** application/x-tar ***/
4761 static GstStaticCaps tar_caps = GST_STATIC_CAPS ("application/x-tar");
4763 #define TAR_CAPS (gst_static_caps_get(&tar_caps))
4764 #define OLDGNU_MAGIC "ustar " /* 7 chars and a NUL */
4765 #define NEWGNU_MAGIC "ustar" /* 5 chars and a NUL */
4767 tar_type_find (GstTypeFind * tf, gpointer unused)
4769 const guint8 *data = gst_type_find_peek (tf, 257, 8);
4771 /* of course we are not certain, but we don't want other typefind funcs
4772 * to detect formats of files within the tar archive, e.g. mp3s */
4774 if (memcmp (data, OLDGNU_MAGIC, 8) == 0) { /* sic */
4775 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
4776 } else if (memcmp (data, NEWGNU_MAGIC, 6) == 0 && /* sic */
4777 g_ascii_isdigit (data[6]) && g_ascii_isdigit (data[7])) {
4778 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
4783 /*** application/x-ar ***/
4785 static GstStaticCaps ar_caps = GST_STATIC_CAPS ("application/x-ar");
4787 #define AR_CAPS (gst_static_caps_get(&ar_caps))
4789 ar_type_find (GstTypeFind * tf, gpointer unused)
4791 const guint8 *data = gst_type_find_peek (tf, 0, 24);
4793 if (data && memcmp (data, "!<arch>", 7) == 0) {
4796 for (i = 7; i < 24; ++i) {
4797 if (!g_ascii_isprint (data[i]) && data[i] != '\n') {
4798 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AR_CAPS);
4802 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AR_CAPS);
4806 /*** audio/x-au ***/
4808 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4809 * as it is only possible to register one typefind factory per 'name'
4810 * (which is in this case the caps), and the first one would be replaced by
4811 * the second one. */
4812 static GstStaticCaps au_caps = GST_STATIC_CAPS ("audio/x-au");
4814 #define AU_CAPS (gst_static_caps_get(&au_caps))
4816 au_type_find (GstTypeFind * tf, gpointer unused)
4818 const guint8 *data = gst_type_find_peek (tf, 0, 4);
4821 if (memcmp (data, ".snd", 4) == 0 || memcmp (data, "dns.", 4) == 0) {
4822 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AU_CAPS);
4828 /*** video/x-nuv ***/
4830 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4831 * as it is only possible to register one typefind factory per 'name'
4832 * (which is in this case the caps), and the first one would be replaced by
4833 * the second one. */
4834 static GstStaticCaps nuv_caps = GST_STATIC_CAPS ("video/x-nuv");
4836 #define NUV_CAPS (gst_static_caps_get(&nuv_caps))
4838 nuv_type_find (GstTypeFind * tf, gpointer unused)
4840 const guint8 *data = gst_type_find_peek (tf, 0, 11);
4843 if (memcmp (data, "MythTVVideo", 11) == 0
4844 || memcmp (data, "NuppelVideo", 11) == 0) {
4845 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, NUV_CAPS);
4850 /*** audio/x-paris ***/
4851 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
4852 static GstStaticCaps paris_caps = GST_STATIC_CAPS ("audio/x-paris");
4854 #define PARIS_CAPS (gst_static_caps_get(&paris_caps))
4856 paris_type_find (GstTypeFind * tf, gpointer unused)
4858 const guint8 *data = gst_type_find_peek (tf, 0, 4);
4861 if (memcmp (data, " paf", 4) == 0 || memcmp (data, "fap ", 4) == 0) {
4862 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, PARIS_CAPS);
4867 /*** audio/x-sbc ***/
4868 static GstStaticCaps sbc_caps = GST_STATIC_CAPS ("audio/x-sbc");
4869 #define SBC_CAPS (gst_static_caps_get(&sbc_caps))
4872 sbc_check_header (const guint8 * data, gsize len, guint * rate,
4875 static const guint16 sbc_rates[4] = { 16000, 32000, 44100, 48000 };
4876 static const guint8 sbc_blocks[4] = { 4, 8, 12, 16 };
4877 guint n_blocks, ch_mode, n_subbands, bitpool;
4879 if (data[0] != 0x9C || len < 4)
4882 n_blocks = sbc_blocks[(data[1] >> 4) & 0x03];
4883 ch_mode = (data[1] >> 2) & 0x03;
4884 n_subbands = (data[1] & 0x01) ? 8 : 4;
4889 *rate = sbc_rates[(data[1] >> 6) & 0x03];
4890 *channels = (ch_mode == 0) ? 1 : 2;
4893 return 4 + (n_subbands * 1) / 2 + (n_blocks * 1 * bitpool) / 8;
4894 else if (ch_mode == 1)
4895 return 4 + (n_subbands * 2) / 2 + (n_blocks * 2 * bitpool) / 8;
4896 else if (ch_mode == 2)
4897 return 4 + (n_subbands * 2) / 2 + (n_blocks * bitpool) / 8;
4898 else if (ch_mode == 3)
4899 return 4 + (n_subbands * 2) / 2 + (n_subbands + n_blocks * bitpool) / 8;
4905 sbc_type_find (GstTypeFind * tf, gpointer unused)
4909 guint i, rate, channels, offset = 0;
4911 for (i = 0; i < 10; ++i) {
4912 data = gst_type_find_peek (tf, offset, 8);
4916 frame_len = sbc_check_header (data, 8, &rate, &channels);
4920 offset += frame_len;
4922 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE, "audio/x-sbc",
4923 "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels,
4924 "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
4927 /*** audio/iLBC-sh ***/
4928 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
4929 static GstStaticCaps ilbc_caps = GST_STATIC_CAPS ("audio/iLBC-sh");
4931 #define ILBC_CAPS (gst_static_caps_get(&ilbc_caps))
4933 ilbc_type_find (GstTypeFind * tf, gpointer unused)
4935 const guint8 *data = gst_type_find_peek (tf, 0, 8);
4938 if (memcmp (data, "#!iLBC30", 8) == 0 || memcmp (data, "#!iLBC20", 8) == 0) {
4939 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, ILBC_CAPS);
4944 /*** application/x-ms-dos-executable ***/
4946 static GstStaticCaps msdos_caps =
4947 GST_STATIC_CAPS ("application/x-ms-dos-executable");
4948 #define MSDOS_CAPS (gst_static_caps_get(&msdos_caps))
4949 /* see http://www.madchat.org/vxdevl/papers/winsys/pefile/pefile.htm */
4951 msdos_type_find (GstTypeFind * tf, gpointer unused)
4953 const guint8 *data = gst_type_find_peek (tf, 0, 64);
4955 if (data && data[0] == 'M' && data[1] == 'Z' &&
4956 GST_READ_UINT16_LE (data + 8) == 4) {
4957 guint32 pe_offset = GST_READ_UINT32_LE (data + 60);
4959 data = gst_type_find_peek (tf, pe_offset, 2);
4960 if (data && data[0] == 'P' && data[1] == 'E') {
4961 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, MSDOS_CAPS);
4966 /*** application/x-mmsh ***/
4968 static GstStaticCaps mmsh_caps = GST_STATIC_CAPS ("application/x-mmsh");
4970 #define MMSH_CAPS gst_static_caps_get(&mmsh_caps)
4972 /* This is to recognise mssh-over-http */
4974 mmsh_type_find (GstTypeFind * tf, gpointer unused)
4976 static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
4977 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
4982 data = gst_type_find_peek (tf, 0, 2 + 2 + 4 + 2 + 2 + 16);
4983 if (data && data[0] == 0x24 && data[1] == 0x48 &&
4984 GST_READ_UINT16_LE (data + 2) > 2 + 2 + 4 + 2 + 2 + 16 &&
4985 memcmp (data + 2 + 2 + 4 + 2 + 2, asf_marker, 16) == 0) {
4986 gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MMSH_CAPS);
4990 /*** video/x-dirac ***/
4992 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4993 * as it is only possible to register one typefind factory per 'name'
4994 * (which is in this case the caps), and the first one would be replaced by
4995 * the second one. */
4996 static GstStaticCaps dirac_caps = GST_STATIC_CAPS ("video/x-dirac");
4998 #define DIRAC_CAPS (gst_static_caps_get(&dirac_caps))
5000 dirac_type_find (GstTypeFind * tf, gpointer unused)
5002 const guint8 *data = gst_type_find_peek (tf, 0, 8);
5005 if (memcmp (data, "BBCD", 4) == 0 || memcmp (data, "KW-DIRAC", 8) == 0) {
5006 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DIRAC_CAPS);
5011 /*** video/vivo ***/
5013 static GstStaticCaps vivo_caps = GST_STATIC_CAPS ("video/vivo");
5015 #define VIVO_CAPS gst_static_caps_get(&vivo_caps)
5018 vivo_type_find (GstTypeFind * tf, gpointer unused)
5020 static const guint8 vivo_marker[] = { 'V', 'e', 'r', 's', 'i', 'o', 'n',
5021 ':', 'V', 'i', 'v', 'o', '/'
5026 data = gst_type_find_peek (tf, 0, 1024);
5027 if (data == NULL || data[0] != 0x00)
5030 if ((data[1] & 0x80)) {
5031 if ((data[2] & 0x80))
5033 hdr_len = ((guint) (data[1] & 0x7f)) << 7;
5043 /* 1008 = 1022 - strlen ("Version:Vivo/") - 1 */
5044 while (pos < 1008 && data[pos] == '\r' && data[pos + 1] == '\n')
5047 if (memcmp (data + pos, vivo_marker, sizeof (vivo_marker)) == 0) {
5048 gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VIVO_CAPS);
5052 /*** XDG MIME typefinder (to avoid false positives mostly) ***/
5056 xdgmime_typefind (GstTypeFind * find, gpointer user_data)
5059 gsize length = 16384;
5064 if ((tf_length = gst_type_find_get_length (find)) > 0)
5065 length = MIN (length, tf_length);
5067 if ((data = gst_type_find_peek (find, 0, length)) == NULL)
5070 tmp = g_content_type_guess (NULL, data, length, NULL);
5071 if (tmp == NULL || g_content_type_is_unknown (tmp)) {
5076 mimetype = g_content_type_get_mime_type (tmp);
5079 if (mimetype == NULL)
5082 GST_DEBUG ("Got mimetype '%s'", mimetype);
5084 /* Ignore audio/video types:
5085 * - our own typefinders in -base are likely to be better at this
5086 * (and if they're not, we really want to fix them, that's why we don't
5087 * report xdg-detected audio/video types at all, not even with a low
5089 * - we want to detect GStreamer media types and not MIME types
5090 * - the purpose of this xdg mime finder is mainly to prevent false
5091 * positives of non-media formats, not to typefind audio/video formats */
5092 if (g_str_has_prefix (mimetype, "audio/") ||
5093 g_str_has_prefix (mimetype, "video/")) {
5094 GST_LOG ("Ignoring audio/video mime type");
5099 /* Again, we mainly want the xdg typefinding to prevent false-positives on
5100 * non-media formats, so suggest the type with a probability that trumps
5101 * uncertain results of our typefinders, but not more than that. */
5102 GST_LOG ("Suggesting '%s' with probability POSSIBLE", mimetype);
5103 gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE, mimetype, NULL);
5106 #endif /* USE_GIO */
5108 /*** Windows icon typefinder (to avoid false positives mostly) ***/
5111 windows_icon_typefind (GstTypeFind * find, gpointer user_data)
5115 guint16 type, nimages;
5116 gint32 size, offset;
5118 datalen = gst_type_find_get_length (find);
5119 if ((data = gst_type_find_peek (find, 0, 6)) == NULL)
5122 /* header - simple and not enough to rely on it alone */
5123 if (GST_READ_UINT16_LE (data) != 0)
5125 type = GST_READ_UINT16_LE (data + 2);
5126 if (type != 1 && type != 2)
5128 nimages = GST_READ_UINT16_LE (data + 4);
5129 if (nimages == 0) /* we can assume we can't have an empty image file ? */
5133 if (data[6 + 3] != 0)
5136 guint16 planes = GST_READ_UINT16_LE (data + 6 + 4);
5140 size = GST_READ_UINT32_LE (data + 6 + 8);
5141 offset = GST_READ_UINT32_LE (data + 6 + 12);
5142 if (offset < 0 || size <= 0 || size >= datalen || offset >= datalen
5143 || size + offset > datalen)
5146 gst_type_find_suggest_simple (find, GST_TYPE_FIND_NEARLY_CERTAIN,
5147 "image/x-icon", NULL);
5150 /*** WAP WBMP typefinder ***/
5153 wbmp_typefind (GstTypeFind * find, gpointer user_data)
5159 /* http://en.wikipedia.org/wiki/Wireless_Application_Protocol_Bitmap_Format */
5160 datalen = gst_type_find_get_length (find);
5164 data = gst_type_find_peek (find, 0, 5);
5168 /* want 0x00 0x00 at start */
5169 if (*data++ != 0 || *data++ != 0)
5172 /* min header size */
5175 /* let's assume max width/height is 65536 */
5178 w = (w << 8) | *data++;
5182 data = gst_type_find_peek (find, 4, 2);
5188 h = (h << 8) | *data++;
5194 if (w == 0 || h == 0)
5197 /* now add bitmap size */
5198 size += h * (GST_ROUND_UP_8 (w) / 8);
5200 if (datalen == size) {
5201 gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE - 10,
5202 "image/vnd.wap.wbmp", NULL);
5206 /*** DEGAS Atari images (also to avoid false positives, see #625129) ***/
5208 degas_type_find (GstTypeFind * tf, gpointer private)
5210 /* No magic, but it should have a fixed size and a few invalid values */
5211 /* http://www.fileformat.info/format/atari/spec/6ecf9f6eb5be494284a47feb8a214687/view.htm */
5217 len = gst_type_find_get_length (tf);
5218 if (len < 34) /* smallest header of the lot */
5220 data = gst_type_find_peek (tf, 0, 4);
5221 if (G_UNLIKELY (data == NULL))
5223 resolution = GST_READ_UINT16_BE (data);
5225 /* could be DEGAS */
5226 if (resolution <= 2)
5227 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5228 "image/x-degas", NULL);
5229 } else if (len == 32066) {
5230 /* could be DEGAS Elite */
5231 if (resolution <= 2) {
5232 data = gst_type_find_peek (tf, len - 16, 8);
5233 if (G_UNLIKELY (data == NULL))
5235 for (n = 0; n < 4; n++) {
5236 if (GST_READ_UINT16_BE (data + n * 2) > 2)
5239 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5240 "image/x-degas", NULL);
5242 } else if (len >= 66 && len < 32066) {
5243 /* could be compressed DEGAS Elite, but it's compressed and so we can't rely on size,
5244 it does have 4 16 bytes values near the end that are 0-2 though. */
5245 if ((resolution & 0x8000) && (resolution & 0x7fff) <= 2) {
5246 data = gst_type_find_peek (tf, len - 16, 8);
5247 if (G_UNLIKELY (data == NULL))
5249 for (n = 0; n < 4; n++) {
5250 if (GST_READ_UINT16_BE (data + n * 2) > 2)
5253 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5254 "image/x-degas", NULL);
5262 y4m_typefind (GstTypeFind * tf, gpointer private)
5266 data = gst_type_find_peek (tf, 0, 10);
5267 if (data != NULL && memcmp (data, "YUV4MPEG2 ", 10) == 0) {
5268 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY,
5269 "application/x-yuv4mpeg", "y4mversion", G_TYPE_INT, 2, NULL);
5273 /*** DVD ISO images (looks like H.264, see #674069) ***/
5275 dvdiso_type_find (GstTypeFind * tf, gpointer private)
5277 /* 0x8000 bytes of zeros, then "\001CD001" */
5281 len = gst_type_find_get_length (tf);
5284 data = gst_type_find_peek (tf, 0, 0x8006);
5285 if (G_UNLIKELY (data == NULL))
5287 for (len = 0; len < 0x8000; len++)
5290 /* Can the '1' be anything else ? My three samples all have '1'. */
5291 if (memcmp (data + 0x8000, "\001CD001", 6))
5294 /* May need more inspection, we may be able to demux some of them */
5295 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY,
5296 "application/octet-stream", NULL);
5299 /* SSA/ASS subtitles
5301 * http://en.wikipedia.org/wiki/SubStation_Alpha
5302 * http://matroska.org/technical/specs/subtitles/ssa.html
5305 ssa_type_find (GstTypeFind * tf, gpointer private)
5307 const gchar *start, *end, *ver_str, *media_type = NULL;
5309 gchar *str, *script_type, *p = NULL;
5312 data = gst_type_find_peek (tf, 0, 32);
5317 /* FIXME: detect utf-16/32 BOM and convert before typefinding the rest */
5319 /* there might be a UTF-8 BOM at the beginning */
5320 if (memcmp (data, "[Script Info]", 13) != 0 &&
5321 memcmp (data + 3, "[Script Info]", 13) != 0) {
5325 /* now check if we have SSA or ASS */
5326 len = gst_type_find_get_length (tf);
5330 data = gst_type_find_peek (tf, 0, len);
5335 start = (gchar *) memchr (data, '[', 5);
5337 len -= (start - (gchar *) data);
5339 /* ignore anything non-UTF8 for now, in future we might at least allow
5340 * other UTF variants that are clearly prefixed with the appropriate BOM */
5341 if (!g_utf8_validate (start, len, &end) && (len - (end - start)) > 6) {
5342 GST_FIXME ("non-UTF8 SSA/ASS file");
5346 /* something at start, but not a UTF-8 BOM? */
5347 if (data[0] != '[' && (data[0] != 0xEF || data[1] != 0xBB || data[2] != 0xBF))
5350 /* ignore any partial UTF-8 characters at the end */
5353 /* create a NUL-terminated string so it's easier to process it safely */
5354 str = g_strndup (start, len - 1);
5355 script_type = strstr (str, "ScriptType:");
5356 if (script_type != NULL) {
5359 ver_str = script_type + 11;
5360 while (*ver_str == ' ' || *ver_str == 'v' || *ver_str == 'V')
5362 version = g_ascii_strtod (ver_str, &p);
5363 if (version == 4.0 && p != NULL && *p == '+')
5364 media_type = "application/x-ass";
5365 else if (version >= 1.0 && version <= 4.0)
5366 media_type = "application/x-ssa";
5369 if (media_type == NULL) {
5370 if (strstr (str, "[v4+ Styles]") || strstr (str, "[V4+ Styles]"))
5371 media_type = "application/x-ass";
5372 else if (strstr (str, "[v4 Styles]") || strstr (str, "[V4 Styles]"))
5373 media_type = "application/x-ssa";
5376 if (media_type != NULL) {
5377 gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
5378 media_type, "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
5380 GST_WARNING ("could not detect SSA/ASS variant");
5386 /*** video/x-pva ***/
5388 static GstStaticCaps pva_caps = GST_STATIC_CAPS ("video/x-pva");
5390 #define PVA_CAPS gst_static_caps_get(&pva_caps)
5393 pva_type_find (GstTypeFind * tf, gpointer private)
5397 data = gst_type_find_peek (tf, 0, 5);
5402 if (data[0] == 'A' && data[1] == 'V' && data[2] < 3 && data[4] == 0x55)
5403 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, PVA_CAPS);
5406 /*** audio/audible ***/
5408 /* derived from pyaudibletags
5409 * http://code.google.com/p/pyaudibletags/source/browse/trunk/pyaudibletags.py
5411 static GstStaticCaps aa_caps = GST_STATIC_CAPS ("audio/x-audible");
5413 #define AA_CAPS gst_static_caps_get(&aa_caps)
5416 aa_type_find (GstTypeFind * tf, gpointer private)
5420 data = gst_type_find_peek (tf, 0, 12);
5424 if (GST_READ_UINT32_BE (data + 4) == 0x57907536) {
5427 media_len = gst_type_find_get_length (tf);
5428 if (media_len > 0 && GST_READ_UINT32_BE (data) == media_len)
5429 gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AA_CAPS);
5431 gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AA_CAPS);
5435 /*** generic typefind for streams that have some data at a specific position***/
5446 start_with_type_find (GstTypeFind * tf, gpointer private)
5448 GstTypeFindData *start_with = (GstTypeFindData *) private;
5451 GST_LOG ("trying to find mime type %s with the first %u bytes of data",
5452 gst_structure_get_name (gst_caps_get_structure (start_with->caps, 0)),
5454 data = gst_type_find_peek (tf, 0, start_with->size);
5455 if (data && memcmp (data, start_with->data, start_with->size) == 0) {
5456 gst_type_find_suggest (tf, start_with->probability, start_with->caps);
5461 sw_data_destroy (GstTypeFindData * sw_data)
5463 if (G_LIKELY (sw_data->caps != NULL))
5464 gst_caps_unref (sw_data->caps);
5465 g_slice_free (GstTypeFindData, sw_data);
5468 #define TYPE_FIND_REGISTER_START_WITH(plugin,name,rank,ext,_data,_size,_probability)\
5470 GstTypeFindData *sw_data = g_slice_new (GstTypeFindData); \
5471 sw_data->data = (const guint8 *)_data; \
5472 sw_data->size = _size; \
5473 sw_data->probability = _probability; \
5474 sw_data->caps = gst_caps_new_empty_simple (name); \
5475 if (!gst_type_find_register (plugin, name, rank, start_with_type_find,\
5476 ext, sw_data->caps, sw_data, \
5477 (GDestroyNotify) (sw_data_destroy))) { \
5478 sw_data_destroy (sw_data); \
5482 /*** same for riff types ***/
5485 riff_type_find (GstTypeFind * tf, gpointer private)
5487 GstTypeFindData *riff_data = (GstTypeFindData *) private;
5488 const guint8 *data = gst_type_find_peek (tf, 0, 12);
5490 if (data && (memcmp (data, "RIFF", 4) == 0 || memcmp (data, "AVF0", 4) == 0)) {
5492 if (memcmp (data, riff_data->data, 4) == 0)
5493 gst_type_find_suggest (tf, riff_data->probability, riff_data->caps);
5497 #define TYPE_FIND_REGISTER_RIFF(plugin,name,rank,ext,_data) \
5499 GstTypeFindData *sw_data = g_slice_new (GstTypeFindData); \
5500 sw_data->data = (gpointer)_data; \
5501 sw_data->size = 4; \
5502 sw_data->probability = GST_TYPE_FIND_MAXIMUM; \
5503 sw_data->caps = gst_caps_new_empty_simple (name); \
5504 if (!gst_type_find_register (plugin, name, rank, riff_type_find, \
5505 ext, sw_data->caps, sw_data, \
5506 (GDestroyNotify) (sw_data_destroy))) { \
5507 sw_data_destroy (sw_data); \
5512 /*** plugin initialization ***/
5514 #define TYPE_FIND_REGISTER(plugin,name,rank,func,ext,caps,priv,notify) \
5516 if (!gst_type_find_register (plugin, name, rank, func, ext, caps, priv, notify))\
5522 plugin_init (GstPlugin * plugin)
5524 /* can't initialize this via a struct as caps can't be statically initialized */
5526 GST_DEBUG_CATEGORY_INIT (type_find_debug, "typefindfunctions",
5527 GST_DEBUG_FG_GREEN | GST_DEBUG_BG_RED, "generic type find functions");
5529 /* note: asx/wax/wmx are XML files, asf doesn't handle them */
5530 /* must use strings, macros don't accept initializers */
5531 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ms-asf", GST_RANK_SECONDARY,
5533 "\060\046\262\165\216\146\317\021\246\331\000\252\000\142\316\154", 16,
5534 GST_TYPE_FIND_MAXIMUM);
5535 TYPE_FIND_REGISTER (plugin, "audio/x-musepack", GST_RANK_PRIMARY,
5536 musepack_type_find, "mpc,mpp,mp+", MUSEPACK_CAPS, NULL, NULL);
5537 TYPE_FIND_REGISTER (plugin, "audio/x-au", GST_RANK_MARGINAL,
5538 au_type_find, "au,snd", AU_CAPS, NULL, NULL);
5539 TYPE_FIND_REGISTER_RIFF (plugin, "video/x-msvideo", GST_RANK_PRIMARY,
5541 TYPE_FIND_REGISTER_RIFF (plugin, "audio/qcelp", GST_RANK_PRIMARY,
5543 TYPE_FIND_REGISTER_RIFF (plugin, "video/x-cdxa", GST_RANK_PRIMARY,
5545 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-vcd", GST_RANK_PRIMARY,
5546 "dat", "\000\377\377\377\377\377\377\377\377\377\377\000", 12,
5547 GST_TYPE_FIND_MAXIMUM);
5548 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-imelody", GST_RANK_PRIMARY,
5549 "imy,ime,imelody", "BEGIN:IMELODY", 13, GST_TYPE_FIND_MAXIMUM);
5551 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-smoke", GST_RANK_PRIMARY,
5552 NULL, "\x80smoke\x00\x01\x00", 6, GST_TYPE_FIND_MAXIMUM);
5554 TYPE_FIND_REGISTER (plugin, "audio/midi", GST_RANK_PRIMARY, mid_type_find,
5555 "mid,midi", MID_CAPS, NULL, NULL);
5556 TYPE_FIND_REGISTER_RIFF (plugin, "audio/riff-midi", GST_RANK_PRIMARY,
5557 "mid,midi", "RMID");
5558 TYPE_FIND_REGISTER (plugin, "audio/mobile-xmf", GST_RANK_PRIMARY,
5559 mxmf_type_find, "mxmf", MXMF_CAPS, NULL, NULL);
5560 TYPE_FIND_REGISTER (plugin, "video/x-fli", GST_RANK_MARGINAL, flx_type_find,
5561 "flc,fli", FLX_CAPS, NULL, NULL);
5562 TYPE_FIND_REGISTER (plugin, "application/x-id3v2", GST_RANK_PRIMARY + 103,
5563 id3v2_type_find, "mp3,mp2,mp1,mpga,ogg,flac,tta", ID3_CAPS, NULL, NULL);
5564 TYPE_FIND_REGISTER (plugin, "application/x-id3v1", GST_RANK_PRIMARY + 101,
5565 id3v1_type_find, "mp3,mp2,mp1,mpga,ogg,flac,tta", ID3_CAPS, NULL, NULL);
5566 TYPE_FIND_REGISTER (plugin, "application/x-apetag", GST_RANK_PRIMARY + 102,
5567 apetag_type_find, "mp3,ape,mpc,wv", APETAG_CAPS, NULL, NULL);
5568 TYPE_FIND_REGISTER (plugin, "audio/x-ttafile", GST_RANK_PRIMARY,
5569 tta_type_find, "tta", TTA_CAPS, NULL, NULL);
5570 TYPE_FIND_REGISTER (plugin, "audio/x-mod", GST_RANK_SECONDARY, mod_type_find,
5571 "669,amf,ams,dbm,digi,dmf,dsm,gdm,far,imf,it,j2b,mdl,med,mod,mt2,mtm,"
5572 "okt,psm,ptm,sam,s3m,stm,stx,ult,umx,xm", MOD_CAPS, NULL, NULL);
5573 TYPE_FIND_REGISTER (plugin, "audio/mpeg", GST_RANK_PRIMARY, mp3_type_find,
5574 "mp3,mp2,mp1,mpga", MP3_CAPS, NULL, NULL);
5575 TYPE_FIND_REGISTER (plugin, "audio/x-ac3", GST_RANK_PRIMARY, ac3_type_find,
5576 "ac3,eac3", AC3_CAPS, NULL, NULL);
5577 TYPE_FIND_REGISTER (plugin, "audio/x-dts", GST_RANK_SECONDARY, dts_type_find,
5578 "dts", DTS_CAPS, NULL, NULL);
5579 TYPE_FIND_REGISTER (plugin, "audio/x-gsm", GST_RANK_PRIMARY, NULL, "gsm",
5580 GSM_CAPS, NULL, NULL);
5581 TYPE_FIND_REGISTER (plugin, "video/mpeg-sys", GST_RANK_PRIMARY,
5582 mpeg_sys_type_find, "mpe,mpeg,mpg", MPEG_SYS_CAPS, NULL, NULL);
5583 TYPE_FIND_REGISTER (plugin, "video/mpegts", GST_RANK_PRIMARY,
5584 mpeg_ts_type_find, "ts,mts", MPEGTS_CAPS, NULL, NULL);
5585 TYPE_FIND_REGISTER (plugin, "application/ogg", GST_RANK_PRIMARY,
5586 ogganx_type_find, "ogg,oga,ogv,ogm,ogx,spx,anx,axa,axv", OGG_CAPS,
5588 TYPE_FIND_REGISTER (plugin, "video/mpeg-elementary", GST_RANK_MARGINAL,
5589 mpeg_video_stream_type_find, "mpv,mpeg,mpg", MPEG_VIDEO_CAPS, NULL, NULL);
5590 TYPE_FIND_REGISTER (plugin, "video/mpeg4", GST_RANK_PRIMARY,
5591 mpeg4_video_type_find, "m4v", MPEG_VIDEO_CAPS, NULL, NULL);
5592 TYPE_FIND_REGISTER (plugin, "video/x-h263", GST_RANK_SECONDARY,
5593 h263_video_type_find, "h263,263", H263_VIDEO_CAPS, NULL, NULL);
5594 TYPE_FIND_REGISTER (plugin, "video/x-h264", GST_RANK_PRIMARY,
5595 h264_video_type_find, "h264,x264,264", H264_VIDEO_CAPS, NULL, NULL);
5596 TYPE_FIND_REGISTER (plugin, "video/x-h265", GST_RANK_PRIMARY,
5597 h265_video_type_find, "h265,x265,265", H265_VIDEO_CAPS, NULL, NULL);
5598 TYPE_FIND_REGISTER (plugin, "video/x-nuv", GST_RANK_SECONDARY, nuv_type_find,
5599 "nuv", NUV_CAPS, NULL, NULL);
5602 TYPE_FIND_REGISTER (plugin, "audio/x-m4a", GST_RANK_PRIMARY, m4a_type_find,
5603 "m4a", M4A_CAPS, NULL, NULL);
5604 TYPE_FIND_REGISTER (plugin, "application/x-3gp", GST_RANK_PRIMARY,
5605 q3gp_type_find, "3gp", Q3GP_CAPS, NULL, NULL);
5606 TYPE_FIND_REGISTER (plugin, "video/quicktime", GST_RANK_PRIMARY,
5607 qt_type_find, "mov,mp4", QT_CAPS, NULL, NULL);
5608 TYPE_FIND_REGISTER (plugin, "image/x-quicktime", GST_RANK_SECONDARY,
5609 qtif_type_find, "qif,qtif,qti", QTIF_CAPS, NULL, NULL);
5610 TYPE_FIND_REGISTER (plugin, "image/jp2", GST_RANK_PRIMARY,
5611 jp2_type_find, "jp2", JP2_CAPS, NULL, NULL);
5612 TYPE_FIND_REGISTER (plugin, "video/mj2", GST_RANK_PRIMARY,
5613 jp2_type_find, "mj2", MJ2_CAPS, NULL, NULL);
5615 TYPE_FIND_REGISTER (plugin, "text/html", GST_RANK_SECONDARY, html_type_find,
5616 "htm,html", HTML_CAPS, NULL, NULL);
5617 TYPE_FIND_REGISTER_START_WITH (plugin, "application/vnd.rn-realmedia",
5618 GST_RANK_SECONDARY, "ra,ram,rm,rmvb", ".RMF", 4, GST_TYPE_FIND_MAXIMUM);
5619 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-pn-realaudio",
5620 GST_RANK_SECONDARY, "ra,ram,rm,rmvb", ".ra\375", 4,
5621 GST_TYPE_FIND_MAXIMUM);
5622 TYPE_FIND_REGISTER (plugin, "application/x-shockwave-flash",
5623 GST_RANK_SECONDARY, swf_type_find, "swf,swfl", SWF_CAPS, NULL, NULL);
5624 TYPE_FIND_REGISTER (plugin, "application/dash+xml",
5625 GST_RANK_PRIMARY, dash_mpd_type_find, "mpd,MPD", DASH_CAPS, NULL, NULL);
5626 TYPE_FIND_REGISTER (plugin, "application/vnd.ms-sstr+xml",
5627 GST_RANK_PRIMARY, mss_manifest_type_find, NULL, MSS_MANIFEST_CAPS, NULL,
5629 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-flv", GST_RANK_SECONDARY,
5630 "flv", "FLV", 3, GST_TYPE_FIND_MAXIMUM);
5631 TYPE_FIND_REGISTER (plugin, "text/plain", GST_RANK_MARGINAL, utf8_type_find,
5632 "txt", UTF8_CAPS, NULL, NULL);
5633 TYPE_FIND_REGISTER (plugin, "text/utf-16", GST_RANK_MARGINAL, utf16_type_find,
5634 "txt", UTF16_CAPS, NULL, NULL);
5635 TYPE_FIND_REGISTER (plugin, "text/utf-32", GST_RANK_MARGINAL, utf32_type_find,
5636 "txt", UTF32_CAPS, NULL, NULL);
5637 TYPE_FIND_REGISTER (plugin, "text/uri-list", GST_RANK_MARGINAL, uri_type_find,
5638 "ram", URI_CAPS, NULL, NULL);
5639 TYPE_FIND_REGISTER (plugin, "application/x-hls", GST_RANK_MARGINAL,
5640 hls_type_find, "m3u8", HLS_CAPS, NULL, NULL);
5641 TYPE_FIND_REGISTER (plugin, "application/sdp", GST_RANK_SECONDARY,
5642 sdp_type_find, "sdp", SDP_CAPS, NULL, NULL);
5643 TYPE_FIND_REGISTER (plugin, "application/smil", GST_RANK_SECONDARY,
5644 smil_type_find, "smil", SMIL_CAPS, NULL, NULL);
5645 TYPE_FIND_REGISTER (plugin, "application/ttml+xml", GST_RANK_SECONDARY,
5646 ttml_xml_type_find, "ttml+xml", TTML_XML_CAPS, NULL, NULL);
5647 TYPE_FIND_REGISTER (plugin, "application/xml", GST_RANK_MARGINAL,
5648 xml_type_find, "xml", GENERIC_XML_CAPS, NULL, NULL);
5649 TYPE_FIND_REGISTER_RIFF (plugin, "audio/x-wav", GST_RANK_PRIMARY, "wav",
5651 TYPE_FIND_REGISTER (plugin, "audio/x-aiff", GST_RANK_SECONDARY,
5652 aiff_type_find, "aiff,aif,aifc", AIFF_CAPS, NULL, NULL);
5653 TYPE_FIND_REGISTER (plugin, "audio/x-svx", GST_RANK_SECONDARY, svx_type_find,
5654 "iff,svx", SVX_CAPS, NULL, NULL);
5655 TYPE_FIND_REGISTER (plugin, "audio/x-paris", GST_RANK_SECONDARY,
5656 paris_type_find, "paf", PARIS_CAPS, NULL, NULL);
5657 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nist", GST_RANK_SECONDARY,
5658 "nist", "NIST", 4, GST_TYPE_FIND_MAXIMUM);
5659 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-voc", GST_RANK_SECONDARY,
5660 "voc", "Creative", 8, GST_TYPE_FIND_MAXIMUM);
5661 TYPE_FIND_REGISTER (plugin, "audio/x-sds", GST_RANK_SECONDARY, sds_type_find,
5662 "sds", SDS_CAPS, NULL, NULL);
5663 TYPE_FIND_REGISTER (plugin, "audio/x-ircam", GST_RANK_SECONDARY,
5664 ircam_type_find, "sf", IRCAM_CAPS, NULL, NULL);
5665 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-w64", GST_RANK_SECONDARY,
5666 "w64", "riff", 4, GST_TYPE_FIND_MAXIMUM);
5667 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-rf64", GST_RANK_PRIMARY,
5668 "rf64", "RF64", 4, GST_TYPE_FIND_MAXIMUM);
5669 TYPE_FIND_REGISTER (plugin, "audio/x-shorten", GST_RANK_SECONDARY,
5670 shn_type_find, "shn", SHN_CAPS, NULL, NULL);
5671 TYPE_FIND_REGISTER (plugin, "application/x-ape", GST_RANK_SECONDARY,
5672 ape_type_find, "ape", APE_CAPS, NULL, NULL);
5673 TYPE_FIND_REGISTER (plugin, "image/jpeg", GST_RANK_PRIMARY + 15,
5674 jpeg_type_find, "jpg,jpe,jpeg", JPEG_CAPS, NULL, NULL);
5675 TYPE_FIND_REGISTER_START_WITH (plugin, "image/gif", GST_RANK_PRIMARY, "gif",
5676 "GIF8", 4, GST_TYPE_FIND_MAXIMUM);
5677 TYPE_FIND_REGISTER_START_WITH (plugin, "image/png", GST_RANK_PRIMARY + 14,
5678 "png", "\211PNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
5679 TYPE_FIND_REGISTER (plugin, "image/bmp", GST_RANK_PRIMARY, bmp_type_find,
5680 "bmp", BMP_CAPS, NULL, NULL);
5681 TYPE_FIND_REGISTER (plugin, "image/tiff", GST_RANK_PRIMARY, tiff_type_find,
5682 "tif,tiff", TIFF_CAPS, NULL, NULL);
5683 TYPE_FIND_REGISTER_RIFF (plugin, "image/webp", GST_RANK_PRIMARY,
5685 TYPE_FIND_REGISTER (plugin, "image/x-exr", GST_RANK_PRIMARY, exr_type_find,
5686 "exr", EXR_CAPS, NULL, NULL);
5687 TYPE_FIND_REGISTER (plugin, "image/x-portable-pixmap", GST_RANK_SECONDARY,
5688 pnm_type_find, "pnm,ppm,pgm,pbm", PNM_CAPS, NULL, NULL);
5689 TYPE_FIND_REGISTER (plugin, "video/x-matroska", GST_RANK_PRIMARY,
5690 matroska_type_find, "mkv,mka,mk3d,webm", MATROSKA_CAPS, NULL, NULL);
5691 TYPE_FIND_REGISTER (plugin, "application/mxf", GST_RANK_PRIMARY,
5692 mxf_type_find, "mxf", MXF_CAPS, NULL, NULL);
5693 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mve", GST_RANK_SECONDARY,
5694 "mve", "Interplay MVE File\032\000\032\000\000\001\063\021", 26,
5695 GST_TYPE_FIND_MAXIMUM);
5696 TYPE_FIND_REGISTER (plugin, "video/x-dv", GST_RANK_SECONDARY, dv_type_find,
5697 "dv,dif", DV_CAPS, NULL, NULL);
5698 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-nb-sh", GST_RANK_PRIMARY,
5699 "amr", "#!AMR", 5, GST_TYPE_FIND_LIKELY);
5700 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-wb-sh", GST_RANK_PRIMARY,
5701 "amr", "#!AMR-WB", 7, GST_TYPE_FIND_MAXIMUM);
5702 TYPE_FIND_REGISTER (plugin, "audio/iLBC-sh", GST_RANK_PRIMARY, ilbc_type_find,
5703 "ilbc", ILBC_CAPS, NULL, NULL);
5704 TYPE_FIND_REGISTER (plugin, "audio/x-sbc", GST_RANK_MARGINAL, sbc_type_find,
5705 "sbc", SBC_CAPS, NULL, NULL);
5706 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sid", GST_RANK_MARGINAL,
5707 "sid", "PSID", 4, GST_TYPE_FIND_MAXIMUM);
5708 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xcf", GST_RANK_SECONDARY,
5709 "xcf", "gimp xcf", 8, GST_TYPE_FIND_MAXIMUM);
5710 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mng", GST_RANK_SECONDARY,
5711 "mng", "\212MNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
5712 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-jng", GST_RANK_SECONDARY,
5713 "jng", "\213JNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
5714 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xpixmap", GST_RANK_SECONDARY,
5715 "xpm", "/* XPM */", 9, GST_TYPE_FIND_MAXIMUM);
5716 TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-sun-raster",
5717 GST_RANK_SECONDARY, "ras", "\131\246\152\225", 4, GST_TYPE_FIND_MAXIMUM);
5718 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-bzip",
5719 GST_RANK_SECONDARY, "bz2", "BZh", 3, GST_TYPE_FIND_LIKELY);
5720 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-gzip",
5721 GST_RANK_SECONDARY, "gz", "\037\213", 2, GST_TYPE_FIND_LIKELY);
5722 TYPE_FIND_REGISTER_START_WITH (plugin, "application/zip", GST_RANK_SECONDARY,
5723 "zip", "PK\003\004", 4, GST_TYPE_FIND_LIKELY);
5724 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-compress",
5725 GST_RANK_SECONDARY, "Z", "\037\235", 2, GST_TYPE_FIND_LIKELY);
5726 TYPE_FIND_REGISTER (plugin, "subtitle/x-kate", GST_RANK_MARGINAL,
5727 kate_type_find, NULL, NULL, NULL, NULL);
5728 TYPE_FIND_REGISTER (plugin, "audio/x-flac", GST_RANK_PRIMARY, flac_type_find,
5729 "flac", FLAC_CAPS, NULL, NULL);
5730 TYPE_FIND_REGISTER (plugin, "audio/x-vorbis", GST_RANK_PRIMARY,
5731 vorbis_type_find, NULL, VORBIS_CAPS, NULL, NULL);
5732 TYPE_FIND_REGISTER (plugin, "video/x-theora", GST_RANK_PRIMARY,
5733 theora_type_find, NULL, THEORA_CAPS, NULL, NULL);
5734 TYPE_FIND_REGISTER (plugin, "application/x-ogm-video", GST_RANK_PRIMARY,
5735 ogmvideo_type_find, NULL, OGMVIDEO_CAPS, NULL, NULL);
5736 TYPE_FIND_REGISTER (plugin, "application/x-ogm-audio", GST_RANK_PRIMARY,
5737 ogmaudio_type_find, NULL, OGMAUDIO_CAPS, NULL, NULL);
5738 TYPE_FIND_REGISTER (plugin, "application/x-ogm-text", GST_RANK_PRIMARY,
5739 ogmtext_type_find, NULL, OGMTEXT_CAPS, NULL, NULL);
5740 TYPE_FIND_REGISTER (plugin, "audio/x-speex", GST_RANK_PRIMARY,
5741 speex_type_find, NULL, SPEEX_CAPS, NULL, NULL);
5742 TYPE_FIND_REGISTER (plugin, "audio/x-celt", GST_RANK_PRIMARY, celt_type_find,
5743 NULL, CELT_CAPS, NULL, NULL);
5744 TYPE_FIND_REGISTER (plugin, "application/x-ogg-skeleton", GST_RANK_PRIMARY,
5745 oggskel_type_find, NULL, OGG_SKELETON_CAPS, NULL, NULL);
5746 TYPE_FIND_REGISTER (plugin, "text/x-cmml", GST_RANK_PRIMARY, cmml_type_find,
5747 NULL, CMML_CAPS, NULL, NULL);
5748 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-executable",
5749 GST_RANK_MARGINAL, NULL, "\177ELF", 4, GST_TYPE_FIND_MAXIMUM);
5750 TYPE_FIND_REGISTER (plugin, "audio/aac", GST_RANK_SECONDARY, aac_type_find,
5751 "aac,adts,adif,loas", AAC_CAPS, NULL, NULL);
5752 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-spc", GST_RANK_SECONDARY,
5753 "spc", "SNES-SPC700 Sound File Data", 27, GST_TYPE_FIND_MAXIMUM);
5754 TYPE_FIND_REGISTER (plugin, "audio/x-wavpack", GST_RANK_SECONDARY,
5755 wavpack_type_find, "wv,wvp", WAVPACK_CAPS, NULL, NULL);
5756 TYPE_FIND_REGISTER (plugin, "audio/x-wavpack-correction", GST_RANK_SECONDARY,
5757 wavpack_type_find, "wvc", WAVPACK_CORRECTION_CAPS, NULL, NULL);
5758 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-caf", GST_RANK_SECONDARY,
5759 "caf", "caff\000\001", 6, GST_TYPE_FIND_MAXIMUM);
5760 TYPE_FIND_REGISTER (plugin, "application/postscript", GST_RANK_SECONDARY,
5761 postscript_type_find, "ps", POSTSCRIPT_CAPS, NULL, NULL);
5762 TYPE_FIND_REGISTER (plugin, "image/svg+xml", GST_RANK_SECONDARY,
5763 svg_type_find, "svg", SVG_CAPS, NULL, NULL);
5764 TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-rar",
5765 GST_RANK_SECONDARY, "rar", "Rar!", 4, GST_TYPE_FIND_LIKELY);
5766 TYPE_FIND_REGISTER (plugin, "application/x-tar", GST_RANK_SECONDARY,
5767 tar_type_find, "tar", TAR_CAPS, NULL, NULL);
5768 TYPE_FIND_REGISTER (plugin, "application/x-ar", GST_RANK_SECONDARY,
5769 ar_type_find, "a", AR_CAPS, NULL, NULL);
5770 TYPE_FIND_REGISTER (plugin, "application/x-ms-dos-executable",
5771 GST_RANK_SECONDARY, msdos_type_find, "dll,exe,ocx,sys,scr,msstyles,cpl",
5772 MSDOS_CAPS, NULL, NULL);
5773 TYPE_FIND_REGISTER (plugin, "video/x-dirac", GST_RANK_PRIMARY,
5774 dirac_type_find, NULL, DIRAC_CAPS, NULL, NULL);
5775 TYPE_FIND_REGISTER (plugin, "multipart/x-mixed-replace", GST_RANK_SECONDARY,
5776 multipart_type_find, NULL, MULTIPART_CAPS, NULL, NULL);
5777 TYPE_FIND_REGISTER (plugin, "application/x-mmsh", GST_RANK_SECONDARY,
5778 mmsh_type_find, NULL, MMSH_CAPS, NULL, NULL);
5779 TYPE_FIND_REGISTER (plugin, "video/vivo", GST_RANK_SECONDARY, vivo_type_find,
5780 "viv", VIVO_CAPS, NULL, NULL);
5781 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nsf", GST_RANK_SECONDARY,
5782 "nsf", "NESM\x1a", 5, GST_TYPE_FIND_MAXIMUM);
5783 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gym", GST_RANK_SECONDARY,
5784 "gym", "GYMX", 4, GST_TYPE_FIND_MAXIMUM);
5785 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-ay", GST_RANK_SECONDARY, "ay",
5786 "ZXAYEMUL", 8, GST_TYPE_FIND_MAXIMUM);
5787 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gbs", GST_RANK_SECONDARY,
5788 "gbs", "GBS\x01", 4, GST_TYPE_FIND_MAXIMUM);
5789 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-vgm", GST_RANK_SECONDARY,
5790 "vgm", "Vgm\x20", 4, GST_TYPE_FIND_MAXIMUM);
5791 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sap", GST_RANK_SECONDARY,
5792 "sap", "SAP\x0d\x0a" "AUTHOR\x20", 12, GST_TYPE_FIND_MAXIMUM);
5793 TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ivf", GST_RANK_SECONDARY,
5794 "ivf", "DKIF", 4, GST_TYPE_FIND_NEARLY_CERTAIN);
5795 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-kss", GST_RANK_SECONDARY,
5796 "kss", "KSSX\0", 5, GST_TYPE_FIND_MAXIMUM);
5797 TYPE_FIND_REGISTER_START_WITH (plugin, "application/pdf", GST_RANK_SECONDARY,
5798 "pdf", "%PDF-", 5, GST_TYPE_FIND_LIKELY);
5799 TYPE_FIND_REGISTER_START_WITH (plugin, "application/msword",
5800 GST_RANK_SECONDARY, "doc", "\320\317\021\340\241\261\032\341", 8,
5801 GST_TYPE_FIND_LIKELY);
5802 /* Mac OS X .DS_Store files tend to be taken for video/mpeg */
5803 TYPE_FIND_REGISTER_START_WITH (plugin, "application/octet-stream",
5804 GST_RANK_SECONDARY, "DS_Store", "\000\000\000\001Bud1", 8,
5805 GST_TYPE_FIND_LIKELY);
5806 TYPE_FIND_REGISTER_START_WITH (plugin, "image/vnd.adobe.photoshop",
5807 GST_RANK_SECONDARY, "psd", "8BPS\000\001\000\000\000\000", 10,
5808 GST_TYPE_FIND_LIKELY);
5809 TYPE_FIND_REGISTER (plugin, "image/vnd.wap.wbmp", GST_RANK_MARGINAL,
5810 wbmp_typefind, NULL, NULL, NULL, NULL);
5811 TYPE_FIND_REGISTER (plugin, "application/x-yuv4mpeg", GST_RANK_SECONDARY,
5812 y4m_typefind, NULL, NULL, NULL, NULL);
5813 TYPE_FIND_REGISTER (plugin, "image/x-icon", GST_RANK_MARGINAL,
5814 windows_icon_typefind, NULL, NULL, NULL, NULL);
5817 TYPE_FIND_REGISTER (plugin, "xdgmime-base", GST_RANK_MARGINAL,
5818 xdgmime_typefind, NULL, NULL, NULL, NULL);
5821 TYPE_FIND_REGISTER (plugin, "image/x-degas", GST_RANK_MARGINAL,
5822 degas_type_find, NULL, NULL, NULL, NULL);
5823 TYPE_FIND_REGISTER (plugin, "application/octet-stream", GST_RANK_MARGINAL,
5824 dvdiso_type_find, NULL, NULL, NULL, NULL);
5826 TYPE_FIND_REGISTER (plugin, "application/x-ssa", GST_RANK_SECONDARY,
5827 ssa_type_find, "ssa,ass", NULL, NULL, NULL);
5829 TYPE_FIND_REGISTER (plugin, "video/x-pva", GST_RANK_SECONDARY,
5830 pva_type_find, "pva", PVA_CAPS, NULL, NULL);
5832 TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-xi", GST_RANK_SECONDARY,
5833 "xi", "Extended Instrument: ", 21, GST_TYPE_FIND_MAXIMUM);
5835 TYPE_FIND_REGISTER (plugin, "audio/audible", GST_RANK_MARGINAL,
5836 aa_type_find, "aa,aax", AA_CAPS, NULL, NULL);
5841 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
5844 "default typefind functions",
5845 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)