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