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