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