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