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