Merge remote-tracking branch 'origin/master' into 0.11
[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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, 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 /* so our code stays ready for 0.11 */
50 #define gst_type_find_peek(tf,off,len) \
51     ((const guint8 *)gst_type_find_peek((tf),(off),(len)))
52
53 /* DataScanCtx: helper for typefind functions that scan through data
54  * step-by-step, to avoid doing a peek at each and every offset */
55
56 #define DATA_SCAN_CTX_CHUNK_SIZE 4096
57
58 typedef struct
59 {
60   guint64 offset;
61   const guint8 *data;
62   gint size;
63 } DataScanCtx;
64
65 static inline void
66 data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip)
67 {
68   c->offset += bytes_to_skip;
69   if (G_LIKELY (c->size > bytes_to_skip)) {
70     c->size -= bytes_to_skip;
71     c->data += bytes_to_skip;
72   } else {
73     c->data += c->size;
74     c->size = 0;
75   }
76 }
77
78 static inline gboolean
79 data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, gint min_len)
80 {
81   const guint8 *data;
82   guint64 len;
83   guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
84
85   if (G_LIKELY (c->size >= min_len))
86     return TRUE;
87
88   data = gst_type_find_peek (tf, c->offset, chunk_len);
89   if (G_LIKELY (data != NULL)) {
90     c->data = data;
91     c->size = chunk_len;
92     return TRUE;
93   }
94
95   /* if there's less than our chunk size, try to get as much as we can, but
96    * always at least min_len bytes (we might be typefinding the first buffer
97    * of the stream and not have as much data available as we'd like) */
98   len = gst_type_find_get_length (tf);
99   if (len > 0) {
100     len = CLAMP (len - c->offset, min_len, chunk_len);
101   } else {
102     len = min_len;
103   }
104
105   data = gst_type_find_peek (tf, c->offset, len);
106   if (data != NULL) {
107     c->data = data;
108     c->size = len;
109     return TRUE;
110   }
111
112   return FALSE;
113 }
114
115 static inline gboolean
116 data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset,
117     const gchar * data, guint len)
118 {
119   if (!data_scan_ctx_ensure_data (tf, c, offset + len))
120     return FALSE;
121
122   return (memcmp (c->data + offset, data, len) == 0);
123 }
124
125 /*** text/plain ***/
126 static gboolean xml_check_first_element (GstTypeFind * tf,
127     const gchar * element, guint elen, gboolean strict);
128 static gboolean sdp_check_header (GstTypeFind * tf);
129
130 static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
131
132 #define UTF8_CAPS gst_static_caps_get(&utf8_caps)
133
134 static gboolean
135 utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
136     GstTypeFindProbability * prob)
137 {
138   const guint8 *data;
139
140   /* randomly decided values */
141   guint min_size = 16;          /* minimum size  */
142   guint size = 32 * 1024;       /* starting size */
143   guint probability = 95;       /* starting probability */
144   guint step = 10;              /* how much we reduce probability in each
145                                  * iteration */
146
147   while (probability > step && size > min_size) {
148     data = gst_type_find_peek (tf, offset, size);
149     if (data) {
150       gchar *end;
151       gchar *start = (gchar *) data;
152
153       if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) {   /* allow last char to be cut off */
154         *prob = probability;
155         return TRUE;
156       }
157       *prob = 0;
158       return FALSE;
159     }
160     size /= 2;
161     probability -= step;
162   }
163   *prob = 0;
164   return FALSE;
165 }
166
167 static void
168 utf8_type_find (GstTypeFind * tf, gpointer unused)
169 {
170   GstTypeFindProbability start_prob, mid_prob;
171   guint64 length;
172
173   /* leave xml to the xml typefinders */
174   if (xml_check_first_element (tf, "", 0, TRUE))
175     return;
176
177   /* leave sdp to the sdp typefinders */
178   if (sdp_check_header (tf))
179     return;
180
181   /* check beginning of stream */
182   if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob))
183     return;
184
185   GST_LOG ("start is plain text with probability of %u", start_prob);
186
187   /* POSSIBLE is the highest probability we ever return if we can't
188    * probe into the middle of the file and don't know its length */
189
190   length = gst_type_find_get_length (tf);
191   if (length == 0 || length == (guint64) - 1) {
192     gst_type_find_suggest (tf, MIN (start_prob, GST_TYPE_FIND_POSSIBLE),
193         UTF8_CAPS);
194     return;
195   }
196
197   if (length < 64 * 1024) {
198     gst_type_find_suggest (tf, start_prob, UTF8_CAPS);
199     return;
200   }
201
202   /* check middle of stream */
203   if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob))
204     return;
205
206   GST_LOG ("middle is plain text with probability of %u", mid_prob);
207   gst_type_find_suggest (tf, (start_prob + mid_prob) / 2, UTF8_CAPS);
208 }
209
210 /*** text/utf-16 and text/utf-32} ***/
211 /* While UTF-8 is unicode too, using text/plain for UTF-16 and UTF-32
212    is going to break stuff. */
213
214 typedef struct
215 {
216   size_t bomlen;
217   const char *const bom;
218     gboolean (*checker) (const guint8 *, gint, gint);
219   int boost;
220   int endianness;
221 } GstUnicodeTester;
222
223 static gboolean
224 check_utf16 (const guint8 * data, gint len, gint endianness)
225 {
226   GstByteReader br;
227   guint16 high, low;
228
229   low = high = 0;
230
231   if (len & 1)
232     return FALSE;
233
234   gst_byte_reader_init (&br, data, len);
235   while (len >= 2) {
236     /* test first for a single 16 bit value in the BMP */
237     if (endianness == G_BIG_ENDIAN)
238       gst_byte_reader_get_uint16_be (&br, &high);
239     else
240       gst_byte_reader_get_uint16_le (&br, &high);
241     if (high >= 0xD800 && high <= 0xDBFF) {
242       /* start of a surrogate pair */
243       if (len < 4)
244         return FALSE;
245       len -= 2;
246       if (endianness == G_BIG_ENDIAN)
247         gst_byte_reader_get_uint16_be (&br, &low);
248       else
249         gst_byte_reader_get_uint16_le (&br, &low);
250       if (low >= 0xDC00 && low <= 0xDFFF) {
251         /* second half of the surrogate pair */
252       } else
253         return FALSE;
254     } else {
255       if (high >= 0xDC00 && high <= 0xDFFF)
256         return FALSE;
257     }
258     len -= 2;
259   }
260   return TRUE;
261 }
262
263 static gboolean
264 check_utf32 (const guint8 * data, gint len, gint endianness)
265 {
266   if (len & 3)
267     return FALSE;
268   while (len > 3) {
269     guint32 v;
270     if (endianness == G_BIG_ENDIAN)
271       v = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
272     else
273       v = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
274     if (v >= 0x10FFFF)
275       return FALSE;
276     data += 4;
277     len -= 4;
278   }
279   return TRUE;
280 }
281
282 static void
283 unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester,
284     guint n_tester, const char *media_type)
285 {
286   size_t n;
287   gint len = 4;
288   const guint8 *data = gst_type_find_peek (tf, 0, len);
289   int prob = -1;
290   const gint max_scan_size = 256 * 1024;
291   int endianness = 0;
292
293   if (!data) {
294     len = 2;
295     data = gst_type_find_peek (tf, 0, len);
296     if (!data)
297       return;
298   }
299
300   /* find a large enough size that works */
301   while (len < max_scan_size) {
302     size_t newlen = len << 1;
303     const guint8 *newdata = gst_type_find_peek (tf, 0, newlen);
304     if (!newdata)
305       break;
306     len = newlen;
307     data = newdata;
308   }
309
310   for (n = 0; n < n_tester; ++n) {
311     int bom_boost = 0, tmpprob;
312     if (len >= tester[n].bomlen) {
313       if (!memcmp (data, tester[n].bom, tester[n].bomlen))
314         bom_boost = tester[n].boost;
315     }
316     if (!(*tester[n].checker) (data, len, tester[n].endianness))
317       continue;
318     tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost;
319     if (tmpprob > prob) {
320       prob = tmpprob;
321       endianness = tester[n].endianness;
322     }
323   }
324
325   if (prob > 0) {
326     GST_DEBUG ("This is valid %s %s", media_type,
327         endianness == G_BIG_ENDIAN ? "be" : "le");
328     gst_type_find_suggest_simple (tf, prob, media_type,
329         "endianness", G_TYPE_INT, endianness, NULL);
330   }
331 }
332
333 static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16");
334
335 #define UTF16_CAPS gst_static_caps_get(&utf16_caps)
336
337 static void
338 utf16_type_find (GstTypeFind * tf, gpointer unused)
339 {
340   static const GstUnicodeTester utf16tester[2] = {
341     {2, "\xff\xfe", check_utf16, 10, G_LITTLE_ENDIAN},
342     {2, "\xfe\xff", check_utf16, 20, G_BIG_ENDIAN},
343   };
344   unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester),
345       "text/utf-16");
346 }
347
348 static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32");
349
350 #define UTF32_CAPS gst_static_caps_get(&utf32_caps)
351
352 static void
353 utf32_type_find (GstTypeFind * tf, gpointer unused)
354 {
355   static const GstUnicodeTester utf32tester[2] = {
356     {4, "\xff\xfe\x00\x00", check_utf32, 10, G_LITTLE_ENDIAN},
357     {4, "\x00\x00\xfe\xff", check_utf32, 20, G_BIG_ENDIAN}
358   };
359   unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester),
360       "text/utf-32");
361 }
362
363 /*** text/uri-list ***/
364
365 static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
366
367 #define URI_CAPS (gst_static_caps_get(&uri_caps))
368 #define BUFFER_SIZE 16          /* If the string is < 16 bytes we're screwed */
369 #define INC_BUFFER {                                                    \
370   pos++;                                                                \
371   if (pos == BUFFER_SIZE) {                                             \
372     pos = 0;                                                            \
373     offset += BUFFER_SIZE;                                              \
374     data = gst_type_find_peek (tf, offset, BUFFER_SIZE);                \
375     if (data == NULL) return;                                           \
376   } else {                                                              \
377     data++;                                                             \
378   }                                                                     \
379 }
380 static void
381 uri_type_find (GstTypeFind * tf, gpointer unused)
382 {
383   const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
384   guint pos = 0;
385   guint offset = 0;
386
387   if (data) {
388     /* Search for # comment lines */
389     while (*data == '#') {
390       /* Goto end of line */
391       while (*data != '\n') {
392         INC_BUFFER;
393       }
394
395       INC_BUFFER;
396     }
397
398     if (!g_ascii_isalpha (*data)) {
399       /* Had a non alpha char - can't be uri-list */
400       return;
401     }
402
403     INC_BUFFER;
404
405     while (g_ascii_isalnum (*data)) {
406       INC_BUFFER;
407     }
408
409     if (*data != ':') {
410       /* First non alpha char is not a : */
411       return;
412     }
413
414     /* Get the next 2 bytes as well */
415     data = gst_type_find_peek (tf, offset + pos, 3);
416     if (data == NULL)
417       return;
418
419     if (data[1] != '/' && data[2] != '/') {
420       return;
421     }
422
423     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, URI_CAPS);
424   }
425 }
426
427 /*** application/x-hls ***/
428
429 static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls");
430 #define HLS_CAPS (gst_static_caps_get(&hls_caps))
431
432 /* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */
433 static void
434 hls_type_find (GstTypeFind * tf, gpointer unused)
435 {
436   DataScanCtx c = { 0, NULL, 0 };
437
438   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 7)))
439     return;
440
441   if (memcmp (c.data, "#EXTM3U", 7))
442     return;
443
444   data_scan_ctx_advance (tf, &c, 7);
445
446   /* Check only the first 256 bytes */
447   while (c.offset < 256) {
448     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21)))
449       return;
450
451     /* Search for # comment lines */
452     if (c.data[0] == '#' && (memcmp (c.data, "#EXT-X-TARGETDURATION", 21) == 0
453             || memcmp (c.data, "#EXT-X-STREAM-INF", 17) == 0)) {
454       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HLS_CAPS);
455       return;
456     }
457
458     data_scan_ctx_advance (tf, &c, 1);
459   }
460 }
461
462
463 /*** application/xml **********************************************************/
464
465 #define XML_BUFFER_SIZE 16
466 #define XML_INC_BUFFER {                                                \
467   pos++;                                                                \
468   if (pos == XML_BUFFER_SIZE) {                                         \
469     pos = 0;                                                            \
470     offset += XML_BUFFER_SIZE;                                          \
471     data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE);            \
472     if (data == NULL) return FALSE;                                     \
473   } else {                                                              \
474     data++;                                                             \
475   }                                                                     \
476 }
477
478 static gboolean
479 xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
480     gboolean strict)
481 {
482   gboolean got_xmldec;
483   const guint8 *data;
484   guint offset = 0;
485   guint pos = 0;
486
487   data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
488   if (!data)
489     return FALSE;
490
491   /* look for the XMLDec
492    * see XML spec 2.8, Prolog and Document Type Declaration
493    * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
494   got_xmldec = (memcmp (data, "<?xml", 5) == 0);
495
496   if (strict && !got_xmldec)
497     return FALSE;
498
499   /* skip XMLDec in any case if we've got one */
500   if (got_xmldec) {
501     pos += 5;
502     data += 5;
503   }
504
505   /* look for the first element, it has to be the requested element. Bail
506    * out if it is not within the first 4kB. */
507   while (data && (offset + pos) < 4096) {
508     while (*data != '<' && (offset + pos) < 4096) {
509       XML_INC_BUFFER;
510     }
511
512     XML_INC_BUFFER;
513     if (!g_ascii_isalpha (*data)) {
514       /* if not alphabetic, it's a PI or an element / attribute declaration
515        * like <?xxx or <!xxx */
516       XML_INC_BUFFER;
517       continue;
518     }
519
520     /* the first normal element, check if it's the one asked for */
521     data = gst_type_find_peek (tf, offset + pos, elen + 1);
522     return (data && element && strncmp ((char *) data, element, elen) == 0);
523   }
524
525   return FALSE;
526 }
527
528 static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml");
529
530 #define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps))
531 static void
532 xml_type_find (GstTypeFind * tf, gpointer unused)
533 {
534   if (xml_check_first_element (tf, "", 0, TRUE)) {
535     gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
536   }
537 }
538
539 /*** application/sdp *********************************************************/
540
541 static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp");
542
543 #define SDP_CAPS (gst_static_caps_get(&sdp_caps))
544 static gboolean
545 sdp_check_header (GstTypeFind * tf)
546 {
547   const guint8 *data;
548
549   data = gst_type_find_peek (tf, 0, 5);
550   if (!data)
551     return FALSE;
552
553   /* sdp must start with v=0[\r]\n */
554   if (memcmp (data, "v=0", 3))
555     return FALSE;
556
557   if (data[3] == '\r' && data[4] == '\n')
558     return TRUE;
559   if (data[3] == '\n')
560     return TRUE;
561
562   return FALSE;
563 }
564
565 static void
566 sdp_type_find (GstTypeFind * tf, gpointer unused)
567 {
568   if (sdp_check_header (tf))
569     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS);
570 }
571
572 /*** application/smil *********************************************************/
573
574 static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil");
575
576 #define SMIL_CAPS (gst_static_caps_get(&smil_caps))
577 static void
578 smil_type_find (GstTypeFind * tf, gpointer unused)
579 {
580   if (xml_check_first_element (tf, "smil", 4, FALSE)) {
581     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
582   }
583 }
584
585 /*** text/html ***/
586
587 static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html");
588
589 #define HTML_CAPS gst_static_caps_get (&html_caps)
590
591 static void
592 html_type_find (GstTypeFind * tf, gpointer unused)
593 {
594   const gchar *d, *data;
595
596   data = (const gchar *) gst_type_find_peek (tf, 0, 16);
597   if (!data)
598     return;
599
600   if (!g_ascii_strncasecmp (data, "<!DOCTYPE HTML", 14)) {
601     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
602   } else if (xml_check_first_element (tf, "html", 4, FALSE)) {
603     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
604   } else if ((d = memchr (data, '<', 16))) {
605     data = (const gchar *) gst_type_find_peek (tf, d - data, 6);
606     if (data && g_ascii_strncasecmp (data, "<html>", 6) == 0) {
607       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
608     }
609   }
610 }
611
612 /*** audio/midi ***/
613
614 static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi");
615
616 #define MID_CAPS gst_static_caps_get(&mid_caps)
617 static void
618 mid_type_find (GstTypeFind * tf, gpointer unused)
619 {
620   const guint8 *data = gst_type_find_peek (tf, 0, 4);
621
622   /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
623   if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h'
624       && data[3] == 'd')
625     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS);
626 }
627
628 /*** audio/mobile-xmf ***/
629
630 static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf");
631
632 #define MXMF_CAPS gst_static_caps_get(&mxmf_caps)
633 static void
634 mxmf_type_find (GstTypeFind * tf, gpointer unused)
635 {
636   const guint8 *data = NULL;
637
638   /* Search FileId "XMF_" 4 bytes */
639   data = gst_type_find_peek (tf, 0, 4);
640   if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
641       && data[3] == '_') {
642     /* Search Format version "2.00" 4 bytes */
643     data = gst_type_find_peek (tf, 4, 4);
644     if (data && data[0] == '2' && data[1] == '.' && data[2] == '0'
645         && data[3] == '0') {
646       /* Search TypeId 2     1 byte */
647       data = gst_type_find_peek (tf, 11, 1);
648       if (data && data[0] == 2) {
649         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS);
650       }
651     }
652   }
653 }
654
655
656 /*** video/x-fli ***/
657
658 static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
659
660 #define FLX_CAPS gst_static_caps_get(&flx_caps)
661 static void
662 flx_type_find (GstTypeFind * tf, gpointer unused)
663 {
664   const guint8 *data = gst_type_find_peek (tf, 0, 134);
665
666   if (data) {
667     /* check magic and the frame type of the first frame */
668     if ((data[4] == 0x11 || data[4] == 0x12 ||
669             data[4] == 0x30 || data[4] == 0x44) &&
670         data[5] == 0xaf &&
671         ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
672       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS);
673     }
674     return;
675   }
676   data = gst_type_find_peek (tf, 0, 6);
677   if (data) {
678     /* check magic only */
679     if ((data[4] == 0x11 || data[4] == 0x12 ||
680             data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
681       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
682     }
683     return;
684   }
685 }
686
687 /*** application/x-id3 ***/
688
689 static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
690
691 #define ID3_CAPS gst_static_caps_get(&id3_caps)
692 static void
693 id3v2_type_find (GstTypeFind * tf, gpointer unused)
694 {
695   const guint8 *data = gst_type_find_peek (tf, 0, 10);
696
697   if (data && memcmp (data, "ID3", 3) == 0 &&
698       data[3] != 0xFF && data[4] != 0xFF &&
699       (data[6] & 0x80) == 0 && (data[7] & 0x80) == 0 &&
700       (data[8] & 0x80) == 0 && (data[9] & 0x80) == 0) {
701     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
702   }
703 }
704
705 static void
706 id3v1_type_find (GstTypeFind * tf, gpointer unused)
707 {
708   const guint8 *data = gst_type_find_peek (tf, -128, 3);
709
710   if (data && memcmp (data, "TAG", 3) == 0) {
711     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
712   }
713 }
714
715 /*** application/x-ape ***/
716
717 static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag");
718
719 #define APETAG_CAPS gst_static_caps_get(&apetag_caps)
720 static void
721 apetag_type_find (GstTypeFind * tf, gpointer unused)
722 {
723   const guint8 *data;
724
725   /* APEv1/2 at start of file */
726   data = gst_type_find_peek (tf, 0, 8);
727   if (data && !memcmp (data, "APETAGEX", 8)) {
728     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
729     return;
730   }
731
732   /* APEv1/2 at end of file */
733   data = gst_type_find_peek (tf, -32, 8);
734   if (data && !memcmp (data, "APETAGEX", 8)) {
735     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
736     return;
737   }
738 }
739
740 /*** audio/x-ttafile ***/
741
742 static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
743
744 #define TTA_CAPS gst_static_caps_get(&tta_caps)
745 static void
746 tta_type_find (GstTypeFind * tf, gpointer unused)
747 {
748   const guint8 *data = gst_type_find_peek (tf, 0, 3);
749
750   if (data) {
751     if (memcmp (data, "TTA", 3) == 0) {
752       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS);
753       return;
754     }
755   }
756 }
757
758 /*** audio/x-flac ***/
759 static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");
760
761 #define FLAC_CAPS (gst_static_caps_get(&flac_caps))
762
763 static void
764 flac_type_find (GstTypeFind * tf, gpointer unused)
765 {
766   DataScanCtx c = { 0, NULL, 0 };
767
768   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
769     return;
770
771   /* standard flac (also old/broken flac-in-ogg with an initial 4-byte marker
772    * packet and without the usual packet framing) */
773   if (memcmp (c.data, "fLaC", 4) == 0) {
774     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
775     return;
776   }
777
778   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
779     return;
780
781   /* flac-in-ogg, see http://flac.sourceforge.net/ogg_mapping.html */
782   if (memcmp (c.data, "\177FLAC\001", 6) == 0) {
783     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
784     return;
785   }
786
787 /* disabled because it happily typefinds /dev/urandom as audio/x-flac, and
788  * because I yet have to see header-less flac in the wild */
789 #if 0
790   /* flac without headers (subset format) */
791   /* 64K should be enough */
792   while (c.offset < (64 * 1024)) {
793     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
794       break;
795
796     /* look for frame header,
797      * http://flac.sourceforge.net/format.html#frame_header
798      */
799     if (c.data[0] == 0xff && (c.data[1] >> 2) == 0x3e) {
800       /* bit 15 in the header must be 0 */
801       if (((c.data[1] >> 1) & 0x01) == 0x01)
802         goto advance;
803
804       /* blocksize must be != 0x00 */
805       if ((c.data[2] >> 4) == 0x00)
806         goto advance;
807
808       /* samplerate must be != 0x0f */
809       if ((c.data[2] & 0x0f) == 0x0f)
810         goto advance;
811       /* also 0 is invalid, as it means get the info from the header and we
812        * don't have headers if we are here */
813       if ((c.data[2] & 0x0f) == 0x00)
814         goto advance;
815
816       /* channel assignment must be < 11 */
817       if ((c.data[3] >> 4) >= 11)
818         goto advance;
819
820       /* sample size must be != 0x07 and != 0x05 */
821       if (((c.data[3] >> 1) & 0x07) == 0x07)
822         goto advance;
823       if (((c.data[3] >> 1) & 0x07) == 0x05)
824         goto advance;
825       /* also 0 is invalid, as it means get the info from the header and we
826        * don't have headers if we are here */
827       if (((c.data[3] >> 1) & 0x07) == 0x00)
828         goto advance;
829
830       /* next bit must be 0 */
831       if ((c.data[3] & 0x01) == 0x01)
832         goto advance;
833
834       /* FIXME: shouldn't we include the crc check ? */
835
836       GST_DEBUG ("Found flac without headers at %d", (gint) c.offset);
837       gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS);
838       return;
839     }
840   advance:
841     data_scan_ctx_advance (tf, &c, 1);
842   }
843 #endif
844 }
845
846 /*** audio/mpeg version 2, 4 ***/
847
848 static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
849     "mpegversion = (int) { 2, 4 }, framed = (bool) false");
850 #define AAC_CAPS (gst_static_caps_get(&aac_caps))
851 #define AAC_AMOUNT (4096)
852 static void
853 aac_type_find (GstTypeFind * tf, gpointer unused)
854 {
855   /* LUT to convert the AudioObjectType from the ADTS header to a string */
856   DataScanCtx c = { 0, NULL, 0 };
857
858   while (c.offset < AAC_AMOUNT) {
859     guint snc, len;
860
861     /* detect adts header or adif header.
862      * The ADIF header is 4 bytes, that should be OK. The ADTS header, on
863      * the other hand, is 14 bits only, so we require one valid frame with
864      * again a valid syncpoint on the next one (28 bits) for certainty. We
865      * require 4 kB, which is quite a lot, since frames are generally 200-400
866      * bytes.
867      * LOAS has 2 possible syncwords, which are 11 bits and 16 bits long.
868      * The following stream syntax depends on which one is found.
869      */
870     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
871       break;
872
873     snc = GST_READ_UINT16_BE (c.data);
874     if (G_UNLIKELY ((snc & 0xfff6) == 0xfff0)) {
875       /* ADTS header - find frame length */
876       GST_DEBUG ("Found one ADTS syncpoint at offset 0x%" G_GINT64_MODIFIER
877           "x, tracing next...", c.offset);
878       len = ((c.data[3] & 0x03) << 11) |
879           (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);
880
881       if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
882         GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
883         goto next;
884       }
885
886       /* check if there's a second ADTS frame */
887       snc = GST_READ_UINT16_BE (c.data + len);
888       if ((snc & 0xfff6) == 0xfff0) {
889         GstCaps *caps;
890         guint mpegversion, sample_freq_idx, channel_config, profile_idx, rate;
891         guint8 audio_config[2];
892
893         mpegversion = (c.data[1] & 0x08) ? 2 : 4;
894         profile_idx = c.data[2] >> 6;
895         sample_freq_idx = ((c.data[2] & 0x3c) >> 2);
896         channel_config = ((c.data[2] & 0x01) << 2) + (c.data[3] >> 6);
897
898         GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%"
899             G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len);
900
901         /* 0xd and 0xe are reserved. 0xf means the sample frequency is directly
902          * specified in the header, but that's not allowed for ADTS */
903         if (sample_freq_idx > 0xc) {
904           GST_DEBUG ("Unexpected sample frequency index %d or wrong sync",
905               sample_freq_idx);
906           goto next;
907         }
908
909         rate = gst_codec_utils_aac_get_sample_rate_from_index (sample_freq_idx);
910         GST_LOG ("ADTS: profile=%u, rate=%u", profile_idx, rate);
911
912         /* The ADTS frame header is slightly different from the
913          * AudioSpecificConfig defined for the MPEG-4 container, so we just
914          * construct enough of it for getting the level here. */
915         /* ADTS counts profiles from 0 instead of 1 to save bits */
916         audio_config[0] = (profile_idx + 1) << 3;
917         audio_config[0] |= (sample_freq_idx >> 1) & 0x7;
918         audio_config[1] = (sample_freq_idx & 0x1) << 7;
919         audio_config[1] |= (channel_config & 0xf) << 3;
920
921         caps = gst_caps_new_simple ("audio/mpeg",
922             "framed", G_TYPE_BOOLEAN, FALSE,
923             "mpegversion", G_TYPE_INT, mpegversion,
924             "stream-format", G_TYPE_STRING, "adts", NULL);
925
926         gst_codec_utils_aac_caps_set_level_and_profile (caps, audio_config, 2);
927
928         /* add rate and number of channels if we can */
929         if (channel_config != 0 && channel_config <= 7) {
930           const guint channels_map[] = { 0, 1, 2, 3, 4, 5, 6, 8 };
931
932           gst_caps_set_simple (caps, "channels", G_TYPE_INT,
933               channels_map[channel_config], "rate", G_TYPE_INT, rate, NULL);
934         }
935
936         gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps);
937         gst_caps_unref (caps);
938         break;
939       }
940
941       GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
942     } else if (G_UNLIKELY (((snc & 0xffe0) == 0x56e0) || (snc == 0x4de1))) {
943       /* LOAS frame */
944
945       GST_DEBUG ("Found one LOAS syncword at offset 0x%" G_GINT64_MODIFIER
946           "x, tracing next...", c.offset);
947
948       /* check length of frame for each type of detectable LOAS streams */
949       if (snc == 0x4de1) {
950         /* EPAudioSyncStream */
951         len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
952             ((c.data[4] & 0x80) >> 7);
953         /* add size of EP sync stream header */
954         len += 7;
955       } else {
956         /* AudioSyncStream */
957         len = ((c.data[1] & 0x1f) << 8) | c.data[2];
958         /* add size of sync stream header */
959         len += 3;
960       }
961
962       if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
963         GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
964         goto next;
965       }
966
967       /* check if there's a second LOAS frame */
968       snc = GST_READ_UINT16_BE (c.data + len);
969       if (((snc & 0xffe0) == 0x56e0) || (snc == 0x4de1)) {
970         GST_DEBUG ("Found second LOAS syncword at offset 0x%"
971             G_GINT64_MODIFIER "x, framelen %u", c.offset, len);
972
973         gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
974             "framed", G_TYPE_BOOLEAN, FALSE,
975             "mpegversion", G_TYPE_INT, 4,
976             "stream-format", G_TYPE_STRING, "loas", NULL);
977         break;
978       }
979
980       GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
981     } else if (!memcmp (c.data, "ADIF", 4)) {
982       /* ADIF header */
983       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
984           "framed", G_TYPE_BOOLEAN, FALSE, "mpegversion", G_TYPE_INT, 4,
985           "stream-format", G_TYPE_STRING, "adif", NULL);
986       break;
987     }
988
989   next:
990
991     data_scan_ctx_advance (tf, &c, 1);
992   }
993 }
994
995 /*** audio/mpeg version 1 ***/
996
997 /*
998  * The chance that random data is identified as a valid mp3 header is 63 / 2^18
999  * (0.024%) per try. This makes the function for calculating false positives
1000  *   1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize)
1001  * This has the following probabilities of false positives:
1002  * datasize               MIN_HEADERS
1003  * (bytes)      1       2       3       4
1004  * 4096         62.6%    0.02%   0%      0%
1005  * 16384        98%      0.09%   0%      0%
1006  * 1 MiB       100%      5.88%   0%      0%
1007  * 1 GiB       100%    100%      1.44%   0%
1008  * 1 TiB       100%    100%    100%      0.35%
1009  * This means that the current choice (3 headers by most of the time 4096 byte
1010  * buffers is pretty safe for now.
1011  *
1012  * The max. size of each frame is 1440 bytes, which means that for N frames to
1013  * be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 bytes of data.
1014  * Assuming we step into the stream right after the frame header, this
1015  * means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes
1016  * of data (5762) to always detect any mp3.
1017  */
1018
1019 static const guint mp3types_bitrates[2][3][16] =
1020     { {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
1021     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
1022     {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
1023 {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
1024     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
1025     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}},
1026 };
1027
1028 static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
1029 {22050, 24000, 16000},
1030 {44100, 48000, 32000}
1031 };
1032
1033 static inline guint
1034 mp3_type_frame_length_from_header (guint32 header, guint * put_layer,
1035     guint * put_channels, guint * put_bitrate, guint * put_samplerate,
1036     gboolean * may_be_free_format, gint possible_free_framelen)
1037 {
1038   guint bitrate, layer, length, mode, samplerate, version, channels;
1039
1040   if ((header & 0xffe00000) != 0xffe00000)
1041     return 0;
1042
1043   /* we don't need extension, copyright, original or
1044    * emphasis for the frame length */
1045   header >>= 6;
1046
1047   /* mode */
1048   mode = header & 0x3;
1049   header >>= 3;
1050
1051   /* padding */
1052   length = header & 0x1;
1053   header >>= 1;
1054
1055   /* sampling frequency */
1056   samplerate = header & 0x3;
1057   if (samplerate == 3)
1058     return 0;
1059   header >>= 2;
1060
1061   /* bitrate index */
1062   bitrate = header & 0xF;
1063   if (bitrate == 0 && possible_free_framelen == -1) {
1064     GST_LOG ("Possibly a free format mp3 - signaling");
1065     *may_be_free_format = TRUE;
1066   }
1067   if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1))
1068     return 0;
1069
1070   /* ignore error correction, too */
1071   header >>= 5;
1072
1073   /* layer */
1074   layer = 4 - (header & 0x3);
1075   if (layer == 4)
1076     return 0;
1077   header >>= 2;
1078
1079   /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */
1080   version = header & 0x3;
1081   if (version == 1)
1082     return 0;
1083
1084   /* lookup */
1085   channels = (mode == 3) ? 1 : 2;
1086   samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
1087   if (bitrate == 0) {
1088     if (layer == 1) {
1089       length *= 4;
1090       length += possible_free_framelen;
1091       bitrate = length * samplerate / 48000;
1092     } else {
1093       length += possible_free_framelen;
1094       bitrate = length * samplerate /
1095           ((layer == 3 && version != 3) ? 72000 : 144000);
1096     }
1097   } else {
1098     /* calculating */
1099     bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
1100     if (layer == 1) {
1101       length = ((12000 * bitrate / samplerate) + length) * 4;
1102     } else {
1103       length += ((layer == 3
1104               && version != 3) ? 72000 : 144000) * bitrate / samplerate;
1105     }
1106   }
1107
1108   GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
1109   GST_LOG
1110       ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
1111       " - channels = %u", samplerate, bitrate, layer, version, channels);
1112
1113   if (put_layer)
1114     *put_layer = layer;
1115   if (put_channels)
1116     *put_channels = channels;
1117   if (put_bitrate)
1118     *put_bitrate = bitrate;
1119   if (put_samplerate)
1120     *put_samplerate = samplerate;
1121
1122   return length;
1123 }
1124
1125
1126 static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, "
1127     "mpegversion = (int) 1, layer = (int) [ 1, 3 ]");
1128 #define MP3_CAPS (gst_static_caps_get(&mp3_caps))
1129 /*
1130  * random values for typefinding
1131  * if no more data is available, we will return a probability of
1132  * (found_headers/TRY_HEADERS) * (MAXIMUM * (TRY_SYNC - bytes_skipped)
1133  *        / TRY_SYNC)
1134  * if found_headers >= MIN_HEADERS
1135  */
1136 #define GST_MP3_TYPEFIND_MIN_HEADERS (2)
1137 #define GST_MP3_TYPEFIND_TRY_HEADERS (5)
1138 #define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */
1139 #define GST_MP3_TYPEFIND_SYNC_SIZE (2048)
1140 #define GST_MP3_WRONG_HEADER (10)
1141
1142 static void
1143 mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
1144     guint * found_layer, GstTypeFindProbability * found_prob)
1145 {
1146   const guint8 *data = NULL;
1147   const guint8 *data_end = NULL;
1148   guint size;
1149   guint64 skipped;
1150   gint last_free_offset = -1;
1151   gint last_free_framelen = -1;
1152   gboolean headerstart = TRUE;
1153
1154   *found_layer = 0;
1155   *found_prob = 0;
1156
1157   size = 0;
1158   skipped = 0;
1159   while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) {
1160     if (size <= 0) {
1161       size = GST_MP3_TYPEFIND_SYNC_SIZE * 2;
1162       do {
1163         size /= 2;
1164         data = gst_type_find_peek (tf, skipped + start_off, size);
1165       } while (size > 10 && !data);
1166       if (!data)
1167         break;
1168       data_end = data + size;
1169     }
1170     if (*data == 0xFF) {
1171       const guint8 *head_data = NULL;
1172       guint layer = 0, bitrate, samplerate, channels;
1173       guint found = 0;          /* number of valid headers found */
1174       guint64 offset = skipped;
1175       gboolean changed = FALSE;
1176
1177       while (found < GST_MP3_TYPEFIND_TRY_HEADERS) {
1178         guint32 head;
1179         guint length;
1180         guint prev_layer = 0;
1181         guint prev_channels = 0, prev_samplerate = 0;
1182         gboolean free = FALSE;
1183
1184         if ((gint64) (offset - skipped + 4) >= 0 &&
1185             data + offset - skipped + 4 < data_end) {
1186           head_data = data + offset - skipped;
1187         } else {
1188           head_data = gst_type_find_peek (tf, offset + start_off, 4);
1189         }
1190         if (!head_data)
1191           break;
1192         head = GST_READ_UINT32_BE (head_data);
1193         if (!(length = mp3_type_frame_length_from_header (head, &layer,
1194                     &channels, &bitrate, &samplerate, &free,
1195                     last_free_framelen))) {
1196           if (free) {
1197             if (last_free_offset == -1)
1198               last_free_offset = offset;
1199             else {
1200               last_free_framelen = offset - last_free_offset;
1201               offset = last_free_offset;
1202               continue;
1203             }
1204           } else {
1205             last_free_framelen = -1;
1206           }
1207
1208           /* Mark the fact that we didn't find a valid header at the beginning */
1209           if (found == 0)
1210             headerstart = FALSE;
1211
1212           GST_LOG ("%d. header at offset %" G_GUINT64_FORMAT
1213               " (0x%" G_GINT64_MODIFIER "x) was not an mp3 header "
1214               "(possibly-free: %s)", found + 1, start_off + offset,
1215               start_off + offset, free ? "yes" : "no");
1216           break;
1217         }
1218         if ((prev_layer && prev_layer != layer) ||
1219             /* (prev_bitrate && prev_bitrate != bitrate) || <-- VBR */
1220             (prev_samplerate && prev_samplerate != samplerate) ||
1221             (prev_channels && prev_channels != channels)) {
1222           /* this means an invalid property, or a change, which might mean
1223            * that this is not a mp3 but just a random bytestream. It could
1224            * be a freaking funky encoded mp3 though. We'll just not count
1225            * this header*/
1226           if (prev_layer)
1227             changed = TRUE;
1228           prev_layer = layer;
1229           prev_channels = channels;
1230           prev_samplerate = samplerate;
1231         } else {
1232           found++;
1233           GST_LOG ("found %d. header at offset %" G_GUINT64_FORMAT " (0x%"
1234               G_GINT64_MODIFIER "X)", found, start_off + offset,
1235               start_off + offset);
1236         }
1237         offset += length;
1238       }
1239       g_assert (found <= GST_MP3_TYPEFIND_TRY_HEADERS);
1240       if (head_data == NULL &&
1241           gst_type_find_peek (tf, offset + start_off - 1, 1) == NULL)
1242         /* Incomplete last frame - don't count it. */
1243         found--;
1244       if (found == GST_MP3_TYPEFIND_TRY_HEADERS ||
1245           (found >= GST_MP3_TYPEFIND_MIN_HEADERS && head_data == NULL)) {
1246         /* we can make a valid guess */
1247         guint probability = found * GST_TYPE_FIND_MAXIMUM *
1248             (GST_MP3_TYPEFIND_TRY_SYNC - skipped) /
1249             GST_MP3_TYPEFIND_TRY_HEADERS / GST_MP3_TYPEFIND_TRY_SYNC;
1250
1251         if (!headerstart
1252             && probability > (GST_TYPE_FIND_MINIMUM + GST_MP3_WRONG_HEADER))
1253           probability -= GST_MP3_WRONG_HEADER;
1254         if (probability < GST_TYPE_FIND_MINIMUM)
1255           probability = GST_TYPE_FIND_MINIMUM;
1256         if (start_off > 0)
1257           probability /= 2;
1258         if (!changed)
1259           probability = (probability + GST_TYPE_FIND_MAXIMUM) / 2;
1260
1261         GST_INFO
1262             ("audio/mpeg calculated %u  =  %u  *  %u / %u  *  (%u - %"
1263             G_GUINT64_FORMAT ") / %u", probability, GST_TYPE_FIND_MAXIMUM,
1264             found, GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC,
1265             (guint64) skipped, GST_MP3_TYPEFIND_TRY_SYNC);
1266         /* make sure we're not id3 tagged */
1267         head_data = gst_type_find_peek (tf, -128, 3);
1268         if (head_data && (memcmp (head_data, "TAG", 3) == 0)) {
1269           probability = 0;
1270         }
1271         g_assert (probability <= GST_TYPE_FIND_MAXIMUM);
1272
1273         *found_prob = probability;
1274         if (probability > 0)
1275           *found_layer = layer;
1276         return;
1277       }
1278     }
1279     data++;
1280     skipped++;
1281     size--;
1282   }
1283 }
1284
1285 static void
1286 mp3_type_find (GstTypeFind * tf, gpointer unused)
1287 {
1288   GstTypeFindProbability prob, mid_prob;
1289   const guint8 *data;
1290   guint layer, mid_layer;
1291   guint64 length;
1292
1293   mp3_type_find_at_offset (tf, 0, &layer, &prob);
1294   length = gst_type_find_get_length (tf);
1295
1296   if (length == 0 || length == (guint64) - 1) {
1297     if (prob != 0)
1298       goto suggest;
1299     return;
1300   }
1301
1302   /* if we're pretty certain already, skip the additional check */
1303   if (prob >= GST_TYPE_FIND_LIKELY)
1304     goto suggest;
1305
1306   mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob);
1307
1308   if (mid_prob > 0) {
1309     if (prob == 0) {
1310       GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob);
1311       layer = mid_layer;
1312       prob = mid_prob;
1313       goto suggest;
1314     }
1315
1316     if (layer != mid_layer) {
1317       GST_WARNING ("audio/mpeg layer discrepancy: %u vs. %u", layer, mid_layer);
1318       return;                   /* FIXME: or should we just go with the one in the middle? */
1319     }
1320
1321     /* detected mpeg audio both in middle of the file and at the start */
1322     prob = (prob + mid_prob) / 2;
1323     goto suggest;
1324   }
1325
1326   /* let's see if there's a valid header right at the start */
1327   data = gst_type_find_peek (tf, 0, 4); /* use min. frame size? */
1328   if (data && mp3_type_frame_length_from_header (GST_READ_UINT32_BE (data),
1329           &layer, NULL, NULL, NULL, NULL, 0) != 0) {
1330     if (prob == 0)
1331       prob = GST_TYPE_FIND_POSSIBLE - 10;
1332     else
1333       prob = MAX (GST_TYPE_FIND_POSSIBLE - 10, prob + 10);
1334   }
1335
1336   if (prob > 0)
1337     goto suggest;
1338
1339   return;
1340
1341 suggest:
1342   {
1343     g_return_if_fail (layer >= 1 && layer <= 3);
1344
1345     gst_type_find_suggest_simple (tf, prob, "audio/mpeg",
1346         "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
1347   }
1348 }
1349
1350 /*** audio/x-musepack ***/
1351
1352 static GstStaticCaps musepack_caps =
1353 GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }");
1354
1355 #define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps))
1356 static void
1357 musepack_type_find (GstTypeFind * tf, gpointer unused)
1358 {
1359   const guint8 *data = gst_type_find_peek (tf, 0, 4);
1360   GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM;
1361   gint streamversion = -1;
1362
1363   if (data && memcmp (data, "MP+", 3) == 0) {
1364     streamversion = 7;
1365     if ((data[3] & 0x7f) == 7) {
1366       prop = GST_TYPE_FIND_MAXIMUM;
1367     } else {
1368       prop = GST_TYPE_FIND_LIKELY + 10;
1369     }
1370   } else if (data && memcmp (data, "MPCK", 4) == 0) {
1371     streamversion = 8;
1372     prop = GST_TYPE_FIND_MAXIMUM;
1373   }
1374
1375   if (streamversion != -1) {
1376     gst_type_find_suggest_simple (tf, prop, "audio/x-musepack",
1377         "streamversion", G_TYPE_INT, streamversion, NULL);
1378   }
1379 }
1380
1381 /*** audio/x-ac3 ***/
1382 /* FIXME 0.11: should be audio/ac3, but isn't for backwards compatibility */
1383 static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3");
1384
1385 #define AC3_CAPS (gst_static_caps_get(&ac3_caps))
1386
1387 static GstStaticCaps eac3_caps = GST_STATIC_CAPS ("audio/x-eac3");
1388
1389 #define EAC3_CAPS (gst_static_caps_get(&eac3_caps))
1390
1391 struct ac3_frmsize
1392 {
1393   unsigned short bit_rate;
1394   unsigned short frm_size[3];
1395 };
1396
1397 static const struct ac3_frmsize ac3_frmsizecod_tbl[] = {
1398   {32, {64, 69, 96}},
1399   {32, {64, 70, 96}},
1400   {40, {80, 87, 120}},
1401   {40, {80, 88, 120}},
1402   {48, {96, 104, 144}},
1403   {48, {96, 105, 144}},
1404   {56, {112, 121, 168}},
1405   {56, {112, 122, 168}},
1406   {64, {128, 139, 192}},
1407   {64, {128, 140, 192}},
1408   {80, {160, 174, 240}},
1409   {80, {160, 175, 240}},
1410   {96, {192, 208, 288}},
1411   {96, {192, 209, 288}},
1412   {112, {224, 243, 336}},
1413   {112, {224, 244, 336}},
1414   {128, {256, 278, 384}},
1415   {128, {256, 279, 384}},
1416   {160, {320, 348, 480}},
1417   {160, {320, 349, 480}},
1418   {192, {384, 417, 576}},
1419   {192, {384, 418, 576}},
1420   {224, {448, 487, 672}},
1421   {224, {448, 488, 672}},
1422   {256, {512, 557, 768}},
1423   {256, {512, 558, 768}},
1424   {320, {640, 696, 960}},
1425   {320, {640, 697, 960}},
1426   {384, {768, 835, 1152}},
1427   {384, {768, 836, 1152}},
1428   {448, {896, 975, 1344}},
1429   {448, {896, 976, 1344}},
1430   {512, {1024, 1114, 1536}},
1431   {512, {1024, 1115, 1536}},
1432   {576, {1152, 1253, 1728}},
1433   {576, {1152, 1254, 1728}},
1434   {640, {1280, 1393, 1920}},
1435   {640, {1280, 1394, 1920}}
1436 };
1437
1438 static void
1439 ac3_type_find (GstTypeFind * tf, gpointer unused)
1440 {
1441   DataScanCtx c = { 0, NULL, 0 };
1442
1443   /* Search for an ac3 frame; not necessarily right at the start, but give it
1444    * a lower probability if not found right at the start. Check that the
1445    * frame is followed by a second frame at the expected offset.
1446    * We could also check the two ac3 CRCs, but we don't do that right now */
1447   while (c.offset < 1024) {
1448     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5)))
1449       break;
1450
1451     if (c.data[0] == 0x0b && c.data[1] == 0x77) {
1452       guint bsid = c.data[5] >> 3;
1453
1454       if (bsid <= 8) {
1455         /* ac3 */
1456         guint fscod = c.data[4] >> 6;
1457         guint frmsizecod = c.data[4] & 0x3f;
1458
1459         if (fscod < 3 && frmsizecod < 38) {
1460           DataScanCtx c_next = c;
1461           guint frame_size;
1462
1463           frame_size = ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod];
1464           GST_LOG ("possible AC3 frame sync at offset %"
1465               G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1466           if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1467             data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1468
1469             if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1470               fscod = c_next.data[4] >> 6;
1471               frmsizecod = c_next.data[4] & 0x3f;
1472
1473               if (fscod < 3 && frmsizecod < 38) {
1474                 GstTypeFindProbability prob;
1475
1476                 GST_LOG ("found second AC3 frame (size=%u), looks good",
1477                     ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]);
1478                 if (c.offset == 0)
1479                   prob = GST_TYPE_FIND_MAXIMUM;
1480                 else
1481                   prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1482
1483                 gst_type_find_suggest (tf, prob, AC3_CAPS);
1484                 return;
1485               }
1486             } else {
1487               GST_LOG ("no second AC3 frame found, false sync");
1488             }
1489           }
1490         }
1491       } else if (bsid <= 16 && bsid > 10) {
1492         /* eac3 */
1493         DataScanCtx c_next = c;
1494         guint frame_size;
1495
1496         frame_size = (((c.data[2] & 0x07) << 8) + c.data[3]) + 1;
1497         GST_LOG ("possible E-AC3 frame sync at offset %"
1498             G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1499         if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1500           data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1501
1502           if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1503             GstTypeFindProbability prob;
1504
1505             GST_LOG ("found second E-AC3 frame, looks good");
1506             if (c.offset == 0)
1507               prob = GST_TYPE_FIND_MAXIMUM;
1508             else
1509               prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1510
1511             gst_type_find_suggest (tf, prob, EAC3_CAPS);
1512             return;
1513           } else {
1514             GST_LOG ("no second E-AC3 frame found, false sync");
1515           }
1516         }
1517       } else {
1518         GST_LOG ("invalid AC3 BSID: %u", bsid);
1519       }
1520     }
1521     data_scan_ctx_advance (tf, &c, 1);
1522   }
1523 }
1524
1525 /*** audio/x-dts ***/
1526 static GstStaticCaps dts_caps = GST_STATIC_CAPS ("audio/x-dts");
1527 #define DTS_CAPS (gst_static_caps_get (&dts_caps))
1528 #define DTS_MIN_FRAMESIZE 96
1529 #define DTS_MAX_FRAMESIZE 18725 /* 16384*16/14 */
1530
1531 static gboolean
1532 dts_parse_frame_header (DataScanCtx * c, guint * frame_size,
1533     guint * sample_rate, guint * channels, guint * depth, guint * endianness)
1534 {
1535   static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
1536     22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
1537   };
1538   static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
1539     6, 6, 6, 7, 8, 8
1540   };
1541   guint16 hdr[8];
1542   guint32 marker;
1543   guint num_blocks, chans, lfe, i;
1544
1545   marker = GST_READ_UINT32_BE (c->data);
1546
1547   /* raw big endian or 14-bit big endian */
1548   if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
1549     *endianness = G_BIG_ENDIAN;
1550     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1551       hdr[i] = GST_READ_UINT16_BE (c->data + (i * sizeof (guint16)));
1552   } else
1553     /* raw little endian or 14-bit little endian */
1554   if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
1555     *endianness = G_LITTLE_ENDIAN;
1556     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1557       hdr[i] = GST_READ_UINT16_LE (c->data + (i * sizeof (guint16)));
1558   } else {
1559     return FALSE;
1560   }
1561
1562   GST_LOG ("dts sync marker 0x%08x at offset %u", marker, (guint) c->offset);
1563
1564   /* 14-bit mode */
1565   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
1566     if ((hdr[2] & 0xFFF0) != 0x07F0)
1567       return FALSE;
1568     /* discard top 2 bits (2 void), shift in 2 */
1569     hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
1570     /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
1571     hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
1572     hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
1573     hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
1574     hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
1575     hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
1576     hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
1577     g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
1578     *depth = 14;
1579   } else {
1580     *depth = 16;
1581   }
1582
1583   GST_LOG ("frame header: %04x%04x%04x%04x", hdr[2], hdr[3], hdr[4], hdr[5]);
1584
1585   num_blocks = (hdr[2] >> 2) & 0x7F;
1586   *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
1587   chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
1588   *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
1589   lfe = (hdr[5] >> 9) & 0x03;
1590
1591   if (num_blocks < 5 || *frame_size < 96 || *sample_rate == 0)
1592     return FALSE;
1593
1594   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
1595     *frame_size = (*frame_size * 16) / 14;      /* FIXME: round up? */
1596
1597   if (chans < G_N_ELEMENTS (channels_table))
1598     *channels = channels_table[chans] + ((lfe) ? 1 : 0);
1599   else
1600     *channels = 0;
1601
1602   return TRUE;
1603 }
1604
1605 static void
1606 dts_type_find (GstTypeFind * tf, gpointer unused)
1607 {
1608   DataScanCtx c = { 0, NULL, 0 };
1609
1610   /* Search for an dts frame; not necessarily right at the start, but give it
1611    * a lower probability if not found right at the start. Check that the
1612    * frame is followed by a second frame at the expected offset. */
1613   while (c.offset <= DTS_MAX_FRAMESIZE) {
1614     guint frame_size = 0, rate = 0, chans = 0, depth = 0, endianness = 0;
1615
1616     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, DTS_MIN_FRAMESIZE)))
1617       return;
1618
1619     if (G_UNLIKELY (dts_parse_frame_header (&c, &frame_size, &rate, &chans,
1620                 &depth, &endianness))) {
1621       GstTypeFindProbability prob;
1622       DataScanCtx next_c;
1623
1624       prob = (c.offset == 0) ? GST_TYPE_FIND_LIKELY : GST_TYPE_FIND_POSSIBLE;
1625
1626       /* check for second frame sync */
1627       next_c = c;
1628       data_scan_ctx_advance (tf, &next_c, frame_size);
1629       if (data_scan_ctx_ensure_data (tf, &next_c, 4)) {
1630         GST_LOG ("frame size: %u 0x%04x", frame_size, frame_size);
1631         GST_MEMDUMP ("second frame sync", next_c.data, 4);
1632         if (GST_READ_UINT32_BE (c.data) == GST_READ_UINT32_BE (next_c.data))
1633           prob = GST_TYPE_FIND_MAXIMUM;
1634       }
1635
1636       if (chans > 0) {
1637         gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1638             "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
1639             "depth", G_TYPE_INT, depth, "endianness", G_TYPE_INT, endianness,
1640             "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1641       } else {
1642         gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1643             "rate", G_TYPE_INT, rate, "depth", G_TYPE_INT, depth,
1644             "endianness", G_TYPE_INT, endianness,
1645             "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1646       }
1647
1648       return;
1649     }
1650
1651     data_scan_ctx_advance (tf, &c, 1);
1652   }
1653 }
1654
1655 /*** gsm ***/
1656
1657 /* can only be detected by using the extension, in which case we use the default
1658  * GSM properties */
1659 static GstStaticCaps gsm_caps =
1660 GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1");
1661
1662 #define GSM_CAPS (gst_static_caps_get(&gsm_caps))
1663
1664 /*** wavpack ***/
1665
1666 static GstStaticCaps wavpack_caps =
1667 GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false");
1668
1669 #define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps))
1670
1671 static GstStaticCaps wavpack_correction_caps =
1672 GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");
1673
1674 #define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps))
1675
1676 static void
1677 wavpack_type_find (GstTypeFind * tf, gpointer unused)
1678 {
1679   guint64 offset;
1680   guint32 blocksize;
1681   const guint8 *data;
1682
1683   data = gst_type_find_peek (tf, 0, 32);
1684   if (!data)
1685     return;
1686
1687   if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k')
1688     return;
1689
1690   /* Note: wavpack blocks can be fairly large (easily 60-110k), possibly
1691    * larger than the max. limits imposed by certain typefinding elements
1692    * like id3demux or apedemux, so typefinding is most likely only going to
1693    * work in pull-mode */
1694   blocksize = GST_READ_UINT32_LE (data + 4);
1695   GST_LOG ("wavpack header, blocksize=0x%04x", blocksize);
1696   offset = 32;
1697   while (offset < 32 + blocksize) {
1698     guint32 sublen;
1699
1700     /* get chunk header */
1701     GST_LOG ("peeking at chunk at offset 0x%04x", (guint) offset);
1702     data = gst_type_find_peek (tf, offset, 4);
1703     if (data == NULL)
1704       break;
1705     sublen = ((guint32) data[1]) << 1;
1706     if (data[0] & 0x80) {
1707       sublen |= (((guint32) data[2]) << 9) | (((guint32) data[3]) << 17);
1708       sublen += 1 + 3;          /* id + length */
1709     } else {
1710       sublen += 1 + 1;          /* id + length */
1711     }
1712     if (sublen > blocksize - offset + 32) {
1713       GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
1714           blocksize - offset);
1715       break;
1716     }
1717     if ((data[0] & 0x20) == 0) {
1718       switch (data[0] & 0x0f) {
1719         case 0xa:              /* ID_WV_BITSTREAM  */
1720         case 0xc:              /* ID_WVX_BITSTREAM */
1721           gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, WAVPACK_CAPS);
1722           return;
1723         case 0xb:              /* ID_WVC_BITSTREAM */
1724           gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY,
1725               WAVPACK_CORRECTION_CAPS);
1726           return;
1727         default:
1728           break;
1729       }
1730     }
1731     offset += sublen;
1732   }
1733 }
1734
1735 /*** application/postscrip ***/
1736 static GstStaticCaps postscript_caps =
1737 GST_STATIC_CAPS ("application/postscript");
1738
1739 #define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps))
1740
1741 static void
1742 postscript_type_find (GstTypeFind * tf, gpointer unused)
1743 {
1744   const guint8 *data = gst_type_find_peek (tf, 0, 3);
1745   if (!data)
1746     return;
1747
1748   if (data[0] == 0x04)
1749     data++;
1750   if (data[0] == '%' && data[1] == '!')
1751     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS);
1752
1753 }
1754
1755 /*** image/svg+xml ***/
1756 static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml");
1757
1758 #define SVG_CAPS (gst_static_caps_get(&svg_caps))
1759
1760 static void
1761 svg_type_find (GstTypeFind * tf, gpointer unused)
1762 {
1763   static const gchar svg_doctype[] = "!DOCTYPE svg";
1764   static const gchar svg_tag[] = "<svg";
1765   DataScanCtx c = { 0, NULL, 0 };
1766
1767   while (c.offset <= 1024) {
1768     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
1769       break;
1770
1771     if (memcmp (svg_doctype, c.data, 12) == 0) {
1772       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS);
1773       return;
1774     } else if (memcmp (svg_tag, c.data, 4) == 0) {
1775       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS);
1776       return;
1777     }
1778     data_scan_ctx_advance (tf, &c, 1);
1779   }
1780 }
1781
1782 /*** multipart/x-mixed-replace mimestream ***/
1783
1784 static GstStaticCaps multipart_caps =
1785 GST_STATIC_CAPS ("multipart/x-mixed-replace");
1786 #define MULTIPART_CAPS gst_static_caps_get(&multipart_caps)
1787
1788 /* multipart/x-mixed replace is: 
1789  *   <maybe some whitespace>--<some ascii chars>[\r]\n
1790  *   <more ascii chars>[\r]\nContent-type:<more ascii>[\r]\n */
1791 static void
1792 multipart_type_find (GstTypeFind * tf, gpointer unused)
1793 {
1794   const guint8 *data;
1795   const guint8 *x;
1796
1797 #define MULTIPART_MAX_BOUNDARY_OFFSET 16
1798   data = gst_type_find_peek (tf, 0, MULTIPART_MAX_BOUNDARY_OFFSET);
1799   if (!data)
1800     return;
1801
1802   for (x = data;
1803       x - data < MULTIPART_MAX_BOUNDARY_OFFSET - 2 && g_ascii_isspace (*x);
1804       x++);
1805   if (x[0] != '-' || x[1] != '-')
1806     return;
1807
1808   /* Could be okay, peek what should be enough for a complete header */
1809 #define MULTIPART_MAX_HEADER_SIZE 256
1810   data = gst_type_find_peek (tf, 0, MULTIPART_MAX_HEADER_SIZE);
1811   if (!data)
1812     return;
1813
1814   for (x = data; x - data < MULTIPART_MAX_HEADER_SIZE - 14; x++) {
1815     if (!isascii (*x)) {
1816       return;
1817     }
1818     if (*x == '\n' &&
1819         !g_ascii_strncasecmp ("content-type:", (gchar *) x + 1, 13)) {
1820       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MULTIPART_CAPS);
1821       return;
1822     }
1823   }
1824 }
1825
1826 /*** video/mpeg systemstream ***/
1827 static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, "
1828     "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]");
1829
1830 #define MPEG_SYS_CAPS gst_static_caps_get(&mpeg_sys_caps)
1831 #define IS_MPEG_HEADER(data) (G_UNLIKELY((((guint8 *)(data))[0] == 0x00) &&  \
1832                                          (((guint8 *)(data))[1] == 0x00) &&  \
1833                                          (((guint8 *)(data))[2] == 0x01)))
1834
1835 #define IS_MPEG_PACK_CODE(b) ((b) == 0xBA)
1836 #define IS_MPEG_SYS_CODE(b) ((b) == 0xBB)
1837 #define IS_MPEG_PACK_HEADER(data)       (IS_MPEG_HEADER (data) &&            \
1838                                          IS_MPEG_PACK_CODE (((guint8 *)(data))[3]))
1839
1840 #define IS_MPEG_PES_CODE(b) (((b) & 0xF0) == 0xE0 || ((b) & 0xF0) == 0xC0 || \
1841                              (b) >= 0xBD)
1842 #define IS_MPEG_PES_HEADER(data)        (IS_MPEG_HEADER (data) &&            \
1843                                          IS_MPEG_PES_CODE (((guint8 *)(data))[3]))
1844
1845 #define MPEG2_MAX_PROBE_LENGTH (128 * 1024)     /* 128kB should be 64 packs of the 
1846                                                  * most common 2kB pack size. */
1847
1848 #define MPEG2_MIN_SYS_HEADERS 2
1849 #define MPEG2_MAX_SYS_HEADERS 5
1850
1851 static gboolean
1852 mpeg_sys_is_valid_pack (GstTypeFind * tf, const guint8 * data, guint len,
1853     guint * pack_size)
1854 {
1855   /* Check the pack header @ offset for validity, assuming that the 4 byte header
1856    * itself has already been checked. */
1857   guint8 stuff_len;
1858
1859   if (len < 12)
1860     return FALSE;
1861
1862   /* Check marker bits */
1863   if ((data[4] & 0xC4) == 0x44) {
1864     /* MPEG-2 PACK */
1865     if (len < 14)
1866       return FALSE;
1867
1868     if ((data[6] & 0x04) != 0x04 ||
1869         (data[8] & 0x04) != 0x04 ||
1870         (data[9] & 0x01) != 0x01 || (data[12] & 0x03) != 0x03)
1871       return FALSE;
1872
1873     stuff_len = data[13] & 0x07;
1874
1875     /* Check the following header bytes, if we can */
1876     if ((14 + stuff_len + 4) <= len) {
1877       if (!IS_MPEG_HEADER (data + 14 + stuff_len))
1878         return FALSE;
1879     }
1880     if (pack_size)
1881       *pack_size = 14 + stuff_len;
1882     return TRUE;
1883   } else if ((data[4] & 0xF1) == 0x21) {
1884     /* MPEG-1 PACK */
1885     if ((data[6] & 0x01) != 0x01 ||
1886         (data[8] & 0x01) != 0x01 ||
1887         (data[9] & 0x80) != 0x80 || (data[11] & 0x01) != 0x01)
1888       return FALSE;
1889
1890     /* Check the following header bytes, if we can */
1891     if ((12 + 4) <= len) {
1892       if (!IS_MPEG_HEADER (data + 12))
1893         return FALSE;
1894     }
1895     if (pack_size)
1896       *pack_size = 12;
1897     return TRUE;
1898   }
1899
1900   return FALSE;
1901 }
1902
1903 static gboolean
1904 mpeg_sys_is_valid_pes (GstTypeFind * tf, const guint8 * data, guint len,
1905     guint * pack_size)
1906 {
1907   guint pes_packet_len;
1908
1909   /* Check the PES header at the given position, assuming the header code itself
1910    * was already checked */
1911   if (len < 6)
1912     return FALSE;
1913
1914   /* For MPEG Program streams, unbounded PES is not allowed, so we must have a
1915    * valid length present */
1916   pes_packet_len = GST_READ_UINT16_BE (data + 4);
1917   if (pes_packet_len == 0)
1918     return FALSE;
1919
1920   /* Check the following header, if we can */
1921   if (6 + pes_packet_len + 4 <= len) {
1922     if (!IS_MPEG_HEADER (data + 6 + pes_packet_len))
1923       return FALSE;
1924   }
1925
1926   if (pack_size)
1927     *pack_size = 6 + pes_packet_len;
1928   return TRUE;
1929 }
1930
1931 static gboolean
1932 mpeg_sys_is_valid_sys (GstTypeFind * tf, const guint8 * data, guint len,
1933     guint * pack_size)
1934 {
1935   guint sys_hdr_len;
1936
1937   /* Check the System header at the given position, assuming the header code itself
1938    * was already checked */
1939   if (len < 6)
1940     return FALSE;
1941   sys_hdr_len = GST_READ_UINT16_BE (data + 4);
1942   if (sys_hdr_len < 6)
1943     return FALSE;
1944
1945   /* Check the following header, if we can */
1946   if (6 + sys_hdr_len + 4 <= len) {
1947     if (!IS_MPEG_HEADER (data + 6 + sys_hdr_len))
1948       return FALSE;
1949   }
1950
1951   if (pack_size)
1952     *pack_size = 6 + sys_hdr_len;
1953
1954   return TRUE;
1955 }
1956
1957 /* calculation of possibility to identify random data as mpeg systemstream:
1958  * bits that must match in header detection:            32 (or more)
1959  * chance that random data is identifed:                1/2^32
1960  * chance that MPEG2_MIN_PACK_HEADERS headers are identified:
1961  *       1/2^(32*MPEG2_MIN_PACK_HEADERS)
1962  * chance that this happens in MPEG2_MAX_PROBE_LENGTH bytes:
1963  *       1-(1+1/2^(32*MPEG2_MIN_PACK_HEADERS)^MPEG2_MAX_PROBE_LENGTH)
1964  * for current values:
1965  *       1-(1+1/2^(32*4)^101024)
1966  *       = <some_number>
1967  * Since we also check marker bits and pes packet lengths, this probability is a
1968  * very coarse upper bound.
1969  */
1970 static void
1971 mpeg_sys_type_find (GstTypeFind * tf, gpointer unused)
1972 {
1973   const guint8 *data, *data0, *first_sync, *end;
1974   gint mpegversion = 0;
1975   guint pack_headers = 0;
1976   guint pes_headers = 0;
1977   guint pack_size;
1978   guint since_last_sync = 0;
1979   guint32 sync_word = 0xffffffff;
1980
1981   G_STMT_START {
1982     gint len;
1983
1984     len = MPEG2_MAX_PROBE_LENGTH;
1985     do {
1986       len = len / 2;
1987       data = gst_type_find_peek (tf, 0, 5 + len);
1988     } while (data == NULL && len >= 32);
1989
1990     if (!data)
1991       return;
1992
1993     end = data + len;
1994   }
1995   G_STMT_END;
1996
1997   data0 = data;
1998   first_sync = NULL;
1999
2000   while (data < end) {
2001     sync_word <<= 8;
2002     if (sync_word == 0x00000100) {
2003       /* Found potential sync word */
2004       if (first_sync == NULL)
2005         first_sync = data - 3;
2006
2007       if (since_last_sync > 4) {
2008         /* If more than 4 bytes since the last sync word, reset our counters,
2009          * as we're only interested in counting contiguous packets */
2010         pes_headers = pack_headers = 0;
2011       }
2012       pack_size = 0;
2013
2014       if (IS_MPEG_PACK_CODE (data[0])) {
2015         if ((data[1] & 0xC0) == 0x40) {
2016           /* MPEG-2 */
2017           mpegversion = 2;
2018         } else if ((data[1] & 0xF0) == 0x20) {
2019           mpegversion = 1;
2020         }
2021         if (mpegversion != 0 &&
2022             mpeg_sys_is_valid_pack (tf, data - 3, end - data + 3, &pack_size)) {
2023           pack_headers++;
2024         }
2025       } else if (IS_MPEG_PES_CODE (data[0])) {
2026         /* PES stream */
2027         if (mpeg_sys_is_valid_pes (tf, data - 3, end - data + 3, &pack_size)) {
2028           pes_headers++;
2029           if (mpegversion == 0)
2030             mpegversion = 2;
2031         }
2032       } else if (IS_MPEG_SYS_CODE (data[0])) {
2033         if (mpeg_sys_is_valid_sys (tf, data - 3, end - data + 3, &pack_size)) {
2034           pack_headers++;
2035         }
2036       }
2037
2038       /* If we found a packet with a known size, skip the bytes in it and loop
2039        * around to check the next packet. */
2040       if (pack_size != 0) {
2041         data += pack_size - 3;
2042         sync_word = 0xffffffff;
2043         since_last_sync = 0;
2044         continue;
2045       }
2046     }
2047
2048     sync_word |= data[0];
2049     since_last_sync++;
2050     data++;
2051
2052     /* If we have found MAX headers, and *some* were pes headers (pack headers
2053      * are optional in an mpeg system stream) then return our high-probability
2054      * result */
2055     if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MAX_SYS_HEADERS)
2056       goto suggest;
2057   }
2058
2059   /* If we at least saw MIN headers, and *some* were pes headers (pack headers
2060    * are optional in an mpeg system stream) then return a lower-probability 
2061    * result */
2062   if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MIN_SYS_HEADERS)
2063     goto suggest;
2064
2065   return;
2066 suggest:
2067   {
2068     guint prob;
2069
2070     prob = GST_TYPE_FIND_POSSIBLE + (10 * (pack_headers + pes_headers));
2071     prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2072
2073     /* lower probability if the first packet wasn't right at the start */
2074     if (data0 != first_sync && prob >= 10)
2075       prob -= 10;
2076
2077     GST_LOG ("Suggesting MPEG %d system stream, %d packs, %d pes, prob %u%%\n",
2078         mpegversion, pack_headers, pes_headers, prob);
2079
2080     gst_type_find_suggest_simple (tf, prob, "video/mpeg",
2081         "systemstream", G_TYPE_BOOLEAN, TRUE,
2082         "mpegversion", G_TYPE_INT, mpegversion, NULL);
2083   }
2084 };
2085
2086 /*** video/mpegts Transport Stream ***/
2087 static GstStaticCaps mpegts_caps = GST_STATIC_CAPS ("video/mpegts, "
2088     "systemstream = (boolean) true, packetsize = (int) [ 188, 208 ]");
2089 #define MPEGTS_CAPS gst_static_caps_get(&mpegts_caps)
2090
2091 #define GST_MPEGTS_TYPEFIND_MIN_HEADERS 4
2092 #define GST_MPEGTS_TYPEFIND_MAX_HEADERS 10
2093 #define GST_MPEGTS_MAX_PACKET_SIZE 208
2094 #define GST_MPEGTS_TYPEFIND_SYNC_SIZE \
2095             (GST_MPEGTS_TYPEFIND_MIN_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2096 #define GST_MPEGTS_TYPEFIND_MAX_SYNC \
2097             (GST_MPEGTS_TYPEFIND_MAX_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2098 #define GST_MPEGTS_TYPEFIND_SCAN_LENGTH \
2099             (GST_MPEGTS_TYPEFIND_MAX_SYNC * 4)
2100
2101 #define MPEGTS_HDR_SIZE 4
2102 /* Check for sync byte, error_indicator == 0 and packet has payload */
2103 #define IS_MPEGTS_HEADER(data) (((data)[0] == 0x47) && \
2104                                 (((data)[1] & 0x80) == 0x00) && \
2105                                 (((data)[3] & 0x30) != 0x00))
2106
2107 /* Helper function to search ahead at intervals of packet_size for mpegts
2108  * headers */
2109 static gint
2110 mpeg_ts_probe_headers (GstTypeFind * tf, guint64 offset, gint packet_size)
2111 {
2112   /* We always enter this function having found at least one header already */
2113   gint found = 1;
2114   const guint8 *data = NULL;
2115
2116   GST_LOG ("looking for mpeg-ts packets of size %u", packet_size);
2117   while (found < GST_MPEGTS_TYPEFIND_MAX_HEADERS) {
2118     offset += packet_size;
2119
2120     data = gst_type_find_peek (tf, offset, MPEGTS_HDR_SIZE);
2121     if (data == NULL || !IS_MPEGTS_HEADER (data))
2122       return found;
2123
2124     found++;
2125     GST_LOG ("mpeg-ts sync #%2d at offset %" G_GUINT64_FORMAT, found, offset);
2126   }
2127
2128   return found;
2129 }
2130
2131 /* Try and detect at least 4 packets in at most 10 packets worth of
2132  * data. Need to try several possible packet sizes */
2133 static void
2134 mpeg_ts_type_find (GstTypeFind * tf, gpointer unused)
2135 {
2136   /* TS packet sizes to test: normal, DVHS packet size and 
2137    * FEC with 16 or 20 byte codes packet size. */
2138   const gint pack_sizes[] = { 188, 192, 204, 208 };
2139   const guint8 *data = NULL;
2140   guint size = 0;
2141   guint64 skipped = 0;
2142
2143   while (skipped < GST_MPEGTS_TYPEFIND_SCAN_LENGTH) {
2144     if (size < MPEGTS_HDR_SIZE) {
2145       data = gst_type_find_peek (tf, skipped, GST_MPEGTS_TYPEFIND_SYNC_SIZE);
2146       if (!data)
2147         break;
2148       size = GST_MPEGTS_TYPEFIND_SYNC_SIZE;
2149     }
2150
2151     /* Have at least MPEGTS_HDR_SIZE bytes at this point */
2152     if (IS_MPEGTS_HEADER (data)) {
2153       gint p;
2154
2155       GST_LOG ("possible mpeg-ts sync at offset %" G_GUINT64_FORMAT, skipped);
2156
2157       for (p = 0; p < G_N_ELEMENTS (pack_sizes); p++) {
2158         gint found;
2159
2160         /* Probe ahead at size pack_sizes[p] */
2161         found = mpeg_ts_probe_headers (tf, skipped, pack_sizes[p]);
2162         if (found >= GST_MPEGTS_TYPEFIND_MIN_HEADERS) {
2163           gint probability;
2164
2165           /* found at least 4 headers. 10 headers = MAXIMUM probability. 
2166            * Arbitrarily, I assigned 10% probability for each header we
2167            * found, 40% -> 100% */
2168           probability = MIN (10 * found, GST_TYPE_FIND_MAXIMUM);
2169
2170           gst_type_find_suggest_simple (tf, probability, "video/mpegts",
2171               "systemstream", G_TYPE_BOOLEAN, TRUE,
2172               "packetsize", G_TYPE_INT, pack_sizes[p], NULL);
2173           return;
2174         }
2175       }
2176     }
2177     data++;
2178     skipped++;
2179     size--;
2180   }
2181 }
2182
2183 #define GST_MPEGVID_TYPEFIND_TRY_PICTURES 6
2184 #define GST_MPEGVID_TYPEFIND_TRY_SYNC (100 * 1024)      /* 100 kB */
2185
2186 /* Scan ahead a maximum of max_extra_offset bytes until the next IS_MPEG_HEADER
2187  * offset.  After the call, offset will be after the 0x000001, i.e. at the 4th
2188  * byte of the MPEG header.  Returns TRUE if a header was found, FALSE if not.
2189  */
2190 static gboolean
2191 mpeg_find_next_header (GstTypeFind * tf, DataScanCtx * c,
2192     guint64 max_extra_offset)
2193 {
2194   guint64 extra_offset;
2195
2196   for (extra_offset = 0; extra_offset <= max_extra_offset; ++extra_offset) {
2197     if (!data_scan_ctx_ensure_data (tf, c, 4))
2198       return FALSE;
2199     if (IS_MPEG_HEADER (c->data)) {
2200       data_scan_ctx_advance (tf, c, 3);
2201       return TRUE;
2202     }
2203     data_scan_ctx_advance (tf, c, 1);
2204   }
2205   return FALSE;
2206 }
2207
2208 /*** video/mpeg MPEG-4 elementary video stream ***/
2209
2210 static GstStaticCaps mpeg4_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2211     "systemstream=(boolean)false, mpegversion=4, parsed=(boolean)false");
2212 #define MPEG4_VIDEO_CAPS gst_static_caps_get(&mpeg4_video_caps)
2213
2214 /*
2215  * This typefind is based on the elementary video header defined in
2216  * http://xhelmboyx.tripod.com/formats/mpeg-layout.txt
2217  * In addition, it allows the visual object sequence header to be
2218  * absent, and even the VOS header to be absent.  In the latter case,
2219  * a number of VOPs have to be present.
2220  */
2221 static void
2222 mpeg4_video_type_find (GstTypeFind * tf, gpointer unused)
2223 {
2224   DataScanCtx c = { 0, NULL, 0 };
2225   gboolean seen_vios_at_0 = FALSE;
2226   gboolean seen_vios = FALSE;
2227   gboolean seen_vos = FALSE;
2228   gboolean seen_vol = FALSE;
2229   guint num_vop_headers = 0;
2230   guint8 sc;
2231
2232   while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2233     if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2234       break;
2235
2236     if (!mpeg_find_next_header (tf, &c,
2237             GST_MPEGVID_TYPEFIND_TRY_SYNC - c.offset))
2238       break;
2239
2240     sc = c.data[0];
2241
2242     /* visual_object_sequence_start_code */
2243     if (sc == 0xB0) {
2244       if (seen_vios)
2245         break;                  /* Terminate at second vios */
2246       if (c.offset == 0)
2247         seen_vios_at_0 = TRUE;
2248       seen_vios = TRUE;
2249       data_scan_ctx_advance (tf, &c, 2);
2250       if (!mpeg_find_next_header (tf, &c, 0))
2251         break;
2252
2253       sc = c.data[0];
2254
2255       /* Optional metadata */
2256       if (sc == 0xB2)
2257         if (!mpeg_find_next_header (tf, &c, 24))
2258           break;
2259     }
2260
2261     /* visual_object_start_code (consider it optional) */
2262     if (sc == 0xB5) {
2263       data_scan_ctx_advance (tf, &c, 2);
2264       /* may contain ID marker and YUV clamping */
2265       if (!mpeg_find_next_header (tf, &c, 7))
2266         break;
2267
2268       sc = c.data[0];
2269     }
2270
2271     /* video_object_start_code */
2272     if (sc <= 0x1F) {
2273       if (seen_vos)
2274         break;                  /* Terminate at second vos */
2275       seen_vos = TRUE;
2276       data_scan_ctx_advance (tf, &c, 2);
2277       continue;
2278     }
2279
2280     /* video_object_layer_start_code */
2281     if (sc >= 0x20 && sc <= 0x2F) {
2282       seen_vol = TRUE;
2283       data_scan_ctx_advance (tf, &c, 5);
2284       continue;
2285     }
2286
2287     /* video_object_plane_start_code */
2288     if (sc == 0xB6) {
2289       num_vop_headers++;
2290       data_scan_ctx_advance (tf, &c, 2);
2291       continue;
2292     }
2293
2294     /* Unknown start code. */
2295   }
2296
2297   if (num_vop_headers > 0 || seen_vol) {
2298     GstTypeFindProbability probability = 0;
2299
2300     GST_LOG ("Found %d pictures, vios: %d, vos:%d, vol:%d", num_vop_headers,
2301         seen_vios, seen_vos, seen_vol);
2302
2303     if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios_at_0
2304         && seen_vos && seen_vol)
2305       probability = GST_TYPE_FIND_MAXIMUM - 1;
2306     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios
2307         && seen_vos && seen_vol)
2308       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2309     else if (seen_vios_at_0 && seen_vos && seen_vol)
2310       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2311     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vos
2312         && seen_vol)
2313       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2314     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vol)
2315       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2316     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2317       probability = GST_TYPE_FIND_LIKELY - 1;
2318     else if (num_vop_headers > 2 && seen_vios && seen_vos && seen_vol)
2319       probability = GST_TYPE_FIND_LIKELY - 9;
2320     else if (seen_vios && seen_vos && seen_vol)
2321       probability = GST_TYPE_FIND_LIKELY - 20;
2322     else if (num_vop_headers > 0 && seen_vos && seen_vol)
2323       probability = GST_TYPE_FIND_POSSIBLE;
2324     else if (num_vop_headers > 0)
2325       probability = GST_TYPE_FIND_POSSIBLE - 10;
2326     else if (seen_vos && seen_vol)
2327       probability = GST_TYPE_FIND_POSSIBLE - 20;
2328
2329     gst_type_find_suggest (tf, probability, MPEG4_VIDEO_CAPS);
2330   }
2331 }
2332
2333 /*** video/x-h263 H263 video stream ***/
2334 static GstStaticCaps h263_video_caps = GST_STATIC_CAPS ("video/x-h263");
2335
2336 #define H263_VIDEO_CAPS gst_static_caps_get(&h263_video_caps)
2337
2338 #define H263_MAX_PROBE_LENGTH (128 * 1024)
2339
2340 static void
2341 h263_video_type_find (GstTypeFind * tf, gpointer unused)
2342 {
2343   DataScanCtx c = { 0, NULL, 0 };
2344   guint64 data = 0;
2345   guint64 psc = 0;
2346   guint8 tr = 0;
2347   guint format;
2348   guint good = 0;
2349   guint bad = 0;
2350
2351   while (c.offset < H263_MAX_PROBE_LENGTH) {
2352     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2353       break;
2354
2355     /* Find the picture start code */
2356     data = (data << 8) + c.data[0];
2357     psc = data & G_GUINT64_CONSTANT (0xfffffc0000);
2358     if (psc == 0x800000) {
2359       /* Found PSC */
2360       /* TR */
2361       tr = (data & 0x3fc) >> 2;
2362       /* Source Format */
2363       format = tr & 0x07;
2364
2365       /* Now that we have a Valid PSC, check if we also have a valid PTYPE and
2366          the Source Format, which should range between 1 and 5 */
2367       if (((tr >> 6) == 0x2) && (format > 0 && format < 6))
2368         good++;
2369       else
2370         bad++;
2371
2372       /* FIXME: maybe bail out early if we get mostly bad syncs ? */
2373     }
2374
2375     data_scan_ctx_advance (tf, &c, 1);
2376   }
2377
2378   if (good > 0 && bad == 0)
2379     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H263_VIDEO_CAPS);
2380   else if (good > 2 * bad)
2381     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H263_VIDEO_CAPS);
2382
2383   return;
2384 }
2385
2386 /*** video/x-h264 H264 elementary video stream ***/
2387
2388 static GstStaticCaps h264_video_caps =
2389 GST_STATIC_CAPS ("video/x-h264,stream-format=byte-stream");
2390
2391 #define H264_VIDEO_CAPS gst_static_caps_get(&h264_video_caps)
2392
2393 #define H264_MAX_PROBE_LENGTH (128 * 1024)      /* 128kB for HD should be enough. */
2394
2395 static void
2396 h264_video_type_find (GstTypeFind * tf, gpointer unused)
2397 {
2398   DataScanCtx c = { 0, NULL, 0 };
2399
2400   /* Stream consists of: a series of sync codes (00 00 00 01) followed 
2401    * by NALs
2402    */
2403   int nut, ref;
2404   int good = 0;
2405   int bad = 0;
2406
2407   while (c.offset < H264_MAX_PROBE_LENGTH) {
2408     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2409       break;
2410
2411     if (IS_MPEG_HEADER (c.data)) {
2412       nut = c.data[3] & 0x9f;   /* forbiden_zero_bit | nal_unit_type */
2413       ref = c.data[3] & 0x60;   /* nal_ref_idc */
2414
2415       /* if forbidden bit is different to 0 won't be h264 */
2416       if (nut > 0x1f) {
2417         bad++;
2418         break;
2419       }
2420
2421       /* collect statistics about the NAL types */
2422       if ((nut >= 1 && nut <= 13) || nut == 19) {
2423         if ((nut == 5 && ref == 0) ||
2424             ((nut == 6 || (nut >= 9 && nut <= 12)) && ref != 0)) {
2425           bad++;
2426         } else {
2427           good++;
2428         }
2429       } else if (nut >= 14 && nut <= 33) {
2430         /* reserved */
2431         /* Theoretically these are good, since if they exist in the
2432            stream it merely means that a newer backwards-compatible
2433            h.264 stream.  But we should be identifying that separately. */
2434         bad++;
2435       } else {
2436         /* unspecified, application specific */
2437         /* don't consider these bad */
2438       }
2439
2440       GST_DEBUG ("good %d bad %d", good, bad);
2441
2442       if (good >= 10 && bad < 4) {
2443         gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H264_VIDEO_CAPS);
2444         return;
2445       }
2446
2447       data_scan_ctx_advance (tf, &c, 4);
2448     }
2449     data_scan_ctx_advance (tf, &c, 1);
2450   }
2451
2452   if (good >= 2 && bad < 1) {
2453     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H264_VIDEO_CAPS);
2454     return;
2455   }
2456 }
2457
2458 /*** video/mpeg video stream ***/
2459
2460 static GstStaticCaps mpeg_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2461     "systemstream = (boolean) false");
2462 #define MPEG_VIDEO_CAPS gst_static_caps_get(&mpeg_video_caps)
2463
2464 /*
2465  * Idea is the same as MPEG system stream typefinding: We check each
2466  * byte of the stream to see if - from that point on - the stream
2467  * matches a predefined set of marker bits as defined in the MPEG
2468  * video specs.
2469  *
2470  * I'm sure someone will do a chance calculation here too.
2471  */
2472
2473 static void
2474 mpeg_video_stream_type_find (GstTypeFind * tf, gpointer unused)
2475 {
2476   DataScanCtx c = { 0, NULL, 0 };
2477   gboolean seen_seq_at_0 = FALSE;
2478   gboolean seen_seq = FALSE;
2479   gboolean seen_gop = FALSE;
2480   guint64 last_pic_offset = 0;
2481   guint num_pic_headers = 0;
2482   gint found = 0;
2483
2484   while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2485     if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2486       break;
2487
2488     if (!data_scan_ctx_ensure_data (tf, &c, 5))
2489       break;
2490
2491     if (!IS_MPEG_HEADER (c.data))
2492       goto next;
2493
2494     /* a pack header indicates that this isn't an elementary stream */
2495     if (c.data[3] == 0xBA && mpeg_sys_is_valid_pack (tf, c.data, c.size, NULL))
2496       return;
2497
2498     /* do we have a sequence header? */
2499     if (c.data[3] == 0xB3) {
2500       seen_seq_at_0 = seen_seq_at_0 || (c.offset == 0);
2501       seen_seq = TRUE;
2502       data_scan_ctx_advance (tf, &c, 4 + 8);
2503       continue;
2504     }
2505
2506     /* or a GOP header */
2507     if (c.data[3] == 0xB8) {
2508       seen_gop = TRUE;
2509       data_scan_ctx_advance (tf, &c, 8);
2510       continue;
2511     }
2512
2513     /* but what we'd really like to see is a picture header */
2514     if (c.data[3] == 0x00) {
2515       ++num_pic_headers;
2516       last_pic_offset = c.offset;
2517       data_scan_ctx_advance (tf, &c, 8);
2518       continue;
2519     }
2520
2521     /* ... each followed by a slice header with slice_vertical_pos=1 that's
2522      * not too far away from the previously seen picture header. */
2523     if (c.data[3] == 0x01 && num_pic_headers > found &&
2524         (c.offset - last_pic_offset) >= 4 &&
2525         (c.offset - last_pic_offset) <= 64) {
2526       data_scan_ctx_advance (tf, &c, 4);
2527       found += 1;
2528       continue;
2529     }
2530
2531   next:
2532
2533     data_scan_ctx_advance (tf, &c, 1);
2534   }
2535
2536   if (found > 0 || seen_seq) {
2537     GstTypeFindProbability probability = 0;
2538
2539     GST_LOG ("Found %d pictures, seq:%d, gop:%d", found, seen_seq, seen_gop);
2540
2541     if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq && seen_gop)
2542       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2543     else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq)
2544       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2545     else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2546       probability = GST_TYPE_FIND_LIKELY;
2547     else if (seen_seq_at_0 && seen_gop && found > 2)
2548       probability = GST_TYPE_FIND_LIKELY - 10;
2549     else if (seen_seq && seen_gop && found > 2)
2550       probability = GST_TYPE_FIND_LIKELY - 20;
2551     else if (seen_seq_at_0 && found > 0)
2552       probability = GST_TYPE_FIND_POSSIBLE;
2553     else if (seen_seq && found > 0)
2554       probability = GST_TYPE_FIND_POSSIBLE - 5;
2555     else if (found > 0)
2556       probability = GST_TYPE_FIND_POSSIBLE - 10;
2557     else if (seen_seq)
2558       probability = GST_TYPE_FIND_POSSIBLE - 20;
2559
2560     gst_type_find_suggest_simple (tf, probability, "video/mpeg",
2561         "systemstream", G_TYPE_BOOLEAN, FALSE,
2562         "mpegversion", G_TYPE_INT, 1, NULL);
2563   }
2564 }
2565
2566 /*** audio/x-aiff ***/
2567
2568 static GstStaticCaps aiff_caps = GST_STATIC_CAPS ("audio/x-aiff");
2569
2570 #define AIFF_CAPS gst_static_caps_get(&aiff_caps)
2571 static void
2572 aiff_type_find (GstTypeFind * tf, gpointer unused)
2573 {
2574   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2575
2576   if (data && memcmp (data, "FORM", 4) == 0) {
2577     data += 8;
2578     if (memcmp (data, "AIFF", 4) == 0 || memcmp (data, "AIFC", 4) == 0)
2579       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AIFF_CAPS);
2580   }
2581 }
2582
2583 /*** audio/x-svx ***/
2584
2585 static GstStaticCaps svx_caps = GST_STATIC_CAPS ("audio/x-svx");
2586
2587 #define SVX_CAPS gst_static_caps_get(&svx_caps)
2588 static void
2589 svx_type_find (GstTypeFind * tf, gpointer unused)
2590 {
2591   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2592
2593   if (data && memcmp (data, "FORM", 4) == 0) {
2594     data += 8;
2595     if (memcmp (data, "8SVX", 4) == 0 || memcmp (data, "16SV", 4) == 0)
2596       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVX_CAPS);
2597   }
2598 }
2599
2600 /*** audio/x-shorten ***/
2601
2602 static GstStaticCaps shn_caps = GST_STATIC_CAPS ("audio/x-shorten");
2603
2604 #define SHN_CAPS gst_static_caps_get(&shn_caps)
2605 static void
2606 shn_type_find (GstTypeFind * tf, gpointer unused)
2607 {
2608   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2609
2610   if (data && memcmp (data, "ajkg", 4) == 0) {
2611     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2612   }
2613   data = gst_type_find_peek (tf, -8, 8);
2614   if (data && memcmp (data, "SHNAMPSK", 8) == 0) {
2615     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
2616   }
2617 }
2618
2619 /*** application/x-ape ***/
2620
2621 static GstStaticCaps ape_caps = GST_STATIC_CAPS ("application/x-ape");
2622
2623 #define APE_CAPS gst_static_caps_get(&ape_caps)
2624 static void
2625 ape_type_find (GstTypeFind * tf, gpointer unused)
2626 {
2627   const guint8 *data = gst_type_find_peek (tf, 0, 4);
2628
2629   if (data && memcmp (data, "MAC ", 4) == 0) {
2630     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, APE_CAPS);
2631   }
2632 }
2633
2634 /*** ISO FORMATS ***/
2635
2636 /*** audio/x-m4a ***/
2637
2638 static GstStaticCaps m4a_caps = GST_STATIC_CAPS ("audio/x-m4a");
2639
2640 #define M4A_CAPS (gst_static_caps_get(&m4a_caps))
2641 static void
2642 m4a_type_find (GstTypeFind * tf, gpointer unused)
2643 {
2644   const guint8 *data = gst_type_find_peek (tf, 4, 8);
2645
2646   if (data && (memcmp (data, "ftypM4A ", 8) == 0)) {
2647     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, M4A_CAPS);
2648   }
2649 }
2650
2651 /*** application/x-3gp ***/
2652
2653 /* The Q is there because variables can't start with a number. */
2654 static GstStaticCaps q3gp_caps = GST_STATIC_CAPS ("application/x-3gp");
2655 #define Q3GP_CAPS (gst_static_caps_get(&q3gp_caps))
2656
2657 static const gchar *
2658 q3gp_type_find_get_profile (const guint8 * data)
2659 {
2660   switch (GST_MAKE_FOURCC (data[0], data[1], data[2], 0)) {
2661     case GST_MAKE_FOURCC ('3', 'g', 'g', 0):
2662       return "general";
2663     case GST_MAKE_FOURCC ('3', 'g', 'p', 0):
2664       return "basic";
2665     case GST_MAKE_FOURCC ('3', 'g', 's', 0):
2666       return "streaming-server";
2667     case GST_MAKE_FOURCC ('3', 'g', 'r', 0):
2668       return "progressive-download";
2669     default:
2670       break;
2671   }
2672   return NULL;
2673 }
2674
2675 static void
2676 q3gp_type_find (GstTypeFind * tf, gpointer unused)
2677 {
2678   const gchar *profile;
2679   guint32 ftyp_size = 0;
2680   gint offset = 0;
2681   const guint8 *data = NULL;
2682
2683   if ((data = gst_type_find_peek (tf, 0, 12)) == NULL) {
2684     return;
2685   }
2686
2687   data += 4;
2688   if (memcmp (data, "ftyp", 4) != 0) {
2689     return;
2690   }
2691
2692   /* check major brand */
2693   data += 4;
2694   if ((profile = q3gp_type_find_get_profile (data))) {
2695     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
2696         "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
2697     return;
2698   }
2699
2700   /* check compatible brands */
2701   if ((data = gst_type_find_peek (tf, 0, 4)) != NULL) {
2702     ftyp_size = GST_READ_UINT32_BE (data);
2703   }
2704   for (offset = 16; offset < ftyp_size; offset += 4) {
2705     if ((data = gst_type_find_peek (tf, offset, 3)) == NULL) {
2706       break;
2707     }
2708     if ((profile = q3gp_type_find_get_profile (data))) {
2709       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
2710           "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
2711       return;
2712     }
2713   }
2714
2715   return;
2716
2717 }
2718
2719 /*** video/mj2 and image/jp2 ***/
2720 static GstStaticCaps mj2_caps = GST_STATIC_CAPS ("video/mj2");
2721
2722 #define MJ2_CAPS gst_static_caps_get(&mj2_caps)
2723
2724 static GstStaticCaps jp2_caps = GST_STATIC_CAPS ("image/jp2");
2725
2726 #define JP2_CAPS gst_static_caps_get(&jp2_caps)
2727
2728 static void
2729 jp2_type_find (GstTypeFind * tf, gpointer unused)
2730 {
2731   const guint8 *data;
2732
2733   data = gst_type_find_peek (tf, 0, 24);
2734   if (!data)
2735     return;
2736
2737   /* jp2 signature */
2738   if (memcmp (data, "\000\000\000\014jP  \015\012\207\012", 12) != 0)
2739     return;
2740
2741   /* check ftyp box */
2742   data += 12;
2743   if (memcmp (data + 4, "ftyp", 4) == 0) {
2744     if (memcmp (data + 8, "jp2 ", 4) == 0)
2745       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, JP2_CAPS);
2746     else if (memcmp (data + 8, "mjp2", 4) == 0)
2747       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MJ2_CAPS);
2748   }
2749 }
2750
2751 /*** video/quicktime ***/
2752
2753 static GstStaticCaps qt_caps = GST_STATIC_CAPS ("video/quicktime");
2754
2755 #define QT_CAPS gst_static_caps_get(&qt_caps)
2756 #define STRNCMP(x,y,z) (strncmp ((char*)(x), (char*)(y), z))
2757
2758 /* FIXME 0.11: go through http://www.ftyps.com/ */
2759 static void
2760 qt_type_find (GstTypeFind * tf, gpointer unused)
2761 {
2762   const guint8 *data;
2763   guint tip = 0;
2764   guint64 offset = 0;
2765   guint64 size;
2766   const gchar *variant = NULL;
2767
2768   while ((data = gst_type_find_peek (tf, offset, 12)) != NULL) {
2769     guint64 new_offset;
2770
2771     if (STRNCMP (&data[4], "ftypqt  ", 8) == 0) {
2772       tip = GST_TYPE_FIND_MAXIMUM;
2773       break;
2774     }
2775
2776     if (STRNCMP (&data[4], "ftypisom", 8) == 0 ||
2777         STRNCMP (&data[4], "ftypavc1", 8) == 0 ||
2778         STRNCMP (&data[4], "ftypmp42", 8) == 0) {
2779       tip = GST_TYPE_FIND_MAXIMUM;
2780       variant = "iso";
2781       break;
2782     }
2783
2784     /* box/atom types that are in common with ISO base media file format */
2785     if (STRNCMP (&data[4], "moov", 4) == 0 ||
2786         STRNCMP (&data[4], "mdat", 4) == 0 ||
2787         STRNCMP (&data[4], "ftyp", 4) == 0 ||
2788         STRNCMP (&data[4], "free", 4) == 0 ||
2789         STRNCMP (&data[4], "uuid", 4) == 0 ||
2790         STRNCMP (&data[4], "skip", 4) == 0) {
2791       if (tip == 0) {
2792         tip = GST_TYPE_FIND_LIKELY;
2793       } else {
2794         tip = GST_TYPE_FIND_NEARLY_CERTAIN;
2795       }
2796     }
2797     /* other box/atom types, apparently quicktime specific */
2798     else if (STRNCMP (&data[4], "pnot", 4) == 0 ||
2799         STRNCMP (&data[4], "PICT", 4) == 0 ||
2800         STRNCMP (&data[4], "wide", 4) == 0 ||
2801         STRNCMP (&data[4], "prfl", 4) == 0) {
2802       tip = GST_TYPE_FIND_MAXIMUM;
2803       break;
2804     } else {
2805       tip = 0;
2806       break;
2807     }
2808
2809     size = GST_READ_UINT32_BE (data);
2810     /* check compatible brands rather than ever expaning major brands above */
2811     if ((STRNCMP (&data[4], "ftyp", 4) == 0) && (size >= 16)) {
2812       new_offset = offset + 12;
2813       while (new_offset + 4 <= offset + size) {
2814         data = gst_type_find_peek (tf, new_offset, 4);
2815         if (data == NULL)
2816           goto done;
2817         if (STRNCMP (&data[4], "isom", 4) == 0 ||
2818             STRNCMP (&data[4], "avc1", 4) == 0 ||
2819             STRNCMP (&data[4], "mp41", 4) == 0 ||
2820             STRNCMP (&data[4], "mp42", 4) == 0) {
2821           tip = GST_TYPE_FIND_MAXIMUM;
2822           variant = "iso";
2823           goto done;
2824         }
2825         new_offset += 4;
2826       }
2827     }
2828     if (size == 1) {
2829       const guint8 *sizedata;
2830
2831       sizedata = gst_type_find_peek (tf, offset + 8, 8);
2832       if (sizedata == NULL)
2833         break;
2834
2835       size = GST_READ_UINT64_BE (sizedata);
2836     } else {
2837       if (size < 8)
2838         break;
2839     }
2840     new_offset = offset + size;
2841     if (new_offset <= offset)
2842       break;
2843     offset = new_offset;
2844   }
2845
2846 done:
2847   if (tip > 0) {
2848     if (variant) {
2849       GstCaps *caps = gst_caps_copy (QT_CAPS);
2850
2851       gst_caps_set_simple (caps, "variant", G_TYPE_STRING, variant, NULL);
2852       gst_type_find_suggest (tf, tip, caps);
2853       gst_caps_unref (caps);
2854     } else {
2855       gst_type_find_suggest (tf, tip, QT_CAPS);
2856     }
2857   }
2858 };
2859
2860
2861 /*** image/x-quicktime ***/
2862
2863 static GstStaticCaps qtif_caps = GST_STATIC_CAPS ("image/x-quicktime");
2864
2865 #define QTIF_CAPS gst_static_caps_get(&qtif_caps)
2866
2867 /* how many atoms we check before we give up */
2868 #define QTIF_MAXROUNDS 25
2869
2870 static void
2871 qtif_type_find (GstTypeFind * tf, gpointer unused)
2872 {
2873   const guint8 *data;
2874   gboolean found_idsc = FALSE;
2875   gboolean found_idat = FALSE;
2876   guint64 offset = 0;
2877   guint rounds = 0;
2878
2879   while ((data = gst_type_find_peek (tf, offset, 8)) != NULL) {
2880     guint64 size;
2881
2882     size = GST_READ_UINT32_BE (data);
2883     if (size == 1) {
2884       const guint8 *sizedata;
2885
2886       sizedata = gst_type_find_peek (tf, offset + 8, 8);
2887       if (sizedata == NULL)
2888         break;
2889
2890       size = GST_READ_UINT64_BE (sizedata);
2891     }
2892     if (size < 8)
2893       break;
2894
2895     if (STRNCMP (data + 4, "idsc", 4) == 0)
2896       found_idsc = TRUE;
2897     if (STRNCMP (data + 4, "idat", 4) == 0)
2898       found_idat = TRUE;
2899
2900     if (found_idsc && found_idat) {
2901       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, QTIF_CAPS);
2902       return;
2903     }
2904
2905     offset += size;
2906     if (++rounds > QTIF_MAXROUNDS)
2907       break;
2908   }
2909
2910   if (found_idsc || found_idat) {
2911     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, QTIF_CAPS);
2912     return;
2913   }
2914 };
2915
2916 /*** audio/x-mod ***/
2917
2918 static GstStaticCaps mod_caps = GST_STATIC_CAPS ("audio/x-mod");
2919
2920 #define MOD_CAPS gst_static_caps_get(&mod_caps)
2921 /* FIXME: M15 CheckType to do */
2922 static void
2923 mod_type_find (GstTypeFind * tf, gpointer unused)
2924 {
2925   const guint8 *data;
2926
2927   /* MOD */
2928   if ((data = gst_type_find_peek (tf, 1080, 4)) != NULL) {
2929     /* Protracker and variants */
2930     if ((memcmp (data, "M.K.", 4) == 0) || (memcmp (data, "M!K!", 4) == 0) ||
2931         /* Star Tracker */
2932         (memcmp (data, "FLT", 3) == 0 && isdigit (data[3])) ||
2933         (memcmp (data, "EXO", 3) == 0 && isdigit (data[3])) ||
2934         /* Oktalyzer (Amiga) */
2935         (memcmp (data, "OKTA", 4) == 0) ||
2936         /* Oktalyser (Atari) */
2937         (memcmp (data, "CD81", 4) == 0) ||
2938         /* Fasttracker */
2939         (memcmp (data + 1, "CHN", 3) == 0 && isdigit (data[0])) ||
2940         /* Fasttracker or Taketracker */
2941         (memcmp (data + 2, "CH", 2) == 0 && isdigit (data[0])
2942             && isdigit (data[1])) || (memcmp (data + 2, "CN", 2) == 0
2943             && isdigit (data[0]) && isdigit (data[1]))) {
2944       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
2945       return;
2946     }
2947   }
2948   /* XM */
2949   if ((data = gst_type_find_peek (tf, 0, 38)) != NULL) {
2950     if (memcmp (data, "Extended Module: ", 17) == 0 && data[37] == 0x1A) {
2951       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
2952       return;
2953     }
2954   }
2955   /* OKT */
2956   if (data || (data = gst_type_find_peek (tf, 0, 8)) != NULL) {
2957     if (memcmp (data, "OKTASONG", 8) == 0) {
2958       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
2959       return;
2960     }
2961   }
2962   if (data || (data = gst_type_find_peek (tf, 0, 4)) != NULL) {
2963     /* 669 */
2964     if ((memcmp (data, "if", 2) == 0) || (memcmp (data, "JN", 2) == 0)) {
2965       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MOD_CAPS);
2966       return;
2967     }
2968     /* AMF */
2969     if ((memcmp (data, "AMF", 3) == 0 && data[3] > 10 && data[3] < 14) ||
2970         /* IT */
2971         (memcmp (data, "IMPM", 4) == 0) ||
2972         /* MED */
2973         (memcmp (data, "MMD0", 4) == 0) || (memcmp (data, "MMD1", 4) == 0) ||
2974         /* MTM */
2975         (memcmp (data, "MTM", 3) == 0)) {
2976       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
2977       return;
2978     }
2979     /* DSM */
2980     if (memcmp (data, "RIFF", 4) == 0) {
2981       const guint8 *data2 = gst_type_find_peek (tf, 8, 4);
2982
2983       if (data2) {
2984         if (memcmp (data2, "DSMF", 4) == 0) {
2985           gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
2986           return;
2987         }
2988       }
2989     }
2990     /* FAM */
2991     if (memcmp (data, "FAM\xFE", 4) == 0) {
2992       const guint8 *data2 = gst_type_find_peek (tf, 44, 3);
2993
2994       if (data2) {
2995         if (memcmp (data2, "compare", 3) == 0) {
2996           gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
2997           return;
2998         }
2999       } else {
3000         gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MOD_CAPS);
3001         return;
3002       }
3003     }
3004     /* GDM */
3005     if (memcmp (data, "GDM\xFE", 4) == 0) {
3006       const guint8 *data2 = gst_type_find_peek (tf, 71, 4);
3007
3008       if (data2) {
3009         if (memcmp (data2, "GMFS", 4) == 0) {
3010           gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3011           return;
3012         }
3013       } else {
3014         gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MOD_CAPS);
3015         return;
3016       }
3017     }
3018   }
3019   /* IMF */
3020   if ((data = gst_type_find_peek (tf, 60, 4)) != NULL) {
3021     if (memcmp (data, "IM10", 4) == 0) {
3022       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3023       return;
3024     }
3025   }
3026   /* S3M */
3027   if ((data = gst_type_find_peek (tf, 44, 4)) != NULL) {
3028     if (memcmp (data, "SCRM", 4) == 0) {
3029       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3030       return;
3031     }
3032   }
3033   /* STM */
3034   if ((data = gst_type_find_peek (tf, 20, 8)) != NULL) {
3035     if (g_ascii_strncasecmp ((gchar *) data, "!Scream!", 8) == 0 ||
3036         g_ascii_strncasecmp ((gchar *) data, "BMOD2STM", 8) == 0) {
3037       const guint8 *id, *stmtype;
3038
3039       if ((id = gst_type_find_peek (tf, 28, 1)) == NULL)
3040         return;
3041       if ((stmtype = gst_type_find_peek (tf, 29, 1)) == NULL)
3042         return;
3043       if (*id == 0x1A && *stmtype == 2)
3044         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3045       return;
3046     }
3047   }
3048   /* AMF */
3049   if ((data = gst_type_find_peek (tf, 0, 19)) != NULL) {
3050     if (memcmp (data, "ASYLUM Music Format", 19) == 0) {
3051       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MOD_CAPS);
3052       return;
3053     }
3054   }
3055 }
3056
3057 /*** application/x-shockwave-flash ***/
3058
3059 static GstStaticCaps swf_caps =
3060 GST_STATIC_CAPS ("application/x-shockwave-flash");
3061 #define SWF_CAPS (gst_static_caps_get(&swf_caps))
3062 static void
3063 swf_type_find (GstTypeFind * tf, gpointer unused)
3064 {
3065   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3066
3067   if (data && (data[0] == 'F' || data[0] == 'C') &&
3068       data[1] == 'W' && data[2] == 'S') {
3069     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SWF_CAPS);
3070   }
3071 }
3072
3073 /*** image/jpeg ***/
3074
3075 #define JPEG_MARKER_IS_START_OF_FRAME(x) \
3076     ((x)>=0xc0 && (x) <= 0xcf && (x)!=0xc4 && (x)!=0xc8 && (x)!=0xcc)
3077
3078 static GstStaticCaps jpeg_caps = GST_STATIC_CAPS ("image/jpeg");
3079
3080 #define JPEG_CAPS (gst_static_caps_get(&jpeg_caps))
3081 static void
3082 jpeg_type_find (GstTypeFind * tf, gpointer unused)
3083 {
3084   GstTypeFindProbability prob = GST_TYPE_FIND_POSSIBLE;
3085   DataScanCtx c = { 0, NULL, 0 };
3086   GstCaps *caps;
3087   guint num_markers;
3088
3089   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 2)))
3090     return;
3091
3092   if (c.data[0] != 0xff || c.data[1] != 0xd8)
3093     return;
3094
3095   num_markers = 1;
3096   data_scan_ctx_advance (tf, &c, 2);
3097
3098   caps = gst_caps_copy (JPEG_CAPS);
3099
3100   while (data_scan_ctx_ensure_data (tf, &c, 4) && c.offset < (200 * 1024)) {
3101     guint16 len;
3102     guint8 marker;
3103
3104     if (c.data[0] != 0xff)
3105       break;
3106
3107     marker = c.data[1];
3108     if (G_UNLIKELY (marker == 0xff)) {
3109       data_scan_ctx_advance (tf, &c, 1);
3110       continue;
3111     }
3112
3113     data_scan_ctx_advance (tf, &c, 2);
3114
3115     /* we assume all markers we'll see before SOF have a payload length; if
3116      * that's not the case we'll just detect a false sync and bail out, but
3117      * still report POSSIBLE probability */
3118     len = GST_READ_UINT16_BE (c.data);
3119
3120     GST_LOG ("possible JPEG marker 0x%02x (@0x%04x), segment length %u",
3121         marker, (guint) c.offset, len);
3122
3123     if (!data_scan_ctx_ensure_data (tf, &c, len))
3124       break;
3125
3126     if (marker == 0xc4 ||       /* DEFINE_HUFFMAN_TABLES          */
3127         marker == 0xcc ||       /* DEFINE_ARITHMETIC_CONDITIONING */
3128         marker == 0xdb ||       /* DEFINE_QUANTIZATION_TABLES     */
3129         marker == 0xdd ||       /* DEFINE_RESTART_INTERVAL        */
3130         marker == 0xfe) {       /* COMMENT                        */
3131       data_scan_ctx_advance (tf, &c, len);
3132       ++num_markers;
3133     } else if (marker == 0xe0 && len >= (2 + 4) &&      /* APP0 */
3134         data_scan_ctx_memcmp (tf, &c, 2, "JFIF", 4)) {
3135       GST_LOG ("found JFIF tag");
3136       prob = GST_TYPE_FIND_MAXIMUM;
3137       data_scan_ctx_advance (tf, &c, len);
3138       ++num_markers;
3139       /* we continue until we find a start of frame marker */
3140     } else if (marker == 0xe1 && len >= (2 + 4) &&      /* APP1 */
3141         data_scan_ctx_memcmp (tf, &c, 2, "Exif", 4)) {
3142       GST_LOG ("found Exif tag");
3143       prob = GST_TYPE_FIND_MAXIMUM;
3144       data_scan_ctx_advance (tf, &c, len);
3145       ++num_markers;
3146       /* we continue until we find a start of frame marker */
3147     } else if (marker >= 0xe0 && marker <= 0xef) {      /* APPn */
3148       data_scan_ctx_advance (tf, &c, len);
3149       ++num_markers;
3150     } else if (JPEG_MARKER_IS_START_OF_FRAME (marker) && len >= (2 + 8)) {
3151       int h, w;
3152
3153       h = GST_READ_UINT16_BE (c.data + 2 + 1);
3154       w = GST_READ_UINT16_BE (c.data + 2 + 1 + 2);
3155       if (h == 0 || w == 0) {
3156         GST_WARNING ("bad width %u and/or height %u in SOF header", w, h);
3157         break;
3158       }
3159
3160       GST_LOG ("SOF at offset %" G_GUINT64_FORMAT ", num_markers=%d, "
3161           "WxH=%dx%d", c.offset - 2, num_markers, w, h);
3162
3163       if (num_markers >= 5 || prob == GST_TYPE_FIND_MAXIMUM)
3164         prob = GST_TYPE_FIND_MAXIMUM;
3165       else
3166         prob = GST_TYPE_FIND_LIKELY;
3167
3168       gst_caps_set_simple (caps, "width", G_TYPE_INT, w,
3169           "height", G_TYPE_INT, h, "sof-marker", G_TYPE_INT, marker & 0xf,
3170           NULL);
3171
3172       break;
3173     } else {
3174       GST_WARNING ("bad length or unexpected JPEG marker 0xff 0x%02x", marker);
3175       break;
3176     }
3177   }
3178
3179   gst_type_find_suggest (tf, prob, caps);
3180   gst_caps_unref (caps);
3181 }
3182
3183 /*** image/bmp ***/
3184
3185 static GstStaticCaps bmp_caps = GST_STATIC_CAPS ("image/bmp");
3186
3187 #define BMP_CAPS (gst_static_caps_get(&bmp_caps))
3188 static void
3189 bmp_type_find (GstTypeFind * tf, gpointer unused)
3190 {
3191   DataScanCtx c = { 0, NULL, 0 };
3192   guint32 struct_size, w, h, planes, bpp;
3193
3194   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 54)))
3195     return;
3196
3197   if (c.data[0] != 'B' || c.data[1] != 'M')
3198     return;
3199
3200   /* skip marker + size */
3201   data_scan_ctx_advance (tf, &c, 2 + 4);
3202
3203   /* reserved, must be 0 */
3204   if (c.data[0] != 0 || c.data[1] != 0 || c.data[2] != 0 || c.data[3] != 0)
3205     return;
3206
3207   data_scan_ctx_advance (tf, &c, 2 + 2);
3208
3209   /* offset to start of image data in bytes (check for sanity) */
3210   GST_LOG ("offset=%u", GST_READ_UINT32_LE (c.data));
3211   if (GST_READ_UINT32_LE (c.data) > (10 * 1024 * 1024))
3212     return;
3213
3214   struct_size = GST_READ_UINT32_LE (c.data + 4);
3215   GST_LOG ("struct_size=%u", struct_size);
3216
3217   data_scan_ctx_advance (tf, &c, 4 + 4);
3218
3219   if (struct_size == 0x0C) {
3220     w = GST_READ_UINT16_LE (c.data);
3221     h = GST_READ_UINT16_LE (c.data + 2);
3222     planes = GST_READ_UINT16_LE (c.data + 2 + 2);
3223     bpp = GST_READ_UINT16_LE (c.data + 2 + 2 + 2);
3224   } else if (struct_size == 40 || struct_size == 64 || struct_size == 108
3225       || struct_size == 124 || struct_size == 0xF0) {
3226     w = GST_READ_UINT32_LE (c.data);
3227     h = GST_READ_UINT32_LE (c.data + 4);
3228     planes = GST_READ_UINT16_LE (c.data + 4 + 4);
3229     bpp = GST_READ_UINT16_LE (c.data + 4 + 4 + 2);
3230   } else {
3231     return;
3232   }
3233
3234   /* image sizes sanity check */
3235   GST_LOG ("w=%u, h=%u, planes=%u, bpp=%u", w, h, planes, bpp);
3236   if (w == 0 || w > 0xfffff || h == 0 || h > 0xfffff || planes != 1 ||
3237       (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32))
3238     return;
3239
3240   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "image/bmp",
3241       "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, "bpp", G_TYPE_INT, bpp,
3242       NULL);
3243 }
3244
3245 /*** image/tiff ***/
3246 static GstStaticCaps tiff_caps = GST_STATIC_CAPS ("image/tiff, "
3247     "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }");
3248 #define TIFF_CAPS (gst_static_caps_get(&tiff_caps))
3249 static GstStaticCaps tiff_be_caps = GST_STATIC_CAPS ("image/tiff, "
3250     "endianness = (int) BIG_ENDIAN");
3251 #define TIFF_BE_CAPS (gst_static_caps_get(&tiff_be_caps))
3252 static GstStaticCaps tiff_le_caps = GST_STATIC_CAPS ("image/tiff, "
3253     "endianness = (int) LITTLE_ENDIAN");
3254 #define TIFF_LE_CAPS (gst_static_caps_get(&tiff_le_caps))
3255 static void
3256 tiff_type_find (GstTypeFind * tf, gpointer ununsed)
3257 {
3258   const guint8 *data = gst_type_find_peek (tf, 0, 8);
3259   guint8 le_header[4] = { 0x49, 0x49, 0x2A, 0x00 };
3260   guint8 be_header[4] = { 0x4D, 0x4D, 0x00, 0x2A };
3261
3262   if (data) {
3263     if (memcmp (data, le_header, 4) == 0) {
3264       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_LE_CAPS);
3265     } else if (memcmp (data, be_header, 4) == 0) {
3266       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_BE_CAPS);
3267     }
3268   }
3269 }
3270
3271 /*** PNM ***/
3272
3273 static GstStaticCaps pnm_caps = GST_STATIC_CAPS ("image/x-portable-bitmap; "
3274     "image/x-portable-graymap; image/x-portable-pixmap; "
3275     "image/x-portable-anymap");
3276
3277 #define PNM_CAPS (gst_static_caps_get(&pnm_caps))
3278
3279 #define IS_PNM_WHITESPACE(c) \
3280     ((c) == ' ' || (c) == '\r' || (c) == '\n' || (c) == 't')
3281
3282 static void
3283 pnm_type_find (GstTypeFind * tf, gpointer ununsed)
3284 {
3285   const gchar *media_type = NULL;
3286   DataScanCtx c = { 0, NULL, 0 };
3287   guint h = 0, w = 0;
3288
3289   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 16)))
3290     return;
3291
3292   /* see http://en.wikipedia.org/wiki/Netpbm_format */
3293   if (c.data[0] != 'P' || c.data[1] < '1' || c.data[1] > '7' ||
3294       !IS_PNM_WHITESPACE (c.data[2]) ||
3295       (c.data[3] != '#' && c.data[3] < '0' && c.data[3] > '9'))
3296     return;
3297
3298   switch (c.data[1]) {
3299     case '1':
3300       media_type = "image/x-portable-bitmap";   /* ASCII */
3301       break;
3302     case '2':
3303       media_type = "image/x-portable-graymap";  /* ASCII */
3304       break;
3305     case '3':
3306       media_type = "image/x-portable-pixmap";   /* ASCII */
3307       break;
3308     case '4':
3309       media_type = "image/x-portable-bitmap";   /* Raw */
3310       break;
3311     case '5':
3312       media_type = "image/x-portable-graymap";  /* Raw */
3313       break;
3314     case '6':
3315       media_type = "image/x-portable-pixmap";   /* Raw */
3316       break;
3317     case '7':
3318       media_type = "image/x-portable-anymap";
3319       break;
3320     default:
3321       g_return_if_reached ();
3322   }
3323
3324   /* try to extract width and height as well */
3325   if (c.data[1] != '7') {
3326     gchar s[64] = { 0, }
3327     , sep1, sep2;
3328
3329     /* need to skip any comment lines first */
3330     data_scan_ctx_advance (tf, &c, 3);
3331     while (c.data[0] == '#') {  /* we know there's still data left */
3332       data_scan_ctx_advance (tf, &c, 1);
3333       while (c.data[0] != '\n' && c.data[0] != '\r') {
3334         if (!data_scan_ctx_ensure_data (tf, &c, 4))
3335           return;
3336         data_scan_ctx_advance (tf, &c, 1);
3337       }
3338       data_scan_ctx_advance (tf, &c, 1);
3339       GST_LOG ("skipped comment line in PNM header");
3340     }
3341
3342     if (!data_scan_ctx_ensure_data (tf, &c, 32) &&
3343         !data_scan_ctx_ensure_data (tf, &c, 4)) {
3344       return;
3345     }
3346
3347     /* need to NUL-terminate data for sscanf */
3348     memcpy (s, c.data, MIN (sizeof (s) - 1, c.size));
3349     if (sscanf (s, "%u%c%u%c", &w, &sep1, &h, &sep2) == 4 &&
3350         IS_PNM_WHITESPACE (sep1) && IS_PNM_WHITESPACE (sep2) &&
3351         w > 0 && w < G_MAXINT && h > 0 && h < G_MAXINT) {
3352       GST_LOG ("extracted PNM width and height: %dx%d", w, h);
3353     } else {
3354       w = 0;
3355       h = 0;
3356     }
3357   } else {
3358     /* FIXME: extract width + height for anymaps too */
3359   }
3360
3361   if (w > 0 && h > 0) {
3362     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type,
3363         "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, NULL);
3364   } else {
3365     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, media_type, NULL);
3366   }
3367 }
3368
3369 static GstStaticCaps sds_caps = GST_STATIC_CAPS ("audio/x-sds");
3370
3371 #define SDS_CAPS (gst_static_caps_get(&sds_caps))
3372 static void
3373 sds_type_find (GstTypeFind * tf, gpointer ununsed)
3374 {
3375   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3376   guint8 mask[4] = { 0xFF, 0xFF, 0x80, 0xFF };
3377   guint8 match[4] = { 0xF0, 0x7E, 0, 0x01 };
3378   gint x;
3379
3380   if (data) {
3381     for (x = 0; x < 4; x++) {
3382       if ((data[x] & mask[x]) != match[x]) {
3383         return;
3384       }
3385     }
3386     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDS_CAPS);
3387   }
3388 }
3389
3390 static GstStaticCaps ircam_caps = GST_STATIC_CAPS ("audio/x-ircam");
3391
3392 #define IRCAM_CAPS (gst_static_caps_get(&ircam_caps))
3393 static void
3394 ircam_type_find (GstTypeFind * tf, gpointer ununsed)
3395 {
3396   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3397   guint8 mask[4] = { 0xFF, 0xFF, 0xF8, 0xFF };
3398   guint8 match[4] = { 0x64, 0xA3, 0x00, 0x00 };
3399   gint x;
3400   gboolean matched = TRUE;
3401
3402   if (!data) {
3403     return;
3404   }
3405   for (x = 0; x < 4; x++) {
3406     if ((data[x] & mask[x]) != match[x]) {
3407       matched = FALSE;
3408     }
3409   }
3410   if (matched) {
3411     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, IRCAM_CAPS);
3412     return;
3413   }
3414   /* now try the reverse version */
3415   matched = TRUE;
3416   for (x = 0; x < 4; x++) {
3417     if ((data[x] & mask[3 - x]) != match[3 - x]) {
3418       matched = FALSE;
3419     }
3420   }
3421 }
3422
3423 /* EBML typefind helper */
3424 static gboolean
3425 ebml_check_header (GstTypeFind * tf, const gchar * doctype, int doctype_len)
3426 {
3427   /* 4 bytes for EBML ID, 1 byte for header length identifier */
3428   const guint8 *data = gst_type_find_peek (tf, 0, 4 + 1);
3429   gint len_mask = 0x80, size = 1, n = 1, total;
3430
3431   if (!data)
3432     return FALSE;
3433
3434   /* ebml header? */
3435   if (data[0] != 0x1A || data[1] != 0x45 || data[2] != 0xDF || data[3] != 0xA3)
3436     return FALSE;
3437
3438   /* length of header */
3439   total = data[4];
3440   while (size <= 8 && !(total & len_mask)) {
3441     size++;
3442     len_mask >>= 1;
3443   }
3444   if (size > 8)
3445     return FALSE;
3446   total &= (len_mask - 1);
3447   while (n < size)
3448     total = (total << 8) | data[4 + n++];
3449
3450   /* get new data for full header, 4 bytes for EBML ID,
3451    * EBML length tag and the actual header */
3452   data = gst_type_find_peek (tf, 0, 4 + size + total);
3453   if (!data)
3454     return FALSE;
3455
3456   /* only check doctype if asked to do so */
3457   if (doctype == NULL || doctype_len == 0)
3458     return TRUE;
3459
3460   /* the header must contain the doctype. For now, we don't parse the
3461    * whole header but simply check for the availability of that array
3462    * of characters inside the header. Not fully fool-proof, but good
3463    * enough. */
3464   for (n = 4 + size; n <= 4 + size + total - doctype_len; n++)
3465     if (!memcmp (&data[n], doctype, doctype_len))
3466       return TRUE;
3467
3468   return FALSE;
3469 }
3470
3471 /*** video/x-matroska ***/
3472 static GstStaticCaps matroska_caps = GST_STATIC_CAPS ("video/x-matroska");
3473
3474 #define MATROSKA_CAPS (gst_static_caps_get(&matroska_caps))
3475 static void
3476 matroska_type_find (GstTypeFind * tf, gpointer ununsed)
3477 {
3478   if (ebml_check_header (tf, "matroska", 8))
3479     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MATROSKA_CAPS);
3480   else if (ebml_check_header (tf, NULL, 0))
3481     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MATROSKA_CAPS);
3482 }
3483
3484 /*** video/webm ***/
3485 static GstStaticCaps webm_caps = GST_STATIC_CAPS ("video/webm");
3486
3487 #define WEBM_CAPS (gst_static_caps_get(&webm_caps))
3488 static void
3489 webm_type_find (GstTypeFind * tf, gpointer ununsed)
3490 {
3491   if (ebml_check_header (tf, "webm", 4))
3492     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, WEBM_CAPS);
3493 }
3494
3495 /*** application/mxf ***/
3496 static GstStaticCaps mxf_caps = GST_STATIC_CAPS ("application/mxf");
3497
3498 #define MXF_MAX_PROBE_LENGTH (1024 * 64)
3499 #define MXF_CAPS (gst_static_caps_get(&mxf_caps))
3500
3501 /*
3502  * MXF files start with a header partition pack key of 16 bytes which is defined
3503  * at SMPTE-377M 6.1. Before this there can be up to 64K of run-in which _must_
3504  * not contain the partition pack key.
3505  */
3506 static void
3507 mxf_type_find (GstTypeFind * tf, gpointer ununsed)
3508 {
3509   static const guint8 partition_pack_key[] =
3510       { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01,
3511     0x01
3512   };
3513   DataScanCtx c = { 0, NULL, 0 };
3514
3515   while (c.offset <= MXF_MAX_PROBE_LENGTH) {
3516     guint i;
3517     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 1024)))
3518       break;
3519
3520     /* look over in chunks of 1kbytes to avoid too much overhead */
3521
3522     for (i = 0; i < 1024 - 16; i++) {
3523       /* Check first byte before calling more expensive memcmp function */
3524       if (G_UNLIKELY (c.data[i] == 0x06
3525               && memcmp (c.data + i, partition_pack_key, 13) == 0)) {
3526         /* Header partition pack? */
3527         if (c.data[i + 13] != 0x02)
3528           goto advance;
3529
3530         /* Partition status */
3531         if (c.data[i + 14] >= 0x05)
3532           goto advance;
3533
3534         /* Reserved, must be 0x00 */
3535         if (c.data[i + 15] != 0x00)
3536           goto advance;
3537
3538         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXF_CAPS);
3539         return;
3540       }
3541     }
3542
3543   advance:
3544     data_scan_ctx_advance (tf, &c, 1024 - 16);
3545   }
3546 }
3547
3548 /*** video/x-dv ***/
3549
3550 static GstStaticCaps dv_caps = GST_STATIC_CAPS ("video/x-dv, "
3551     "systemstream = (boolean) true");
3552 #define DV_CAPS (gst_static_caps_get(&dv_caps))
3553 static void
3554 dv_type_find (GstTypeFind * tf, gpointer private)
3555 {
3556   const guint8 *data;
3557
3558   data = gst_type_find_peek (tf, 0, 5);
3559
3560   /* check for DIF  and DV flag */
3561   if (data && (data[0] == 0x1f) && (data[1] == 0x07) && (data[2] == 0x00)) {
3562     const gchar *format;
3563
3564     if (data[3] & 0x80) {
3565       format = "PAL";
3566     } else {
3567       format = "NTSC";
3568     }
3569
3570     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "video/x-dv",
3571         "systemstream", G_TYPE_BOOLEAN, TRUE,
3572         "format", G_TYPE_STRING, format, NULL);
3573   }
3574 }
3575
3576
3577 /*** application/ogg and application/x-annodex ***/
3578 static GstStaticCaps ogg_caps = GST_STATIC_CAPS ("application/ogg");
3579 static GstStaticCaps annodex_caps = GST_STATIC_CAPS ("application/x-annodex");
3580 static GstStaticCaps ogg_annodex_caps =
3581     GST_STATIC_CAPS ("application/ogg;application/x-annodex");
3582
3583 #define OGGANX_CAPS (gst_static_caps_get(&ogg_annodex_caps))
3584
3585 static void
3586 ogganx_type_find (GstTypeFind * tf, gpointer private)
3587 {
3588   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3589
3590   if ((data != NULL) && (memcmp (data, "OggS", 4) == 0)) {
3591
3592     /* Check for an annodex fishbone header */
3593     data = gst_type_find_peek (tf, 28, 8);
3594     if (data && memcmp (data, "fishead\0", 8) == 0)
3595       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
3596           gst_static_caps_get (&annodex_caps));
3597
3598     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM,
3599         gst_static_caps_get (&ogg_caps));
3600   }
3601 }
3602
3603 /*** audio/x-vorbis ***/
3604 static GstStaticCaps vorbis_caps = GST_STATIC_CAPS ("audio/x-vorbis");
3605
3606 #define VORBIS_CAPS (gst_static_caps_get(&vorbis_caps))
3607 static void
3608 vorbis_type_find (GstTypeFind * tf, gpointer private)
3609 {
3610   const guint8 *data = gst_type_find_peek (tf, 0, 30);
3611
3612   if (data) {
3613     guint blocksize_0;
3614     guint blocksize_1;
3615
3616     /* 1 byte packet type (identification=0x01)
3617        6 byte string "vorbis"
3618        4 byte vorbis version */
3619     if (memcmp (data, "\001vorbis\000\000\000\000", 11) != 0)
3620       return;
3621     data += 11;
3622     /* 1 byte channels must be != 0 */
3623     if (data[0] == 0)
3624       return;
3625     data++;
3626     /* 4 byte samplerate must be != 0 */
3627     if (GST_READ_UINT32_LE (data) == 0)
3628       return;
3629     data += 16;
3630     /* blocksize checks */
3631     blocksize_0 = data[0] & 0x0F;
3632     blocksize_1 = (data[0] & 0xF0) >> 4;
3633     if (blocksize_0 > blocksize_1)
3634       return;
3635     if (blocksize_0 < 6 || blocksize_0 > 13)
3636       return;
3637     if (blocksize_1 < 6 || blocksize_1 > 13)
3638       return;
3639     data++;
3640     /* framing bit */
3641     if ((data[0] & 0x01) != 1)
3642       return;
3643     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VORBIS_CAPS);
3644   }
3645 }
3646
3647 /*** video/x-theora ***/
3648
3649 static GstStaticCaps theora_caps = GST_STATIC_CAPS ("video/x-theora");
3650
3651 #define THEORA_CAPS (gst_static_caps_get(&theora_caps))
3652 static void
3653 theora_type_find (GstTypeFind * tf, gpointer private)
3654 {
3655   const guint8 *data = gst_type_find_peek (tf, 0, 7);   //42);
3656
3657   if (data) {
3658     if (data[0] != 0x80)
3659       return;
3660     if (memcmp (&data[1], "theora", 6) != 0)
3661       return;
3662     /* FIXME: make this more reliable when specs are out */
3663
3664     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, THEORA_CAPS);
3665   }
3666 }
3667
3668 /*** kate ***/
3669 static void
3670 kate_type_find (GstTypeFind * tf, gpointer private)
3671 {
3672   const guint8 *data = gst_type_find_peek (tf, 0, 64);
3673   gchar category[16] = { 0, };
3674
3675   if (G_UNLIKELY (data == NULL))
3676     return;
3677
3678   /* see: http://wiki.xiph.org/index.php/OggKate#Format_specification */
3679   if (G_LIKELY (memcmp (data, "\200kate\0\0\0", 8) != 0))
3680     return;
3681
3682   /* make sure we always have a NUL-terminated string */
3683   memcpy (category, data + 48, 15);
3684   GST_LOG ("kate category: %s", category);
3685   /* canonical categories for subtitles: subtitles, spu-subtitles, SUB, K-SPU */
3686   if (strcmp (category, "subtitles") == 0 || strcmp (category, "SUB") == 0 ||
3687       strcmp (category, "spu-subtitles") == 0 ||
3688       strcmp (category, "K-SPU") == 0) {
3689     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3690         "subtitle/x-kate", NULL);
3691   } else {
3692     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3693         "application/x-kate", NULL);
3694   }
3695 }
3696
3697 /*** application/x-ogm-video or audio***/
3698
3699 static GstStaticCaps ogmvideo_caps =
3700 GST_STATIC_CAPS ("application/x-ogm-video");
3701 #define OGMVIDEO_CAPS (gst_static_caps_get(&ogmvideo_caps))
3702 static void
3703 ogmvideo_type_find (GstTypeFind * tf, gpointer private)
3704 {
3705   const guint8 *data = gst_type_find_peek (tf, 0, 9);
3706
3707   if (data) {
3708     if (memcmp (data, "\001video\000\000\000", 9) != 0)
3709       return;
3710     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMVIDEO_CAPS);
3711   }
3712 }
3713
3714 static GstStaticCaps ogmaudio_caps =
3715 GST_STATIC_CAPS ("application/x-ogm-audio");
3716 #define OGMAUDIO_CAPS (gst_static_caps_get(&ogmaudio_caps))
3717 static void
3718 ogmaudio_type_find (GstTypeFind * tf, gpointer private)
3719 {
3720   const guint8 *data = gst_type_find_peek (tf, 0, 9);
3721
3722   if (data) {
3723     if (memcmp (data, "\001audio\000\000\000", 9) != 0)
3724       return;
3725     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMAUDIO_CAPS);
3726   }
3727 }
3728
3729 static GstStaticCaps ogmtext_caps = GST_STATIC_CAPS ("application/x-ogm-text");
3730
3731 #define OGMTEXT_CAPS (gst_static_caps_get(&ogmtext_caps))
3732 static void
3733 ogmtext_type_find (GstTypeFind * tf, gpointer private)
3734 {
3735   const guint8 *data = gst_type_find_peek (tf, 0, 9);
3736
3737   if (data) {
3738     if (memcmp (data, "\001text\000\000\000\000", 9) != 0)
3739       return;
3740     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMTEXT_CAPS);
3741   }
3742 }
3743
3744 /*** audio/x-speex ***/
3745
3746 static GstStaticCaps speex_caps = GST_STATIC_CAPS ("audio/x-speex");
3747
3748 #define SPEEX_CAPS (gst_static_caps_get(&speex_caps))
3749 static void
3750 speex_type_find (GstTypeFind * tf, gpointer private)
3751 {
3752   const guint8 *data = gst_type_find_peek (tf, 0, 80);
3753
3754   if (data) {
3755     /* 8 byte string "Speex   "
3756        24 byte speex version string + int */
3757     if (memcmp (data, "Speex   ", 8) != 0)
3758       return;
3759     data += 32;
3760
3761     /* 4 byte header size >= 80 */
3762     if (GST_READ_UINT32_LE (data) < 80)
3763       return;
3764     data += 4;
3765
3766     /* 4 byte sample rate <= 48000 */
3767     if (GST_READ_UINT32_LE (data) > 48000)
3768       return;
3769     data += 4;
3770
3771     /* currently there are only 3 speex modes. */
3772     if (GST_READ_UINT32_LE (data) > 3)
3773       return;
3774     data += 12;
3775
3776     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SPEEX_CAPS);
3777   }
3778 }
3779
3780 /*** audio/x-celt ***/
3781
3782 static GstStaticCaps celt_caps = GST_STATIC_CAPS ("audio/x-celt");
3783
3784 #define CELT_CAPS (gst_static_caps_get(&celt_caps))
3785 static void
3786 celt_type_find (GstTypeFind * tf, gpointer private)
3787 {
3788   const guint8 *data = gst_type_find_peek (tf, 0, 8);
3789
3790   if (data) {
3791     /* 8 byte string "CELT   " */
3792     if (memcmp (data, "CELT    ", 8) != 0)
3793       return;
3794
3795     /* TODO: Check other values of the CELT header */
3796     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CELT_CAPS);
3797   }
3798 }
3799
3800 /*** application/x-ogg-skeleton ***/
3801 static GstStaticCaps ogg_skeleton_caps =
3802 GST_STATIC_CAPS ("application/x-ogg-skeleton, parsed=(boolean)FALSE");
3803 #define OGG_SKELETON_CAPS (gst_static_caps_get(&ogg_skeleton_caps))
3804 static void
3805 oggskel_type_find (GstTypeFind * tf, gpointer private)
3806 {
3807   const guint8 *data = gst_type_find_peek (tf, 0, 12);
3808
3809   if (data) {
3810     /* 8 byte string "fishead\0" for the ogg skeleton stream */
3811     if (memcmp (data, "fishead\0", 8) != 0)
3812       return;
3813     data += 8;
3814
3815     /* Require that the header contains version 3.0 */
3816     if (GST_READ_UINT16_LE (data) != 3)
3817       return;
3818     data += 2;
3819     if (GST_READ_UINT16_LE (data) != 0)
3820       return;
3821
3822     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGG_SKELETON_CAPS);
3823   }
3824 }
3825
3826 static GstStaticCaps cmml_caps = GST_STATIC_CAPS ("text/x-cmml");
3827
3828 #define CMML_CAPS (gst_static_caps_get(&cmml_caps))
3829 static void
3830 cmml_type_find (GstTypeFind * tf, gpointer private)
3831 {
3832   /* Header is 12 bytes minimum (though we don't check the minor version */
3833   const guint8 *data = gst_type_find_peek (tf, 0, 12);
3834
3835   if (data) {
3836
3837     /* 8 byte string "CMML\0\0\0\0" for the magic number */
3838     if (memcmp (data, "CMML\0\0\0\0", 8) != 0)
3839       return;
3840     data += 8;
3841
3842     /* Require that the header contains at least version 2.0 */
3843     if (GST_READ_UINT16_LE (data) < 2)
3844       return;
3845
3846     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CMML_CAPS);
3847   }
3848 }
3849
3850 /*** application/x-tar ***/
3851
3852 static GstStaticCaps tar_caps = GST_STATIC_CAPS ("application/x-tar");
3853
3854 #define TAR_CAPS (gst_static_caps_get(&tar_caps))
3855 #define OLDGNU_MAGIC "ustar  "  /* 7 chars and a NUL */
3856 #define NEWGNU_MAGIC "ustar"    /* 5 chars and a NUL */
3857 static void
3858 tar_type_find (GstTypeFind * tf, gpointer unused)
3859 {
3860   const guint8 *data = gst_type_find_peek (tf, 257, 8);
3861
3862   /* of course we are not certain, but we don't want other typefind funcs
3863    * to detect formats of files within the tar archive, e.g. mp3s */
3864   if (data) {
3865     if (memcmp (data, OLDGNU_MAGIC, 8) == 0) {  /* sic */
3866       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
3867     } else if (memcmp (data, NEWGNU_MAGIC, 6) == 0 &&   /* sic */
3868         g_ascii_isdigit (data[6]) && g_ascii_isdigit (data[7])) {
3869       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
3870     }
3871   }
3872 }
3873
3874 /*** application/x-ar ***/
3875
3876 static GstStaticCaps ar_caps = GST_STATIC_CAPS ("application/x-ar");
3877
3878 #define AR_CAPS (gst_static_caps_get(&ar_caps))
3879 static void
3880 ar_type_find (GstTypeFind * tf, gpointer unused)
3881 {
3882   const guint8 *data = gst_type_find_peek (tf, 0, 24);
3883
3884   if (data && memcmp (data, "!<arch>", 7) == 0) {
3885     gint i;
3886
3887     for (i = 7; i < 24; ++i) {
3888       if (!g_ascii_isprint (data[i]) && data[i] != '\n') {
3889         gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AR_CAPS);
3890       }
3891     }
3892
3893     gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AR_CAPS);
3894   }
3895 }
3896
3897 /*** audio/x-au ***/
3898
3899 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
3900  * as it is only possible to register one typefind factory per 'name'
3901  * (which is in this case the caps), and the first one would be replaced by
3902  * the second one. */
3903 static GstStaticCaps au_caps = GST_STATIC_CAPS ("audio/x-au");
3904
3905 #define AU_CAPS (gst_static_caps_get(&au_caps))
3906 static void
3907 au_type_find (GstTypeFind * tf, gpointer unused)
3908 {
3909   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3910
3911   if (data) {
3912     if (memcmp (data, ".snd", 4) == 0 || memcmp (data, "dns.", 4) == 0) {
3913       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AU_CAPS);
3914     }
3915   }
3916 }
3917
3918
3919 /*** video/x-nuv ***/
3920
3921 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
3922  * as it is only possible to register one typefind factory per 'name'
3923  * (which is in this case the caps), and the first one would be replaced by
3924  * the second one. */
3925 static GstStaticCaps nuv_caps = GST_STATIC_CAPS ("video/x-nuv");
3926
3927 #define NUV_CAPS (gst_static_caps_get(&nuv_caps))
3928 static void
3929 nuv_type_find (GstTypeFind * tf, gpointer unused)
3930 {
3931   const guint8 *data = gst_type_find_peek (tf, 0, 11);
3932
3933   if (data) {
3934     if (memcmp (data, "MythTVVideo", 11) == 0
3935         || memcmp (data, "NuppelVideo", 11) == 0) {
3936       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, NUV_CAPS);
3937     }
3938   }
3939 }
3940
3941 /*** audio/x-paris ***/
3942 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
3943 static GstStaticCaps paris_caps = GST_STATIC_CAPS ("audio/x-paris");
3944
3945 #define PARIS_CAPS (gst_static_caps_get(&paris_caps))
3946 static void
3947 paris_type_find (GstTypeFind * tf, gpointer unused)
3948 {
3949   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3950
3951   if (data) {
3952     if (memcmp (data, " paf", 4) == 0 || memcmp (data, "fap ", 4) == 0) {
3953       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, PARIS_CAPS);
3954     }
3955   }
3956 }
3957
3958 /*** audio/iLBC-sh ***/
3959 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
3960 static GstStaticCaps ilbc_caps = GST_STATIC_CAPS ("audio/iLBC-sh");
3961
3962 #define ILBC_CAPS (gst_static_caps_get(&ilbc_caps))
3963 static void
3964 ilbc_type_find (GstTypeFind * tf, gpointer unused)
3965 {
3966   const guint8 *data = gst_type_find_peek (tf, 0, 8);
3967
3968   if (data) {
3969     if (memcmp (data, "#!iLBC30", 8) == 0 || memcmp (data, "#!iLBC20", 8) == 0) {
3970       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, ILBC_CAPS);
3971     }
3972   }
3973 }
3974
3975 /*** application/x-ms-dos-executable ***/
3976
3977 static GstStaticCaps msdos_caps =
3978 GST_STATIC_CAPS ("application/x-ms-dos-executable");
3979 #define MSDOS_CAPS (gst_static_caps_get(&msdos_caps))
3980 /* see http://www.madchat.org/vxdevl/papers/winsys/pefile/pefile.htm */
3981 static void
3982 msdos_type_find (GstTypeFind * tf, gpointer unused)
3983 {
3984   const guint8 *data = gst_type_find_peek (tf, 0, 64);
3985
3986   if (data && data[0] == 'M' && data[1] == 'Z' &&
3987       GST_READ_UINT16_LE (data + 8) == 4) {
3988     guint32 pe_offset = GST_READ_UINT32_LE (data + 60);
3989
3990     data = gst_type_find_peek (tf, pe_offset, 2);
3991     if (data && data[0] == 'P' && data[1] == 'E') {
3992       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, MSDOS_CAPS);
3993     }
3994   }
3995 }
3996
3997 /*** application/x-mmsh ***/
3998
3999 static GstStaticCaps mmsh_caps = GST_STATIC_CAPS ("application/x-mmsh");
4000
4001 #define MMSH_CAPS gst_static_caps_get(&mmsh_caps)
4002
4003 /* This is to recognise mssh-over-http */
4004 static void
4005 mmsh_type_find (GstTypeFind * tf, gpointer unused)
4006 {
4007   static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
4008     0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
4009   };
4010
4011   const guint8 *data;
4012
4013   data = gst_type_find_peek (tf, 0, 2 + 2 + 4 + 2 + 2 + 16);
4014   if (data && data[0] == 0x24 && data[1] == 0x48 &&
4015       GST_READ_UINT16_LE (data + 2) > 2 + 2 + 4 + 2 + 2 + 16 &&
4016       memcmp (data + 2 + 2 + 4 + 2 + 2, asf_marker, 16) == 0) {
4017     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MMSH_CAPS);
4018   }
4019 }
4020
4021 /*** video/x-dirac ***/
4022
4023 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
4024  * as it is only possible to register one typefind factory per 'name'
4025  * (which is in this case the caps), and the first one would be replaced by
4026  * the second one. */
4027 static GstStaticCaps dirac_caps = GST_STATIC_CAPS ("video/x-dirac");
4028
4029 #define DIRAC_CAPS (gst_static_caps_get(&dirac_caps))
4030 static void
4031 dirac_type_find (GstTypeFind * tf, gpointer unused)
4032 {
4033   const guint8 *data = gst_type_find_peek (tf, 0, 8);
4034
4035   if (data) {
4036     if (memcmp (data, "BBCD", 4) == 0 || memcmp (data, "KW-DIRAC", 8) == 0) {
4037       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DIRAC_CAPS);
4038     }
4039   }
4040 }
4041
4042 /*** video/vivo ***/
4043
4044 static GstStaticCaps vivo_caps = GST_STATIC_CAPS ("video/vivo");
4045
4046 #define VIVO_CAPS gst_static_caps_get(&vivo_caps)
4047
4048 static void
4049 vivo_type_find (GstTypeFind * tf, gpointer unused)
4050 {
4051   static const guint8 vivo_marker[] = { 'V', 'e', 'r', 's', 'i', 'o', 'n',
4052     ':', 'V', 'i', 'v', 'o', '/'
4053   };
4054   const guint8 *data;
4055   guint hdr_len, pos;
4056
4057   data = gst_type_find_peek (tf, 0, 1024);
4058   if (data == NULL || data[0] != 0x00)
4059     return;
4060
4061   if ((data[1] & 0x80)) {
4062     if ((data[2] & 0x80))
4063       return;
4064     hdr_len = ((guint) (data[1] & 0x7f)) << 7;
4065     hdr_len += data[2];
4066     if (hdr_len > 2048)
4067       return;
4068     pos = 3;
4069   } else {
4070     hdr_len = data[1];
4071     pos = 2;
4072   }
4073
4074   /* 1008 = 1022 - strlen ("Version:Vivo/") - 1 */
4075   while (pos < 1008 && data[pos] == '\r' && data[pos + 1] == '\n')
4076     pos += 2;
4077
4078   if (memcmp (data + pos, vivo_marker, sizeof (vivo_marker)) == 0) {
4079     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VIVO_CAPS);
4080   }
4081 }
4082
4083 /*** XDG MIME typefinder (to avoid false positives mostly) ***/
4084
4085 #ifdef USE_GIO
4086 static void
4087 xdgmime_typefind (GstTypeFind * find, gpointer user_data)
4088 {
4089   gchar *mimetype;
4090   gsize length = 16384;
4091   guint64 tf_length;
4092   const guint8 *data;
4093   gchar *tmp;
4094
4095   if ((tf_length = gst_type_find_get_length (find)) > 0)
4096     length = MIN (length, tf_length);
4097
4098   if ((data = gst_type_find_peek (find, 0, length)) == NULL)
4099     return;
4100
4101   tmp = g_content_type_guess (NULL, data, length, NULL);
4102   if (tmp == NULL || g_content_type_is_unknown (tmp)) {
4103     g_free (tmp);
4104     return;
4105   }
4106
4107   mimetype = g_content_type_get_mime_type (tmp);
4108   g_free (tmp);
4109
4110   if (mimetype == NULL)
4111     return;
4112
4113   GST_DEBUG ("Got mimetype '%s'", mimetype);
4114
4115   /* Ignore audio/video types:
4116    *  - our own typefinders in -base are likely to be better at this
4117    *    (and if they're not, we really want to fix them, that's why we don't
4118    *    report xdg-detected audio/video types at all, not even with a low
4119    *    probability)
4120    *  - we want to detect GStreamer media types and not MIME types
4121    *  - the purpose of this xdg mime finder is mainly to prevent false
4122    *    positives of non-media formats, not to typefind audio/video formats */
4123   if (g_str_has_prefix (mimetype, "audio/") ||
4124       g_str_has_prefix (mimetype, "video/")) {
4125     GST_LOG ("Ignoring audio/video mime type");
4126     g_free (mimetype);
4127     return;
4128   }
4129
4130   /* Again, we mainly want the xdg typefinding to prevent false-positives on
4131    * non-media formats, so suggest the type with a probability that trumps
4132    * uncertain results of our typefinders, but not more than that. */
4133   GST_LOG ("Suggesting '%s' with probability POSSIBLE", mimetype);
4134   gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE, mimetype, NULL);
4135   g_free (mimetype);
4136 }
4137 #endif /* USE_GIO */
4138
4139 /*** Windows icon typefinder (to avoid false positives mostly) ***/
4140
4141 static void
4142 windows_icon_typefind (GstTypeFind * find, gpointer user_data)
4143 {
4144   const guint8 *data;
4145   gint64 datalen;
4146   guint16 type, nimages;
4147   gint32 size, offset;
4148
4149   datalen = gst_type_find_get_length (find);
4150   if ((data = gst_type_find_peek (find, 0, 6)) == NULL)
4151     return;
4152
4153   /* header - simple and not enough to rely on it alone */
4154   if (GST_READ_UINT16_LE (data) != 0)
4155     return;
4156   type = GST_READ_UINT16_LE (data + 2);
4157   if (type != 1 && type != 2)
4158     return;
4159   nimages = GST_READ_UINT16_LE (data + 4);
4160   if (nimages == 0)             /* we can assume we can't have an empty image file ? */
4161     return;
4162
4163   /* first image */
4164   if (data[6 + 3] != 0)
4165     return;
4166   if (type == 1) {
4167     guint16 planes = GST_READ_UINT16_LE (data + 6 + 4);
4168     if (planes > 1)
4169       return;
4170   }
4171   size = GST_READ_UINT32_LE (data + 6 + 8);
4172   offset = GST_READ_UINT32_LE (data + 6 + 12);
4173   if (offset < 0 || size <= 0 || size >= datalen || offset >= datalen
4174       || size + offset > datalen)
4175     return;
4176
4177   gst_type_find_suggest_simple (find, GST_TYPE_FIND_NEARLY_CERTAIN,
4178       "image/x-icon", NULL);
4179 }
4180
4181 /*** WAP WBMP typefinder ***/
4182
4183 static void
4184 wbmp_typefind (GstTypeFind * find, gpointer user_data)
4185 {
4186   const guint8 *data;
4187   gint64 datalen;
4188   guint w, h, size;
4189
4190   /* http://en.wikipedia.org/wiki/Wireless_Application_Protocol_Bitmap_Format */
4191   datalen = gst_type_find_get_length (find);
4192   if (datalen == 0)
4193     return;
4194
4195   data = gst_type_find_peek (find, 0, 5);
4196   if (data == NULL)
4197     return;
4198
4199   /* want 0x00 0x00 at start */
4200   if (*data++ != 0 || *data++ != 0)
4201     return;
4202
4203   /* min header size */
4204   size = 4;
4205
4206   /* let's assume max width/height is 65536 */
4207   w = *data++;
4208   if ((w & 0x80)) {
4209     w = (w << 8) | *data++;
4210     if ((w & 0x80))
4211       return;
4212     ++size;
4213     data = gst_type_find_peek (find, 4, 2);
4214     if (data == NULL)
4215       return;
4216   }
4217   h = *data++;
4218   if ((h & 0x80)) {
4219     h = (h << 8) | *data++;
4220     if ((h & 0x80))
4221       return;
4222     ++size;
4223   }
4224
4225   if (w == 0 || h == 0)
4226     return;
4227
4228   /* now add bitmap size */
4229   size += h * (GST_ROUND_UP_8 (w) / 8);
4230
4231   if (datalen == size) {
4232     gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE - 10,
4233         "image/vnd.wap.wbmp", NULL);
4234   }
4235 }
4236
4237 /*** DEGAS Atari images (also to avoid false positives, see #625129) ***/
4238 static void
4239 degas_type_find (GstTypeFind * tf, gpointer private)
4240 {
4241   /* No magic, but it should have a fixed size and a few invalid values */
4242   /* http://www.fileformat.info/format/atari/spec/6ecf9f6eb5be494284a47feb8a214687/view.htm */
4243   gint64 len;
4244   const guint8 *data;
4245   guint16 resolution;
4246   int n;
4247
4248   len = gst_type_find_get_length (tf);
4249   if (len < 34)                 /* smallest header of the lot */
4250     return;
4251   data = gst_type_find_peek (tf, 0, 4);
4252   if (G_UNLIKELY (data == NULL))
4253     return;
4254   resolution = GST_READ_UINT16_BE (data);
4255   if (len == 32034) {
4256     /* could be DEGAS */
4257     if (resolution <= 2)
4258       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
4259           "image/x-degas", NULL);
4260   } else if (len == 32066) {
4261     /* could be DEGAS Elite */
4262     if (resolution <= 2) {
4263       data = gst_type_find_peek (tf, len - 16, 8);
4264       if (G_UNLIKELY (data == NULL))
4265         return;
4266       for (n = 0; n < 4; n++) {
4267         if (GST_READ_UINT16_BE (data + n * 2) > 2)
4268           return;
4269       }
4270       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
4271           "image/x-degas", NULL);
4272     }
4273   } else if (len >= 66 && len < 32066) {
4274     /* could be compressed DEGAS Elite, but it's compressed and so we can't rely on size,
4275        it does have 4 16 bytes values near the end that are 0-2 though. */
4276     if ((resolution & 0x8000) && (resolution & 0x7fff) <= 2) {
4277       data = gst_type_find_peek (tf, len - 16, 8);
4278       if (G_UNLIKELY (data == NULL))
4279         return;
4280       for (n = 0; n < 4; n++) {
4281         if (GST_READ_UINT16_BE (data + n * 2) > 2)
4282           return;
4283       }
4284       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
4285           "image/x-degas", NULL);
4286     }
4287   }
4288 }
4289
4290 /*** generic typefind for streams that have some data at a specific position***/
4291 typedef struct
4292 {
4293   const guint8 *data;
4294   guint size;
4295   guint probability;
4296   GstCaps *caps;
4297 }
4298 GstTypeFindData;
4299
4300 static void
4301 start_with_type_find (GstTypeFind * tf, gpointer private)
4302 {
4303   GstTypeFindData *start_with = (GstTypeFindData *) private;
4304   const guint8 *data;
4305
4306   GST_LOG ("trying to find mime type %s with the first %u bytes of data",
4307       gst_structure_get_name (gst_caps_get_structure (start_with->caps, 0)),
4308       start_with->size);
4309   data = gst_type_find_peek (tf, 0, start_with->size);
4310   if (data && memcmp (data, start_with->data, start_with->size) == 0) {
4311     gst_type_find_suggest (tf, start_with->probability, start_with->caps);
4312   }
4313 }
4314
4315 static void
4316 sw_data_destroy (GstTypeFindData * sw_data)
4317 {
4318   if (G_LIKELY (sw_data->caps != NULL))
4319     gst_caps_unref (sw_data->caps);
4320   g_free (sw_data);
4321 }
4322
4323 #define TYPE_FIND_REGISTER_START_WITH(plugin,name,rank,ext,_data,_size,_probability)\
4324 G_BEGIN_DECLS{                                                          \
4325   GstTypeFindData *sw_data = g_new (GstTypeFindData, 1);                \
4326   sw_data->data = (const guint8 *)_data;                                \
4327   sw_data->size = _size;                                                \
4328   sw_data->probability = _probability;                                  \
4329   sw_data->caps = gst_caps_new_empty_simple (name);                     \
4330   if (!gst_type_find_register (plugin, name, rank, start_with_type_find,\
4331                       (char **) ext, sw_data->caps, sw_data,            \
4332                      (GDestroyNotify) (sw_data_destroy))) {             \
4333     gst_caps_unref (sw_data->caps);                                     \
4334     g_free (sw_data);                                                   \
4335   }                                                                     \
4336 }G_END_DECLS
4337
4338 /*** same for riff types ***/
4339
4340 static void
4341 riff_type_find (GstTypeFind * tf, gpointer private)
4342 {
4343   GstTypeFindData *riff_data = (GstTypeFindData *) private;
4344   const guint8 *data = gst_type_find_peek (tf, 0, 12);
4345
4346   if (data && (memcmp (data, "RIFF", 4) == 0 || memcmp (data, "AVF0", 4) == 0)) {
4347     data += 8;
4348     if (memcmp (data, riff_data->data, 4) == 0)
4349       gst_type_find_suggest (tf, riff_data->probability, riff_data->caps);
4350   }
4351 }
4352
4353 #define TYPE_FIND_REGISTER_RIFF(plugin,name,rank,ext,_data)             \
4354 G_BEGIN_DECLS{                                                          \
4355   GstTypeFindData *sw_data = g_new (GstTypeFindData, 1);                \
4356   sw_data->data = (gpointer)_data;                                      \
4357   sw_data->size = 4;                                                    \
4358   sw_data->probability = GST_TYPE_FIND_MAXIMUM;                         \
4359   sw_data->caps = gst_caps_new_empty_simple (name);                     \
4360   if (!gst_type_find_register (plugin, name, rank, riff_type_find,      \
4361                       (char **) ext, sw_data->caps, sw_data,            \
4362                       (GDestroyNotify) (sw_data_destroy))) {            \
4363     gst_caps_unref (sw_data->caps);                                     \
4364     g_free (sw_data);                                                   \
4365   }                                                                     \
4366 }G_END_DECLS
4367
4368
4369 /*** plugin initialization ***/
4370
4371 #define TYPE_FIND_REGISTER(plugin,name,rank,func,ext,caps,priv,notify) \
4372 G_BEGIN_DECLS{\
4373   if (!gst_type_find_register (plugin, name, rank, func, (char **) ext, caps, priv, notify))\
4374     return FALSE; \
4375 }G_END_DECLS
4376
4377
4378 static gboolean
4379 plugin_init (GstPlugin * plugin)
4380 {
4381   /* can't initialize this via a struct as caps can't be statically initialized */
4382
4383   /* note: asx/wax/wmx are XML files, asf doesn't handle them */
4384   /* FIXME-0.11: these should be const,
4385      this requires gstreamer/gst/gsttypefind::gst_type_find_register()
4386      to have define the parameter as const
4387    */
4388   static const gchar *asf_exts[] = { "asf", "wm", "wma", "wmv", NULL };
4389   static const gchar *au_exts[] = { "au", "snd", NULL };
4390   static const gchar *avi_exts[] = { "avi", NULL };
4391   static const gchar *qcp_exts[] = { "qcp", NULL };
4392   static const gchar *cdxa_exts[] = { "dat", NULL };
4393   static const gchar *flac_exts[] = { "flac", NULL };
4394   static const gchar *flx_exts[] = { "flc", "fli", NULL };
4395   static const gchar *id3_exts[] =
4396       { "mp3", "mp2", "mp1", "mpga", "ogg", "flac", "tta", NULL };
4397   static const gchar *apetag_exts[] = { "mp3", "ape", "mpc", "wv", NULL };
4398   static const gchar *tta_exts[] = { "tta", NULL };
4399   static const gchar *mod_exts[] = { "669", "amf", "dsm", "gdm", "far", "imf",
4400     "it", "med", "mod", "mtm", "okt", "sam",
4401     "s3m", "stm", "stx", "ult", "xm", NULL
4402   };
4403   static const gchar *mp3_exts[] = { "mp3", "mp2", "mp1", "mpga", NULL };
4404   static const gchar *ac3_exts[] = { "ac3", "eac3", NULL };
4405   static const gchar *dts_exts[] = { "dts", NULL };
4406   static const gchar *gsm_exts[] = { "gsm", NULL };
4407   static const gchar *musepack_exts[] = { "mpc", "mpp", "mp+", NULL };
4408   static const gchar *mpeg_sys_exts[] = { "mpe", "mpeg", "mpg", NULL };
4409   static const gchar *mpeg_video_exts[] = { "mpv", "mpeg", "mpg", NULL };
4410   static const gchar *mpeg_ts_exts[] = { "ts", "mts", NULL };
4411   static const gchar *ogg_exts[] = { "anx", "ogg", "ogm", NULL };
4412   static const gchar *qt_exts[] = { "mov", NULL };
4413   static const gchar *qtif_exts[] = { "qif", "qtif", "qti", NULL };
4414   static const gchar *mj2_exts[] = { "mj2", NULL };
4415   static const gchar *jp2_exts[] = { "jp2", NULL };
4416   static const gchar *rm_exts[] = { "ra", "ram", "rm", "rmvb", NULL };
4417   static const gchar *swf_exts[] = { "swf", "swfl", NULL };
4418   static const gchar *utf8_exts[] = { "txt", NULL };
4419   static const gchar *unicode_exts[] = { "txt", NULL };
4420   static const gchar *wav_exts[] = { "wav", NULL };
4421   static const gchar *aiff_exts[] = { "aiff", "aif", "aifc", NULL };
4422   static const gchar *svx_exts[] = { "iff", "svx", NULL };
4423   static const gchar *paris_exts[] = { "paf", NULL };
4424   static const gchar *nist_exts[] = { "nist", NULL };
4425   static const gchar *voc_exts[] = { "voc", NULL };
4426   static const gchar *sds_exts[] = { "sds", NULL };
4427   static const gchar *ircam_exts[] = { "sf", NULL };
4428   static const gchar *w64_exts[] = { "w64", NULL };
4429   static const gchar *shn_exts[] = { "shn", NULL };
4430   static const gchar *ape_exts[] = { "ape", NULL };
4431   static const gchar *uri_exts[] = { "ram", NULL };
4432   static const gchar *hls_exts[] = { "m3u8", NULL };
4433   static const gchar *sdp_exts[] = { "sdp", NULL };
4434   static const gchar *smil_exts[] = { "smil", NULL };
4435   static const gchar *html_exts[] = { "htm", "html", NULL };
4436   static const gchar *xml_exts[] = { "xml", NULL };
4437   static const gchar *jpeg_exts[] = { "jpg", "jpe", "jpeg", NULL };
4438   static const gchar *gif_exts[] = { "gif", NULL };
4439   static const gchar *png_exts[] = { "png", NULL };
4440   static const gchar *bmp_exts[] = { "bmp", NULL };
4441   static const gchar *tiff_exts[] = { "tif", "tiff", NULL };
4442   static const gchar *matroska_exts[] = { "mkv", "mka", NULL };
4443   static const gchar *webm_exts[] = { "webm", NULL };
4444   static const gchar *mve_exts[] = { "mve", NULL };
4445   static const gchar *dv_exts[] = { "dv", "dif", NULL };
4446   static const gchar *amr_exts[] = { "amr", NULL };
4447   static const gchar *ilbc_exts[] = { "ilbc", NULL };
4448   static const gchar *sid_exts[] = { "sid", NULL };
4449   static const gchar *xcf_exts[] = { "xcf", NULL };
4450   static const gchar *mng_exts[] = { "mng", NULL };
4451   static const gchar *jng_exts[] = { "jng", NULL };
4452   static const gchar *xpm_exts[] = { "xpm", NULL };
4453   static const gchar *pnm_exts[] = { "pnm", "ppm", "pgm", "pbm", NULL };
4454   static const gchar *ras_exts[] = { "ras", NULL };
4455   static const gchar *bz2_exts[] = { "bz2", NULL };
4456   static const gchar *gz_exts[] = { "gz", NULL };
4457   static const gchar *zip_exts[] = { "zip", NULL };
4458   static const gchar *compress_exts[] = { "Z", NULL };
4459   static const gchar *m4a_exts[] = { "m4a", NULL };
4460   static const gchar *q3gp_exts[] = { "3gp", NULL };
4461   static const gchar *aac_exts[] = { "aac", "adts", "adif", "loas", NULL };
4462   static const gchar *spc_exts[] = { "spc", NULL };
4463   static const gchar *wavpack_exts[] = { "wv", "wvp", NULL };
4464   static const gchar *wavpack_correction_exts[] = { "wvc", NULL };
4465   static const gchar *rar_exts[] = { "rar", NULL };
4466   static const gchar *tar_exts[] = { "tar", NULL };
4467   static const gchar *ar_exts[] = { "a", NULL };
4468   static const gchar *msdos_exts[] = { "dll", "exe", "ocx", "sys", "scr",
4469     "msstyles", "cpl", NULL
4470   };
4471   static const gchar *flv_exts[] = { "flv", NULL };
4472   static const gchar *m4v_exts[] = { "m4v", NULL };
4473   static const gchar *h263_exts[] = { "h263", "263", NULL };
4474   static const gchar *h264_exts[] = { "h264", "x264", "264", NULL };
4475   static const gchar *nuv_exts[] = { "nuv", NULL };
4476   static const gchar *vivo_exts[] = { "viv", NULL };
4477   static const gchar *nsf_exts[] = { "nsf", NULL };
4478   static const gchar *gym_exts[] = { "gym", NULL };
4479   static const gchar *ay_exts[] = { "ay", NULL };
4480   static const gchar *gbs_exts[] = { "gbs", NULL };
4481   static const gchar *kss_exts[] = { "kss", NULL };
4482   static const gchar *sap_exts[] = { "sap", NULL };
4483   static const gchar *vgm_exts[] = { "vgm", NULL };
4484   static const gchar *mid_exts[] = { "mid", "midi", NULL };
4485   static const gchar *mxmf_exts[] = { "mxmf", NULL };
4486   static const gchar *imelody_exts[] = { "imy", "ime", "imelody", NULL };
4487   static const gchar *pdf_exts[] = { "pdf", NULL };
4488   static const gchar *ps_exts[] = { "ps", NULL };
4489   static const gchar *svg_exts[] = { "svg", NULL };
4490   static const gchar *mxf_exts[] = { "mxf", NULL };
4491   static const gchar *ivf_exts[] = { "ivf", NULL };
4492   static const gchar *msword_exts[] = { "doc", NULL };
4493   static const gchar *dsstore_exts[] = { "DS_Store", NULL };
4494   static const gchar *psd_exts[] = { "psd", NULL };
4495   static const gchar *y4m_exts[] = { "y4m", NULL };
4496
4497   GST_DEBUG_CATEGORY_INIT (type_find_debug, "typefindfunctions",
4498       GST_DEBUG_FG_GREEN | GST_DEBUG_BG_RED, "generic type find functions");
4499
4500   /* must use strings, macros don't accept initializers */
4501   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ms-asf", GST_RANK_SECONDARY,
4502       asf_exts,
4503       "\060\046\262\165\216\146\317\021\246\331\000\252\000\142\316\154", 16,
4504       GST_TYPE_FIND_MAXIMUM);
4505   TYPE_FIND_REGISTER (plugin, "audio/x-musepack", GST_RANK_PRIMARY,
4506       musepack_type_find, musepack_exts, MUSEPACK_CAPS, NULL, NULL);
4507   TYPE_FIND_REGISTER (plugin, "audio/x-au", GST_RANK_MARGINAL,
4508       au_type_find, au_exts, AU_CAPS, NULL, NULL);
4509   TYPE_FIND_REGISTER_RIFF (plugin, "video/x-msvideo", GST_RANK_PRIMARY,
4510       avi_exts, "AVI ");
4511   TYPE_FIND_REGISTER_RIFF (plugin, "audio/qcelp", GST_RANK_PRIMARY,
4512       qcp_exts, "QLCM");
4513   TYPE_FIND_REGISTER_RIFF (plugin, "video/x-cdxa", GST_RANK_PRIMARY,
4514       cdxa_exts, "CDXA");
4515   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-vcd", GST_RANK_PRIMARY,
4516       cdxa_exts, "\000\377\377\377\377\377\377\377\377\377\377\000", 12,
4517       GST_TYPE_FIND_MAXIMUM);
4518   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-imelody", GST_RANK_PRIMARY,
4519       imelody_exts, "BEGIN:IMELODY", 13, GST_TYPE_FIND_MAXIMUM);
4520 #if 0
4521   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-smoke", GST_RANK_PRIMARY,
4522       NULL, "\x80smoke\x00\x01\x00", 6, GST_TYPE_FIND_MAXIMUM);
4523 #endif
4524   TYPE_FIND_REGISTER (plugin, "audio/midi", GST_RANK_PRIMARY, mid_type_find,
4525       mid_exts, MID_CAPS, NULL, NULL);
4526   TYPE_FIND_REGISTER_RIFF (plugin, "audio/riff-midi", GST_RANK_PRIMARY,
4527       mid_exts, "RMID");
4528   TYPE_FIND_REGISTER (plugin, "audio/mobile-xmf", GST_RANK_PRIMARY,
4529       mxmf_type_find, mxmf_exts, MXMF_CAPS, NULL, NULL);
4530   TYPE_FIND_REGISTER (plugin, "video/x-fli", GST_RANK_MARGINAL, flx_type_find,
4531       flx_exts, FLX_CAPS, NULL, NULL);
4532   TYPE_FIND_REGISTER (plugin, "application/x-id3v2", GST_RANK_PRIMARY + 103,
4533       id3v2_type_find, id3_exts, ID3_CAPS, NULL, NULL);
4534   TYPE_FIND_REGISTER (plugin, "application/x-id3v1", GST_RANK_PRIMARY + 101,
4535       id3v1_type_find, id3_exts, ID3_CAPS, NULL, NULL);
4536   TYPE_FIND_REGISTER (plugin, "application/x-apetag", GST_RANK_PRIMARY + 102,
4537       apetag_type_find, apetag_exts, APETAG_CAPS, NULL, NULL);
4538   TYPE_FIND_REGISTER (plugin, "audio/x-ttafile", GST_RANK_PRIMARY,
4539       tta_type_find, tta_exts, TTA_CAPS, NULL, NULL);
4540   TYPE_FIND_REGISTER (plugin, "audio/x-mod", GST_RANK_SECONDARY, mod_type_find,
4541       mod_exts, MOD_CAPS, NULL, NULL);
4542   TYPE_FIND_REGISTER (plugin, "audio/mpeg", GST_RANK_PRIMARY, mp3_type_find,
4543       mp3_exts, MP3_CAPS, NULL, NULL);
4544   TYPE_FIND_REGISTER (plugin, "audio/x-ac3", GST_RANK_PRIMARY, ac3_type_find,
4545       ac3_exts, AC3_CAPS, NULL, NULL);
4546   TYPE_FIND_REGISTER (plugin, "audio/x-dts", GST_RANK_SECONDARY, dts_type_find,
4547       dts_exts, DTS_CAPS, NULL, NULL);
4548   TYPE_FIND_REGISTER (plugin, "audio/x-gsm", GST_RANK_PRIMARY, NULL, gsm_exts,
4549       GSM_CAPS, NULL, NULL);
4550   TYPE_FIND_REGISTER (plugin, "video/mpeg-sys", GST_RANK_PRIMARY,
4551       mpeg_sys_type_find, mpeg_sys_exts, MPEG_SYS_CAPS, NULL, NULL);
4552   TYPE_FIND_REGISTER (plugin, "video/mpegts", GST_RANK_PRIMARY,
4553       mpeg_ts_type_find, mpeg_ts_exts, MPEGTS_CAPS, NULL, NULL);
4554   TYPE_FIND_REGISTER (plugin, "application/ogg", GST_RANK_PRIMARY,
4555       ogganx_type_find, ogg_exts, OGGANX_CAPS, NULL, NULL);
4556   TYPE_FIND_REGISTER (plugin, "video/mpeg-elementary", GST_RANK_MARGINAL,
4557       mpeg_video_stream_type_find, mpeg_video_exts, MPEG_VIDEO_CAPS, NULL,
4558       NULL);
4559   TYPE_FIND_REGISTER (plugin, "video/mpeg4", GST_RANK_PRIMARY,
4560       mpeg4_video_type_find, m4v_exts, MPEG_VIDEO_CAPS, NULL, NULL);
4561   TYPE_FIND_REGISTER (plugin, "video/x-h263", GST_RANK_SECONDARY,
4562       h263_video_type_find, h263_exts, H263_VIDEO_CAPS, NULL, NULL);
4563   TYPE_FIND_REGISTER (plugin, "video/x-h264", GST_RANK_PRIMARY,
4564       h264_video_type_find, h264_exts, H264_VIDEO_CAPS, NULL, NULL);
4565   TYPE_FIND_REGISTER (plugin, "video/x-nuv", GST_RANK_SECONDARY, nuv_type_find,
4566       nuv_exts, NUV_CAPS, NULL, NULL);
4567
4568   /* ISO formats */
4569   TYPE_FIND_REGISTER (plugin, "audio/x-m4a", GST_RANK_PRIMARY, m4a_type_find,
4570       m4a_exts, M4A_CAPS, NULL, NULL);
4571   TYPE_FIND_REGISTER (plugin, "application/x-3gp", GST_RANK_PRIMARY,
4572       q3gp_type_find, q3gp_exts, Q3GP_CAPS, NULL, NULL);
4573   TYPE_FIND_REGISTER (plugin, "video/quicktime", GST_RANK_SECONDARY,
4574       qt_type_find, qt_exts, QT_CAPS, NULL, NULL);
4575   TYPE_FIND_REGISTER (plugin, "image/x-quicktime", GST_RANK_SECONDARY,
4576       qtif_type_find, qtif_exts, QTIF_CAPS, NULL, NULL);
4577   TYPE_FIND_REGISTER (plugin, "image/jp2", GST_RANK_PRIMARY,
4578       jp2_type_find, jp2_exts, JP2_CAPS, NULL, NULL);
4579   TYPE_FIND_REGISTER (plugin, "video/mj2", GST_RANK_PRIMARY,
4580       jp2_type_find, mj2_exts, MJ2_CAPS, NULL, NULL);
4581
4582   TYPE_FIND_REGISTER (plugin, "text/html", GST_RANK_SECONDARY, html_type_find,
4583       html_exts, HTML_CAPS, NULL, NULL);
4584   TYPE_FIND_REGISTER_START_WITH (plugin, "application/vnd.rn-realmedia",
4585       GST_RANK_SECONDARY, rm_exts, ".RMF", 4, GST_TYPE_FIND_MAXIMUM);
4586   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-pn-realaudio",
4587       GST_RANK_SECONDARY, rm_exts, ".ra\375", 4, GST_TYPE_FIND_MAXIMUM);
4588   TYPE_FIND_REGISTER (plugin, "application/x-shockwave-flash",
4589       GST_RANK_SECONDARY, swf_type_find, swf_exts, SWF_CAPS, NULL, NULL);
4590   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-flv", GST_RANK_SECONDARY,
4591       flv_exts, "FLV", 3, GST_TYPE_FIND_MAXIMUM);
4592   TYPE_FIND_REGISTER (plugin, "text/plain", GST_RANK_MARGINAL, utf8_type_find,
4593       utf8_exts, UTF8_CAPS, NULL, NULL);
4594   TYPE_FIND_REGISTER (plugin, "text/utf-16", GST_RANK_MARGINAL, utf16_type_find,
4595       unicode_exts, UTF16_CAPS, NULL, NULL);
4596   TYPE_FIND_REGISTER (plugin, "text/utf-32", GST_RANK_MARGINAL, utf32_type_find,
4597       unicode_exts, UTF32_CAPS, NULL, NULL);
4598   TYPE_FIND_REGISTER (plugin, "text/uri-list", GST_RANK_MARGINAL, uri_type_find,
4599       uri_exts, URI_CAPS, NULL, NULL);
4600   TYPE_FIND_REGISTER (plugin, "application/x-hls", GST_RANK_MARGINAL,
4601       hls_type_find, hls_exts, HLS_CAPS, NULL, NULL);
4602   TYPE_FIND_REGISTER (plugin, "application/sdp", GST_RANK_SECONDARY,
4603       sdp_type_find, sdp_exts, SDP_CAPS, NULL, NULL);
4604   TYPE_FIND_REGISTER (plugin, "application/smil", GST_RANK_SECONDARY,
4605       smil_type_find, smil_exts, SMIL_CAPS, NULL, NULL);
4606   TYPE_FIND_REGISTER (plugin, "application/xml", GST_RANK_MARGINAL,
4607       xml_type_find, xml_exts, GENERIC_XML_CAPS, NULL, NULL);
4608   TYPE_FIND_REGISTER_RIFF (plugin, "audio/x-wav", GST_RANK_PRIMARY, wav_exts,
4609       "WAVE");
4610   TYPE_FIND_REGISTER (plugin, "audio/x-aiff", GST_RANK_SECONDARY,
4611       aiff_type_find, aiff_exts, AIFF_CAPS, NULL, NULL);
4612   TYPE_FIND_REGISTER (plugin, "audio/x-svx", GST_RANK_SECONDARY, svx_type_find,
4613       svx_exts, SVX_CAPS, NULL, NULL);
4614   TYPE_FIND_REGISTER (plugin, "audio/x-paris", GST_RANK_SECONDARY,
4615       paris_type_find, paris_exts, PARIS_CAPS, NULL, NULL);
4616   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nist", GST_RANK_SECONDARY,
4617       nist_exts, "NIST", 4, GST_TYPE_FIND_MAXIMUM);
4618   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-voc", GST_RANK_SECONDARY,
4619       voc_exts, "Creative", 8, GST_TYPE_FIND_MAXIMUM);
4620   TYPE_FIND_REGISTER (plugin, "audio/x-sds", GST_RANK_SECONDARY, sds_type_find,
4621       sds_exts, SDS_CAPS, NULL, NULL);
4622   TYPE_FIND_REGISTER (plugin, "audio/x-ircam", GST_RANK_SECONDARY,
4623       ircam_type_find, ircam_exts, IRCAM_CAPS, NULL, NULL);
4624   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-w64", GST_RANK_SECONDARY,
4625       w64_exts, "riff", 4, GST_TYPE_FIND_MAXIMUM);
4626   TYPE_FIND_REGISTER (plugin, "audio/x-shorten", GST_RANK_SECONDARY,
4627       shn_type_find, shn_exts, SHN_CAPS, NULL, NULL);
4628   TYPE_FIND_REGISTER (plugin, "application/x-ape", GST_RANK_SECONDARY,
4629       ape_type_find, ape_exts, APE_CAPS, NULL, NULL);
4630   TYPE_FIND_REGISTER (plugin, "image/jpeg", GST_RANK_PRIMARY + 15,
4631       jpeg_type_find, jpeg_exts, JPEG_CAPS, NULL, NULL);
4632   TYPE_FIND_REGISTER_START_WITH (plugin, "image/gif", GST_RANK_PRIMARY,
4633       gif_exts, "GIF8", 4, GST_TYPE_FIND_MAXIMUM);
4634   TYPE_FIND_REGISTER_START_WITH (plugin, "image/png", GST_RANK_PRIMARY + 14,
4635       png_exts, "\211PNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
4636   TYPE_FIND_REGISTER (plugin, "image/bmp", GST_RANK_PRIMARY, bmp_type_find,
4637       bmp_exts, BMP_CAPS, NULL, NULL);
4638   TYPE_FIND_REGISTER (plugin, "image/tiff", GST_RANK_PRIMARY, tiff_type_find,
4639       tiff_exts, TIFF_CAPS, NULL, NULL);
4640   TYPE_FIND_REGISTER (plugin, "image/x-portable-pixmap", GST_RANK_SECONDARY,
4641       pnm_type_find, pnm_exts, PNM_CAPS, NULL, NULL);
4642   TYPE_FIND_REGISTER (plugin, "video/x-matroska", GST_RANK_PRIMARY,
4643       matroska_type_find, matroska_exts, MATROSKA_CAPS, NULL, NULL);
4644   TYPE_FIND_REGISTER (plugin, "video/webm", GST_RANK_PRIMARY,
4645       webm_type_find, webm_exts, WEBM_CAPS, NULL, NULL);
4646   TYPE_FIND_REGISTER (plugin, "application/mxf", GST_RANK_PRIMARY,
4647       mxf_type_find, mxf_exts, MXF_CAPS, NULL, NULL);
4648   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mve", GST_RANK_SECONDARY,
4649       mve_exts, "Interplay MVE File\032\000\032\000\000\001\063\021", 26,
4650       GST_TYPE_FIND_MAXIMUM);
4651   TYPE_FIND_REGISTER (plugin, "video/x-dv", GST_RANK_SECONDARY, dv_type_find,
4652       dv_exts, DV_CAPS, NULL, NULL);
4653   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-nb-sh", GST_RANK_PRIMARY,
4654       amr_exts, "#!AMR", 5, GST_TYPE_FIND_LIKELY);
4655   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-wb-sh", GST_RANK_PRIMARY,
4656       amr_exts, "#!AMR-WB", 7, GST_TYPE_FIND_MAXIMUM);
4657   TYPE_FIND_REGISTER (plugin, "audio/iLBC-sh", GST_RANK_PRIMARY,
4658       ilbc_type_find, ilbc_exts, ILBC_CAPS, NULL, NULL);
4659   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sid", GST_RANK_MARGINAL,
4660       sid_exts, "PSID", 4, GST_TYPE_FIND_MAXIMUM);
4661   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xcf", GST_RANK_SECONDARY,
4662       xcf_exts, "gimp xcf", 8, GST_TYPE_FIND_MAXIMUM);
4663   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mng", GST_RANK_SECONDARY,
4664       mng_exts, "\212MNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
4665   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-jng", GST_RANK_SECONDARY,
4666       jng_exts, "\213JNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
4667   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xpixmap", GST_RANK_SECONDARY,
4668       xpm_exts, "/* XPM */", 9, GST_TYPE_FIND_MAXIMUM);
4669   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-sun-raster",
4670       GST_RANK_SECONDARY, ras_exts, "\131\246\152\225", 4,
4671       GST_TYPE_FIND_MAXIMUM);
4672   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-bzip",
4673       GST_RANK_SECONDARY, bz2_exts, "BZh", 3, GST_TYPE_FIND_LIKELY);
4674   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-gzip",
4675       GST_RANK_SECONDARY, gz_exts, "\037\213", 2, GST_TYPE_FIND_LIKELY);
4676   TYPE_FIND_REGISTER_START_WITH (plugin, "application/zip", GST_RANK_SECONDARY,
4677       zip_exts, "PK\003\004", 4, GST_TYPE_FIND_LIKELY);
4678   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-compress",
4679       GST_RANK_SECONDARY, compress_exts, "\037\235", 2, GST_TYPE_FIND_LIKELY);
4680   TYPE_FIND_REGISTER (plugin, "subtitle/x-kate", GST_RANK_MARGINAL,
4681       kate_type_find, NULL, NULL, NULL, NULL);
4682   TYPE_FIND_REGISTER (plugin, "audio/x-flac", GST_RANK_PRIMARY,
4683       flac_type_find, flac_exts, FLAC_CAPS, NULL, NULL);
4684   TYPE_FIND_REGISTER (plugin, "audio/x-vorbis", GST_RANK_PRIMARY,
4685       vorbis_type_find, NULL, VORBIS_CAPS, NULL, NULL);
4686   TYPE_FIND_REGISTER (plugin, "video/x-theora", GST_RANK_PRIMARY,
4687       theora_type_find, NULL, THEORA_CAPS, NULL, NULL);
4688   TYPE_FIND_REGISTER (plugin, "application/x-ogm-video", GST_RANK_PRIMARY,
4689       ogmvideo_type_find, NULL, OGMVIDEO_CAPS, NULL, NULL);
4690   TYPE_FIND_REGISTER (plugin, "application/x-ogm-audio", GST_RANK_PRIMARY,
4691       ogmaudio_type_find, NULL, OGMAUDIO_CAPS, NULL, NULL);
4692   TYPE_FIND_REGISTER (plugin, "application/x-ogm-text", GST_RANK_PRIMARY,
4693       ogmtext_type_find, NULL, OGMTEXT_CAPS, NULL, NULL);
4694   TYPE_FIND_REGISTER (plugin, "audio/x-speex", GST_RANK_PRIMARY,
4695       speex_type_find, NULL, SPEEX_CAPS, NULL, NULL);
4696   TYPE_FIND_REGISTER (plugin, "audio/x-celt", GST_RANK_PRIMARY,
4697       celt_type_find, NULL, CELT_CAPS, NULL, NULL);
4698   TYPE_FIND_REGISTER (plugin, "application/x-ogg-skeleton", GST_RANK_PRIMARY,
4699       oggskel_type_find, NULL, OGG_SKELETON_CAPS, NULL, NULL);
4700   TYPE_FIND_REGISTER (plugin, "text/x-cmml", GST_RANK_PRIMARY, cmml_type_find,
4701       NULL, CMML_CAPS, NULL, NULL);
4702   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-executable",
4703       GST_RANK_MARGINAL, NULL, "\177ELF", 4, GST_TYPE_FIND_MAXIMUM);
4704   TYPE_FIND_REGISTER (plugin, "audio/aac", GST_RANK_SECONDARY,
4705       aac_type_find, aac_exts, AAC_CAPS, NULL, NULL);
4706   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-spc", GST_RANK_SECONDARY,
4707       spc_exts, "SNES-SPC700 Sound File Data", 27, GST_TYPE_FIND_MAXIMUM);
4708   TYPE_FIND_REGISTER (plugin, "audio/x-wavpack", GST_RANK_SECONDARY,
4709       wavpack_type_find, wavpack_exts, WAVPACK_CAPS, NULL, NULL);
4710   TYPE_FIND_REGISTER (plugin, "audio/x-wavpack-correction", GST_RANK_SECONDARY,
4711       wavpack_type_find, wavpack_correction_exts, WAVPACK_CORRECTION_CAPS, NULL,
4712       NULL);
4713   TYPE_FIND_REGISTER (plugin, "application/postscript", GST_RANK_SECONDARY,
4714       postscript_type_find, ps_exts, POSTSCRIPT_CAPS, NULL, NULL);
4715   TYPE_FIND_REGISTER (plugin, "image/svg+xml", GST_RANK_SECONDARY,
4716       svg_type_find, svg_exts, SVG_CAPS, NULL, NULL);
4717   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-rar",
4718       GST_RANK_SECONDARY, rar_exts, "Rar!", 4, GST_TYPE_FIND_LIKELY);
4719   TYPE_FIND_REGISTER (plugin, "application/x-tar", GST_RANK_SECONDARY,
4720       tar_type_find, tar_exts, TAR_CAPS, NULL, NULL);
4721   TYPE_FIND_REGISTER (plugin, "application/x-ar", GST_RANK_SECONDARY,
4722       ar_type_find, ar_exts, AR_CAPS, NULL, NULL);
4723   TYPE_FIND_REGISTER (plugin, "application/x-ms-dos-executable",
4724       GST_RANK_SECONDARY, msdos_type_find, msdos_exts, MSDOS_CAPS, NULL, NULL);
4725   TYPE_FIND_REGISTER (plugin, "video/x-dirac", GST_RANK_PRIMARY,
4726       dirac_type_find, NULL, DIRAC_CAPS, NULL, NULL);
4727   TYPE_FIND_REGISTER (plugin, "multipart/x-mixed-replace", GST_RANK_SECONDARY,
4728       multipart_type_find, NULL, MULTIPART_CAPS, NULL, NULL);
4729   TYPE_FIND_REGISTER (plugin, "application/x-mmsh", GST_RANK_SECONDARY,
4730       mmsh_type_find, NULL, MMSH_CAPS, NULL, NULL);
4731   TYPE_FIND_REGISTER (plugin, "video/vivo", GST_RANK_SECONDARY,
4732       vivo_type_find, vivo_exts, VIVO_CAPS, NULL, NULL);
4733   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nsf",
4734       GST_RANK_SECONDARY, nsf_exts, "NESM\x1a", 5, GST_TYPE_FIND_MAXIMUM);
4735   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gym",
4736       GST_RANK_SECONDARY, gym_exts, "GYMX", 4, GST_TYPE_FIND_MAXIMUM);
4737   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-ay",
4738       GST_RANK_SECONDARY, ay_exts, "ZXAYEMUL", 8, GST_TYPE_FIND_MAXIMUM);
4739   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gbs",
4740       GST_RANK_SECONDARY, gbs_exts, "GBS\x01", 4, GST_TYPE_FIND_MAXIMUM);
4741   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-vgm",
4742       GST_RANK_SECONDARY, vgm_exts, "Vgm\x20", 4, GST_TYPE_FIND_MAXIMUM);
4743   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sap",
4744       GST_RANK_SECONDARY, sap_exts, "SAP\x0d\x0a" "AUTHOR\x20", 12,
4745       GST_TYPE_FIND_MAXIMUM);
4746   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ivf", GST_RANK_SECONDARY,
4747       ivf_exts, "DKIF", 4, GST_TYPE_FIND_NEARLY_CERTAIN);
4748   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-kss", GST_RANK_SECONDARY,
4749       kss_exts, "KSSX\0", 5, GST_TYPE_FIND_MAXIMUM);
4750   TYPE_FIND_REGISTER_START_WITH (plugin, "application/pdf", GST_RANK_SECONDARY,
4751       pdf_exts, "%PDF-", 5, GST_TYPE_FIND_LIKELY);
4752   TYPE_FIND_REGISTER_START_WITH (plugin, "application/msword",
4753       GST_RANK_SECONDARY, msword_exts, "\320\317\021\340\241\261\032\341", 8,
4754       GST_TYPE_FIND_LIKELY);
4755   /* Mac OS X .DS_Store files tend to be taken for video/mpeg */
4756   TYPE_FIND_REGISTER_START_WITH (plugin, "application/octet-stream",
4757       GST_RANK_SECONDARY, dsstore_exts, "\000\000\000\001Bud1", 8,
4758       GST_TYPE_FIND_LIKELY);
4759   TYPE_FIND_REGISTER_START_WITH (plugin, "image/vnd.adobe.photoshop",
4760       GST_RANK_SECONDARY, psd_exts, "8BPS\000\001\000\000\000\000", 10,
4761       GST_TYPE_FIND_LIKELY);
4762   TYPE_FIND_REGISTER (plugin, "image/vnd.wap.wbmp", GST_RANK_MARGINAL,
4763       wbmp_typefind, NULL, NULL, NULL, NULL);
4764   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-yuv4mpeg",
4765       GST_RANK_SECONDARY, y4m_exts, "YUV4MPEG2 ", 10, GST_TYPE_FIND_LIKELY);
4766   TYPE_FIND_REGISTER (plugin, "image/x-icon", GST_RANK_MARGINAL,
4767       windows_icon_typefind, NULL, NULL, NULL, NULL);
4768
4769 #ifdef USE_GIO
4770   TYPE_FIND_REGISTER (plugin, "xdgmime-base", GST_RANK_MARGINAL,
4771       xdgmime_typefind, NULL, NULL, NULL, NULL);
4772 #endif
4773
4774   TYPE_FIND_REGISTER (plugin, "image/x-degas", GST_RANK_MARGINAL,
4775       degas_type_find, NULL, NULL, NULL, NULL);
4776
4777   return TRUE;
4778 }
4779
4780 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
4781     GST_VERSION_MINOR,
4782     "typefindfunctions",
4783     "default typefind functions",
4784     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)