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