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