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