typefindfunctions: Add typefinder for TTML+XML
[platform/upstream/gstreamer.git] / gst / typefind / gsttypefindfunctions.c
1 /* GStreamer
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>
5  *
6  * gsttypefindfunctions.c: collection of various typefind functions
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <glib.h>
29 #include <glib/gprintf.h>
30
31 /* don't want to add gio xdgmime typefinder if gio was disabled via configure */
32 #ifdef HAVE_GIO
33 #include <gio/gio.h>
34 #define USE_GIO
35 #endif
36
37 #include <gst/gst.h>
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <ctype.h>
42
43 #include <gst/pbutils/pbutils.h>
44 #include <gst/base/gstbytereader.h>
45
46 GST_DEBUG_CATEGORY_STATIC (type_find_debug);
47 #define GST_CAT_DEFAULT type_find_debug
48
49 /* DataScanCtx: helper for typefind functions that scan through data
50  * step-by-step, to avoid doing a peek at each and every offset */
51
52 #define DATA_SCAN_CTX_CHUNK_SIZE 4096
53
54 typedef struct
55 {
56   guint64 offset;
57   const guint8 *data;
58   gint size;
59 } DataScanCtx;
60
61 static inline void
62 data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip)
63 {
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;
68   } else {
69     c->data += c->size;
70     c->size = 0;
71   }
72 }
73
74 static inline gboolean
75 data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, gint min_len)
76 {
77   const guint8 *data;
78   guint64 len;
79   guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
80
81   if (G_LIKELY (c->size >= min_len))
82     return TRUE;
83
84   data = gst_type_find_peek (tf, c->offset, chunk_len);
85   if (G_LIKELY (data != NULL)) {
86     c->data = data;
87     c->size = chunk_len;
88     return TRUE;
89   }
90
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);
95   if (len > 0) {
96     len = CLAMP (len - c->offset, min_len, chunk_len);
97   } else {
98     len = min_len;
99   }
100
101   data = gst_type_find_peek (tf, c->offset, len);
102   if (data != NULL) {
103     c->data = data;
104     c->size = len;
105     return TRUE;
106   }
107
108   return FALSE;
109 }
110
111 static inline gboolean
112 data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset,
113     const gchar * data, guint len)
114 {
115   if (!data_scan_ctx_ensure_data (tf, c, offset + len))
116     return FALSE;
117
118   return (memcmp (c->data + offset, data, len) == 0);
119 }
120
121 /*** text/plain ***/
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);
125
126 static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
127
128 #define UTF8_CAPS gst_static_caps_get(&utf8_caps)
129
130 static gboolean
131 utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
132     GstTypeFindProbability * prob)
133 {
134   const guint8 *data;
135
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
141                                  * iteration */
142
143   while (probability > step && size > min_size) {
144     data = gst_type_find_peek (tf, offset, size);
145     if (data) {
146       gchar *end;
147       gchar *start = (gchar *) data;
148
149       if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) {   /* allow last char to be cut off */
150         *prob = probability;
151         return TRUE;
152       }
153       *prob = 0;
154       return FALSE;
155     }
156     size /= 2;
157     probability -= step;
158   }
159   *prob = 0;
160   return FALSE;
161 }
162
163 static void
164 utf8_type_find (GstTypeFind * tf, gpointer unused)
165 {
166   GstTypeFindProbability start_prob, mid_prob;
167   guint64 length;
168
169   /* leave xml to the xml typefinders */
170   if (xml_check_first_element (tf, "", 0, TRUE))
171     return;
172
173   /* leave sdp to the sdp typefinders */
174   if (sdp_check_header (tf))
175     return;
176
177   /* check beginning of stream */
178   if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob))
179     return;
180
181   GST_LOG ("start is plain text with probability of %u", start_prob);
182
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 */
185
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),
189         UTF8_CAPS);
190     return;
191   }
192
193   if (length < 64 * 1024) {
194     gst_type_find_suggest (tf, start_prob, UTF8_CAPS);
195     return;
196   }
197
198   /* check middle of stream */
199   if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob))
200     return;
201
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);
204 }
205
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. */
209
210 typedef struct
211 {
212   size_t bomlen;
213   const char *const bom;
214     gboolean (*checker) (const guint8 *, gint, gint);
215   int boost;
216   int endianness;
217 } GstUnicodeTester;
218
219 static gboolean
220 check_utf16 (const guint8 * data, gint len, gint endianness)
221 {
222   GstByteReader br;
223   guint16 high, low;
224
225   low = high = 0;
226
227   if (len & 1)
228     return FALSE;
229
230   gst_byte_reader_init (&br, data, len);
231   while (len >= 2) {
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);
235     else
236       high = gst_byte_reader_get_uint16_le_unchecked (&br);
237     if (high >= 0xD800 && high <= 0xDBFF) {
238       /* start of a surrogate pair */
239       if (len < 4)
240         return FALSE;
241       len -= 2;
242       if (endianness == G_BIG_ENDIAN)
243         low = gst_byte_reader_get_uint16_be_unchecked (&br);
244       else
245         low = gst_byte_reader_get_uint16_le_unchecked (&br);
246       if (low >= 0xDC00 && low <= 0xDFFF) {
247         /* second half of the surrogate pair */
248       } else
249         return FALSE;
250     } else {
251       if (high >= 0xDC00 && high <= 0xDFFF)
252         return FALSE;
253     }
254     len -= 2;
255   }
256   return TRUE;
257 }
258
259 static gboolean
260 check_utf32 (const guint8 * data, gint len, gint endianness)
261 {
262   if (len & 3)
263     return FALSE;
264   while (len > 3) {
265     guint32 v;
266     if (endianness == G_BIG_ENDIAN)
267       v = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
268     else
269       v = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
270     if (v >= 0x10FFFF)
271       return FALSE;
272     data += 4;
273     len -= 4;
274   }
275   return TRUE;
276 }
277
278 static void
279 unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester,
280     guint n_tester, const char *media_type, gboolean require_bom)
281 {
282   size_t n;
283   gint len = 4;
284   const guint8 *data = gst_type_find_peek (tf, 0, len);
285   int prob = -1;
286   const gint max_scan_size = 256 * 1024;
287   int endianness = 0;
288
289   if (!data) {
290     len = 2;
291     data = gst_type_find_peek (tf, 0, len);
292     if (!data)
293       return;
294   }
295
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);
300     if (!newdata)
301       break;
302     len = newlen;
303     data = newdata;
304   }
305
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;
311     }
312     if (require_bom && bom_boost == 0)
313       continue;
314     if (!(*tester[n].checker) (data, len, tester[n].endianness))
315       continue;
316     tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost;
317     if (tmpprob > prob) {
318       prob = tmpprob;
319       endianness = tester[n].endianness;
320     }
321   }
322
323   if (prob > 0) {
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);
328   }
329 }
330
331 static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16");
332
333 #define UTF16_CAPS gst_static_caps_get(&utf16_caps)
334
335 static void
336 utf16_type_find (GstTypeFind * tf, gpointer unused)
337 {
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},
341   };
342   unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester),
343       "text/utf-16", TRUE);
344 }
345
346 static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32");
347
348 #define UTF32_CAPS gst_static_caps_get(&utf32_caps)
349
350 static void
351 utf32_type_find (GstTypeFind * tf, gpointer unused)
352 {
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}
356   };
357   unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester),
358       "text/utf-32", TRUE);
359 }
360
361 /*** text/uri-list ***/
362
363 static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
364
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 {                                                    \
368   pos++;                                                                \
369   if (pos == BUFFER_SIZE) {                                             \
370     pos = 0;                                                            \
371     offset += BUFFER_SIZE;                                              \
372     data = gst_type_find_peek (tf, offset, BUFFER_SIZE);                \
373     if (data == NULL) return;                                           \
374   } else {                                                              \
375     data++;                                                             \
376   }                                                                     \
377 }
378 static void
379 uri_type_find (GstTypeFind * tf, gpointer unused)
380 {
381   const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
382   guint pos = 0;
383   guint offset = 0;
384
385   if (data) {
386     /* Search for # comment lines */
387     while (*data == '#') {
388       /* Goto end of line */
389       while (*data != '\n') {
390         INC_BUFFER;
391       }
392
393       INC_BUFFER;
394     }
395
396     if (!g_ascii_isalpha (*data)) {
397       /* Had a non alpha char - can't be uri-list */
398       return;
399     }
400
401     INC_BUFFER;
402
403     while (g_ascii_isalnum (*data)) {
404       INC_BUFFER;
405     }
406
407     if (*data != ':') {
408       /* First non alpha char is not a : */
409       return;
410     }
411
412     /* Get the next 2 bytes as well */
413     data = gst_type_find_peek (tf, offset + pos, 3);
414     if (data == NULL)
415       return;
416
417     if (data[1] != '/' && data[2] != '/') {
418       return;
419     }
420
421     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, URI_CAPS);
422   }
423 }
424
425 /*** application/x-hls ***/
426
427 static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls");
428 #define HLS_CAPS (gst_static_caps_get(&hls_caps))
429
430 /* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */
431 static void
432 hls_type_find (GstTypeFind * tf, gpointer unused)
433 {
434   DataScanCtx c = { 0, NULL, 0 };
435
436   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 7)))
437     return;
438
439   if (memcmp (c.data, "#EXTM3U", 7))
440     return;
441
442   data_scan_ctx_advance (tf, &c, 7);
443
444   /* Check only the first 256 bytes */
445   while (c.offset < 256) {
446     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21)))
447       return;
448
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);
454       return;
455     }
456
457     data_scan_ctx_advance (tf, &c, 1);
458   }
459 }
460
461
462 /*** application/xml **********************************************************/
463
464 #define XML_BUFFER_SIZE 16
465 #define XML_INC_BUFFER {                                                \
466   pos++;                                                                \
467   if (pos == XML_BUFFER_SIZE) {                                         \
468     pos = 0;                                                            \
469     offset += XML_BUFFER_SIZE;                                          \
470     data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE);            \
471     if (data == NULL) return FALSE;                                     \
472   } else {                                                              \
473     data++;                                                             \
474   }                                                                     \
475 }
476
477 #define XML_INC_BUFFER_DATA {                                           \
478   pos++;                                                                \
479   if (pos >= length) {                                                  \
480     return FALSE;                                                       \
481   } else {                                                              \
482     data++;                                                             \
483   }                                                                     \
484 }
485
486 static gboolean
487 xml_check_first_element_from_data (const guint8 * data, guint length,
488     const gchar * element, guint elen, gboolean strict)
489 {
490   gboolean got_xmldec;
491   guint pos = 0;
492
493   g_return_val_if_fail (data != NULL, FALSE);
494
495   if (length <= 5)
496     return FALSE;
497
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);
502
503   if (strict && !got_xmldec)
504     return FALSE;
505
506   /* skip XMLDec in any case if we've got one */
507   if (got_xmldec) {
508     pos += 5;
509     data += 5;
510   }
511
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)) {
516       XML_INC_BUFFER_DATA;
517     }
518
519     XML_INC_BUFFER_DATA;
520     if (!g_ascii_isalpha (*data)) {
521       /* if not alphabetic, it's a PI or an element / attribute declaration
522        * like <?xxx or <!xxx */
523       XML_INC_BUFFER_DATA;
524       continue;
525     }
526
527     /* the first normal element, check if it's the one asked for */
528     if (pos + elen + 1 >= length)
529       return FALSE;
530     return (element && strncmp ((const char *) data, element, elen) == 0);
531   }
532
533   return FALSE;
534 }
535
536 static gboolean
537 xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
538     gboolean strict)
539 {
540   gboolean got_xmldec;
541   const guint8 *data;
542   guint offset = 0;
543   guint pos = 0;
544
545   data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
546   if (!data)
547     return FALSE;
548
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);
553
554   if (strict && !got_xmldec)
555     return FALSE;
556
557   /* skip XMLDec in any case if we've got one */
558   if (got_xmldec) {
559     pos += 5;
560     data += 5;
561   }
562
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) {
567       XML_INC_BUFFER;
568     }
569
570     XML_INC_BUFFER;
571     if (!g_ascii_isalpha (*data)) {
572       /* if not alphabetic, it's a PI or an element / attribute declaration
573        * like <?xxx or <!xxx */
574       XML_INC_BUFFER;
575       continue;
576     }
577
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);
581   }
582
583   return FALSE;
584 }
585
586 static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml");
587
588 #define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps))
589 static void
590 xml_type_find (GstTypeFind * tf, gpointer unused)
591 {
592   if (xml_check_first_element (tf, "", 0, TRUE)) {
593     gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
594   }
595 }
596
597 /*** application/dash+xml ****************************************************/
598
599 static GstStaticCaps dash_caps = GST_STATIC_CAPS ("application/dash+xml");
600
601 #define DASH_CAPS gst_static_caps_get (&dash_caps)
602
603 static void
604 dash_mpd_type_find (GstTypeFind * tf, gpointer unused)
605 {
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);
609   }
610 }
611
612 /*** application/sdp *********************************************************/
613
614 static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp");
615
616 #define SDP_CAPS (gst_static_caps_get(&sdp_caps))
617 static gboolean
618 sdp_check_header (GstTypeFind * tf)
619 {
620   const guint8 *data;
621
622   data = gst_type_find_peek (tf, 0, 5);
623   if (!data)
624     return FALSE;
625
626   /* sdp must start with v=0[\r]\n */
627   if (memcmp (data, "v=0", 3))
628     return FALSE;
629
630   if (data[3] == '\r' && data[4] == '\n')
631     return TRUE;
632   if (data[3] == '\n')
633     return TRUE;
634
635   return FALSE;
636 }
637
638 static void
639 sdp_type_find (GstTypeFind * tf, gpointer unused)
640 {
641   if (sdp_check_header (tf))
642     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS);
643 }
644
645 /*** application/smil *********************************************************/
646
647 static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil");
648
649 #define SMIL_CAPS (gst_static_caps_get(&smil_caps))
650 static void
651 smil_type_find (GstTypeFind * tf, gpointer unused)
652 {
653   if (xml_check_first_element (tf, "smil", 4, FALSE)) {
654     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
655   }
656 }
657
658 /*** application/ttml+xml *****************************************************/
659
660 static GstStaticCaps ttml_xml_caps = GST_STATIC_CAPS ("application/ttml+xml");
661
662 #define TTML_XML_CAPS (gst_static_caps_get(&ttml_xml_caps))
663 static void
664 ttml_xml_type_find (GstTypeFind * tf, gpointer unused)
665 {
666   if (xml_check_first_element (tf, "tt", 2, FALSE)) {
667     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTML_XML_CAPS);
668   }
669 }
670
671 /*** text/html ***/
672
673 static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html");
674
675 #define HTML_CAPS gst_static_caps_get (&html_caps)
676
677 static void
678 html_type_find (GstTypeFind * tf, gpointer unused)
679 {
680   const gchar *d, *data;
681
682   data = (const gchar *) gst_type_find_peek (tf, 0, 16);
683   if (!data)
684     return;
685
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);
694     }
695   }
696 }
697
698 /*** audio/midi ***/
699
700 static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi");
701
702 #define MID_CAPS gst_static_caps_get(&mid_caps)
703 static void
704 mid_type_find (GstTypeFind * tf, gpointer unused)
705 {
706   const guint8 *data = gst_type_find_peek (tf, 0, 4);
707
708   /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
709   if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h'
710       && data[3] == 'd')
711     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS);
712 }
713
714 /*** audio/mobile-xmf ***/
715
716 static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf");
717
718 #define MXMF_CAPS gst_static_caps_get(&mxmf_caps)
719 static void
720 mxmf_type_find (GstTypeFind * tf, gpointer unused)
721 {
722   const guint8 *data = NULL;
723
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'
727       && data[3] == '_') {
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'
731         && data[3] == '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);
736       }
737     }
738   }
739 }
740
741
742 /*** video/x-fli ***/
743
744 static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
745
746 #define FLX_CAPS gst_static_caps_get(&flx_caps)
747 static void
748 flx_type_find (GstTypeFind * tf, gpointer unused)
749 {
750   const guint8 *data = gst_type_find_peek (tf, 0, 134);
751
752   if (data) {
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) &&
756         data[5] == 0xaf &&
757         ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
758       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS);
759     }
760     return;
761   }
762   data = gst_type_find_peek (tf, 0, 6);
763   if (data) {
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);
768     }
769     return;
770   }
771 }
772
773 /*** application/x-id3 ***/
774
775 static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
776
777 #define ID3_CAPS gst_static_caps_get(&id3_caps)
778 static void
779 id3v2_type_find (GstTypeFind * tf, gpointer unused)
780 {
781   const guint8 *data = gst_type_find_peek (tf, 0, 10);
782
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);
788   }
789 }
790
791 static void
792 id3v1_type_find (GstTypeFind * tf, gpointer unused)
793 {
794   const guint8 *data = gst_type_find_peek (tf, -128, 3);
795
796   if (data && memcmp (data, "TAG", 3) == 0) {
797     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
798   }
799 }
800
801 /*** application/x-ape ***/
802
803 static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag");
804
805 #define APETAG_CAPS gst_static_caps_get(&apetag_caps)
806 static void
807 apetag_type_find (GstTypeFind * tf, gpointer unused)
808 {
809   const guint8 *data;
810
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);
815     return;
816   }
817
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);
822     return;
823   }
824 }
825
826 /*** audio/x-ttafile ***/
827
828 static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
829
830 #define TTA_CAPS gst_static_caps_get(&tta_caps)
831 static void
832 tta_type_find (GstTypeFind * tf, gpointer unused)
833 {
834   const guint8 *data = gst_type_find_peek (tf, 0, 3);
835
836   if (data) {
837     if (memcmp (data, "TTA", 3) == 0) {
838       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS);
839       return;
840     }
841   }
842 }
843
844 /*** audio/x-flac ***/
845 static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");
846
847 #define FLAC_CAPS (gst_static_caps_get(&flac_caps))
848
849 static void
850 flac_type_find (GstTypeFind * tf, gpointer unused)
851 {
852   DataScanCtx c = { 0, NULL, 0 };
853
854   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
855     return;
856
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);
861     return;
862   }
863
864   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
865     return;
866
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);
870     return;
871   }
872
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 */
875 #if 0
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)))
880       break;
881
882     /* look for frame header,
883      * http://flac.sourceforge.net/format.html#frame_header
884      */
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)
888         goto advance;
889
890       /* blocksize must be != 0x00 */
891       if ((c.data[2] >> 4) == 0x00)
892         goto advance;
893
894       /* samplerate must be != 0x0f */
895       if ((c.data[2] & 0x0f) == 0x0f)
896         goto advance;
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)
900         goto advance;
901
902       /* channel assignment must be < 11 */
903       if ((c.data[3] >> 4) >= 11)
904         goto advance;
905
906       /* sample size must be != 0x07 and != 0x05 */
907       if (((c.data[3] >> 1) & 0x07) == 0x07)
908         goto advance;
909       if (((c.data[3] >> 1) & 0x07) == 0x05)
910         goto advance;
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)
914         goto advance;
915
916       /* next bit must be 0 */
917       if ((c.data[3] & 0x01) == 0x01)
918         goto advance;
919
920       /* FIXME: shouldn't we include the crc check ? */
921
922       GST_DEBUG ("Found flac without headers at %d", (gint) c.offset);
923       gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS);
924       return;
925     }
926   advance:
927     data_scan_ctx_advance (tf, &c, 1);
928   }
929 #endif
930 }
931
932 /* TODO: we could probably make a generic function for this.. */
933 static gint
934 aac_type_find_scan_loas_frames_ep (GstTypeFind * tf, DataScanCtx * scan_ctx,
935     gint max_frames)
936 {
937   DataScanCtx c = *scan_ctx;
938   guint16 snc;
939   guint len;
940   gint count = 0;
941
942   do {
943     if (!data_scan_ctx_ensure_data (tf, &c, 5))
944       break;
945
946     /* EPAudioSyncStream */
947     len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
948         ((c.data[4] & 0x80) >> 7);
949
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);
952       break;
953     }
954
955     /* check length of frame  */
956     snc = GST_READ_UINT16_BE (c.data + len);
957     if (snc != 0x4de1) {
958       GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
959       break;
960     }
961
962     ++count;
963
964     GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
965         "framelen %u", count, c.offset, len);
966
967     data_scan_ctx_advance (tf, &c, len);
968   } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
969
970   GST_DEBUG ("found %d consecutive frames", count);
971   return count;
972 }
973
974 static gint
975 aac_type_find_scan_loas_frames (GstTypeFind * tf, DataScanCtx * scan_ctx,
976     gint max_frames)
977 {
978   DataScanCtx c = *scan_ctx;
979   guint16 snc;
980   guint len;
981   gint count = 0;
982
983   do {
984     if (!data_scan_ctx_ensure_data (tf, &c, 3))
985       break;
986
987     /* AudioSyncStream */
988     len = ((c.data[1] & 0x1f) << 8) | c.data[2];
989     /* add size of sync stream header */
990     len += 3;
991
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);
994       break;
995     }
996
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);
1001       break;
1002     }
1003
1004     ++count;
1005
1006     GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
1007         "framelen %u", count, c.offset, len);
1008
1009     data_scan_ctx_advance (tf, &c, len);
1010   } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
1011
1012   GST_DEBUG ("found %d consecutive frames", count);
1013   return count;
1014 }
1015
1016 /*** audio/mpeg version 2, 4 ***/
1017
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)
1022 static void
1023 aac_type_find (GstTypeFind * tf, gpointer unused)
1024 {
1025   DataScanCtx c = { 0, NULL, 0 };
1026   GstTypeFindProbability best_probability = GST_TYPE_FIND_NONE;
1027   GstCaps *best_caps = NULL;
1028   guint best_count = 0;
1029
1030   while (c.offset < AAC_AMOUNT) {
1031     guint snc, len, offset, i;
1032
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
1038      * bytes.
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.
1041      */
1042     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
1043       break;
1044
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);
1052
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);
1055         goto next;
1056       }
1057
1058       offset = len;
1059       /* check if there's a second ADTS frame */
1060       snc = GST_READ_UINT16_BE (c.data + offset);
1061       if ((snc & 0xfff6) == 0xfff0) {
1062         GstCaps *caps;
1063         guint mpegversion, sample_freq_idx, channel_config, profile_idx, rate;
1064         guint8 audio_config[2];
1065
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);
1070
1071         GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%"
1072             G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len);
1073
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",
1078               sample_freq_idx);
1079           goto next;
1080         }
1081
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);
1084
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;
1093
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);
1098
1099         gst_codec_utils_aac_caps_set_level_and_profile (caps, audio_config, 2);
1100
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 };
1104
1105           gst_caps_set_simple (caps, "channels", G_TYPE_INT,
1106               channels_map[channel_config], "rate", G_TYPE_INT, rate, NULL);
1107         }
1108
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);
1112
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);
1116         } else {
1117           offset += len;
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",
1125                   len);
1126               break;
1127             }
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));
1132               offset += len;
1133             } else {
1134               break;
1135             }
1136           }
1137           gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 5 * (i - 3), caps);
1138
1139         }
1140         gst_caps_unref (caps);
1141         break;
1142       }
1143
1144       GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
1145     } else if (G_UNLIKELY ((snc & 0xffe0) == 0x56e0 || snc == 0x4de1)) {
1146       gint count;
1147
1148       /* LOAS frame */
1149       GST_INFO ("Possible LOAS syncword at offset 0x%" G_GINT64_MODIFIER
1150           "x, scanning for more frames...", c.offset);
1151
1152       if (snc == 0x4de1)
1153         count = aac_type_find_scan_loas_frames_ep (tf, &c, 20);
1154       else
1155         count = aac_type_find_scan_loas_frames (tf, &c, 20);
1156
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);
1163         best_count = count;
1164         best_probability = GST_TYPE_FIND_POSSIBLE - 10 + count * 3;
1165         if (best_probability >= GST_TYPE_FIND_LIKELY)
1166           break;
1167       }
1168     } else if (!memcmp (c.data, "ADIF", 4)) {
1169       /* ADIF header */
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);
1173       break;
1174     }
1175
1176   next:
1177
1178     data_scan_ctx_advance (tf, &c, 1);
1179   }
1180
1181   if (best_probability > GST_TYPE_FIND_NONE) {
1182     gst_type_find_suggest (tf, best_probability, best_caps);
1183     gst_caps_unref (best_caps);
1184   }
1185 }
1186
1187 /*** audio/mpeg version 1 ***/
1188
1189 /*
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
1195  * (bytes)      1       2       3       4
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.
1203  *
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.
1209  */
1210
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,}},
1218 };
1219
1220 static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
1221 {22050, 24000, 16000},
1222 {44100, 48000, 32000}
1223 };
1224
1225 static inline guint
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)
1229 {
1230   guint bitrate, layer, length, mode, samplerate, version, channels;
1231
1232   if ((header & 0xffe00000) != 0xffe00000)
1233     return 0;
1234
1235   /* we don't need extension, copyright, original or
1236    * emphasis for the frame length */
1237   header >>= 6;
1238
1239   /* mode */
1240   mode = header & 0x3;
1241   header >>= 3;
1242
1243   /* padding */
1244   length = header & 0x1;
1245   header >>= 1;
1246
1247   /* sampling frequency */
1248   samplerate = header & 0x3;
1249   if (samplerate == 3)
1250     return 0;
1251   header >>= 2;
1252
1253   /* bitrate index */
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;
1258   }
1259   if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1))
1260     return 0;
1261
1262   /* ignore error correction, too */
1263   header >>= 5;
1264
1265   /* layer */
1266   layer = 4 - (header & 0x3);
1267   if (layer == 4)
1268     return 0;
1269   header >>= 2;
1270
1271   /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */
1272   version = header & 0x3;
1273   if (version == 1)
1274     return 0;
1275
1276   /* lookup */
1277   channels = (mode == 3) ? 1 : 2;
1278   samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
1279   if (bitrate == 0) {
1280     /* possible freeform mp3 */
1281     if (layer == 1) {
1282       length *= 4;
1283       length += possible_free_framelen;
1284       bitrate = length * samplerate / 48000;
1285     } else {
1286       length += possible_free_framelen;
1287       bitrate = length * samplerate /
1288           ((layer == 3 && version != 3) ? 72000 : 144000);
1289     }
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])
1294       return 0;
1295   } else {
1296     /* calculating */
1297     bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
1298     if (layer == 1) {
1299       length = ((12000 * bitrate / samplerate) + length) * 4;
1300     } else {
1301       length += ((layer == 3
1302               && version != 3) ? 72000 : 144000) * bitrate / samplerate;
1303     }
1304   }
1305
1306   GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
1307   GST_LOG
1308       ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
1309       " - channels = %u", samplerate, bitrate, layer, version, channels);
1310
1311   if (put_layer)
1312     *put_layer = layer;
1313   if (put_channels)
1314     *put_channels = channels;
1315   if (put_bitrate)
1316     *put_bitrate = bitrate;
1317   if (put_samplerate)
1318     *put_samplerate = samplerate;
1319
1320   return length;
1321 }
1322
1323
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))
1327 /*
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)
1331  *        / TRY_SYNC)
1332  * if found_headers >= MIN_HEADERS
1333  */
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)
1339
1340 static void
1341 mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
1342     guint * found_layer, GstTypeFindProbability * found_prob)
1343 {
1344   const guint8 *data = NULL;
1345   const guint8 *data_end = NULL;
1346   guint size;
1347   guint64 skipped;
1348   gint last_free_offset = -1;
1349   gint last_free_framelen = -1;
1350   gboolean headerstart = TRUE;
1351
1352   *found_layer = 0;
1353   *found_prob = 0;
1354
1355   size = 0;
1356   skipped = 0;
1357   while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) {
1358     if (size <= 0) {
1359       size = GST_MP3_TYPEFIND_SYNC_SIZE * 2;
1360       do {
1361         size /= 2;
1362         data = gst_type_find_peek (tf, skipped + start_off, size);
1363       } while (size > 10 && !data);
1364       if (!data)
1365         break;
1366       data_end = data + size;
1367     }
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;
1374
1375       while (found < GST_MP3_TYPEFIND_TRY_HEADERS) {
1376         guint32 head;
1377         guint length;
1378         guint prev_layer = 0;
1379         guint prev_channels = 0, prev_samplerate = 0;
1380         gboolean free = FALSE;
1381
1382         if ((gint64) (offset - skipped + 4) >= 0 &&
1383             data + offset - skipped + 4 < data_end) {
1384           head_data = data + offset - skipped;
1385         } else {
1386           head_data = gst_type_find_peek (tf, offset + start_off, 4);
1387         }
1388         if (!head_data)
1389           break;
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))) {
1394           if (free) {
1395             if (last_free_offset == -1)
1396               last_free_offset = offset;
1397             else {
1398               last_free_framelen = offset - last_free_offset;
1399               offset = last_free_offset;
1400               continue;
1401             }
1402           } else {
1403             last_free_framelen = -1;
1404           }
1405
1406           /* Mark the fact that we didn't find a valid header at the beginning */
1407           if (found == 0)
1408             headerstart = FALSE;
1409
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");
1414           break;
1415         }
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
1423            * this header*/
1424           if (prev_layer)
1425             changed = TRUE;
1426           prev_layer = layer;
1427           prev_channels = channels;
1428           prev_samplerate = samplerate;
1429         } else {
1430           found++;
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);
1434         }
1435         offset += length;
1436       }
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. */
1441         found--;
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;
1448
1449         if (!headerstart
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;
1454         if (start_off > 0)
1455           probability /= 2;
1456         if (!changed)
1457           probability = (probability + GST_TYPE_FIND_MAXIMUM) / 2;
1458
1459         GST_INFO
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)) {
1467           probability = 0;
1468         }
1469         g_assert (probability <= GST_TYPE_FIND_MAXIMUM);
1470
1471         *found_prob = probability;
1472         if (probability > 0)
1473           *found_layer = layer;
1474         return;
1475       }
1476     }
1477     data++;
1478     skipped++;
1479     size--;
1480   }
1481 }
1482
1483 static void
1484 mp3_type_find (GstTypeFind * tf, gpointer unused)
1485 {
1486   GstTypeFindProbability prob, mid_prob;
1487   const guint8 *data;
1488   guint layer, mid_layer;
1489   guint64 length;
1490
1491   mp3_type_find_at_offset (tf, 0, &layer, &prob);
1492   length = gst_type_find_get_length (tf);
1493
1494   if (length == 0 || length == (guint64) - 1) {
1495     if (prob != 0)
1496       goto suggest;
1497     return;
1498   }
1499
1500   /* if we're pretty certain already, skip the additional check */
1501   if (prob >= GST_TYPE_FIND_LIKELY)
1502     goto suggest;
1503
1504   mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob);
1505
1506   if (mid_prob > 0) {
1507     if (prob == 0) {
1508       GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob);
1509       layer = mid_layer;
1510       prob = mid_prob;
1511       goto suggest;
1512     }
1513
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? */
1517     }
1518
1519     /* detected mpeg audio both in middle of the file and at the start */
1520     prob = (prob + mid_prob) / 2;
1521     goto suggest;
1522   }
1523
1524   /* a valid header right at the start makes it more likely
1525    * that this is actually plain mpeg-1 audio */
1526   if (prob > 0) {
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);
1531     }
1532   }
1533
1534   if (prob > 0)
1535     goto suggest;
1536
1537   return;
1538
1539 suggest:
1540   {
1541     g_return_if_fail (layer >= 1 && layer <= 3);
1542
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);
1546   }
1547 }
1548
1549 /*** audio/x-musepack ***/
1550
1551 static GstStaticCaps musepack_caps =
1552 GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }");
1553
1554 #define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps))
1555 static void
1556 musepack_type_find (GstTypeFind * tf, gpointer unused)
1557 {
1558   const guint8 *data = gst_type_find_peek (tf, 0, 4);
1559   GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM;
1560   gint streamversion = -1;
1561
1562   if (data && memcmp (data, "MP+", 3) == 0) {
1563     streamversion = 7;
1564     if ((data[3] & 0x7f) == 7) {
1565       prop = GST_TYPE_FIND_MAXIMUM;
1566     } else {
1567       prop = GST_TYPE_FIND_LIKELY + 10;
1568     }
1569   } else if (data && memcmp (data, "MPCK", 4) == 0) {
1570     streamversion = 8;
1571     prop = GST_TYPE_FIND_MAXIMUM;
1572   }
1573
1574   if (streamversion != -1) {
1575     gst_type_find_suggest_simple (tf, prop, "audio/x-musepack",
1576         "streamversion", G_TYPE_INT, streamversion, NULL);
1577   }
1578 }
1579
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");
1583
1584 #define AC3_CAPS (gst_static_caps_get(&ac3_caps))
1585
1586 static GstStaticCaps eac3_caps = GST_STATIC_CAPS ("audio/x-eac3");
1587
1588 #define EAC3_CAPS (gst_static_caps_get(&eac3_caps))
1589
1590 struct ac3_frmsize
1591 {
1592   unsigned short bit_rate;
1593   unsigned short frm_size[3];
1594 };
1595
1596 static const struct ac3_frmsize ac3_frmsizecod_tbl[] = {
1597   {32, {64, 69, 96}},
1598   {32, {64, 70, 96}},
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}}
1635 };
1636
1637 static void
1638 ac3_type_find (GstTypeFind * tf, gpointer unused)
1639 {
1640   DataScanCtx c = { 0, NULL, 0 };
1641
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)))
1648       break;
1649
1650     if (c.data[0] == 0x0b && c.data[1] == 0x77) {
1651       guint bsid = c.data[5] >> 3;
1652
1653       if (bsid <= 8) {
1654         /* ac3 */
1655         guint fscod = c.data[4] >> 6;
1656         guint frmsizecod = c.data[4] & 0x3f;
1657
1658         if (fscod < 3 && frmsizecod < 38) {
1659           DataScanCtx c_next = c;
1660           guint frame_size;
1661
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);
1667
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;
1671
1672               if (fscod < 3 && frmsizecod < 38) {
1673                 GstTypeFindProbability prob;
1674
1675                 GST_LOG ("found second AC3 frame (size=%u), looks good",
1676                     ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]);
1677                 if (c.offset == 0)
1678                   prob = GST_TYPE_FIND_MAXIMUM;
1679                 else
1680                   prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1681
1682                 gst_type_find_suggest (tf, prob, AC3_CAPS);
1683                 return;
1684               }
1685             } else {
1686               GST_LOG ("no second AC3 frame found, false sync");
1687             }
1688           }
1689         }
1690       } else if (bsid <= 16 && bsid > 10) {
1691         /* eac3 */
1692         DataScanCtx c_next = c;
1693         guint frame_size;
1694
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);
1700
1701           if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1702             GstTypeFindProbability prob;
1703
1704             GST_LOG ("found second E-AC3 frame, looks good");
1705             if (c.offset == 0)
1706               prob = GST_TYPE_FIND_MAXIMUM;
1707             else
1708               prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1709
1710             gst_type_find_suggest (tf, prob, EAC3_CAPS);
1711             return;
1712           } else {
1713             GST_LOG ("no second E-AC3 frame found, false sync");
1714           }
1715         }
1716       } else {
1717         GST_LOG ("invalid AC3 BSID: %u", bsid);
1718       }
1719     }
1720     data_scan_ctx_advance (tf, &c, 1);
1721   }
1722 }
1723
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 */
1729
1730 static gboolean
1731 dts_parse_frame_header (DataScanCtx * c, guint * frame_size,
1732     guint * sample_rate, guint * channels, guint * depth, guint * endianness)
1733 {
1734   static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
1735     22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
1736   };
1737   static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
1738     6, 6, 6, 7, 8, 8
1739   };
1740   guint16 hdr[8];
1741   guint32 marker;
1742   guint num_blocks, chans, lfe, i;
1743
1744   marker = GST_READ_UINT32_BE (c->data);
1745
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)));
1751   } else
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)));
1757   } else {
1758     return FALSE;
1759   }
1760
1761   GST_LOG ("dts sync marker 0x%08x at offset %u", marker, (guint) c->offset);
1762
1763   /* 14-bit mode */
1764   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
1765     if ((hdr[2] & 0xFFF0) != 0x07F0)
1766       return FALSE;
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);
1777     *depth = 14;
1778   } else {
1779     *depth = 16;
1780   }
1781
1782   GST_LOG ("frame header: %04x%04x%04x%04x", hdr[2], hdr[3], hdr[4], hdr[5]);
1783
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;
1789
1790   if (num_blocks < 5 || *frame_size < 96 || *sample_rate == 0)
1791     return FALSE;
1792
1793   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
1794     *frame_size = (*frame_size * 16) / 14;      /* FIXME: round up? */
1795
1796   if (chans < G_N_ELEMENTS (channels_table))
1797     *channels = channels_table[chans] + ((lfe) ? 1 : 0);
1798   else
1799     *channels = 0;
1800
1801   return TRUE;
1802 }
1803
1804 static void
1805 dts_type_find (GstTypeFind * tf, gpointer unused)
1806 {
1807   DataScanCtx c = { 0, NULL, 0 };
1808
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;
1814
1815     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, DTS_MIN_FRAMESIZE)))
1816       return;
1817
1818     if (G_UNLIKELY (dts_parse_frame_header (&c, &frame_size, &rate, &chans,
1819                 &depth, &endianness))) {
1820       GstTypeFindProbability prob;
1821       DataScanCtx next_c;
1822
1823       prob = (c.offset == 0) ? GST_TYPE_FIND_LIKELY : GST_TYPE_FIND_POSSIBLE;
1824
1825       /* check for second frame sync */
1826       next_c = c;
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;
1833       }
1834
1835       if (chans > 0) {
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);
1840       } else {
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);
1845       }
1846
1847       return;
1848     }
1849
1850     data_scan_ctx_advance (tf, &c, 1);
1851   }
1852 }
1853
1854 /*** gsm ***/
1855
1856 /* can only be detected by using the extension, in which case we use the default
1857  * GSM properties */
1858 static GstStaticCaps gsm_caps =
1859 GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1");
1860
1861 #define GSM_CAPS (gst_static_caps_get(&gsm_caps))
1862
1863 /*** wavpack ***/
1864
1865 static GstStaticCaps wavpack_caps =
1866 GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false");
1867
1868 #define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps))
1869
1870 static GstStaticCaps wavpack_correction_caps =
1871 GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");
1872
1873 #define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps))
1874
1875 static void
1876 wavpack_type_find (GstTypeFind * tf, gpointer unused)
1877 {
1878   GstTypeFindProbability base_prob = GST_TYPE_FIND_POSSIBLE;
1879   guint64 offset;
1880   guint32 blocksize;
1881   const guint8 *data;
1882   guint count_wv, count_wvc;
1883
1884   data = gst_type_find_peek (tf, 0, 32);
1885   if (!data)
1886     return;
1887
1888   if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k')
1889     return;
1890
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);
1897   count_wv = 0;
1898   count_wvc = 0;
1899   offset = 32;
1900   while (offset < 8 + blocksize) {
1901     guint32 sublen;
1902
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);
1906     if (data == NULL)
1907       break;
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 */
1912     } else {
1913       sublen += 1 + 1;          /* id + length */
1914     }
1915     if (offset + sublen > 8 + blocksize) {
1916       GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
1917           blocksize - offset);
1918       break;
1919     }
1920     if ((data[0] & 0x20) == 0) {
1921       switch (data[0] & 0x0f) {
1922         case 0xa:              /* ID_WV_BITSTREAM  */
1923         case 0xc:              /* ID_WVX_BITSTREAM */
1924           ++count_wv;
1925           break;
1926         case 0xb:              /* ID_WVC_BITSTREAM */
1927           ++count_wvc;
1928           break;
1929         default:
1930           break;
1931       }
1932       if (count_wv >= 5 || count_wvc >= 5)
1933         break;
1934     }
1935     offset += sublen;
1936   }
1937
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;
1943   }
1944
1945   GST_DEBUG ("wvc=%d, wv=%d", count_wvc, count_wv);
1946
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),
1954         WAVPACK_CAPS);
1955   }
1956 }
1957
1958 /*** application/postscrip ***/
1959 static GstStaticCaps postscript_caps =
1960 GST_STATIC_CAPS ("application/postscript");
1961
1962 #define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps))
1963
1964 static void
1965 postscript_type_find (GstTypeFind * tf, gpointer unused)
1966 {
1967   const guint8 *data = gst_type_find_peek (tf, 0, 3);
1968   if (!data)
1969     return;
1970
1971   if (data[0] == 0x04)
1972     data++;
1973   if (data[0] == '%' && data[1] == '!')
1974     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS);
1975
1976 }
1977
1978 /*** image/svg+xml ***/
1979 static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml");
1980
1981 #define SVG_CAPS (gst_static_caps_get(&svg_caps))
1982
1983 static void
1984 svg_type_find (GstTypeFind * tf, gpointer unused)
1985 {
1986   static const gchar svg_doctype[] = "!DOCTYPE svg";
1987   static const gchar svg_tag[] = "<svg";
1988   DataScanCtx c = { 0, NULL, 0 };
1989
1990   while (c.offset <= 1024) {
1991     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
1992       break;
1993
1994     if (memcmp (svg_doctype, c.data, 12) == 0) {
1995       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS);
1996       return;
1997     } else if (memcmp (svg_tag, c.data, 4) == 0) {
1998       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS);
1999       return;
2000     }
2001     data_scan_ctx_advance (tf, &c, 1);
2002   }
2003 }
2004
2005 /*** multipart/x-mixed-replace mimestream ***/
2006
2007 static GstStaticCaps multipart_caps =
2008 GST_STATIC_CAPS ("multipart/x-mixed-replace");
2009 #define MULTIPART_CAPS gst_static_caps_get(&multipart_caps)
2010
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 */
2014 static void
2015 multipart_type_find (GstTypeFind * tf, gpointer unused)
2016 {
2017   const guint8 *data;
2018   const guint8 *x;
2019
2020 #define MULTIPART_MAX_BOUNDARY_OFFSET 16
2021   data = gst_type_find_peek (tf, 0, MULTIPART_MAX_BOUNDARY_OFFSET);
2022   if (!data)
2023     return;
2024
2025   for (x = data;
2026       x - data < MULTIPART_MAX_BOUNDARY_OFFSET - 2 && g_ascii_isspace (*x);
2027       x++);
2028   if (x[0] != '-' || x[1] != '-')
2029     return;
2030
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);
2034   if (!data)
2035     return;
2036
2037   for (x = data; x - data < MULTIPART_MAX_HEADER_SIZE - 14; x++) {
2038     if (!isascii (*x)) {
2039       return;
2040     }
2041     if (*x == '\n' &&
2042         !g_ascii_strncasecmp ("content-type:", (gchar *) x + 1, 13)) {
2043       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MULTIPART_CAPS);
2044       return;
2045     }
2046   }
2047 }
2048
2049 /*** video/mpeg systemstream ***/
2050 static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, "
2051     "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]");
2052
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)))
2057
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]))
2062
2063 #define IS_MPEG_PES_CODE(b) (((b) & 0xF0) == 0xE0 || ((b) & 0xF0) == 0xC0 || \
2064                              (b) >= 0xBD)
2065 #define IS_MPEG_PES_HEADER(data)        (IS_MPEG_HEADER (data) &&            \
2066                                          IS_MPEG_PES_CODE (((guint8 *)(data))[3]))
2067
2068 #define MPEG2_MAX_PROBE_LENGTH (128 * 1024)     /* 128kB should be 64 packs of the 
2069                                                  * most common 2kB pack size. */
2070
2071 #define MPEG2_MIN_SYS_HEADERS 2
2072 #define MPEG2_MAX_SYS_HEADERS 5
2073
2074 static gboolean
2075 mpeg_sys_is_valid_pack (GstTypeFind * tf, const guint8 * data, guint len,
2076     guint * pack_size)
2077 {
2078   /* Check the pack header @ offset for validity, assuming that the 4 byte header
2079    * itself has already been checked. */
2080   guint8 stuff_len;
2081
2082   if (len < 12)
2083     return FALSE;
2084
2085   /* Check marker bits */
2086   if ((data[4] & 0xC4) == 0x44) {
2087     /* MPEG-2 PACK */
2088     if (len < 14)
2089       return FALSE;
2090
2091     if ((data[6] & 0x04) != 0x04 ||
2092         (data[8] & 0x04) != 0x04 ||
2093         (data[9] & 0x01) != 0x01 || (data[12] & 0x03) != 0x03)
2094       return FALSE;
2095
2096     stuff_len = data[13] & 0x07;
2097
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))
2101         return FALSE;
2102     }
2103     if (pack_size)
2104       *pack_size = 14 + stuff_len;
2105     return TRUE;
2106   } else if ((data[4] & 0xF1) == 0x21) {
2107     /* MPEG-1 PACK */
2108     if ((data[6] & 0x01) != 0x01 ||
2109         (data[8] & 0x01) != 0x01 ||
2110         (data[9] & 0x80) != 0x80 || (data[11] & 0x01) != 0x01)
2111       return FALSE;
2112
2113     /* Check the following header bytes, if we can */
2114     if ((12 + 4) <= len) {
2115       if (!IS_MPEG_HEADER (data + 12))
2116         return FALSE;
2117     }
2118     if (pack_size)
2119       *pack_size = 12;
2120     return TRUE;
2121   }
2122
2123   return FALSE;
2124 }
2125
2126 static gboolean
2127 mpeg_sys_is_valid_pes (GstTypeFind * tf, const guint8 * data, guint len,
2128     guint * pack_size)
2129 {
2130   guint pes_packet_len;
2131
2132   /* Check the PES header at the given position, assuming the header code itself
2133    * was already checked */
2134   if (len < 6)
2135     return FALSE;
2136
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)
2141     return FALSE;
2142
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))
2146       return FALSE;
2147   }
2148
2149   if (pack_size)
2150     *pack_size = 6 + pes_packet_len;
2151   return TRUE;
2152 }
2153
2154 static gboolean
2155 mpeg_sys_is_valid_sys (GstTypeFind * tf, const guint8 * data, guint len,
2156     guint * pack_size)
2157 {
2158   guint sys_hdr_len;
2159
2160   /* Check the System header at the given position, assuming the header code itself
2161    * was already checked */
2162   if (len < 6)
2163     return FALSE;
2164   sys_hdr_len = GST_READ_UINT16_BE (data + 4);
2165   if (sys_hdr_len < 6)
2166     return FALSE;
2167
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))
2171       return FALSE;
2172   }
2173
2174   if (pack_size)
2175     *pack_size = 6 + sys_hdr_len;
2176
2177   return TRUE;
2178 }
2179
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)
2189  *       = <some_number>
2190  * Since we also check marker bits and pes packet lengths, this probability is a
2191  * very coarse upper bound.
2192  */
2193 static void
2194 mpeg_sys_type_find (GstTypeFind * tf, gpointer unused)
2195 {
2196   const guint8 *data, *data0, *first_sync, *end;
2197   gint mpegversion = 0;
2198   guint pack_headers = 0;
2199   guint pes_headers = 0;
2200   guint pack_size;
2201   guint since_last_sync = 0;
2202   guint32 sync_word = 0xffffffff;
2203   guint potential_headers = 0;
2204
2205   G_STMT_START {
2206     gint len;
2207
2208     len = MPEG2_MAX_PROBE_LENGTH;
2209
2210     while (len >= 16) {
2211       data = gst_type_find_peek (tf, 0, 5 + len);
2212       if (data != NULL)
2213         break;
2214       len = len / 2;
2215     }
2216
2217     if (!data)
2218       return;
2219
2220     end = data + len;
2221   }
2222   G_STMT_END;
2223
2224   data0 = data;
2225   first_sync = NULL;
2226
2227   while (data < end) {
2228     sync_word <<= 8;
2229     if (sync_word == 0x00000100) {
2230       /* Found potential sync word */
2231       if (first_sync == NULL)
2232         first_sync = data - 3;
2233
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;
2238       }
2239       pack_size = 0;
2240
2241       potential_headers++;
2242       if (IS_MPEG_PACK_CODE (data[0])) {
2243         if ((data[1] & 0xC0) == 0x40) {
2244           /* MPEG-2 */
2245           mpegversion = 2;
2246         } else if ((data[1] & 0xF0) == 0x20) {
2247           mpegversion = 1;
2248         }
2249         if (mpegversion != 0 &&
2250             mpeg_sys_is_valid_pack (tf, data - 3, end - data + 3, &pack_size)) {
2251           pack_headers++;
2252         }
2253       } else if (IS_MPEG_PES_CODE (data[0])) {
2254         /* PES stream */
2255         if (mpeg_sys_is_valid_pes (tf, data - 3, end - data + 3, &pack_size)) {
2256           pes_headers++;
2257           if (mpegversion == 0)
2258             mpegversion = 2;
2259         }
2260       } else if (IS_MPEG_SYS_CODE (data[0])) {
2261         if (mpeg_sys_is_valid_sys (tf, data - 3, end - data + 3, &pack_size)) {
2262           pack_headers++;
2263         }
2264       }
2265
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;
2272         continue;
2273       }
2274     }
2275
2276     sync_word |= data[0];
2277     since_last_sync++;
2278     data++;
2279
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
2282      * result */
2283     if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MAX_SYS_HEADERS)
2284       goto suggest;
2285   }
2286
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 
2289    * result */
2290   if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MIN_SYS_HEADERS)
2291     goto suggest;
2292
2293   return;
2294 suggest:
2295   {
2296     guint prob;
2297
2298     prob = GST_TYPE_FIND_POSSIBLE + (10 * (pack_headers + pes_headers));
2299     prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2300
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);
2308       prob += 10;
2309       prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2310     }
2311
2312     /* lower probability if the first packet wasn't right at the start */
2313     if (data0 != first_sync && prob >= 10)
2314       prob -= 10;
2315
2316     GST_LOG ("Suggesting MPEG %d system stream, %d packs, %d pes, prob %u%%\n",
2317         mpegversion, pack_headers, pes_headers, prob);
2318
2319     gst_type_find_suggest_simple (tf, prob, "video/mpeg",
2320         "systemstream", G_TYPE_BOOLEAN, TRUE,
2321         "mpegversion", G_TYPE_INT, mpegversion, NULL);
2322   }
2323 };
2324
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)
2329
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)
2339
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))
2345
2346 /* Helper function to search ahead at intervals of packet_size for mpegts
2347  * headers */
2348 static gint
2349 mpeg_ts_probe_headers (GstTypeFind * tf, guint64 offset, gint packet_size)
2350 {
2351   /* We always enter this function having found at least one header already */
2352   gint found = 1;
2353   const guint8 *data = NULL;
2354
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;
2358
2359     data = gst_type_find_peek (tf, offset, MPEGTS_HDR_SIZE);
2360     if (data == NULL || !IS_MPEGTS_HEADER (data))
2361       return found;
2362
2363     found++;
2364     GST_LOG ("mpeg-ts sync #%2d at offset %" G_GUINT64_FORMAT, found, offset);
2365   }
2366
2367   return found;
2368 }
2369
2370 /* Try and detect at least 4 packets in at most 10 packets worth of
2371  * data. Need to try several possible packet sizes */
2372 static void
2373 mpeg_ts_type_find (GstTypeFind * tf, gpointer unused)
2374 {
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;
2379   guint size = 0;
2380   guint64 skipped = 0;
2381
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);
2385       if (!data)
2386         break;
2387       size = GST_MPEGTS_TYPEFIND_SYNC_SIZE;
2388     }
2389
2390     /* Have at least MPEGTS_HDR_SIZE bytes at this point */
2391     if (IS_MPEGTS_HEADER (data)) {
2392       gint p;
2393
2394       GST_LOG ("possible mpeg-ts sync at offset %" G_GUINT64_FORMAT, skipped);
2395
2396       for (p = 0; p < G_N_ELEMENTS (pack_sizes); p++) {
2397         gint found;
2398
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) {
2402           gint probability;
2403
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);
2408
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);
2412           return;
2413         }
2414       }
2415     }
2416     data++;
2417     skipped++;
2418     size--;
2419   }
2420 }
2421
2422 #define GST_MPEGVID_TYPEFIND_TRY_PICTURES 6
2423 #define GST_MPEGVID_TYPEFIND_TRY_SYNC (100 * 1024)      /* 100 kB */
2424
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.
2428  */
2429 static gboolean
2430 mpeg_find_next_header (GstTypeFind * tf, DataScanCtx * c,
2431     guint64 max_extra_offset)
2432 {
2433   guint64 extra_offset;
2434
2435   for (extra_offset = 0; extra_offset <= max_extra_offset; ++extra_offset) {
2436     if (!data_scan_ctx_ensure_data (tf, c, 4))
2437       return FALSE;
2438     if (IS_MPEG_HEADER (c->data)) {
2439       data_scan_ctx_advance (tf, c, 3);
2440       return TRUE;
2441     }
2442     data_scan_ctx_advance (tf, c, 1);
2443   }
2444   return FALSE;
2445 }
2446
2447 /*** video/mpeg MPEG-4 elementary video stream ***/
2448
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)
2452
2453 /*
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.
2459  */
2460 static void
2461 mpeg4_video_type_find (GstTypeFind * tf, gpointer unused)
2462 {
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;
2469   guint8 sc;
2470
2471   while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2472     if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2473       break;
2474
2475     if (!mpeg_find_next_header (tf, &c,
2476             GST_MPEGVID_TYPEFIND_TRY_SYNC - c.offset))
2477       break;
2478
2479     sc = c.data[0];
2480
2481     /* visual_object_sequence_start_code */
2482     if (sc == 0xB0) {
2483       if (seen_vios)
2484         break;                  /* Terminate at second vios */
2485       if (c.offset == 0)
2486         seen_vios_at_0 = TRUE;
2487       seen_vios = TRUE;
2488       data_scan_ctx_advance (tf, &c, 2);
2489       if (!mpeg_find_next_header (tf, &c, 0))
2490         break;
2491
2492       sc = c.data[0];
2493
2494       /* Optional metadata */
2495       if (sc == 0xB2)
2496         if (!mpeg_find_next_header (tf, &c, 24))
2497           break;
2498     }
2499
2500     /* visual_object_start_code (consider it optional) */
2501     if (sc == 0xB5) {
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))
2505         break;
2506
2507       sc = c.data[0];
2508     }
2509
2510     /* video_object_start_code */
2511     if (sc <= 0x1F) {
2512       if (seen_vos)
2513         break;                  /* Terminate at second vos */
2514       seen_vos = TRUE;
2515       data_scan_ctx_advance (tf, &c, 2);
2516       continue;
2517     }
2518
2519     /* video_object_layer_start_code */
2520     if (sc >= 0x20 && sc <= 0x2F) {
2521       seen_vol = TRUE;
2522       data_scan_ctx_advance (tf, &c, 5);
2523       continue;
2524     }
2525
2526     /* video_object_plane_start_code */
2527     if (sc == 0xB6) {
2528       num_vop_headers++;
2529       data_scan_ctx_advance (tf, &c, 2);
2530       continue;
2531     }
2532
2533     /* Unknown start code. */
2534   }
2535
2536   if (num_vop_headers > 0 || seen_vol) {
2537     GstTypeFindProbability probability = 0;
2538
2539     GST_LOG ("Found %d pictures, vios: %d, vos:%d, vol:%d", num_vop_headers,
2540         seen_vios, seen_vos, seen_vol);
2541
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
2551         && seen_vol)
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;
2567
2568     gst_type_find_suggest (tf, probability, MPEG4_VIDEO_CAPS);
2569   }
2570 }
2571
2572 /*** video/x-h263 H263 video stream ***/
2573 static GstStaticCaps h263_video_caps =
2574 GST_STATIC_CAPS ("video/x-h263, variant=(string)itu");
2575
2576 #define H263_VIDEO_CAPS gst_static_caps_get(&h263_video_caps)
2577
2578 #define H263_MAX_PROBE_LENGTH (128 * 1024)
2579
2580 static void
2581 h263_video_type_find (GstTypeFind * tf, gpointer unused)
2582 {
2583   DataScanCtx c = { 0, NULL, 0 };
2584   guint64 data = 0;
2585   guint64 psc = 0;
2586   guint8 tr = 0;
2587   guint format;
2588   guint good = 0;
2589   guint bad = 0;
2590
2591   while (c.offset < H263_MAX_PROBE_LENGTH) {
2592     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2593       break;
2594
2595     /* Find the picture start code */
2596     data = (data << 8) + c.data[0];
2597     psc = data & G_GUINT64_CONSTANT (0xfffffc0000);
2598     if (psc == 0x800000) {
2599       /* Found PSC */
2600       /* TR */
2601       tr = (data & 0x3fc) >> 2;
2602       /* Source Format */
2603       format = tr & 0x07;
2604
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))
2608         good++;
2609       else
2610         bad++;
2611
2612       /* FIXME: maybe bail out early if we get mostly bad syncs ? */
2613     }
2614
2615     data_scan_ctx_advance (tf, &c, 1);
2616   }
2617
2618   GST_LOG ("good: %d, bad: %d", good, bad);
2619
2620   if (good > 2 * bad)
2621     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H263_VIDEO_CAPS);
2622
2623   return;
2624 }
2625
2626 /*** video/x-h264 H264 elementary video stream ***/
2627
2628 static GstStaticCaps h264_video_caps =
2629 GST_STATIC_CAPS ("video/x-h264,stream-format=byte-stream");
2630
2631 #define H264_VIDEO_CAPS gst_static_caps_get(&h264_video_caps)
2632
2633 #define H264_MAX_PROBE_LENGTH (128 * 1024)      /* 128kB for HD should be enough. */
2634
2635 static void
2636 h264_video_type_find (GstTypeFind * tf, gpointer unused)
2637 {
2638   DataScanCtx c = { 0, NULL, 0 };
2639
2640   /* Stream consists of: a series of sync codes (00 00 00 01) followed 
2641    * by NALs
2642    */
2643   gboolean seen_idr = FALSE;
2644   gboolean seen_sps = FALSE;
2645   gboolean seen_pps = FALSE;
2646   gboolean seen_ssps = FALSE;
2647   int nut, ref;
2648   int good = 0;
2649   int bad = 0;
2650
2651   while (c.offset < H264_MAX_PROBE_LENGTH) {
2652     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2653       break;
2654
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 */
2658
2659       /* if forbidden bit is different to 0 won't be h264 */
2660       if (nut > 0x1f) {
2661         bad++;
2662         break;
2663       }
2664
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)) {
2669           bad++;
2670         } else {
2671           if (nut == 7)
2672             seen_sps = TRUE;
2673           else if (nut == 8)
2674             seen_pps = TRUE;
2675           else if (nut == 5)
2676             seen_idr = TRUE;
2677
2678           good++;
2679         }
2680       } else if (nut >= 14 && nut <= 33) {
2681         if (nut == 15) {
2682           seen_ssps = TRUE;
2683           good++;
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) */
2688           if (seen_ssps)
2689             good++;
2690         } else {
2691           /* reserved */
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. */
2695           bad++;
2696         }
2697       } else {
2698         /* unspecified, application specific */
2699         /* don't consider these bad */
2700       }
2701
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);
2704
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);
2707         return;
2708       }
2709
2710       data_scan_ctx_advance (tf, &c, 4);
2711     }
2712     data_scan_ctx_advance (tf, &c, 1);
2713   }
2714
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);
2717
2718   if (good >= 2 && bad == 0) {
2719     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H264_VIDEO_CAPS);
2720   }
2721 }
2722
2723 /*** video/x-h265 H265 elementary video stream ***/
2724
2725 static GstStaticCaps h265_video_caps =
2726 GST_STATIC_CAPS ("video/x-h265,stream-format=byte-stream");
2727
2728 #define H265_VIDEO_CAPS gst_static_caps_get(&h265_video_caps)
2729
2730 #define H265_MAX_PROBE_LENGTH (128 * 1024)      /* 128kB for HD should be enough. */
2731
2732 static void
2733 h265_video_type_find (GstTypeFind * tf, gpointer unused)
2734 {
2735   DataScanCtx c = { 0, NULL, 0 };
2736
2737   /* Stream consists of: a series of sync codes (00 00 00 01) followed
2738    * by NALs
2739    */
2740   gboolean seen_irap = FALSE;
2741   gboolean seen_vps = FALSE;
2742   gboolean seen_sps = FALSE;
2743   gboolean seen_pps = FALSE;
2744   int nut;
2745   int good = 0;
2746   int bad = 0;
2747
2748   while (c.offset < H265_MAX_PROBE_LENGTH) {
2749     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5)))
2750       break;
2751
2752     if (IS_MPEG_HEADER (c.data)) {
2753       /* forbiden_zero_bit | nal_unit_type */
2754       nut = c.data[3] & 0xfe;
2755
2756       /* if forbidden bit is different to 0 won't be h265 */
2757       if (nut > 0x7e) {
2758         bad++;
2759         break;
2760       }
2761       nut = nut >> 1;
2762
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)) {
2766         bad++;
2767         break;
2768       }
2769
2770       /* collect statistics about the NAL types */
2771       if ((nut >= 0 && nut <= 9) || (nut >= 16 && nut <= 21) || (nut >= 32
2772               && nut <= 40)) {
2773         if (nut == 32)
2774           seen_vps = TRUE;
2775         else if (nut == 33)
2776           seen_sps = TRUE;
2777         else if (nut == 34)
2778           seen_pps = TRUE;
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 */
2782           seen_irap = TRUE;
2783         }
2784
2785         good++;
2786       } else if ((nut >= 10 && nut <= 15) || (nut >= 22 && nut <= 31)
2787           || (nut >= 41 && nut <= 47)) {
2788         /* reserved values are counting as bad */
2789         bad++;
2790       } else {
2791         /* unspecified (48..63), application specific */
2792         /* don't consider these as bad */
2793       }
2794
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);
2797
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);
2800         return;
2801       }
2802
2803       data_scan_ctx_advance (tf, &c, 5);
2804     }
2805     data_scan_ctx_advance (tf, &c, 1);
2806   }
2807
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);
2810
2811   if (good >= 2 && bad == 0) {
2812     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H265_VIDEO_CAPS);
2813   }
2814 }
2815
2816 /*** video/mpeg video stream ***/
2817
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)
2821
2822 /*
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
2826  * video specs.
2827  *
2828  * I'm sure someone will do a chance calculation here too.
2829  */
2830
2831 static void
2832 mpeg_video_stream_type_find (GstTypeFind * tf, gpointer unused)
2833 {
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;
2840   gint found = 0;
2841
2842   while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2843     if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2844       break;
2845
2846     if (!data_scan_ctx_ensure_data (tf, &c, 5))
2847       break;
2848
2849     if (!IS_MPEG_HEADER (c.data))
2850       goto next;
2851
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))
2854       return;
2855
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);
2859       seen_seq = TRUE;
2860       data_scan_ctx_advance (tf, &c, 4 + 8);
2861       continue;
2862     }
2863
2864     /* or a GOP header */
2865     if (c.data[3] == 0xB8) {
2866       seen_gop = TRUE;
2867       data_scan_ctx_advance (tf, &c, 8);
2868       continue;
2869     }
2870
2871     /* but what we'd really like to see is a picture header */
2872     if (c.data[3] == 0x00) {
2873       ++num_pic_headers;
2874       last_pic_offset = c.offset;
2875       data_scan_ctx_advance (tf, &c, 8);
2876       continue;
2877     }
2878
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);
2885       found += 1;
2886       continue;
2887     }
2888
2889   next:
2890
2891     data_scan_ctx_advance (tf, &c, 1);
2892   }
2893
2894   if (found > 0 || seen_seq) {
2895     GstTypeFindProbability probability = 0;
2896
2897     GST_LOG ("Found %d pictures, seq:%d, gop:%d", found, seen_seq, seen_gop);
2898
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;
2913     else if (found > 0)
2914       probability = GST_TYPE_FIND_POSSIBLE - 10;
2915     else if (seen_seq)
2916       probability = GST_TYPE_FIND_POSSIBLE - 20;
2917
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);
2921   }
2922 }
2923
2924 /*** audio/x-aiff ***/
2925
2926 static GstStaticCaps aiff_caps = GST_STATIC_CAPS ("audio/x-aiff");
2927
2928 #define AIFF_CAPS gst_static_caps_get(&aiff_caps)
2929 static void
2930 aiff_type_find (GstTypeFind * tf, gpointer unused)
2931 {
2932   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2933
2934   if (data && memcmp (data, "FORM", 4) == 0) {
2935     data += 8;
2936     if (memcmp (data, "AIFF", 4) == 0 || memcmp (data, "AIFC", 4) == 0)
2937       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AIFF_CAPS);
2938   }
2939 }
2940
2941 /*** audio/x-svx ***/
2942
2943 static GstStaticCaps svx_caps = GST_STATIC_CAPS ("audio/x-svx");
2944
2945 #define SVX_CAPS gst_static_caps_get(&svx_caps)
2946 static void
2947 svx_type_find (GstTypeFind * tf, gpointer unused)
2948 {
2949   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2950
2951   if (data && memcmp (data, "FORM", 4) == 0) {
2952     data += 8;
2953     if (memcmp (data, "8SVX", 4) == 0 || memcmp (data, "16SV", 4) == 0)
2954       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVX_CAPS);
2955   }
2956 }
2957
2958 /*** audio/x-shorten ***/
2959
2960 static GstStaticCaps shn_caps = GST_STATIC_CAPS ("audio/x-shorten");
2961
2962 #define SHN_CAPS gst_static_caps_get(&shn_caps)
2963 static void
2964 shn_type_find (GstTypeFind * tf, gpointer unused)
2965 {
2966   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2967
2968   if (data && memcmp (data, "ajkg", 4) == 0) {
2969     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2970   }
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);
2974   }
2975 }
2976
2977 /*** application/x-ape ***/
2978
2979 static GstStaticCaps ape_caps = GST_STATIC_CAPS ("application/x-ape");
2980
2981 #define APE_CAPS gst_static_caps_get(&ape_caps)
2982 static void
2983 ape_type_find (GstTypeFind * tf, gpointer unused)
2984 {
2985   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2986
2987   if (data && memcmp (data, "MAC ", 4) == 0) {
2988     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, APE_CAPS);
2989   }
2990 }
2991
2992 /*** ISO FORMATS ***/
2993
2994 /*** audio/x-m4a ***/
2995
2996 static GstStaticCaps m4a_caps = GST_STATIC_CAPS ("audio/x-m4a");
2997
2998 #define M4A_CAPS (gst_static_caps_get(&m4a_caps))
2999 static void
3000 m4a_type_find (GstTypeFind * tf, gpointer unused)
3001 {
3002   const guint8 *data = gst_type_find_peek (tf, 4, 8);
3003
3004   if (data && (memcmp (data, "ftypM4A ", 8) == 0)) {
3005     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, M4A_CAPS);
3006   }
3007 }
3008
3009 /*** application/x-3gp ***/
3010
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))
3014
3015 static const gchar *
3016 q3gp_type_find_get_profile (const guint8 * data)
3017 {
3018   switch (GST_MAKE_FOURCC (data[0], data[1], data[2], 0)) {
3019     case GST_MAKE_FOURCC ('3', 'g', 'g', 0):
3020       return "general";
3021     case GST_MAKE_FOURCC ('3', 'g', 'p', 0):
3022       return "basic";
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";
3027     default:
3028       break;
3029   }
3030   return NULL;
3031 }
3032
3033 static void
3034 q3gp_type_find (GstTypeFind * tf, gpointer unused)
3035 {
3036   const gchar *profile;
3037   guint32 ftyp_size = 0;
3038   gint offset = 0;
3039   const guint8 *data = NULL;
3040
3041   if ((data = gst_type_find_peek (tf, 0, 12)) == NULL) {
3042     return;
3043   }
3044
3045   data += 4;
3046   if (memcmp (data, "ftyp", 4) != 0) {
3047     return;
3048   }
3049
3050   /* check major brand */
3051   data += 4;
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);
3055     return;
3056   }
3057
3058   /* check compatible brands */
3059   if ((data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3060     ftyp_size = GST_READ_UINT32_BE (data);
3061   }
3062   for (offset = 16; offset < ftyp_size; offset += 4) {
3063     if ((data = gst_type_find_peek (tf, offset, 3)) == NULL) {
3064       break;
3065     }
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);
3069       return;
3070     }
3071   }
3072
3073   return;
3074
3075 }
3076
3077 /*** video/mj2 and image/jp2 ***/
3078 static GstStaticCaps mj2_caps = GST_STATIC_CAPS ("video/mj2");
3079
3080 #define MJ2_CAPS gst_static_caps_get(&mj2_caps)
3081
3082 static GstStaticCaps jp2_caps = GST_STATIC_CAPS ("image/jp2");
3083
3084 #define JP2_CAPS gst_static_caps_get(&jp2_caps)
3085
3086 static void
3087 jp2_type_find (GstTypeFind * tf, gpointer unused)
3088 {
3089   const guint8 *data;
3090
3091   data = gst_type_find_peek (tf, 0, 24);
3092   if (!data)
3093     return;
3094
3095   /* jp2 signature */
3096   if (memcmp (data, "\000\000\000\014jP  \015\012\207\012", 12) != 0)
3097     return;
3098
3099   /* check ftyp box */
3100   data += 12;
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);
3106   }
3107 }
3108
3109 /*** video/quicktime ***/
3110
3111 static GstStaticCaps qt_caps = GST_STATIC_CAPS ("video/quicktime");
3112
3113 #define QT_CAPS gst_static_caps_get(&qt_caps)
3114 #define STRNCMP(x,y,z) (strncmp ((char*)(x), (char*)(y), z))
3115
3116 /* FIXME 0.11: go through http://www.ftyps.com/ */
3117 static void
3118 qt_type_find (GstTypeFind * tf, gpointer unused)
3119 {
3120   const guint8 *data;
3121   guint tip = 0;
3122   guint64 offset = 0;
3123   guint64 size;
3124   const gchar *variant = NULL;
3125
3126   while ((data = gst_type_find_peek (tf, offset, 12)) != NULL) {
3127     guint64 new_offset;
3128
3129     if (STRNCMP (&data[4], "ftypqt  ", 8) == 0) {
3130       tip = GST_TYPE_FIND_MAXIMUM;
3131       break;
3132     }
3133
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;
3138       variant = "iso";
3139       break;
3140     }
3141
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";
3146       break;
3147     }
3148
3149     if (STRNCMP (&data[4], "ftypccff", 8) == 0) {
3150       tip = GST_TYPE_FIND_MAXIMUM;
3151       variant = "ccff";
3152       break;
3153     }
3154
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) {
3163       if (tip == 0) {
3164         tip = GST_TYPE_FIND_LIKELY;
3165       } else {
3166         tip = GST_TYPE_FIND_NEARLY_CERTAIN;
3167       }
3168     }
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;
3175       break;
3176     } else {
3177       tip = 0;
3178       break;
3179     }
3180
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);
3187         if (data == NULL)
3188           goto done;
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;
3196           variant = "iso";
3197           goto done;
3198         }
3199         new_offset += 4;
3200       }
3201     }
3202     if (size == 1) {
3203       const guint8 *sizedata;
3204
3205       sizedata = gst_type_find_peek (tf, offset + 8, 8);
3206       if (sizedata == NULL)
3207         break;
3208
3209       size = GST_READ_UINT64_BE (sizedata);
3210     } else {
3211       if (size < 8)
3212         break;
3213     }
3214     new_offset = offset + size;
3215     if (new_offset <= offset)
3216       break;
3217     offset = new_offset;
3218   }
3219
3220 done:
3221   if (tip > 0) {
3222     if (variant) {
3223       GstCaps *caps = gst_caps_copy (QT_CAPS);
3224
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);
3228     } else {
3229       gst_type_find_suggest (tf, tip, QT_CAPS);
3230     }
3231   }
3232 };
3233
3234
3235 /*** image/x-quicktime ***/
3236
3237 static GstStaticCaps qtif_caps = GST_STATIC_CAPS ("image/x-quicktime");
3238
3239 #define QTIF_CAPS gst_static_caps_get(&qtif_caps)
3240
3241 /* how many atoms we check before we give up */
3242 #define QTIF_MAXROUNDS 25
3243
3244 static void
3245 qtif_type_find (GstTypeFind * tf, gpointer unused)
3246 {
3247   const guint8 *data;
3248   gboolean found_idsc = FALSE;
3249   gboolean found_idat = FALSE;
3250   guint64 offset = 0;
3251   guint rounds = 0;
3252
3253   while ((data = gst_type_find_peek (tf, offset, 8)) != NULL) {
3254     guint64 size;
3255
3256     size = GST_READ_UINT32_BE (data);
3257     if (size == 1) {
3258       const guint8 *sizedata;
3259
3260       sizedata = gst_type_find_peek (tf, offset + 8, 8);
3261       if (sizedata == NULL)
3262         break;
3263
3264       size = GST_READ_UINT64_BE (sizedata);
3265     }
3266     if (size < 8)
3267       break;
3268
3269     if (STRNCMP (data + 4, "idsc", 4) == 0)
3270       found_idsc = TRUE;
3271     if (STRNCMP (data + 4, "idat", 4) == 0)
3272       found_idat = TRUE;
3273
3274     if (found_idsc && found_idat) {
3275       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, QTIF_CAPS);
3276       return;
3277     }
3278
3279     offset += size;
3280     if (++rounds > QTIF_MAXROUNDS)
3281       break;
3282   }
3283
3284   if (found_idsc || found_idat) {
3285     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, QTIF_CAPS);
3286     return;
3287   }
3288 };
3289
3290 /*** audio/x-mod ***/
3291
3292 static GstStaticCaps mod_caps = GST_STATIC_CAPS ("audio/x-mod");
3293
3294 #define MOD_CAPS gst_static_caps_get(&mod_caps)
3295 /* FIXME: M15 CheckType to do */
3296 static void
3297 mod_type_find (GstTypeFind * tf, gpointer unused)
3298 {
3299   const guint8 *data;
3300   GstTypeFindProbability probability;
3301   const char *mod_type = NULL;
3302
3303   /* MOD */
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) ||
3309         /* Star Tracker */
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) ||
3316         /* Taketracker */
3317         (memcmp (data, "TDZ", 3) == 0 && isdigit (data[3])) ||
3318         /* Fasttracker */
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]))) {
3324       mod_type = "mod";
3325       probability = GST_TYPE_FIND_MAXIMUM;
3326       goto suggest_audio_mod_caps;
3327     }
3328   }
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))) {
3334       mod_type = "j2b";
3335       probability = GST_TYPE_FIND_MAXIMUM;
3336       goto suggest_audio_mod_caps;
3337     }
3338   }
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;
3345     }
3346   }
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;
3353     }
3354   }
3355   /* ULT (Ultratracker) */
3356   if ((data = gst_type_find_peek (tf, 0, 14)) != NULL) {
3357     if (memcmp (data, "MAS_UTrack_V00", 14) == 0) {
3358       mod_type = "ult";
3359       probability = GST_TYPE_FIND_MAXIMUM;
3360       goto suggest_audio_mod_caps;
3361     }
3362   }
3363   /* DIGI (DigiBooster) */
3364   if ((data = gst_type_find_peek (tf, 0, 20)) != NULL) {
3365     if (memcmp (data, "DIGI Booster module\0", 20) == 0) {
3366       mod_type = "digi";
3367       probability = GST_TYPE_FIND_MAXIMUM;
3368       goto suggest_audio_mod_caps;
3369     }
3370   }
3371   /* PTM (PolyTracker) */
3372   if ((data = gst_type_find_peek (tf, 0x2C, 4)) != NULL) {
3373     if (memcmp (data, "PTMF", 4) == 0) {
3374       mod_type = "ptm";
3375       probability = GST_TYPE_FIND_LIKELY;
3376       goto suggest_audio_mod_caps;
3377     }
3378   }
3379   /* XM */
3380   if ((data = gst_type_find_peek (tf, 0, 38)) != NULL) {
3381     if ((memcmp (data, "Extended Module: ", 17) == 0) && (data[37] == 0x1A)) {
3382       mod_type = "xm";
3383       probability = GST_TYPE_FIND_MAXIMUM;
3384       goto suggest_audio_mod_caps;
3385     }
3386   }
3387   /* OKT */
3388   if (data || (data = gst_type_find_peek (tf, 0, 8)) != NULL) {
3389     if (memcmp (data, "OKTASONG", 8) == 0) {
3390       mod_type = "okt";
3391       probability = GST_TYPE_FIND_MAXIMUM;
3392       goto suggest_audio_mod_caps;
3393     }
3394   }
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)) {
3401         mod_type = "psm";
3402         probability = GST_TYPE_FIND_MAXIMUM;
3403         goto suggest_audio_mod_caps;
3404       }
3405     }
3406     /* 669 */
3407     if ((memcmp (data, "if", 2) == 0) || (memcmp (data, "JN", 2) == 0)) {
3408       mod_type = "669";
3409       probability = GST_TYPE_FIND_LIKELY;
3410       goto suggest_audio_mod_caps;
3411     }
3412     /* AMF */
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;
3417     }
3418     /* IT */
3419     if (memcmp (data, "IMPM", 4) == 0) {
3420       mod_type = "it";
3421       probability = GST_TYPE_FIND_MAXIMUM;
3422       goto suggest_audio_mod_caps;
3423     }
3424     /* DBM (DigiBooster Pro) */
3425     if (memcmp (data, "DBM0", 4) == 0) {
3426       mod_type = "dbm";
3427       probability = GST_TYPE_FIND_MAXIMUM;
3428       goto suggest_audio_mod_caps;
3429     }
3430     /* MDL (DigiTrakker) */
3431     if (memcmp (data, "DMDL", 4) == 0) {
3432       mod_type = "mdl";
3433       probability = GST_TYPE_FIND_MAXIMUM;
3434       goto suggest_audio_mod_caps;
3435     }
3436     /* MT2 (MadTracker 2.0) */
3437     if (memcmp (data, "MT20", 4) == 0) {
3438       mod_type = "mt2";
3439       probability = GST_TYPE_FIND_MAXIMUM;
3440       goto suggest_audio_mod_caps;
3441     }
3442     /* DMF (X-Tracker) */
3443     if (memcmp (data, "DDMF", 4) == 0) {
3444       mod_type = "dmf";
3445       probability = GST_TYPE_FIND_MAXIMUM;
3446       goto suggest_audio_mod_caps;
3447     }
3448     /* MED */
3449     if ((memcmp (data, "MMD0", 4) == 0) || (memcmp (data, "MMD1", 4) == 0)) {
3450       mod_type = "med";
3451       probability = GST_TYPE_FIND_MAXIMUM;
3452       goto suggest_audio_mod_caps;
3453     }
3454     /* MTM */
3455     if (memcmp (data, "MTM", 3) == 0) {
3456       mod_type = "mtm";
3457       probability = GST_TYPE_FIND_MAXIMUM;
3458       goto suggest_audio_mod_caps;
3459     }
3460     /* DSM */
3461     if (memcmp (data, "RIFF", 4) == 0) {
3462       const guint8 *data2 = gst_type_find_peek (tf, 8, 4);
3463
3464       if (data2) {
3465         if (memcmp (data2, "DSMF", 4) == 0) {
3466           mod_type = "dsm";
3467           probability = GST_TYPE_FIND_MAXIMUM;
3468           goto suggest_audio_mod_caps;
3469         }
3470       }
3471     }
3472     /* FAR (Farandole) */
3473     if (memcmp (data, "FAR\xFE", 4) == 0) {
3474       mod_type = "far";
3475       probability = GST_TYPE_FIND_MAXIMUM;
3476       goto suggest_audio_mod_caps;
3477     }
3478     /* FAM */
3479     if (memcmp (data, "FAM\xFE", 4) == 0) {
3480       const guint8 *data2 = gst_type_find_peek (tf, 44, 3);
3481
3482       if (data2) {
3483         if (memcmp (data2, "compare", 3) == 0) {
3484           mod_type = "fam";
3485           probability = GST_TYPE_FIND_MAXIMUM;
3486           goto suggest_audio_mod_caps;
3487         }
3488         /* otherwise do not suggest anything */
3489       } else {
3490         mod_type = "fam";
3491         probability = GST_TYPE_FIND_LIKELY;
3492         goto suggest_audio_mod_caps;
3493       }
3494     }
3495     /* GDM */
3496     if (memcmp (data, "GDM\xFE", 4) == 0) {
3497       const guint8 *data2 = gst_type_find_peek (tf, 71, 4);
3498
3499       if (data2) {
3500         if (memcmp (data2, "GMFS", 4) == 0) {
3501           mod_type = "gdm";
3502           probability = GST_TYPE_FIND_MAXIMUM;
3503           goto suggest_audio_mod_caps;
3504         }
3505         /* otherwise do not suggest anything */
3506       } else {
3507         mod_type = "gdm";
3508         probability = GST_TYPE_FIND_LIKELY;
3509         goto suggest_audio_mod_caps;
3510       }
3511     }
3512     /* UMX */
3513     if (memcmp (data, "\xC1\x83\x2A\x9E", 4) == 0) {
3514       mod_type = "umx";
3515       probability = GST_TYPE_FIND_POSSIBLE;
3516       goto suggest_audio_mod_caps;
3517     }
3518   }
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) {
3522       mod_type = "far";
3523       probability = GST_TYPE_FIND_POSSIBLE;
3524       goto suggest_audio_mod_caps;
3525     }
3526   }
3527   /* IMF */
3528   if ((data = gst_type_find_peek (tf, 60, 4)) != NULL) {
3529     if (memcmp (data, "IM10", 4) == 0) {
3530       mod_type = "imf";
3531       probability = GST_TYPE_FIND_MAXIMUM;
3532       goto suggest_audio_mod_caps;
3533     }
3534   }
3535   /* S3M */
3536   if ((data = gst_type_find_peek (tf, 44, 4)) != NULL) {
3537     if (memcmp (data, "SCRM", 4) == 0) {
3538       mod_type = "s3m";
3539       probability = GST_TYPE_FIND_MAXIMUM;
3540       goto suggest_audio_mod_caps;
3541     }
3542   }
3543   /* STM */
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;
3548
3549       if ((id = gst_type_find_peek (tf, 28, 1)) == NULL)
3550         return;
3551       if ((stmtype = gst_type_find_peek (tf, 29, 1)) == NULL)
3552         return;
3553       if (*id == 0x1A && *stmtype == 2) {
3554         mod_type = "stm";
3555         probability = GST_TYPE_FIND_MAXIMUM;
3556         goto suggest_audio_mod_caps;
3557       }
3558     }
3559   }
3560   /* AMF */
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;
3566     }
3567   }
3568
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);
3573
3574     gst_type_find_suggest (tf, probability, caps);
3575     gst_caps_unref (caps);
3576   }
3577 }
3578
3579 /*** application/x-shockwave-flash ***/
3580
3581 static GstStaticCaps swf_caps =
3582 GST_STATIC_CAPS ("application/x-shockwave-flash");
3583 #define SWF_CAPS (gst_static_caps_get(&swf_caps))
3584 static void
3585 swf_type_find (GstTypeFind * tf, gpointer unused)
3586 {
3587   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3588
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);
3592   }
3593 }
3594
3595 /*** application/vnd.ms-sstr+xml ***/
3596
3597 static void
3598 mss_manifest_load_utf16 (gunichar2 * utf16_ne, const guint8 * utf16_data,
3599     gsize data_size, guint data_endianness)
3600 {
3601   memcpy (utf16_ne, utf16_data, data_size);
3602   if (data_endianness != G_BYTE_ORDER) {
3603     guint i;
3604
3605     for (i = 0; i < data_size / 2; ++i)
3606       utf16_ne[i] = GUINT16_SWAP_LE_BE (utf16_ne[i]);
3607   }
3608 }
3609
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))
3613 static void
3614 mss_manifest_type_find (GstTypeFind * tf, gpointer unused)
3615 {
3616   gunichar2 utf16_ne[512];
3617   const guint8 *data;
3618   guint data_endianness = 0;
3619   glong n_read = 0, size = 0;
3620   guint length;
3621   gchar *utf8;
3622   gboolean utf8_bom_detected = FALSE;
3623
3624   if (xml_check_first_element (tf, "SmoothStreamingMedia", 20, TRUE)) {
3625     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3626     return;
3627   }
3628
3629   length = gst_type_find_get_length (tf);
3630
3631   /* try detecting the charset */
3632   data = gst_type_find_peek (tf, 0, 3);
3633
3634   if (data == NULL)
3635     return;
3636
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;
3644   else
3645     return;
3646
3647   /* try a default that should be enough */
3648   if (length == 0)
3649     length = 512;
3650   else if (length < 64)
3651     return;
3652   else                          /* the first few bytes should be enough */
3653     length = MIN (1024, length);
3654
3655   data = gst_type_find_peek (tf, 0, length);
3656
3657   if (data == NULL)
3658     return;
3659
3660   /* skip the BOM */
3661   data += 2;
3662   length -= 2;
3663
3664   if (utf8_bom_detected) {
3665     /* skip last byte of the BOM */
3666     data++;
3667     length--;
3668
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);
3672   } else {
3673     length = GST_ROUND_DOWN_2 (length);
3674
3675     /* convert to native endian UTF-16 */
3676     mss_manifest_load_utf16 (utf16_ne, data, length, data_endianness);
3677
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);
3684     }
3685     g_free (utf8);
3686   }
3687 }
3688
3689 /*** image/jpeg ***/
3690
3691 #define JPEG_MARKER_IS_START_OF_FRAME(x) \
3692     ((x)>=0xc0 && (x) <= 0xcf && (x)!=0xc4 && (x)!=0xc8 && (x)!=0xcc)
3693
3694 static GstStaticCaps jpeg_caps = GST_STATIC_CAPS ("image/jpeg");
3695
3696 #define JPEG_CAPS (gst_static_caps_get(&jpeg_caps))
3697 static void
3698 jpeg_type_find (GstTypeFind * tf, gpointer unused)
3699 {
3700   GstTypeFindProbability prob = GST_TYPE_FIND_POSSIBLE;
3701   DataScanCtx c = { 0, NULL, 0 };
3702   GstCaps *caps;
3703   guint num_markers;
3704
3705   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 2)))
3706     return;
3707
3708   if (c.data[0] != 0xff || c.data[1] != 0xd8)
3709     return;
3710
3711   num_markers = 1;
3712   data_scan_ctx_advance (tf, &c, 2);
3713
3714   caps = gst_caps_copy (JPEG_CAPS);
3715
3716   while (data_scan_ctx_ensure_data (tf, &c, 4) && c.offset < (200 * 1024)) {
3717     guint16 len;
3718     guint8 marker;
3719
3720     if (c.data[0] != 0xff)
3721       break;
3722
3723     marker = c.data[1];
3724     if (G_UNLIKELY (marker == 0xff)) {
3725       data_scan_ctx_advance (tf, &c, 1);
3726       continue;
3727     }
3728
3729     data_scan_ctx_advance (tf, &c, 2);
3730
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);
3735
3736     GST_LOG ("possible JPEG marker 0x%02x (@0x%04x), segment length %u",
3737         marker, (guint) c.offset, len);
3738
3739     if (!data_scan_ctx_ensure_data (tf, &c, len))
3740       break;
3741
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);
3748       ++num_markers;
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);
3754       ++num_markers;
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);
3761       ++num_markers;
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);
3765       ++num_markers;
3766     } else if (JPEG_MARKER_IS_START_OF_FRAME (marker) && len >= (2 + 8)) {
3767       int h, w;
3768
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);
3773         break;
3774       }
3775
3776       GST_LOG ("SOF at offset %" G_GUINT64_FORMAT ", num_markers=%d, "
3777           "WxH=%dx%d", c.offset - 2, num_markers, w, h);
3778
3779       if (num_markers >= 5 || prob == GST_TYPE_FIND_MAXIMUM)
3780         prob = GST_TYPE_FIND_MAXIMUM;
3781       else
3782         prob = GST_TYPE_FIND_LIKELY;
3783
3784       gst_caps_set_simple (caps, "width", G_TYPE_INT, w,
3785           "height", G_TYPE_INT, h, "sof-marker", G_TYPE_INT, marker & 0xf,
3786           NULL);
3787
3788       break;
3789     } else {
3790       GST_WARNING ("bad length or unexpected JPEG marker 0xff 0x%02x", marker);
3791       break;
3792     }
3793   }
3794
3795   gst_type_find_suggest (tf, prob, caps);
3796   gst_caps_unref (caps);
3797 }
3798
3799 /*** image/bmp ***/
3800
3801 static GstStaticCaps bmp_caps = GST_STATIC_CAPS ("image/bmp");
3802
3803 #define BMP_CAPS (gst_static_caps_get(&bmp_caps))
3804 static void
3805 bmp_type_find (GstTypeFind * tf, gpointer unused)
3806 {
3807   DataScanCtx c = { 0, NULL, 0 };
3808   guint32 struct_size, w, h, planes, bpp;
3809
3810   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 54)))
3811     return;
3812
3813   if (c.data[0] != 'B' || c.data[1] != 'M')
3814     return;
3815
3816   /* skip marker + size */
3817   data_scan_ctx_advance (tf, &c, 2 + 4);
3818
3819   /* reserved, must be 0 */
3820   if (c.data[0] != 0 || c.data[1] != 0 || c.data[2] != 0 || c.data[3] != 0)
3821     return;
3822
3823   data_scan_ctx_advance (tf, &c, 2 + 2);
3824
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))
3828     return;
3829
3830   struct_size = GST_READ_UINT32_LE (c.data + 4);
3831   GST_LOG ("struct_size=%u", struct_size);
3832
3833   data_scan_ctx_advance (tf, &c, 4 + 4);
3834
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);
3846   } else {
3847     return;
3848   }
3849
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))
3854     return;
3855
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,
3858       NULL);
3859 }
3860
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))
3871 static void
3872 tiff_type_find (GstTypeFind * tf, gpointer ununsed)
3873 {
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 };
3877
3878   if (data) {
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);
3883     }
3884   }
3885 }
3886
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))
3890 static void
3891 exr_type_find (GstTypeFind * tf, gpointer ununsed)
3892 {
3893   const guint8 *data = gst_type_find_peek (tf, 0, 8);
3894
3895   if (data) {
3896     guint32 flags;
3897
3898     if (GST_READ_UINT32_LE (data) != 0x01312f76)
3899       return;
3900
3901     flags = GST_READ_UINT32_LE (data + 4);
3902     if ((flags & 0xff) != 1 && (flags & 0xff) != 2)
3903       return;
3904
3905     /* If bit 9 is set, bit 11 and 12 must be 0 */
3906     if ((flags & 0x200) && (flags & 0x1800))
3907       return;
3908
3909     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, EXR_CAPS);
3910   }
3911 }
3912
3913
3914 /*** PNM ***/
3915
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");
3919
3920 #define PNM_CAPS (gst_static_caps_get(&pnm_caps))
3921
3922 #define IS_PNM_WHITESPACE(c) \
3923     ((c) == ' ' || (c) == '\r' || (c) == '\n' || (c) == 't')
3924
3925 static void
3926 pnm_type_find (GstTypeFind * tf, gpointer ununsed)
3927 {
3928   const gchar *media_type = NULL;
3929   DataScanCtx c = { 0, NULL, 0 };
3930   guint h = 0, w = 0;
3931
3932   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 16)))
3933     return;
3934
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'))
3939     return;
3940
3941   switch (c.data[1]) {
3942     case '1':
3943       media_type = "image/x-portable-bitmap";   /* ASCII */
3944       break;
3945     case '2':
3946       media_type = "image/x-portable-graymap";  /* ASCII */
3947       break;
3948     case '3':
3949       media_type = "image/x-portable-pixmap";   /* ASCII */
3950       break;
3951     case '4':
3952       media_type = "image/x-portable-bitmap";   /* Raw */
3953       break;
3954     case '5':
3955       media_type = "image/x-portable-graymap";  /* Raw */
3956       break;
3957     case '6':
3958       media_type = "image/x-portable-pixmap";   /* Raw */
3959       break;
3960     case '7':
3961       media_type = "image/x-portable-anymap";
3962       break;
3963     default:
3964       g_return_if_reached ();
3965   }
3966
3967   /* try to extract width and height as well */
3968   if (c.data[1] != '7') {
3969     gchar s[64] = { 0, }
3970     , sep1, sep2;
3971
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))
3978           return;
3979         data_scan_ctx_advance (tf, &c, 1);
3980       }
3981       data_scan_ctx_advance (tf, &c, 1);
3982       GST_LOG ("skipped comment line in PNM header");
3983     }
3984
3985     if (!data_scan_ctx_ensure_data (tf, &c, 32) &&
3986         !data_scan_ctx_ensure_data (tf, &c, 4)) {
3987       return;
3988     }
3989
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);
3996     } else {
3997       w = 0;
3998       h = 0;
3999     }
4000   } else {
4001     /* FIXME: extract width + height for anymaps too */
4002   }
4003
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);
4007   } else {
4008     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, media_type, NULL);
4009   }
4010 }
4011
4012 static GstStaticCaps sds_caps = GST_STATIC_CAPS ("audio/x-sds");
4013
4014 #define SDS_CAPS (gst_static_caps_get(&sds_caps))
4015 static void
4016 sds_type_find (GstTypeFind * tf, gpointer ununsed)
4017 {
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 };
4021   gint x;
4022
4023   if (data) {
4024     for (x = 0; x < 4; x++) {
4025       if ((data[x] & mask[x]) != match[x]) {
4026         return;
4027       }
4028     }
4029     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDS_CAPS);
4030   }
4031 }
4032
4033 static GstStaticCaps ircam_caps = GST_STATIC_CAPS ("audio/x-ircam");
4034
4035 #define IRCAM_CAPS (gst_static_caps_get(&ircam_caps))
4036 static void
4037 ircam_type_find (GstTypeFind * tf, gpointer ununsed)
4038 {
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 };
4042   gint x;
4043   gboolean matched = TRUE;
4044
4045   if (!data) {
4046     return;
4047   }
4048   for (x = 0; x < 4; x++) {
4049     if ((data[x] & mask[x]) != match[x]) {
4050       matched = FALSE;
4051     }
4052   }
4053   if (matched) {
4054     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, IRCAM_CAPS);
4055     return;
4056   }
4057   /* now try the reverse version */
4058   matched = TRUE;
4059   for (x = 0; x < 4; x++) {
4060     if ((data[x] & mask[3 - x]) != match[3 - x]) {
4061       matched = FALSE;
4062     }
4063   }
4064 }
4065
4066 /*** Matroska/WebM ***/
4067
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
4078
4079 #define EBML_MAX_LEN (2 * 1024 * 1024)
4080
4081 typedef enum
4082 {
4083   EBML_DOCTYPE_UNKNOWN = 0,
4084   EBML_DOCTYPE_MATROSKA,
4085   EBML_DOCTYPE_WEBM
4086 } GstEbmlDocType;
4087
4088 typedef struct
4089 {
4090   GstEbmlDocType doctype;
4091   guint audio;
4092   guint video;
4093   guint other;
4094   guint video_stereo;
4095   guint chunks;
4096   guint tracks_ok;              /* if we've seen and fully parsed the TRACKS element */
4097 } GstMatroskaInfo;
4098
4099 static inline guint
4100 ebml_read_chunk_header (GstTypeFind * tf, DataScanCtx * c, guint max_size,
4101     guint32 * id, guint64 * size)
4102 {
4103   guint64 mask;
4104   guint msbit_set, i, len, id_len;
4105
4106   if (c->size < 12 || max_size < 1)
4107     return 0;
4108
4109   /* element ID */
4110   *id = c->data[0];
4111   if ((c->data[0] & 0x80) == 0x80) {
4112     id_len = 1;
4113   } else if ((c->data[0] & 0xC0) == 0x40) {
4114     id_len = 2;
4115   } else if ((c->data[0] & 0xE0) == 0x20) {
4116     id_len = 3;
4117   } else if ((c->data[0] & 0xF0) == 0x10) {
4118     id_len = 4;
4119   } else {
4120     return 0;
4121   }
4122
4123   if (max_size < id_len)
4124     return 0;
4125
4126   for (i = 1; i < id_len; ++i) {
4127     *id = (*id << 8) | c->data[i];
4128   }
4129
4130   data_scan_ctx_advance (tf, c, id_len);
4131   max_size -= id_len;
4132
4133   /* size */
4134   if (max_size < 1 || c->data[0] == 0)
4135     return 0;
4136
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;
4141
4142   if (max_size < 1 + len)
4143     return 0;
4144   for (i = 0; i < len; ++i) {
4145     mask = (mask << 8) | 0xff;
4146     *size = (*size << 8) | c->data[1 + i];
4147   }
4148
4149   data_scan_ctx_advance (tf, c, 1 + len);
4150
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;
4156     else
4157       return 0;
4158   }
4159
4160   return id_len + (1 + len);
4161 }
4162
4163 static gboolean
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;
4171
4172   if (depth >= 8)               /* keep SPACES large enough for depth */
4173     return FALSE;
4174
4175   while (chunk_size > 0) {
4176     if (c.offset > EBML_MAX_LEN || !data_scan_ctx_ensure_data (tf, &c, 64))
4177       return FALSE;
4178
4179     hdr_len = ebml_read_chunk_header (tf, &c, chunk_size, &id, &element_size);
4180     if (hdr_len == 0)
4181       return FALSE;
4182
4183     g_assert (hdr_len <= chunk_size);
4184     chunk_size -= hdr_len;
4185
4186     if (element_size > chunk_size)
4187       return FALSE;
4188
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);
4192
4193     if (!data_scan_ctx_ensure_data (tf, &c, element_size)) {
4194       GST_DEBUG ("not enough data");
4195       return FALSE;
4196     }
4197
4198     switch (id) {
4199       case EBML_DOCTYPE:
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;
4204         break;
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");
4209         return FALSE;
4210       case MATROSKA_TRACKS:
4211         GST_LOG ("parsing tracks");
4212         info->tracks_ok =
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);
4216         return FALSE;
4217       case MATROSKA_TRACK_ENTRY:
4218         GST_LOG ("parsing track entry");
4219         if (!ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1))
4220           return FALSE;
4221         break;
4222       case MATROSKA_TRACK_TYPE:{
4223         guint type = 0, i;
4224
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];
4228
4229         GST_DEBUG ("%s   track type %u",
4230             SPACES + sizeof (SPACES) - 1 - (2 * depth), type);
4231
4232         if (type == 1)
4233           ++info->video;
4234         else if (c.data[0] == 2)
4235           ++info->audio;
4236         else
4237           ++info->other;
4238         break;
4239       }
4240       case MATROSKA_STEREO_MODE:
4241         ++info->video_stereo;
4242         break;
4243       case MATROSKA_CLUSTER:
4244         GST_WARNING ("cluster, bailing out (should've found tracks by now)");
4245         return FALSE;
4246       default:
4247         break;
4248     }
4249     data_scan_ctx_advance (tf, &c, element_size);
4250     chunk_size -= element_size;
4251     ++info->chunks;
4252   }
4253
4254   return TRUE;
4255 }
4256
4257 static GstStaticCaps matroska_caps = GST_STATIC_CAPS ("video/x-matroska");
4258
4259 #define MATROSKA_CAPS (gst_static_caps_get(&matroska_caps))
4260 static void
4261 matroska_type_find (GstTypeFind * tf, gpointer ununsed)
4262 {
4263   GstTypeFindProbability prob;
4264   GstMatroskaInfo info = { 0, };
4265   const gchar *type_name;
4266   DataScanCtx c = { 0, NULL, 0 };
4267   gboolean is_audio;
4268   guint64 size;
4269   guint32 id, hdr_len;
4270
4271   if (!data_scan_ctx_ensure_data (tf, &c, 64))
4272     return;
4273
4274   if (GST_READ_UINT32_BE (c.data) != EBML_HEADER)
4275     return;
4276
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);
4279     if (hdr_len == 0)
4280       return;
4281
4282     GST_INFO ("=== top-level chunk %08x, size %" G_GUINT64_FORMAT
4283         " / %" G_GUINT64_FORMAT, id, size, size + hdr_len);
4284
4285     if (!ebml_parse_chunk (tf, &c, id, size, &info, 0))
4286       break;
4287     data_scan_ctx_advance (tf, &c, size);
4288     GST_INFO ("=== done with chunk %08x", id);
4289     if (id == MATROSKA_SEGMENT)
4290       break;
4291   }
4292
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,
4295       info.tracks_ok);
4296
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);
4301
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";
4306   } else {
4307     type_name = (is_audio) ? "audio/x-matroska" : "video/x-matroska";
4308   }
4309
4310   if (info.doctype == EBML_DOCTYPE_UNKNOWN)
4311     prob = GST_TYPE_FIND_LIKELY;
4312   else
4313     prob = GST_TYPE_FIND_MAXIMUM;
4314
4315   gst_type_find_suggest_simple (tf, prob, type_name, NULL);
4316 }
4317
4318 /*** application/mxf ***/
4319 static GstStaticCaps mxf_caps = GST_STATIC_CAPS ("application/mxf");
4320
4321 #define MXF_MAX_PROBE_LENGTH (1024 * 64)
4322 #define MXF_CAPS (gst_static_caps_get(&mxf_caps))
4323
4324 /*
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.
4328  */
4329 static void
4330 mxf_type_find (GstTypeFind * tf, gpointer ununsed)
4331 {
4332   static const guint8 partition_pack_key[] =
4333       { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01,
4334     0x01
4335   };
4336   DataScanCtx c = { 0, NULL, 0 };
4337
4338   while (c.offset <= MXF_MAX_PROBE_LENGTH) {
4339     guint i;
4340     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 1024)))
4341       break;
4342
4343     /* look over in chunks of 1kbytes to avoid too much overhead */
4344
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)
4351           goto advance;
4352
4353         /* Partition status */
4354         if (c.data[i + 14] >= 0x05)
4355           goto advance;
4356
4357         /* Reserved, must be 0x00 */
4358         if (c.data[i + 15] != 0x00)
4359           goto advance;
4360
4361         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXF_CAPS);
4362         return;
4363       }
4364     }
4365
4366   advance:
4367     data_scan_ctx_advance (tf, &c, 1024 - 16);
4368   }
4369 }
4370
4371 /*** video/x-dv ***/
4372
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))
4376 static void
4377 dv_type_find (GstTypeFind * tf, gpointer private)
4378 {
4379   const guint8 *data;
4380
4381   data = gst_type_find_peek (tf, 0, 5);
4382
4383   /* check for DIF  and DV flag */
4384   if (data && (data[0] == 0x1f) && (data[1] == 0x07) && (data[2] == 0x00)) {
4385     const gchar *format;
4386
4387     if (data[3] & 0x80) {
4388       format = "PAL";
4389     } else {
4390       format = "NTSC";
4391     }
4392
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);
4396   }
4397 }
4398
4399
4400 /*** Ogg variants ***/
4401 static GstStaticCaps ogg_caps =
4402     GST_STATIC_CAPS ("application/ogg;video/ogg;audio/ogg;application/kate");
4403
4404 #define OGG_CAPS (gst_static_caps_get(&ogg_caps))
4405
4406 typedef enum
4407 {
4408   OGG_AUDIO = 0,
4409   OGG_VIDEO,
4410   OGG_KATE,
4411   OGG_OTHER,
4412   OGG_SKELETON,
4413   OGG_ANNODEX,
4414   OGG_NUM
4415 } GstOggStreamType;
4416
4417 static void
4418 ogganx_type_find (GstTypeFind * tf, gpointer private)
4419 {
4420   const gchar *media_type;
4421   DataScanCtx c = { 0, NULL, 0 };
4422   guint ogg_syncs = 0;
4423   guint hdr_count[OGG_NUM] = { 0, };
4424   static const struct
4425   {
4426     const gchar marker[10];
4427     guint8 marker_size;
4428     GstOggStreamType stream_type;
4429   } markers[] = {
4430     {
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}
4449   };
4450
4451   while (c.offset < 4096 && data_scan_ctx_ensure_data (tf, &c, 64)) {
4452     guint size, i;
4453
4454     if (memcmp (c.data, "OggS", 5) != 0)
4455       break;
4456
4457     ++ogg_syncs;
4458
4459     /* check if BOS */
4460     if (c.data[5] != 0x02)
4461       break;
4462
4463     /* headers should only have one segment */
4464     if (c.data[26] != 1)
4465       break;
4466
4467     size = c.data[27];
4468     if (size < 8)
4469       break;
4470
4471     data_scan_ctx_advance (tf, &c, 28);
4472
4473     if (!data_scan_ctx_ensure_data (tf, &c, MAX (size, 8)))
4474       break;
4475
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];
4479         break;
4480       }
4481     }
4482
4483     if (i == G_N_ELEMENTS (markers)) {
4484       GST_MEMDUMP ("unknown Ogg stream marker", c.data, size);
4485       ++hdr_count[OGG_OTHER];
4486     }
4487
4488     data_scan_ctx_advance (tf, &c, size);
4489   }
4490
4491   if (ogg_syncs == 0)
4492     return;
4493
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";
4501   } else {
4502     media_type = "application/ogg";
4503   }
4504
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]);
4508
4509   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type, NULL);
4510 }
4511
4512 /*** audio/x-vorbis ***/
4513 static GstStaticCaps vorbis_caps = GST_STATIC_CAPS ("audio/x-vorbis");
4514
4515 #define VORBIS_CAPS (gst_static_caps_get(&vorbis_caps))
4516 static void
4517 vorbis_type_find (GstTypeFind * tf, gpointer private)
4518 {
4519   const guint8 *data = gst_type_find_peek (tf, 0, 30);
4520
4521   if (data) {
4522     guint blocksize_0;
4523     guint blocksize_1;
4524
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)
4529       return;
4530     data += 11;
4531     /* 1 byte channels must be != 0 */
4532     if (data[0] == 0)
4533       return;
4534     data++;
4535     /* 4 byte samplerate must be != 0 */
4536     if (GST_READ_UINT32_LE (data) == 0)
4537       return;
4538     data += 16;
4539     /* blocksize checks */
4540     blocksize_0 = data[0] & 0x0F;
4541     blocksize_1 = (data[0] & 0xF0) >> 4;
4542     if (blocksize_0 > blocksize_1)
4543       return;
4544     if (blocksize_0 < 6 || blocksize_0 > 13)
4545       return;
4546     if (blocksize_1 < 6 || blocksize_1 > 13)
4547       return;
4548     data++;
4549     /* framing bit */
4550     if ((data[0] & 0x01) != 1)
4551       return;
4552     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VORBIS_CAPS);
4553   }
4554 }
4555
4556 /*** video/x-theora ***/
4557
4558 static GstStaticCaps theora_caps = GST_STATIC_CAPS ("video/x-theora");
4559
4560 #define THEORA_CAPS (gst_static_caps_get(&theora_caps))
4561 static void
4562 theora_type_find (GstTypeFind * tf, gpointer private)
4563 {
4564   const guint8 *data = gst_type_find_peek (tf, 0, 7);   //42);
4565
4566   if (data) {
4567     if (data[0] != 0x80)
4568       return;
4569     if (memcmp (&data[1], "theora", 6) != 0)
4570       return;
4571     /* FIXME: make this more reliable when specs are out */
4572
4573     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, THEORA_CAPS);
4574   }
4575 }
4576
4577 /*** kate ***/
4578 static void
4579 kate_type_find (GstTypeFind * tf, gpointer private)
4580 {
4581   const guint8 *data = gst_type_find_peek (tf, 0, 64);
4582   gchar category[16] = { 0, };
4583
4584   if (G_UNLIKELY (data == NULL))
4585     return;
4586
4587   /* see: http://wiki.xiph.org/index.php/OggKate#Format_specification */
4588   if (G_LIKELY (memcmp (data, "\200kate\0\0\0", 8) != 0))
4589     return;
4590
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);
4600   } else {
4601     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
4602         "application/x-kate", NULL);
4603   }
4604 }
4605
4606 /*** application/x-ogm-video or audio***/
4607
4608 static GstStaticCaps ogmvideo_caps =
4609 GST_STATIC_CAPS ("application/x-ogm-video");
4610 #define OGMVIDEO_CAPS (gst_static_caps_get(&ogmvideo_caps))
4611 static void
4612 ogmvideo_type_find (GstTypeFind * tf, gpointer private)
4613 {
4614   const guint8 *data = gst_type_find_peek (tf, 0, 9);
4615
4616   if (data) {
4617     if (memcmp (data, "\001video\000\000\000", 9) != 0)
4618       return;
4619     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMVIDEO_CAPS);
4620   }
4621 }
4622
4623 static GstStaticCaps ogmaudio_caps =
4624 GST_STATIC_CAPS ("application/x-ogm-audio");
4625 #define OGMAUDIO_CAPS (gst_static_caps_get(&ogmaudio_caps))
4626 static void
4627 ogmaudio_type_find (GstTypeFind * tf, gpointer private)
4628 {
4629   const guint8 *data = gst_type_find_peek (tf, 0, 9);
4630
4631   if (data) {
4632     if (memcmp (data, "\001audio\000\000\000", 9) != 0)
4633       return;
4634     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMAUDIO_CAPS);
4635   }
4636 }
4637
4638 static GstStaticCaps ogmtext_caps = GST_STATIC_CAPS ("application/x-ogm-text");
4639
4640 #define OGMTEXT_CAPS (gst_static_caps_get(&ogmtext_caps))
4641 static void
4642 ogmtext_type_find (GstTypeFind * tf, gpointer private)
4643 {
4644   const guint8 *data = gst_type_find_peek (tf, 0, 9);
4645
4646   if (data) {
4647     if (memcmp (data, "\001text\000\000\000\000", 9) != 0)
4648       return;
4649     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMTEXT_CAPS);
4650   }
4651 }
4652
4653 /*** audio/x-speex ***/
4654
4655 static GstStaticCaps speex_caps = GST_STATIC_CAPS ("audio/x-speex");
4656
4657 #define SPEEX_CAPS (gst_static_caps_get(&speex_caps))
4658 static void
4659 speex_type_find (GstTypeFind * tf, gpointer private)
4660 {
4661   const guint8 *data = gst_type_find_peek (tf, 0, 80);
4662
4663   if (data) {
4664     /* 8 byte string "Speex   "
4665        24 byte speex version string + int */
4666     if (memcmp (data, "Speex   ", 8) != 0)
4667       return;
4668     data += 32;
4669
4670     /* 4 byte header size >= 80 */
4671     if (GST_READ_UINT32_LE (data) < 80)
4672       return;
4673     data += 4;
4674
4675     /* 4 byte sample rate <= 48000 */
4676     if (GST_READ_UINT32_LE (data) > 48000)
4677       return;
4678     data += 4;
4679
4680     /* currently there are only 3 speex modes. */
4681     if (GST_READ_UINT32_LE (data) > 3)
4682       return;
4683     data += 12;
4684
4685     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SPEEX_CAPS);
4686   }
4687 }
4688
4689 /*** audio/x-celt ***/
4690
4691 static GstStaticCaps celt_caps = GST_STATIC_CAPS ("audio/x-celt");
4692
4693 #define CELT_CAPS (gst_static_caps_get(&celt_caps))
4694 static void
4695 celt_type_find (GstTypeFind * tf, gpointer private)
4696 {
4697   const guint8 *data = gst_type_find_peek (tf, 0, 8);
4698
4699   if (data) {
4700     /* 8 byte string "CELT   " */
4701     if (memcmp (data, "CELT    ", 8) != 0)
4702       return;
4703
4704     /* TODO: Check other values of the CELT header */
4705     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CELT_CAPS);
4706   }
4707 }
4708
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))
4713 static void
4714 oggskel_type_find (GstTypeFind * tf, gpointer private)
4715 {
4716   const guint8 *data = gst_type_find_peek (tf, 0, 12);
4717
4718   if (data) {
4719     /* 8 byte string "fishead\0" for the ogg skeleton stream */
4720     if (memcmp (data, "fishead\0", 8) != 0)
4721       return;
4722     data += 8;
4723
4724     /* Require that the header contains version 3.0 */
4725     if (GST_READ_UINT16_LE (data) != 3)
4726       return;
4727     data += 2;
4728     if (GST_READ_UINT16_LE (data) != 0)
4729       return;
4730
4731     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGG_SKELETON_CAPS);
4732   }
4733 }
4734
4735 static GstStaticCaps cmml_caps = GST_STATIC_CAPS ("text/x-cmml");
4736
4737 #define CMML_CAPS (gst_static_caps_get(&cmml_caps))
4738 static void
4739 cmml_type_find (GstTypeFind * tf, gpointer private)
4740 {
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);
4743
4744   if (data) {
4745
4746     /* 8 byte string "CMML\0\0\0\0" for the magic number */
4747     if (memcmp (data, "CMML\0\0\0\0", 8) != 0)
4748       return;
4749     data += 8;
4750
4751     /* Require that the header contains at least version 2.0 */
4752     if (GST_READ_UINT16_LE (data) < 2)
4753       return;
4754
4755     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CMML_CAPS);
4756   }
4757 }
4758
4759 /*** application/x-tar ***/
4760
4761 static GstStaticCaps tar_caps = GST_STATIC_CAPS ("application/x-tar");
4762
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 */
4766 static void
4767 tar_type_find (GstTypeFind * tf, gpointer unused)
4768 {
4769   const guint8 *data = gst_type_find_peek (tf, 257, 8);
4770
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 */
4773   if (data) {
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);
4779     }
4780   }
4781 }
4782
4783 /*** application/x-ar ***/
4784
4785 static GstStaticCaps ar_caps = GST_STATIC_CAPS ("application/x-ar");
4786
4787 #define AR_CAPS (gst_static_caps_get(&ar_caps))
4788 static void
4789 ar_type_find (GstTypeFind * tf, gpointer unused)
4790 {
4791   const guint8 *data = gst_type_find_peek (tf, 0, 24);
4792
4793   if (data && memcmp (data, "!<arch>", 7) == 0) {
4794     gint i;
4795
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);
4799       }
4800     }
4801
4802     gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AR_CAPS);
4803   }
4804 }
4805
4806 /*** audio/x-au ***/
4807
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");
4813
4814 #define AU_CAPS (gst_static_caps_get(&au_caps))
4815 static void
4816 au_type_find (GstTypeFind * tf, gpointer unused)
4817 {
4818   const guint8 *data = gst_type_find_peek (tf, 0, 4);
4819
4820   if (data) {
4821     if (memcmp (data, ".snd", 4) == 0 || memcmp (data, "dns.", 4) == 0) {
4822       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AU_CAPS);
4823     }
4824   }
4825 }
4826
4827
4828 /*** video/x-nuv ***/
4829
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");
4835
4836 #define NUV_CAPS (gst_static_caps_get(&nuv_caps))
4837 static void
4838 nuv_type_find (GstTypeFind * tf, gpointer unused)
4839 {
4840   const guint8 *data = gst_type_find_peek (tf, 0, 11);
4841
4842   if (data) {
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);
4846     }
4847   }
4848 }
4849
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");
4853
4854 #define PARIS_CAPS (gst_static_caps_get(&paris_caps))
4855 static void
4856 paris_type_find (GstTypeFind * tf, gpointer unused)
4857 {
4858   const guint8 *data = gst_type_find_peek (tf, 0, 4);
4859
4860   if (data) {
4861     if (memcmp (data, " paf", 4) == 0 || memcmp (data, "fap ", 4) == 0) {
4862       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, PARIS_CAPS);
4863     }
4864   }
4865 }
4866
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))
4870
4871 static gsize
4872 sbc_check_header (const guint8 * data, gsize len, guint * rate,
4873     guint * channels)
4874 {
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;
4878
4879   if (data[0] != 0x9C || len < 4)
4880     return 0;
4881
4882   n_blocks = sbc_blocks[(data[1] >> 4) & 0x03];
4883   ch_mode = (data[1] >> 2) & 0x03;
4884   n_subbands = (data[1] & 0x01) ? 8 : 4;
4885   bitpool = data[2];
4886   if (bitpool < 2)
4887     return 0;
4888
4889   *rate = sbc_rates[(data[1] >> 6) & 0x03];
4890   *channels = (ch_mode == 0) ? 1 : 2;
4891
4892   if (ch_mode == 0)
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;
4900
4901   return 0;
4902 }
4903
4904 static void
4905 sbc_type_find (GstTypeFind * tf, gpointer unused)
4906 {
4907   const guint8 *data;
4908   gsize frame_len;
4909   guint i, rate, channels, offset = 0;
4910
4911   for (i = 0; i < 10; ++i) {
4912     data = gst_type_find_peek (tf, offset, 8);
4913     if (data == NULL)
4914       return;
4915
4916     frame_len = sbc_check_header (data, 8, &rate, &channels);
4917     if (frame_len == 0)
4918       return;
4919
4920     offset += frame_len;
4921   }
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);
4925 }
4926
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");
4930
4931 #define ILBC_CAPS (gst_static_caps_get(&ilbc_caps))
4932 static void
4933 ilbc_type_find (GstTypeFind * tf, gpointer unused)
4934 {
4935   const guint8 *data = gst_type_find_peek (tf, 0, 8);
4936
4937   if (data) {
4938     if (memcmp (data, "#!iLBC30", 8) == 0 || memcmp (data, "#!iLBC20", 8) == 0) {
4939       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, ILBC_CAPS);
4940     }
4941   }
4942 }
4943
4944 /*** application/x-ms-dos-executable ***/
4945
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 */
4950 static void
4951 msdos_type_find (GstTypeFind * tf, gpointer unused)
4952 {
4953   const guint8 *data = gst_type_find_peek (tf, 0, 64);
4954
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);
4958
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);
4962     }
4963   }
4964 }
4965
4966 /*** application/x-mmsh ***/
4967
4968 static GstStaticCaps mmsh_caps = GST_STATIC_CAPS ("application/x-mmsh");
4969
4970 #define MMSH_CAPS gst_static_caps_get(&mmsh_caps)
4971
4972 /* This is to recognise mssh-over-http */
4973 static void
4974 mmsh_type_find (GstTypeFind * tf, gpointer unused)
4975 {
4976   static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
4977     0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
4978   };
4979
4980   const guint8 *data;
4981
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);
4987   }
4988 }
4989
4990 /*** video/x-dirac ***/
4991
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");
4997
4998 #define DIRAC_CAPS (gst_static_caps_get(&dirac_caps))
4999 static void
5000 dirac_type_find (GstTypeFind * tf, gpointer unused)
5001 {
5002   const guint8 *data = gst_type_find_peek (tf, 0, 8);
5003
5004   if (data) {
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);
5007     }
5008   }
5009 }
5010
5011 /*** video/vivo ***/
5012
5013 static GstStaticCaps vivo_caps = GST_STATIC_CAPS ("video/vivo");
5014
5015 #define VIVO_CAPS gst_static_caps_get(&vivo_caps)
5016
5017 static void
5018 vivo_type_find (GstTypeFind * tf, gpointer unused)
5019 {
5020   static const guint8 vivo_marker[] = { 'V', 'e', 'r', 's', 'i', 'o', 'n',
5021     ':', 'V', 'i', 'v', 'o', '/'
5022   };
5023   const guint8 *data;
5024   guint hdr_len, pos;
5025
5026   data = gst_type_find_peek (tf, 0, 1024);
5027   if (data == NULL || data[0] != 0x00)
5028     return;
5029
5030   if ((data[1] & 0x80)) {
5031     if ((data[2] & 0x80))
5032       return;
5033     hdr_len = ((guint) (data[1] & 0x7f)) << 7;
5034     hdr_len += data[2];
5035     if (hdr_len > 2048)
5036       return;
5037     pos = 3;
5038   } else {
5039     hdr_len = data[1];
5040     pos = 2;
5041   }
5042
5043   /* 1008 = 1022 - strlen ("Version:Vivo/") - 1 */
5044   while (pos < 1008 && data[pos] == '\r' && data[pos + 1] == '\n')
5045     pos += 2;
5046
5047   if (memcmp (data + pos, vivo_marker, sizeof (vivo_marker)) == 0) {
5048     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VIVO_CAPS);
5049   }
5050 }
5051
5052 /*** XDG MIME typefinder (to avoid false positives mostly) ***/
5053
5054 #ifdef USE_GIO
5055 static void
5056 xdgmime_typefind (GstTypeFind * find, gpointer user_data)
5057 {
5058   gchar *mimetype;
5059   gsize length = 16384;
5060   guint64 tf_length;
5061   const guint8 *data;
5062   gchar *tmp;
5063
5064   if ((tf_length = gst_type_find_get_length (find)) > 0)
5065     length = MIN (length, tf_length);
5066
5067   if ((data = gst_type_find_peek (find, 0, length)) == NULL)
5068     return;
5069
5070   tmp = g_content_type_guess (NULL, data, length, NULL);
5071   if (tmp == NULL || g_content_type_is_unknown (tmp)) {
5072     g_free (tmp);
5073     return;
5074   }
5075
5076   mimetype = g_content_type_get_mime_type (tmp);
5077   g_free (tmp);
5078
5079   if (mimetype == NULL)
5080     return;
5081
5082   GST_DEBUG ("Got mimetype '%s'", mimetype);
5083
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
5088    *    probability)
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");
5095     g_free (mimetype);
5096     return;
5097   }
5098
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);
5104   g_free (mimetype);
5105 }
5106 #endif /* USE_GIO */
5107
5108 /*** Windows icon typefinder (to avoid false positives mostly) ***/
5109
5110 static void
5111 windows_icon_typefind (GstTypeFind * find, gpointer user_data)
5112 {
5113   const guint8 *data;
5114   gint64 datalen;
5115   guint16 type, nimages;
5116   gint32 size, offset;
5117
5118   datalen = gst_type_find_get_length (find);
5119   if ((data = gst_type_find_peek (find, 0, 6)) == NULL)
5120     return;
5121
5122   /* header - simple and not enough to rely on it alone */
5123   if (GST_READ_UINT16_LE (data) != 0)
5124     return;
5125   type = GST_READ_UINT16_LE (data + 2);
5126   if (type != 1 && type != 2)
5127     return;
5128   nimages = GST_READ_UINT16_LE (data + 4);
5129   if (nimages == 0)             /* we can assume we can't have an empty image file ? */
5130     return;
5131
5132   /* first image */
5133   if (data[6 + 3] != 0)
5134     return;
5135   if (type == 1) {
5136     guint16 planes = GST_READ_UINT16_LE (data + 6 + 4);
5137     if (planes > 1)
5138       return;
5139   }
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)
5144     return;
5145
5146   gst_type_find_suggest_simple (find, GST_TYPE_FIND_NEARLY_CERTAIN,
5147       "image/x-icon", NULL);
5148 }
5149
5150 /*** WAP WBMP typefinder ***/
5151
5152 static void
5153 wbmp_typefind (GstTypeFind * find, gpointer user_data)
5154 {
5155   const guint8 *data;
5156   gint64 datalen;
5157   guint w, h, size;
5158
5159   /* http://en.wikipedia.org/wiki/Wireless_Application_Protocol_Bitmap_Format */
5160   datalen = gst_type_find_get_length (find);
5161   if (datalen == 0)
5162     return;
5163
5164   data = gst_type_find_peek (find, 0, 5);
5165   if (data == NULL)
5166     return;
5167
5168   /* want 0x00 0x00 at start */
5169   if (*data++ != 0 || *data++ != 0)
5170     return;
5171
5172   /* min header size */
5173   size = 4;
5174
5175   /* let's assume max width/height is 65536 */
5176   w = *data++;
5177   if ((w & 0x80)) {
5178     w = (w << 8) | *data++;
5179     if ((w & 0x80))
5180       return;
5181     ++size;
5182     data = gst_type_find_peek (find, 4, 2);
5183     if (data == NULL)
5184       return;
5185   }
5186   h = *data++;
5187   if ((h & 0x80)) {
5188     h = (h << 8) | *data++;
5189     if ((h & 0x80))
5190       return;
5191     ++size;
5192   }
5193
5194   if (w == 0 || h == 0)
5195     return;
5196
5197   /* now add bitmap size */
5198   size += h * (GST_ROUND_UP_8 (w) / 8);
5199
5200   if (datalen == size) {
5201     gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE - 10,
5202         "image/vnd.wap.wbmp", NULL);
5203   }
5204 }
5205
5206 /*** DEGAS Atari images (also to avoid false positives, see #625129) ***/
5207 static void
5208 degas_type_find (GstTypeFind * tf, gpointer private)
5209 {
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 */
5212   gint64 len;
5213   const guint8 *data;
5214   guint16 resolution;
5215   int n;
5216
5217   len = gst_type_find_get_length (tf);
5218   if (len < 34)                 /* smallest header of the lot */
5219     return;
5220   data = gst_type_find_peek (tf, 0, 4);
5221   if (G_UNLIKELY (data == NULL))
5222     return;
5223   resolution = GST_READ_UINT16_BE (data);
5224   if (len == 32034) {
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))
5234         return;
5235       for (n = 0; n < 4; n++) {
5236         if (GST_READ_UINT16_BE (data + n * 2) > 2)
5237           return;
5238       }
5239       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5240           "image/x-degas", NULL);
5241     }
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))
5248         return;
5249       for (n = 0; n < 4; n++) {
5250         if (GST_READ_UINT16_BE (data + n * 2) > 2)
5251           return;
5252       }
5253       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5254           "image/x-degas", NULL);
5255     }
5256   }
5257 }
5258
5259 /*** y4m ***/
5260
5261 static void
5262 y4m_typefind (GstTypeFind * tf, gpointer private)
5263 {
5264   const guint8 *data;
5265
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);
5270   }
5271 }
5272
5273 /*** DVD ISO images (looks like H.264, see #674069) ***/
5274 static void
5275 dvdiso_type_find (GstTypeFind * tf, gpointer private)
5276 {
5277   /* 0x8000 bytes of zeros, then "\001CD001" */
5278   gint64 len;
5279   const guint8 *data;
5280
5281   len = gst_type_find_get_length (tf);
5282   if (len < 0x8006)
5283     return;
5284   data = gst_type_find_peek (tf, 0, 0x8006);
5285   if (G_UNLIKELY (data == NULL))
5286     return;
5287   for (len = 0; len < 0x8000; len++)
5288     if (data[len])
5289       return;
5290   /* Can the '1' be anything else ? My three samples all have '1'. */
5291   if (memcmp (data + 0x8000, "\001CD001", 6))
5292     return;
5293
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);
5297 }
5298
5299 /* SSA/ASS subtitles
5300  *
5301  * http://en.wikipedia.org/wiki/SubStation_Alpha
5302  * http://matroska.org/technical/specs/subtitles/ssa.html
5303  */
5304 static void
5305 ssa_type_find (GstTypeFind * tf, gpointer private)
5306 {
5307   const gchar *start, *end, *ver_str, *media_type = NULL;
5308   const guint8 *data;
5309   gchar *str, *script_type, *p = NULL;
5310   gint64 len;
5311
5312   data = gst_type_find_peek (tf, 0, 32);
5313
5314   if (data == NULL)
5315     return;
5316
5317   /* FIXME: detect utf-16/32 BOM and convert before typefinding the rest */
5318
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) {
5322     return;
5323   }
5324
5325   /* now check if we have SSA or ASS */
5326   len = gst_type_find_get_length (tf);
5327   if (len > 8192)
5328     len = 8192;
5329
5330   data = gst_type_find_peek (tf, 0, len);
5331   if (data == NULL)
5332     return;
5333
5334   /* skip BOM */
5335   start = (gchar *) memchr (data, '[', 5);
5336   g_assert (start);
5337   len -= (start - (gchar *) data);
5338
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");
5343     return;
5344   }
5345
5346   /* something at start,  but not a UTF-8 BOM? */
5347   if (data[0] != '[' && (data[0] != 0xEF || data[1] != 0xBB || data[2] != 0xBF))
5348     return;
5349
5350   /* ignore any partial UTF-8 characters at the end */
5351   len = end - start;
5352
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) {
5357     gdouble version;
5358
5359     ver_str = script_type + 11;
5360     while (*ver_str == ' ' || *ver_str == 'v' || *ver_str == 'V')
5361       ++ver_str;
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";
5367   }
5368
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";
5374   }
5375
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);
5379   } else {
5380     GST_WARNING ("could not detect SSA/ASS variant");
5381   }
5382
5383   g_free (str);
5384 }
5385
5386 /*** video/x-pva ***/
5387
5388 static GstStaticCaps pva_caps = GST_STATIC_CAPS ("video/x-pva");
5389
5390 #define PVA_CAPS gst_static_caps_get(&pva_caps)
5391
5392 static void
5393 pva_type_find (GstTypeFind * tf, gpointer private)
5394 {
5395   const guint8 *data;
5396
5397   data = gst_type_find_peek (tf, 0, 5);
5398
5399   if (data == NULL)
5400     return;
5401
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);
5404 }
5405
5406 /*** audio/audible ***/
5407
5408 /* derived from pyaudibletags
5409  * http://code.google.com/p/pyaudibletags/source/browse/trunk/pyaudibletags.py
5410  */
5411 static GstStaticCaps aa_caps = GST_STATIC_CAPS ("audio/x-audible");
5412
5413 #define AA_CAPS gst_static_caps_get(&aa_caps)
5414
5415 static void
5416 aa_type_find (GstTypeFind * tf, gpointer private)
5417 {
5418   const guint8 *data;
5419
5420   data = gst_type_find_peek (tf, 0, 12);
5421   if (data == NULL)
5422     return;
5423
5424   if (GST_READ_UINT32_BE (data + 4) == 0x57907536) {
5425     guint64 media_len;
5426
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);
5430     else
5431       gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AA_CAPS);
5432   }
5433 }
5434
5435 /*** generic typefind for streams that have some data at a specific position***/
5436 typedef struct
5437 {
5438   const guint8 *data;
5439   guint size;
5440   guint probability;
5441   GstCaps *caps;
5442 }
5443 GstTypeFindData;
5444
5445 static void
5446 start_with_type_find (GstTypeFind * tf, gpointer private)
5447 {
5448   GstTypeFindData *start_with = (GstTypeFindData *) private;
5449   const guint8 *data;
5450
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)),
5453       start_with->size);
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);
5457   }
5458 }
5459
5460 static void
5461 sw_data_destroy (GstTypeFindData * sw_data)
5462 {
5463   if (G_LIKELY (sw_data->caps != NULL))
5464     gst_caps_unref (sw_data->caps);
5465   g_slice_free (GstTypeFindData, sw_data);
5466 }
5467
5468 #define TYPE_FIND_REGISTER_START_WITH(plugin,name,rank,ext,_data,_size,_probability)\
5469 G_BEGIN_DECLS{                                                          \
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);                                          \
5479   }                                                                     \
5480 }G_END_DECLS
5481
5482 /*** same for riff types ***/
5483
5484 static void
5485 riff_type_find (GstTypeFind * tf, gpointer private)
5486 {
5487   GstTypeFindData *riff_data = (GstTypeFindData *) private;
5488   const guint8 *data = gst_type_find_peek (tf, 0, 12);
5489
5490   if (data && (memcmp (data, "RIFF", 4) == 0 || memcmp (data, "AVF0", 4) == 0)) {
5491     data += 8;
5492     if (memcmp (data, riff_data->data, 4) == 0)
5493       gst_type_find_suggest (tf, riff_data->probability, riff_data->caps);
5494   }
5495 }
5496
5497 #define TYPE_FIND_REGISTER_RIFF(plugin,name,rank,ext,_data)             \
5498 G_BEGIN_DECLS{                                                          \
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);                                          \
5508   }                                                                     \
5509 }G_END_DECLS
5510
5511
5512 /*** plugin initialization ***/
5513
5514 #define TYPE_FIND_REGISTER(plugin,name,rank,func,ext,caps,priv,notify) \
5515 G_BEGIN_DECLS{\
5516   if (!gst_type_find_register (plugin, name, rank, func, ext, caps, priv, notify))\
5517     return FALSE; \
5518 }G_END_DECLS
5519
5520
5521 static gboolean
5522 plugin_init (GstPlugin * plugin)
5523 {
5524   /* can't initialize this via a struct as caps can't be statically initialized */
5525
5526   GST_DEBUG_CATEGORY_INIT (type_find_debug, "typefindfunctions",
5527       GST_DEBUG_FG_GREEN | GST_DEBUG_BG_RED, "generic type find functions");
5528
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,
5532       "asf,wm,wma,wmv",
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,
5540       "avi", "AVI ");
5541   TYPE_FIND_REGISTER_RIFF (plugin, "audio/qcelp", GST_RANK_PRIMARY,
5542       "qcp", "QLCM");
5543   TYPE_FIND_REGISTER_RIFF (plugin, "video/x-cdxa", GST_RANK_PRIMARY,
5544       "dat", "CDXA");
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);
5550 #if 0
5551   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-smoke", GST_RANK_PRIMARY,
5552       NULL, "\x80smoke\x00\x01\x00", 6, GST_TYPE_FIND_MAXIMUM);
5553 #endif
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,
5587       NULL, NULL);
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);
5600
5601   /* ISO formats */
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);
5614
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,
5628       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",
5650       "WAVE");
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,
5684       "webp", "WEBP");
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);
5815
5816 #ifdef USE_GIO
5817   TYPE_FIND_REGISTER (plugin, "xdgmime-base", GST_RANK_MARGINAL,
5818       xdgmime_typefind, NULL, NULL, NULL, NULL);
5819 #endif
5820
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);
5825
5826   TYPE_FIND_REGISTER (plugin, "application/x-ssa", GST_RANK_SECONDARY,
5827       ssa_type_find, "ssa,ass", NULL, NULL, NULL);
5828
5829   TYPE_FIND_REGISTER (plugin, "video/x-pva", GST_RANK_SECONDARY,
5830       pva_type_find, "pva", PVA_CAPS, NULL, NULL);
5831
5832   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-xi", GST_RANK_SECONDARY,
5833       "xi", "Extended Instrument: ", 21, GST_TYPE_FIND_MAXIMUM);
5834
5835   TYPE_FIND_REGISTER (plugin, "audio/audible", GST_RANK_MARGINAL,
5836       aa_type_find, "aa,aax", AA_CAPS, NULL, NULL);
5837
5838   return TRUE;
5839 }
5840
5841 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
5842     GST_VERSION_MINOR,
5843     typefindfunctions,
5844     "default typefind functions",
5845     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)