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