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