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