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