subparse: Add drop-out-of-segment property
[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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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 #include <gst/base/gstbytereader.h>
45
46 GST_DEBUG_CATEGORY_STATIC (type_find_debug);
47 #define GST_CAT_DEFAULT type_find_debug
48
49 /* DataScanCtx: helper for typefind functions that scan through data
50  * step-by-step, to avoid doing a peek at each and every offset */
51
52 #define DATA_SCAN_CTX_CHUNK_SIZE 4096
53
54 typedef struct
55 {
56   guint64 offset;
57   const guint8 *data;
58   guint size;
59 } DataScanCtx;
60
61 static inline void
62 data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip)
63 {
64   c->offset += bytes_to_skip;
65   if (G_LIKELY (c->size > bytes_to_skip)) {
66     c->size -= bytes_to_skip;
67     c->data += bytes_to_skip;
68   } else {
69     c->data += c->size;
70     c->size = 0;
71   }
72 }
73
74 static inline gboolean
75 data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, guint min_len)
76 {
77   const guint8 *data;
78   guint64 len;
79   guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
80
81   if (G_LIKELY (c->size >= min_len))
82     return TRUE;
83
84   data = gst_type_find_peek (tf, c->offset, chunk_len);
85   if (G_LIKELY (data != NULL)) {
86     c->data = data;
87     c->size = chunk_len;
88     return TRUE;
89   }
90
91   /* if there's less than our chunk size, try to get as much as we can, but
92    * always at least min_len bytes (we might be typefinding the first buffer
93    * of the stream and not have as much data available as we'd like) */
94   len = gst_type_find_get_length (tf);
95   if (len > 0) {
96     len = CLAMP (len - c->offset, min_len, chunk_len);
97   } else {
98     len = min_len;
99   }
100
101   data = gst_type_find_peek (tf, c->offset, len);
102   if (data != NULL) {
103     c->data = data;
104     c->size = len;
105     return TRUE;
106   }
107
108   return FALSE;
109 }
110
111 static inline gboolean
112 data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset,
113     const gchar * data, guint len)
114 {
115   if (G_UNLIKELY (offset + len >= G_MAXUINT32))
116     return FALSE;
117
118   if (!data_scan_ctx_ensure_data (tf, c, offset + len))
119     return FALSE;
120
121   return (memcmp (c->data + offset, data, len) == 0);
122 }
123
124 /*** text/plain ***/
125 static gboolean xml_check_first_element (GstTypeFind * tf,
126     const gchar * element, guint elen, gboolean strict);
127 static gboolean sdp_check_header (GstTypeFind * tf);
128
129 static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
130
131 #define UTF8_CAPS gst_static_caps_get(&utf8_caps)
132
133 static gboolean
134 utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
135     GstTypeFindProbability * prob)
136 {
137   const guint8 *data;
138
139   /* randomly decided values */
140   guint min_size = 16;          /* minimum size  */
141   guint size = 32 * 1024;       /* starting size */
142   guint probability = 95;       /* starting probability */
143   guint step = 10;              /* how much we reduce probability in each
144                                  * iteration */
145
146   while (probability > step && size > min_size) {
147     data = gst_type_find_peek (tf, offset, size);
148     if (data) {
149       gchar *end;
150       gchar *start = (gchar *) data;
151
152       if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) {   /* allow last char to be cut off */
153         *prob = probability;
154         return TRUE;
155       }
156       *prob = 0;
157       return FALSE;
158     }
159     size /= 2;
160     probability -= step;
161   }
162   *prob = 0;
163   return FALSE;
164 }
165
166 static void
167 utf8_type_find (GstTypeFind * tf, gpointer unused)
168 {
169   GstTypeFindProbability start_prob, mid_prob;
170   guint64 length;
171
172   /* leave xml to the xml typefinders */
173   if (xml_check_first_element (tf, "", 0, TRUE))
174     return;
175
176   /* leave sdp to the sdp typefinders */
177   if (sdp_check_header (tf))
178     return;
179
180   /* check beginning of stream */
181   if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob))
182     return;
183
184   GST_LOG ("start is plain text with probability of %u", start_prob);
185
186   /* POSSIBLE is the highest probability we ever return if we can't
187    * probe into the middle of the file and don't know its length */
188
189   length = gst_type_find_get_length (tf);
190   if (length == 0 || length == (guint64) - 1) {
191     gst_type_find_suggest (tf, MIN (start_prob, GST_TYPE_FIND_POSSIBLE),
192         UTF8_CAPS);
193     return;
194   }
195
196   if (length < 64 * 1024) {
197     gst_type_find_suggest (tf, start_prob, UTF8_CAPS);
198     return;
199   }
200
201   /* check middle of stream */
202   if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob))
203     return;
204
205   GST_LOG ("middle is plain text with probability of %u", mid_prob);
206   gst_type_find_suggest (tf, (start_prob + mid_prob) / 2, UTF8_CAPS);
207 }
208
209 /*** text/utf-16 and text/utf-32} ***/
210 /* While UTF-8 is unicode too, using text/plain for UTF-16 and UTF-32
211    is going to break stuff. */
212
213 typedef struct
214 {
215   size_t bomlen;
216   const char *const bom;
217     gboolean (*checker) (const guint8 *, gint, gint);
218   int boost;
219   int endianness;
220 } GstUnicodeTester;
221
222 static gboolean
223 check_utf16 (const guint8 * data, gint len, gint endianness)
224 {
225   GstByteReader br;
226   guint16 high, low;
227
228   low = high = 0;
229
230   if (len & 1)
231     return FALSE;
232
233   gst_byte_reader_init (&br, data, len);
234   while (len >= 2) {
235     /* test first for a single 16 bit value in the BMP */
236     if (endianness == G_BIG_ENDIAN)
237       high = gst_byte_reader_get_uint16_be_unchecked (&br);
238     else
239       high = gst_byte_reader_get_uint16_le_unchecked (&br);
240     if (high >= 0xD800 && high <= 0xDBFF) {
241       /* start of a surrogate pair */
242       if (len < 4)
243         return FALSE;
244       len -= 2;
245       if (endianness == G_BIG_ENDIAN)
246         low = gst_byte_reader_get_uint16_be_unchecked (&br);
247       else
248         low = gst_byte_reader_get_uint16_le_unchecked (&br);
249       if (low >= 0xDC00 && low <= 0xDFFF) {
250         /* second half of the surrogate pair */
251       } else
252         return FALSE;
253     } else {
254       if (high >= 0xDC00 && high <= 0xDFFF)
255         return FALSE;
256     }
257     len -= 2;
258   }
259   return TRUE;
260 }
261
262 static gboolean
263 check_utf32 (const guint8 * data, gint len, gint endianness)
264 {
265   if (len & 3)
266     return FALSE;
267   while (len > 3) {
268     guint32 v;
269     if (endianness == G_BIG_ENDIAN)
270       v = GST_READ_UINT32_BE (data);
271     else
272       v = GST_READ_UINT32_LE (data);
273     if (v >= 0x10FFFF)
274       return FALSE;
275     data += 4;
276     len -= 4;
277   }
278   return TRUE;
279 }
280
281 static void
282 unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester,
283     guint n_tester, const char *media_type, gboolean require_bom)
284 {
285   gsize n;
286   gsize len = 4;
287   const guint8 *data = gst_type_find_peek (tf, 0, len);
288   int prob = -1;
289   const gint max_scan_size = 256 * 1024;
290   int endianness = 0;
291
292   if (!data) {
293     len = 2;
294     data = gst_type_find_peek (tf, 0, len);
295     if (!data)
296       return;
297   }
298
299   /* find a large enough size that works */
300   while (len < max_scan_size) {
301     size_t newlen = len << 1;
302     const guint8 *newdata = gst_type_find_peek (tf, 0, newlen);
303     if (!newdata)
304       break;
305     len = newlen;
306     data = newdata;
307   }
308
309   for (n = 0; n < n_tester; ++n) {
310     int bom_boost = 0, tmpprob;
311     if (len >= tester[n].bomlen) {
312       if (!memcmp (data, tester[n].bom, tester[n].bomlen))
313         bom_boost = tester[n].boost;
314     }
315     if (require_bom && bom_boost == 0)
316       continue;
317     if (!(*tester[n].checker) (data, len, tester[n].endianness))
318       continue;
319     tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost;
320     if (tmpprob > prob) {
321       prob = tmpprob;
322       endianness = tester[n].endianness;
323     }
324   }
325
326   if (prob > 0) {
327     GST_DEBUG ("This is valid %s %s", media_type,
328         endianness == G_BIG_ENDIAN ? "be" : "le");
329     gst_type_find_suggest_simple (tf, prob, media_type,
330         "endianness", G_TYPE_INT, endianness, NULL);
331   }
332 }
333
334 static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16");
335
336 #define UTF16_CAPS gst_static_caps_get(&utf16_caps)
337
338 static void
339 utf16_type_find (GstTypeFind * tf, gpointer unused)
340 {
341   static const GstUnicodeTester utf16tester[2] = {
342     {2, "\xff\xfe", check_utf16, 10, G_LITTLE_ENDIAN},
343     {2, "\xfe\xff", check_utf16, 20, G_BIG_ENDIAN},
344   };
345   unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester),
346       "text/utf-16", TRUE);
347 }
348
349 static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32");
350
351 #define UTF32_CAPS gst_static_caps_get(&utf32_caps)
352
353 static void
354 utf32_type_find (GstTypeFind * tf, gpointer unused)
355 {
356   static const GstUnicodeTester utf32tester[2] = {
357     {4, "\xff\xfe\x00\x00", check_utf32, 10, G_LITTLE_ENDIAN},
358     {4, "\x00\x00\xfe\xff", check_utf32, 20, G_BIG_ENDIAN}
359   };
360   unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester),
361       "text/utf-32", TRUE);
362 }
363
364 /*** text/uri-list ***/
365 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
366 static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
367
368 #define URI_CAPS (gst_static_caps_get(&uri_caps))
369 #define BUFFER_SIZE 16          /* If the string is < 16 bytes we're screwed */
370 #define INC_BUFFER {                                                    \
371   pos++;                                                                \
372   if (pos == BUFFER_SIZE) {                                             \
373     pos = 0;                                                            \
374     offset += BUFFER_SIZE;                                              \
375     data = gst_type_find_peek (tf, offset, BUFFER_SIZE);                \
376     if (data == NULL) return;                                           \
377   } else {                                                              \
378     data++;                                                             \
379   }                                                                     \
380 }
381
382 static void
383 uri_type_find (GstTypeFind * tf, gpointer unused)
384 {
385   const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
386   guint pos = 0;
387   guint offset = 0;
388
389   if (data) {
390     /* Search for # comment lines */
391     while (*data == '#') {
392       /* Goto end of line */
393       while (*data != '\n') {
394         INC_BUFFER;
395       }
396
397       INC_BUFFER;
398     }
399
400     if (!g_ascii_isalpha (*data)) {
401       /* Had a non alpha char - can't be uri-list */
402       return;
403     }
404
405     INC_BUFFER;
406
407     while (g_ascii_isalnum (*data)) {
408       INC_BUFFER;
409     }
410
411     if (*data != ':') {
412       /* First non alpha char is not a : */
413       return;
414     }
415
416     /* Get the next 2 bytes as well */
417     data = gst_type_find_peek (tf, offset + pos, 3);
418     if (data == NULL)
419       return;
420
421     if (data[1] != '/' && data[2] != '/') {
422       return;
423     }
424
425     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, URI_CAPS);
426   }
427 }
428 #endif
429
430 /*** application/itc ***/
431 static GstStaticCaps itc_caps = GST_STATIC_CAPS ("application/itc");
432 #define ITC_CAPS (gst_static_caps_get(&itc_caps))
433
434 static void
435 itc_type_find (GstTypeFind * tf, gpointer unused)
436 {
437   DataScanCtx c = { 0, NULL, 0 };
438   guint8 magic[8] = { 0x00, 0x00, 0x01, 0x1C, 0x69, 0x74, 0x63, 0x68 };
439   guint8 preamble[4] = { 0x00, 0x00, 0x00, 0x02 };
440   guint8 artwork_marker[8] = { 0x00, 0x00, 0x00, 0x00, 0x61, 0x72, 0x74, 0x77 };
441   guint8 item_marker[4] = { 0x69, 0x74, 0x65, 0x6D };
442   GstTypeFindProbability itc_prob = GST_TYPE_FIND_NONE;
443   int i;
444
445   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8)))
446     return;
447
448   if (memcmp (c.data, magic, 8))
449     return;
450
451   /* At least we found the right magic */
452   itc_prob = GST_TYPE_FIND_MINIMUM;
453   data_scan_ctx_advance (tf, &c, 8);
454
455   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
456     goto done;
457
458   /* Check preamble 3 consecutive times */
459   for (i = 0; i < 3; i++) {
460     if (memcmp (c.data, preamble, 4))
461       goto done;
462     data_scan_ctx_advance (tf, &c, 4);
463   }
464
465   itc_prob = GST_TYPE_FIND_POSSIBLE;
466
467   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8)))
468     goto done;
469
470   if (memcmp (c.data, artwork_marker, 8))
471     goto done;
472
473   itc_prob = GST_TYPE_FIND_LIKELY;
474   data_scan_ctx_advance (tf, &c, 8);
475
476   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 256)))
477     goto done;
478
479   /* ...and 256 0x00 padding bytes on what looks like the header's end */
480   for (i = 0; i < 256; i++) {
481     if (c.data[i])
482       goto done;
483   }
484
485   itc_prob = GST_TYPE_FIND_NEARLY_CERTAIN;
486   data_scan_ctx_advance (tf, &c, 256);
487
488   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8)))
489     goto done;
490
491   if (memcmp (c.data + 4, item_marker, 4))
492     goto done;
493
494   itc_prob = GST_TYPE_FIND_MAXIMUM;
495
496 done:
497   gst_type_find_suggest (tf, itc_prob, ITC_CAPS);
498 }
499
500 /*** application/x-hls ***/
501
502 static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls");
503 #define HLS_CAPS (gst_static_caps_get(&hls_caps))
504
505 /* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */
506 static void
507 hls_type_find (GstTypeFind * tf, gpointer unused)
508 {
509   DataScanCtx c = { 0, NULL, 0 };
510
511   /* Minimum useful size is #EXTM3U\n + 1 tag + ':' = 30 bytes */
512   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 30)))
513     return;
514
515   if (memcmp (c.data, "#EXTM3U", 7))
516     return;
517
518   data_scan_ctx_advance (tf, &c, 7);
519
520   /* Check only the first 4KB */
521   while (c.offset < 4096) {
522     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21)))
523       return;
524
525     /* Search for # comment lines */
526     if (c.data[0] == '#' && (memcmp (c.data, "#EXT-X-TARGETDURATION", 21) == 0
527             || memcmp (c.data, "#EXT-X-STREAM-INF", 17) == 0
528             || memcmp (c.data, "#EXT-X-MEDIA", 12) == 0)) {
529       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HLS_CAPS);
530       return;
531     }
532
533     data_scan_ctx_advance (tf, &c, 1);
534   }
535 }
536
537
538 /*** application/xml **********************************************************/
539
540 #define XML_BUFFER_SIZE 16
541 #define XML_INC_BUFFER {                                                \
542   pos++;                                                                \
543   if (pos == XML_BUFFER_SIZE) {                                         \
544     pos = 0;                                                            \
545     offset += XML_BUFFER_SIZE;                                          \
546     data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE);            \
547     if (data == NULL) return FALSE;                                     \
548   } else {                                                              \
549     data++;                                                             \
550   }                                                                     \
551 }
552
553 #define XML_INC_BUFFER_DATA {                                           \
554   pos++;                                                                \
555   if (pos >= length) {                                                  \
556     return FALSE;                                                       \
557   } else {                                                              \
558     data++;                                                             \
559   }                                                                     \
560 }
561
562 static gboolean
563 xml_check_first_element_from_data (const guint8 * data, guint length,
564     const gchar * element, guint elen, gboolean strict)
565 {
566   gboolean got_xmldec;
567   guint pos = 0;
568
569   g_return_val_if_fail (data != NULL, FALSE);
570
571   if (length <= 5)
572     return FALSE;
573
574   /* look for the XMLDec
575    * see XML spec 2.8, Prolog and Document Type Declaration
576    * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
577   got_xmldec = (memcmp (data, "<?xml", 5) == 0);
578
579   if (strict && !got_xmldec)
580     return FALSE;
581
582   /* skip XMLDec in any case if we've got one */
583   if (got_xmldec) {
584     pos += 5;
585     data += 5;
586   }
587
588   /* look for the first element, it has to be the requested element. Bail
589    * out if it is not within the first 4kB. */
590   while (pos < MIN (4096, length)) {
591     while (*data != '<' && pos < MIN (4096, length)) {
592       XML_INC_BUFFER_DATA;
593     }
594
595     XML_INC_BUFFER_DATA;
596     if (!g_ascii_isalpha (*data)) {
597       /* if not alphabetic, it's a PI or an element / attribute declaration
598        * like <?xxx or <!xxx */
599       XML_INC_BUFFER_DATA;
600       continue;
601     }
602
603     /* the first normal element, check if it's the one asked for */
604     if (pos + elen + 1 >= length)
605       return FALSE;
606     return (element && strncmp ((const char *) data, element, elen) == 0);
607   }
608
609   return FALSE;
610 }
611
612 static gboolean
613 xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
614     gboolean strict)
615 {
616   gboolean got_xmldec;
617   const guint8 *data;
618   guint offset = 0;
619   guint pos = 0;
620
621   data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
622   if (!data)
623     return FALSE;
624
625   /* look for the XMLDec
626    * see XML spec 2.8, Prolog and Document Type Declaration
627    * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
628   got_xmldec = (memcmp (data, "<?xml", 5) == 0);
629
630   if (strict && !got_xmldec)
631     return FALSE;
632
633   /* skip XMLDec in any case if we've got one */
634   if (got_xmldec) {
635     pos += 5;
636     data += 5;
637   }
638
639   /* look for the first element, it has to be the requested element. Bail
640    * out if it is not within the first 4kB. */
641   while (data && (offset + pos) < 4096) {
642     while (*data != '<' && (offset + pos) < 4096) {
643       XML_INC_BUFFER;
644     }
645
646     XML_INC_BUFFER;
647     if (!g_ascii_isalpha (*data)) {
648       /* if not alphabetic, it's a PI or an element / attribute declaration
649        * like <?xxx or <!xxx */
650       XML_INC_BUFFER;
651       continue;
652     }
653
654     /* the first normal element, check if it's the one asked for */
655     data = gst_type_find_peek (tf, offset + pos, elen + 1);
656     return (data && element && strncmp ((char *) data, element, elen) == 0);
657   }
658
659   return FALSE;
660 }
661
662 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
663 static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml");
664
665 #define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps))
666 static void
667 xml_type_find (GstTypeFind * tf, gpointer unused)
668 {
669   if (xml_check_first_element (tf, "", 0, TRUE)) {
670     gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
671   }
672 }
673 #endif
674 /*** application/dash+xml ****************************************************/
675
676 static GstStaticCaps dash_caps = GST_STATIC_CAPS ("application/dash+xml");
677
678 #define DASH_CAPS gst_static_caps_get (&dash_caps)
679
680 static void
681 dash_mpd_type_find (GstTypeFind * tf, gpointer unused)
682 {
683   if (xml_check_first_element (tf, "MPD", 3, FALSE) ||
684       xml_check_first_element (tf, "mpd", 3, FALSE)) {
685     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DASH_CAPS);
686   }
687 }
688
689 /*** application/xges ****************************************************/
690
691 static GstStaticCaps xges_caps = GST_STATIC_CAPS ("application/xges");
692
693 #define XGES_CAPS gst_static_caps_get (&xges_caps)
694
695 static void
696 xges_type_find (GstTypeFind * tf, gpointer unused)
697 {
698   if (xml_check_first_element (tf, "ges", 3, FALSE)) {
699     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, XGES_CAPS);
700   }
701 }
702
703
704 /*** application/sdp *********************************************************/
705
706 static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp");
707
708 #define SDP_CAPS (gst_static_caps_get(&sdp_caps))
709 static gboolean
710 sdp_check_header (GstTypeFind * tf)
711 {
712   const guint8 *data;
713
714   data = gst_type_find_peek (tf, 0, 5);
715   if (!data)
716     return FALSE;
717
718   /* sdp must start with v=0[\r]\n */
719   if (memcmp (data, "v=0", 3))
720     return FALSE;
721
722   if (data[3] == '\r' && data[4] == '\n')
723     return TRUE;
724   if (data[3] == '\n')
725     return TRUE;
726
727   return FALSE;
728 }
729
730 static void
731 sdp_type_find (GstTypeFind * tf, gpointer unused)
732 {
733   if (sdp_check_header (tf))
734     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS);
735 }
736
737 /*** application/smil *********************************************************/
738 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
739 static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil");
740
741 #define SMIL_CAPS (gst_static_caps_get(&smil_caps))
742 static void
743 smil_type_find (GstTypeFind * tf, gpointer unused)
744 {
745   if (xml_check_first_element (tf, "smil", 4, FALSE)) {
746     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
747   }
748 }
749
750 /*** application/ttml+xml *****************************************************/
751
752 static GstStaticCaps ttml_xml_caps = GST_STATIC_CAPS ("application/ttml+xml");
753
754 #define TTML_XML_CAPS (gst_static_caps_get(&ttml_xml_caps))
755 static void
756 ttml_xml_type_find (GstTypeFind * tf, gpointer unused)
757 {
758   if (xml_check_first_element (tf, "tt", 2, FALSE)) {
759     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTML_XML_CAPS);
760   }
761 }
762
763 /*** text/html ***/
764
765 static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html");
766
767 #define HTML_CAPS gst_static_caps_get (&html_caps)
768
769 static void
770 html_type_find (GstTypeFind * tf, gpointer unused)
771 {
772   const gchar *d, *data;
773
774   data = (const gchar *) gst_type_find_peek (tf, 0, 16);
775   if (!data)
776     return;
777
778   if (!g_ascii_strncasecmp (data, "<!DOCTYPE HTML", 14)) {
779     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
780   } else if (xml_check_first_element (tf, "html", 4, FALSE)) {
781     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
782   } else if ((d = memchr (data, '<', 16))) {
783     data = (const gchar *) gst_type_find_peek (tf, d - data, 6);
784     if (data && g_ascii_strncasecmp (data, "<html>", 6) == 0) {
785       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
786     }
787   }
788 }
789 #endif
790 /*** audio/midi ***/
791
792 static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi");
793
794 #define MID_CAPS gst_static_caps_get(&mid_caps)
795 static void
796 mid_type_find (GstTypeFind * tf, gpointer unused)
797 {
798   const guint8 *data = gst_type_find_peek (tf, 0, 4);
799
800   /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
801   if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h'
802       && data[3] == 'd')
803     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS);
804 }
805
806 /*** audio/mobile-xmf ***/
807
808 static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf");
809
810 #define MXMF_CAPS gst_static_caps_get(&mxmf_caps)
811 static void
812 mxmf_type_find (GstTypeFind * tf, gpointer unused)
813 {
814   const guint8 *data = NULL;
815
816   /* Search FileId "XMF_" 4 bytes */
817   data = gst_type_find_peek (tf, 0, 4);
818   if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
819       && data[3] == '_') {
820     /* Search Format version "2.00" 4 bytes */
821     data = gst_type_find_peek (tf, 4, 4);
822     if (data && data[0] == '2' && data[1] == '.' && data[2] == '0'
823         && data[3] == '0') {
824       /* Search TypeId 2     1 byte */
825       data = gst_type_find_peek (tf, 11, 1);
826       if (data && data[0] == 2) {
827         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS);
828       }
829     }
830   }
831 }
832
833
834 /*** video/x-fli ***/
835 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
836 static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
837
838 #define FLX_CAPS gst_static_caps_get(&flx_caps)
839 static void
840 flx_type_find (GstTypeFind * tf, gpointer unused)
841 {
842   const guint8 *data = gst_type_find_peek (tf, 0, 134);
843
844   if (data) {
845     /* check magic and the frame type of the first frame */
846     if ((data[4] == 0x11 || data[4] == 0x12 ||
847             data[4] == 0x30 || data[4] == 0x44) &&
848         data[5] == 0xaf &&
849         ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
850       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS);
851     }
852     return;
853   }
854   data = gst_type_find_peek (tf, 0, 6);
855   if (data) {
856     /* check magic only */
857     if ((data[4] == 0x11 || data[4] == 0x12 ||
858             data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
859       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
860     }
861     return;
862   }
863 }
864 #endif
865 /*** application/x-id3 ***/
866
867 static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
868
869 #define ID3_CAPS gst_static_caps_get(&id3_caps)
870 static void
871 id3v2_type_find (GstTypeFind * tf, gpointer unused)
872 {
873   const guint8 *data = gst_type_find_peek (tf, 0, 10);
874
875   if (data && memcmp (data, "ID3", 3) == 0 &&
876       data[3] != 0xFF && data[4] != 0xFF &&
877       (data[6] & 0x80) == 0 && (data[7] & 0x80) == 0 &&
878       (data[8] & 0x80) == 0 && (data[9] & 0x80) == 0) {
879     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
880   }
881 }
882
883 static void
884 id3v1_type_find (GstTypeFind * tf, gpointer unused)
885 {
886   const guint8 *data = gst_type_find_peek (tf, -128, 3);
887
888   if (data && memcmp (data, "TAG", 3) == 0) {
889     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
890   }
891 }
892
893 /*** application/x-ape ***/
894
895 static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag");
896
897 #define APETAG_CAPS gst_static_caps_get(&apetag_caps)
898 static void
899 apetag_type_find (GstTypeFind * tf, gpointer unused)
900 {
901   const guint8 *data;
902
903   /* APEv1/2 at start of file */
904   data = gst_type_find_peek (tf, 0, 8);
905   if (data && !memcmp (data, "APETAGEX", 8)) {
906     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
907     return;
908   }
909
910   /* APEv1/2 at end of file */
911   data = gst_type_find_peek (tf, -32, 8);
912   if (data && !memcmp (data, "APETAGEX", 8)) {
913     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
914     return;
915   }
916 }
917
918 /*** audio/x-ttafile ***/
919 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
920 static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
921
922 #define TTA_CAPS gst_static_caps_get(&tta_caps)
923 static void
924 tta_type_find (GstTypeFind * tf, gpointer unused)
925 {
926   const guint8 *data = gst_type_find_peek (tf, 0, 3);
927
928   if (data) {
929     if (memcmp (data, "TTA", 3) == 0) {
930       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS);
931       return;
932     }
933   }
934 }
935 #endif
936 /*** audio/x-flac ***/
937 static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");
938
939 #define FLAC_CAPS (gst_static_caps_get(&flac_caps))
940
941 static void
942 flac_type_find (GstTypeFind * tf, gpointer unused)
943 {
944   DataScanCtx c = { 0, NULL, 0 };
945
946   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
947     return;
948
949   /* standard flac (also old/broken flac-in-ogg with an initial 4-byte marker
950    * packet and without the usual packet framing) */
951   if (memcmp (c.data, "fLaC", 4) == 0) {
952     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
953     return;
954   }
955
956   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
957     return;
958
959   /* flac-in-ogg, see http://flac.sourceforge.net/ogg_mapping.html */
960   if (memcmp (c.data, "\177FLAC\001", 6) == 0) {
961     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
962     return;
963   }
964
965 /* disabled because it happily typefinds /dev/urandom as audio/x-flac, and
966  * because I yet have to see header-less flac in the wild */
967 #if 0
968   /* flac without headers (subset format) */
969   /* 64K should be enough */
970   while (c.offset < (64 * 1024)) {
971     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
972       break;
973
974     /* look for frame header,
975      * http://flac.sourceforge.net/format.html#frame_header
976      */
977     if (c.data[0] == 0xff && (c.data[1] >> 2) == 0x3e) {
978       /* bit 15 in the header must be 0 */
979       if (((c.data[1] >> 1) & 0x01) == 0x01)
980         goto advance;
981
982       /* blocksize must be != 0x00 */
983       if ((c.data[2] >> 4) == 0x00)
984         goto advance;
985
986       /* samplerate must be != 0x0f */
987       if ((c.data[2] & 0x0f) == 0x0f)
988         goto advance;
989       /* also 0 is invalid, as it means get the info from the header and we
990        * don't have headers if we are here */
991       if ((c.data[2] & 0x0f) == 0x00)
992         goto advance;
993
994       /* channel assignment must be < 11 */
995       if ((c.data[3] >> 4) >= 11)
996         goto advance;
997
998       /* sample size must be != 0x07 and != 0x05 */
999       if (((c.data[3] >> 1) & 0x07) == 0x07)
1000         goto advance;
1001       if (((c.data[3] >> 1) & 0x07) == 0x05)
1002         goto advance;
1003       /* also 0 is invalid, as it means get the info from the header and we
1004        * don't have headers if we are here */
1005       if (((c.data[3] >> 1) & 0x07) == 0x00)
1006         goto advance;
1007
1008       /* next bit must be 0 */
1009       if ((c.data[3] & 0x01) == 0x01)
1010         goto advance;
1011
1012       /* FIXME: shouldn't we include the crc check ? */
1013
1014       GST_DEBUG ("Found flac without headers at %d", (gint) c.offset);
1015       gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS);
1016       return;
1017     }
1018   advance:
1019     data_scan_ctx_advance (tf, &c, 1);
1020   }
1021 #endif
1022 }
1023
1024 /* TODO: we could probably make a generic function for this.. */
1025 static gint
1026 aac_type_find_scan_loas_frames_ep (GstTypeFind * tf, DataScanCtx * scan_ctx,
1027     gint max_frames)
1028 {
1029   DataScanCtx c = *scan_ctx;
1030   guint16 snc;
1031   guint len;
1032   gint count = 0;
1033
1034   do {
1035     if (!data_scan_ctx_ensure_data (tf, &c, 5))
1036       break;
1037
1038     /* EPAudioSyncStream */
1039     len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
1040         ((c.data[4] & 0x80) >> 7);
1041
1042     if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1043       GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1044       break;
1045     }
1046
1047     /* check length of frame  */
1048     snc = GST_READ_UINT16_BE (c.data + len);
1049     if (snc != 0x4de1) {
1050       GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
1051       break;
1052     }
1053
1054     ++count;
1055
1056     GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
1057         "framelen %u", count, c.offset, len);
1058
1059     data_scan_ctx_advance (tf, &c, len);
1060   } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
1061
1062   GST_DEBUG ("found %d consecutive frames", count);
1063   return count;
1064 }
1065
1066 static gint
1067 aac_type_find_scan_loas_frames (GstTypeFind * tf, DataScanCtx * scan_ctx,
1068     gint max_frames)
1069 {
1070   DataScanCtx c = *scan_ctx;
1071   guint16 snc;
1072   guint len;
1073   gint count = 0;
1074
1075   do {
1076     if (!data_scan_ctx_ensure_data (tf, &c, 3))
1077       break;
1078
1079     /* AudioSyncStream */
1080     len = ((c.data[1] & 0x1f) << 8) | c.data[2];
1081     /* add size of sync stream header */
1082     len += 3;
1083
1084     if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
1085       GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1086       break;
1087     }
1088
1089     /* check length of frame  */
1090     snc = GST_READ_UINT16_BE (c.data + len);
1091     if ((snc & 0xffe0) != 0x56e0) {
1092       GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
1093       break;
1094     }
1095
1096     ++count;
1097
1098     GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
1099         "framelen %u", count, c.offset, len);
1100
1101     data_scan_ctx_advance (tf, &c, len);
1102   } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
1103
1104   GST_DEBUG ("found %d consecutive frames", count);
1105   return count;
1106 }
1107
1108 /*** audio/mpeg version 2, 4 ***/
1109
1110 static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
1111     "mpegversion = (int) { 2, 4 }, framed = (bool) false");
1112 #define AAC_CAPS (gst_static_caps_get(&aac_caps))
1113 #define AAC_AMOUNT (4096)
1114 static void
1115 aac_type_find (GstTypeFind * tf, gpointer unused)
1116 {
1117   DataScanCtx c = { 0, NULL, 0 };
1118   GstTypeFindProbability best_probability = GST_TYPE_FIND_NONE;
1119   GstCaps *best_caps = NULL;
1120   gint best_count = 0;
1121
1122   while (c.offset < AAC_AMOUNT) {
1123     guint snc, len, offset, i;
1124
1125     /* detect adts header or adif header.
1126      * The ADIF header is 4 bytes, that should be OK. The ADTS header, on
1127      * the other hand, is 14 bits only, so we require one valid frame with
1128      * again a valid syncpoint on the next one (28 bits) for certainty. We
1129      * require 4 kB, which is quite a lot, since frames are generally 200-400
1130      * bytes.
1131      * LOAS has 2 possible syncwords, which are 11 bits and 16 bits long.
1132      * The following stream syntax depends on which one is found.
1133      */
1134     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
1135       break;
1136
1137     snc = GST_READ_UINT16_BE (c.data);
1138     if (G_UNLIKELY ((snc & 0xfff6) == 0xfff0)) {
1139       /* ADTS header - find frame length */
1140       GST_DEBUG ("Found one ADTS syncpoint at offset 0x%" G_GINT64_MODIFIER
1141           "x, tracing next...", c.offset);
1142       len = ((c.data[3] & 0x03) << 11) |
1143           (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);
1144
1145       if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 6)) {
1146         GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1147         goto next;
1148       }
1149
1150       offset = len;
1151       /* check if there's a second ADTS frame */
1152       snc = GST_READ_UINT16_BE (c.data + offset);
1153       if ((snc & 0xfff6) == 0xfff0) {
1154         GstCaps *caps;
1155         guint mpegversion, sample_freq_idx, channel_config, profile_idx, rate;
1156         guint8 audio_config[2];
1157
1158         mpegversion = (c.data[1] & 0x08) ? 2 : 4;
1159         profile_idx = c.data[2] >> 6;
1160         sample_freq_idx = ((c.data[2] & 0x3c) >> 2);
1161         channel_config = ((c.data[2] & 0x01) << 2) + (c.data[3] >> 6);
1162
1163         GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%"
1164             G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len);
1165
1166         /* 0xd and 0xe are reserved. 0xf means the sample frequency is directly
1167          * specified in the header, but that's not allowed for ADTS */
1168         if (sample_freq_idx > 0xc) {
1169           GST_DEBUG ("Unexpected sample frequency index %d or wrong sync",
1170               sample_freq_idx);
1171           goto next;
1172         }
1173
1174         rate = gst_codec_utils_aac_get_sample_rate_from_index (sample_freq_idx);
1175         GST_LOG ("ADTS: profile=%u, rate=%u", profile_idx, rate);
1176
1177         /* The ADTS frame header is slightly different from the
1178          * AudioSpecificConfig defined for the MPEG-4 container, so we just
1179          * construct enough of it for getting the level here. */
1180         /* ADTS counts profiles from 0 instead of 1 to save bits */
1181         audio_config[0] = (profile_idx + 1) << 3;
1182         audio_config[0] |= (sample_freq_idx >> 1) & 0x7;
1183         audio_config[1] = (sample_freq_idx & 0x1) << 7;
1184         audio_config[1] |= (channel_config & 0xf) << 3;
1185
1186         caps = gst_caps_new_simple ("audio/mpeg",
1187             "framed", G_TYPE_BOOLEAN, FALSE,
1188             "mpegversion", G_TYPE_INT, mpegversion,
1189             "stream-format", G_TYPE_STRING, "adts", NULL);
1190
1191         gst_codec_utils_aac_caps_set_level_and_profile (caps, audio_config, 2);
1192
1193         /* add rate and number of channels if we can */
1194         if (channel_config != 0 && channel_config <= 7) {
1195           const guint channels_map[] = { 0, 1, 2, 3, 4, 5, 6, 8 };
1196
1197           gst_caps_set_simple (caps, "channels", G_TYPE_INT,
1198               channels_map[channel_config], "rate", G_TYPE_INT, rate, NULL);
1199         }
1200
1201         /* length of the second ADTS frame */
1202         len = ((c.data[offset + 3] & 0x03) << 11) |
1203             (c.data[offset + 4] << 3) | ((c.data[offset + 5] & 0xe0) >> 5);
1204
1205         if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, offset + len + 6)) {
1206           GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
1207           gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps);
1208         } else {
1209           offset += len;
1210           /* find more aac sync to select correctly */
1211           /* check if there's a third/fourth/fifth/sixth ADTS frame, if there is a sixth frame, set probability to maximum:100% */
1212           for (i = 3; i <= 6; i++) {
1213             len = ((c.data[offset + 3] & 0x03) << 11) |
1214                 (c.data[offset + 4] << 3) | ((c.data[offset + 5] & 0xe0) >> 5);
1215             if (len == 0
1216                 || !data_scan_ctx_ensure_data (tf, &c, offset + len + 6)) {
1217               GST_DEBUG ("Wrong sync or next frame not within reach, len=%u",
1218                   len);
1219               break;
1220             }
1221             snc = GST_READ_UINT16_BE (c.data + offset);
1222             if ((snc & 0xfff6) == 0xfff0) {
1223               GST_DEBUG ("Find %und Sync..probability is %u ", i,
1224                   GST_TYPE_FIND_LIKELY + 5 * (i - 2));
1225               offset += len;
1226             } else {
1227               break;
1228             }
1229           }
1230           gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 5 * (i - 3), caps);
1231
1232         }
1233         gst_caps_unref (caps);
1234         break;
1235       }
1236
1237       GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
1238     } else if (G_UNLIKELY ((snc & 0xffe0) == 0x56e0 || snc == 0x4de1)) {
1239       gint count;
1240
1241       /* LOAS frame */
1242       GST_INFO ("Possible LOAS syncword at offset 0x%" G_GINT64_MODIFIER
1243           "x, scanning for more frames...", c.offset);
1244
1245       if (snc == 0x4de1)
1246         count = aac_type_find_scan_loas_frames_ep (tf, &c, 20);
1247       else
1248         count = aac_type_find_scan_loas_frames (tf, &c, 20);
1249
1250       if (count >= 3 && count > best_count) {
1251         gst_caps_replace (&best_caps, NULL);
1252         best_caps = gst_caps_new_simple ("audio/mpeg",
1253             "framed", G_TYPE_BOOLEAN, FALSE,
1254             "mpegversion", G_TYPE_INT, 4,
1255             "stream-format", G_TYPE_STRING, "loas", NULL);
1256         best_count = count;
1257         best_probability = GST_TYPE_FIND_POSSIBLE - 10 + count * 3;
1258         if (best_probability >= GST_TYPE_FIND_LIKELY)
1259           break;
1260       }
1261     } else if (!memcmp (c.data, "ADIF", 4)) {
1262       /* ADIF header */
1263       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
1264           "framed", G_TYPE_BOOLEAN, FALSE, "mpegversion", G_TYPE_INT, 4,
1265           "stream-format", G_TYPE_STRING, "adif", NULL);
1266       break;
1267     }
1268
1269   next:
1270
1271     data_scan_ctx_advance (tf, &c, 1);
1272   }
1273
1274   if (best_probability > GST_TYPE_FIND_NONE) {
1275     gst_type_find_suggest (tf, best_probability, best_caps);
1276     gst_caps_unref (best_caps);
1277   }
1278 }
1279
1280 /*** audio/mpeg version 1 ***/
1281
1282 /*
1283  * The chance that random data is identified as a valid mp3 header is 63 / 2^18
1284  * (0.024%) per try. This makes the function for calculating false positives
1285  *   1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize)
1286  * This has the following probabilities of false positives:
1287  * datasize               MIN_HEADERS
1288  * (bytes)      1       2       3       4
1289  * 4096         62.6%    0.02%   0%      0%
1290  * 16384        98%      0.09%   0%      0%
1291  * 1 MiB       100%      5.88%   0%      0%
1292  * 1 GiB       100%    100%      1.44%   0%
1293  * 1 TiB       100%    100%    100%      0.35%
1294  * This means that the current choice (3 headers by most of the time 4096 byte
1295  * buffers is pretty safe for now.
1296  *
1297  * The max. size of each frame is 1440 bytes, which means that for N frames to
1298  * be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 bytes of data.
1299  * Assuming we step into the stream right after the frame header, this
1300  * means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes
1301  * of data (5762) to always detect any mp3.
1302  */
1303
1304 static const guint mp3types_bitrates[2][3][16] =
1305     { {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
1306     {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
1307     {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
1308 {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
1309     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
1310     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}},
1311 };
1312
1313 static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
1314 {22050, 24000, 16000},
1315 {44100, 48000, 32000}
1316 };
1317
1318 static inline guint
1319 mp3_type_frame_length_from_header (guint32 header, guint * put_layer,
1320     guint * put_channels, guint * put_bitrate, guint * put_samplerate,
1321     gboolean * may_be_free_format, gint possible_free_framelen)
1322 {
1323   guint bitrate, layer, length, mode, samplerate, version, channels;
1324
1325   if ((header & 0xffe00000) != 0xffe00000)
1326     return 0;
1327
1328   /* we don't need extension, copyright, original or
1329    * emphasis for the frame length */
1330   header >>= 6;
1331
1332   /* mode */
1333   mode = header & 0x3;
1334   header >>= 3;
1335
1336   /* padding */
1337   length = header & 0x1;
1338   header >>= 1;
1339
1340   /* sampling frequency */
1341   samplerate = header & 0x3;
1342   if (samplerate == 3)
1343     return 0;
1344   header >>= 2;
1345
1346   /* bitrate index */
1347   bitrate = header & 0xF;
1348   if (bitrate == 0 && possible_free_framelen == -1) {
1349     GST_LOG ("Possibly a free format mp3 - signaling");
1350     *may_be_free_format = TRUE;
1351   }
1352   if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1))
1353     return 0;
1354
1355   /* ignore error correction, too */
1356   header >>= 5;
1357
1358   /* layer */
1359   layer = 4 - (header & 0x3);
1360   if (layer == 4)
1361     return 0;
1362   header >>= 2;
1363
1364   /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */
1365   version = header & 0x3;
1366   if (version == 1)
1367     return 0;
1368
1369   /* lookup */
1370   channels = (mode == 3) ? 1 : 2;
1371   samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
1372   if (bitrate == 0) {
1373     /* possible freeform mp3 */
1374     if (layer == 1) {
1375       length *= 4;
1376       length += possible_free_framelen;
1377       bitrate = length * samplerate / 48000;
1378     } else {
1379       length += possible_free_framelen;
1380       bitrate = length * samplerate /
1381           ((layer == 3 && version != 3) ? 72000 : 144000);
1382     }
1383     /* freeform mp3 should have a higher-than-usually-allowed bitrate */
1384     GST_LOG ("calculated bitrate: %u, max usually: %u", bitrate,
1385         mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][14]);
1386     if (bitrate < mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][14])
1387       return 0;
1388   } else {
1389     /* calculating */
1390     bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
1391     if (layer == 1) {
1392       length = ((12000 * bitrate / samplerate) + length) * 4;
1393     } else {
1394       length += ((layer == 3
1395               && version != 3) ? 72000 : 144000) * bitrate / samplerate;
1396     }
1397   }
1398
1399   GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
1400   GST_LOG
1401       ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
1402       " - channels = %u", samplerate, bitrate, layer, version, channels);
1403
1404   if (put_layer)
1405     *put_layer = layer;
1406   if (put_channels)
1407     *put_channels = channels;
1408   if (put_bitrate)
1409     *put_bitrate = bitrate;
1410   if (put_samplerate)
1411     *put_samplerate = samplerate;
1412
1413   return length;
1414 }
1415
1416
1417 static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, "
1418     "mpegversion = (int) 1, layer = (int) [ 1, 3 ]");
1419 #define MP3_CAPS (gst_static_caps_get(&mp3_caps))
1420 /*
1421  * random values for typefinding
1422  * if no more data is available, we will return a probability of
1423  * (found_headers/TRY_HEADERS) * (MAXIMUM * (TRY_SYNC - bytes_skipped)
1424  *        / TRY_SYNC)
1425  * if found_headers >= MIN_HEADERS
1426  */
1427 #define GST_MP3_TYPEFIND_MIN_HEADERS (2)
1428 #define GST_MP3_TYPEFIND_TRY_HEADERS (5)
1429 #define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */
1430 #define GST_MP3_TYPEFIND_SYNC_SIZE (2048)
1431 #define GST_MP3_WRONG_HEADER (10)
1432
1433 static void
1434 mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
1435     guint * found_layer, GstTypeFindProbability * found_prob)
1436 {
1437   const guint8 *data = NULL;
1438   const guint8 *data_end = NULL;
1439   guint size;
1440   guint64 skipped;
1441   gint last_free_offset = -1;
1442   gint last_free_framelen = -1;
1443   gboolean headerstart = TRUE;
1444
1445   *found_layer = 0;
1446   *found_prob = 0;
1447
1448   size = 0;
1449   skipped = 0;
1450   while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) {
1451     if (size <= 0) {
1452       size = GST_MP3_TYPEFIND_SYNC_SIZE * 2;
1453       do {
1454         size /= 2;
1455         data = gst_type_find_peek (tf, skipped + start_off, size);
1456       } while (size > 10 && !data);
1457       if (!data)
1458         break;
1459       data_end = data + size;
1460     }
1461     if (*data == 0xFF) {
1462       const guint8 *head_data = NULL;
1463       guint layer = 0, bitrate, samplerate, channels;
1464       guint found = 0;          /* number of valid headers found */
1465       guint64 offset = skipped;
1466       gboolean changed = FALSE;
1467       guint prev_layer = 0;
1468       guint prev_channels = 0, prev_samplerate = 0;
1469
1470       while (found < GST_MP3_TYPEFIND_TRY_HEADERS) {
1471         guint32 head;
1472         guint length;
1473         gboolean free = FALSE;
1474
1475         if ((gint64) (offset - skipped + 4) >= 0 &&
1476             data + offset - skipped + 4 < data_end) {
1477           head_data = data + offset - skipped;
1478         } else {
1479           head_data = gst_type_find_peek (tf, offset + start_off, 4);
1480         }
1481         if (!head_data)
1482           break;
1483         head = GST_READ_UINT32_BE (head_data);
1484         if (!(length = mp3_type_frame_length_from_header (head, &layer,
1485                     &channels, &bitrate, &samplerate, &free,
1486                     last_free_framelen))) {
1487           if (free) {
1488             if (last_free_offset == -1)
1489               last_free_offset = offset;
1490             else {
1491               last_free_framelen = offset - last_free_offset;
1492               offset = last_free_offset;
1493               continue;
1494             }
1495           } else {
1496             last_free_framelen = -1;
1497           }
1498
1499           /* Mark the fact that we didn't find a valid header at the beginning */
1500           if (found == 0)
1501             headerstart = FALSE;
1502
1503           GST_LOG ("%d. header at offset %" G_GUINT64_FORMAT
1504               " (0x%" G_GINT64_MODIFIER "x) was not an mp3 header "
1505               "(possibly-free: %s)", found + 1, start_off + offset,
1506               start_off + offset, free ? "yes" : "no");
1507           break;
1508         }
1509         if ((prev_layer && prev_layer != layer) ||
1510             /* (prev_bitrate && prev_bitrate != bitrate) || <-- VBR */
1511             (prev_samplerate && prev_samplerate != samplerate) ||
1512             (prev_channels && prev_channels != channels)) {
1513           /* this means an invalid property, or a change, which might mean
1514            * that this is not a mp3 but just a random bytestream. It could
1515            * be a freaking funky encoded mp3 though. We'll just not count
1516            * this header*/
1517           if (prev_layer)
1518             changed = TRUE;
1519         } else {
1520           found++;
1521           GST_LOG ("found %d. header at offset %" G_GUINT64_FORMAT " (0x%"
1522               G_GINT64_MODIFIER "X)", found, start_off + offset,
1523               start_off + offset);
1524         }
1525         prev_layer = layer;
1526         prev_channels = channels;
1527         prev_samplerate = samplerate;
1528
1529         offset += length;
1530       }
1531       g_assert (found <= GST_MP3_TYPEFIND_TRY_HEADERS);
1532       if (found != 0 && head_data == NULL &&
1533           gst_type_find_peek (tf, offset + start_off - 1, 1) == NULL)
1534         /* Incomplete last frame - don't count it. */
1535         found--;
1536       if (found == GST_MP3_TYPEFIND_TRY_HEADERS ||
1537           (found >= GST_MP3_TYPEFIND_MIN_HEADERS && head_data == NULL)) {
1538         /* we can make a valid guess */
1539         guint probability = found * GST_TYPE_FIND_MAXIMUM *
1540             (GST_MP3_TYPEFIND_TRY_SYNC - skipped) /
1541             GST_MP3_TYPEFIND_TRY_HEADERS / GST_MP3_TYPEFIND_TRY_SYNC;
1542
1543         if (!headerstart
1544             && probability > (GST_TYPE_FIND_MINIMUM + GST_MP3_WRONG_HEADER))
1545           probability -= GST_MP3_WRONG_HEADER;
1546         if (probability < GST_TYPE_FIND_MINIMUM)
1547           probability = GST_TYPE_FIND_MINIMUM;
1548         if (start_off > 0)
1549           probability /= 2;
1550         if (!changed)
1551           probability = (probability + GST_TYPE_FIND_MAXIMUM) / 2;
1552
1553         GST_INFO
1554             ("audio/mpeg calculated %u  =  %u  *  %u / %u  *  (%u - %"
1555             G_GUINT64_FORMAT ") / %u", probability, GST_TYPE_FIND_MAXIMUM,
1556             found, GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC,
1557             (guint64) skipped, GST_MP3_TYPEFIND_TRY_SYNC);
1558         /* make sure we're not id3 tagged */
1559         head_data = gst_type_find_peek (tf, -128, 3);
1560         if (head_data && (memcmp (head_data, "TAG", 3) == 0)) {
1561           probability = 0;
1562         }
1563         g_assert (probability <= GST_TYPE_FIND_MAXIMUM);
1564
1565         *found_prob = probability;
1566         if (probability > 0)
1567           *found_layer = layer;
1568         return;
1569       }
1570     }
1571     data++;
1572     skipped++;
1573     size--;
1574   }
1575 }
1576
1577 static void
1578 mp3_type_find (GstTypeFind * tf, gpointer unused)
1579 {
1580   GstTypeFindProbability prob, mid_prob;
1581   const guint8 *data;
1582   guint layer, mid_layer;
1583   guint64 length;
1584
1585   mp3_type_find_at_offset (tf, 0, &layer, &prob);
1586   length = gst_type_find_get_length (tf);
1587
1588   if (length == 0 || length == (guint64) - 1) {
1589     if (prob != 0)
1590       goto suggest;
1591     return;
1592   }
1593
1594   /* if we're pretty certain already, skip the additional check */
1595   if (prob >= GST_TYPE_FIND_LIKELY)
1596     goto suggest;
1597
1598   mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob);
1599
1600   if (mid_prob > 0) {
1601     if (prob == 0) {
1602       GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob);
1603       layer = mid_layer;
1604       prob = mid_prob;
1605       goto suggest;
1606     }
1607
1608     if (layer != mid_layer) {
1609       GST_WARNING ("audio/mpeg layer discrepancy: %u vs. %u", layer, mid_layer);
1610       return;                   /* FIXME: or should we just go with the one in the middle? */
1611     }
1612
1613     /* detected mpeg audio both in middle of the file and at the start */
1614     prob = (prob + mid_prob) / 2;
1615     goto suggest;
1616   }
1617
1618   /* a valid header right at the start makes it more likely
1619    * that this is actually plain mpeg-1 audio */
1620   if (prob > 0) {
1621     data = gst_type_find_peek (tf, 0, 4);       /* use min. frame size? */
1622     if (data && mp3_type_frame_length_from_header (GST_READ_UINT32_BE (data),
1623             &layer, NULL, NULL, NULL, NULL, 0) != 0) {
1624       prob = MIN (prob + 10, GST_TYPE_FIND_MAXIMUM);
1625     }
1626   }
1627
1628   if (prob > 0)
1629     goto suggest;
1630
1631   return;
1632
1633 suggest:
1634   {
1635     g_return_if_fail (layer >= 1 && layer <= 3);
1636
1637     gst_type_find_suggest_simple (tf, prob, "audio/mpeg",
1638         "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer,
1639         "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
1640   }
1641 }
1642
1643 /*** audio/x-musepack ***/
1644
1645 static GstStaticCaps musepack_caps =
1646 GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }");
1647
1648 #define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps))
1649 static void
1650 musepack_type_find (GstTypeFind * tf, gpointer unused)
1651 {
1652   const guint8 *data = gst_type_find_peek (tf, 0, 4);
1653   GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM;
1654   gint streamversion = -1;
1655
1656   if (data && memcmp (data, "MP+", 3) == 0) {
1657     streamversion = 7;
1658     if ((data[3] & 0x7f) == 7) {
1659       prop = GST_TYPE_FIND_MAXIMUM;
1660     } else {
1661       prop = GST_TYPE_FIND_LIKELY + 10;
1662     }
1663   } else if (data && memcmp (data, "MPCK", 4) == 0) {
1664     streamversion = 8;
1665     prop = GST_TYPE_FIND_MAXIMUM;
1666   }
1667
1668   if (streamversion != -1) {
1669     gst_type_find_suggest_simple (tf, prop, "audio/x-musepack",
1670         "streamversion", G_TYPE_INT, streamversion, NULL);
1671   }
1672 }
1673
1674 /*** audio/x-ac3 ***/
1675 /* FIXME 0.11: should be audio/ac3, but isn't for backwards compatibility */
1676 static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3");
1677
1678 #define AC3_CAPS (gst_static_caps_get(&ac3_caps))
1679
1680 static GstStaticCaps eac3_caps = GST_STATIC_CAPS ("audio/x-eac3");
1681
1682 #define EAC3_CAPS (gst_static_caps_get(&eac3_caps))
1683
1684 struct ac3_frmsize
1685 {
1686   unsigned short bit_rate;
1687   unsigned short frm_size[3];
1688 };
1689
1690 static const struct ac3_frmsize ac3_frmsizecod_tbl[] = {
1691   {32, {64, 69, 96}},
1692   {32, {64, 70, 96}},
1693   {40, {80, 87, 120}},
1694   {40, {80, 88, 120}},
1695   {48, {96, 104, 144}},
1696   {48, {96, 105, 144}},
1697   {56, {112, 121, 168}},
1698   {56, {112, 122, 168}},
1699   {64, {128, 139, 192}},
1700   {64, {128, 140, 192}},
1701   {80, {160, 174, 240}},
1702   {80, {160, 175, 240}},
1703   {96, {192, 208, 288}},
1704   {96, {192, 209, 288}},
1705   {112, {224, 243, 336}},
1706   {112, {224, 244, 336}},
1707   {128, {256, 278, 384}},
1708   {128, {256, 279, 384}},
1709   {160, {320, 348, 480}},
1710   {160, {320, 349, 480}},
1711   {192, {384, 417, 576}},
1712   {192, {384, 418, 576}},
1713   {224, {448, 487, 672}},
1714   {224, {448, 488, 672}},
1715   {256, {512, 557, 768}},
1716   {256, {512, 558, 768}},
1717   {320, {640, 696, 960}},
1718   {320, {640, 697, 960}},
1719   {384, {768, 835, 1152}},
1720   {384, {768, 836, 1152}},
1721   {448, {896, 975, 1344}},
1722   {448, {896, 976, 1344}},
1723   {512, {1024, 1114, 1536}},
1724   {512, {1024, 1115, 1536}},
1725   {576, {1152, 1253, 1728}},
1726   {576, {1152, 1254, 1728}},
1727   {640, {1280, 1393, 1920}},
1728   {640, {1280, 1394, 1920}}
1729 };
1730
1731 static void
1732 ac3_type_find (GstTypeFind * tf, gpointer unused)
1733 {
1734   DataScanCtx c = { 0, NULL, 0 };
1735
1736   /* Search for an ac3 frame; not necessarily right at the start, but give it
1737    * a lower probability if not found right at the start. Check that the
1738    * frame is followed by a second frame at the expected offset.
1739    * We could also check the two ac3 CRCs, but we don't do that right now */
1740   while (c.offset < 1024) {
1741     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
1742       break;
1743
1744     if (c.data[0] == 0x0b && c.data[1] == 0x77) {
1745       guint bsid = c.data[5] >> 3;
1746
1747       if (bsid <= 8) {
1748         /* ac3 */
1749         guint fscod = c.data[4] >> 6;
1750         guint frmsizecod = c.data[4] & 0x3f;
1751
1752         if (fscod < 3 && frmsizecod < 38) {
1753           DataScanCtx c_next = c;
1754           guint frame_size;
1755
1756           frame_size = ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod];
1757           GST_LOG ("possible AC3 frame sync at offset %"
1758               G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1759           if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1760             data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1761
1762             if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1763               fscod = c_next.data[4] >> 6;
1764               frmsizecod = c_next.data[4] & 0x3f;
1765
1766               if (fscod < 3 && frmsizecod < 38) {
1767                 GstTypeFindProbability prob;
1768
1769                 GST_LOG ("found second AC3 frame (size=%u), looks good",
1770                     ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]);
1771                 if (c.offset == 0)
1772                   prob = GST_TYPE_FIND_MAXIMUM;
1773                 else
1774                   prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1775
1776                 gst_type_find_suggest (tf, prob, AC3_CAPS);
1777                 return;
1778               }
1779             } else {
1780               GST_LOG ("no second AC3 frame found, false sync");
1781             }
1782           }
1783         }
1784       } else if (bsid <= 16 && bsid > 10) {
1785         /* eac3 */
1786         DataScanCtx c_next = c;
1787         guint frame_size;
1788
1789         frame_size = (((c.data[2] & 0x07) << 8) + c.data[3]) + 1;
1790         GST_LOG ("possible E-AC3 frame sync at offset %"
1791             G_GUINT64_FORMAT ", size=%u", c.offset, frame_size);
1792         if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) {
1793           data_scan_ctx_advance (tf, &c_next, frame_size * 2);
1794
1795           if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) {
1796             GstTypeFindProbability prob;
1797
1798             GST_LOG ("found second E-AC3 frame, looks good");
1799             if (c.offset == 0)
1800               prob = GST_TYPE_FIND_MAXIMUM;
1801             else
1802               prob = GST_TYPE_FIND_NEARLY_CERTAIN;
1803
1804             gst_type_find_suggest (tf, prob, EAC3_CAPS);
1805             return;
1806           } else {
1807             GST_LOG ("no second E-AC3 frame found, false sync");
1808           }
1809         }
1810       } else {
1811         GST_LOG ("invalid AC3 BSID: %u", bsid);
1812       }
1813     }
1814     data_scan_ctx_advance (tf, &c, 1);
1815   }
1816 }
1817
1818 /*** audio/x-dts ***/
1819 static GstStaticCaps dts_caps = GST_STATIC_CAPS ("audio/x-dts");
1820 #define DTS_CAPS (gst_static_caps_get (&dts_caps))
1821 #define DTS_MIN_FRAMESIZE 96
1822 #define DTS_MAX_FRAMESIZE 18725 /* 16384*16/14 */
1823
1824 static gboolean
1825 dts_parse_frame_header (DataScanCtx * c, guint * frame_size,
1826     guint * sample_rate, guint * channels, guint * depth, guint * endianness)
1827 {
1828   static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025,
1829     22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000
1830   };
1831   static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5,
1832     6, 6, 6, 7, 8, 8
1833   };
1834   guint16 hdr[8];
1835   guint32 marker;
1836   guint num_blocks, chans, lfe, i;
1837
1838   marker = GST_READ_UINT32_BE (c->data);
1839
1840   /* raw big endian or 14-bit big endian */
1841   if (marker == 0x7FFE8001 || marker == 0x1FFFE800) {
1842     *endianness = G_BIG_ENDIAN;
1843     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1844       hdr[i] = GST_READ_UINT16_BE (c->data + (i * sizeof (guint16)));
1845   } else
1846     /* raw little endian or 14-bit little endian */
1847   if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) {
1848     *endianness = G_LITTLE_ENDIAN;
1849     for (i = 0; i < G_N_ELEMENTS (hdr); ++i)
1850       hdr[i] = GST_READ_UINT16_LE (c->data + (i * sizeof (guint16)));
1851   } else {
1852     return FALSE;
1853   }
1854
1855   GST_LOG ("dts sync marker 0x%08x at offset %u", marker, (guint) c->offset);
1856
1857   /* 14-bit mode */
1858   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) {
1859     if ((hdr[2] & 0xFFF0) != 0x07F0)
1860       return FALSE;
1861     /* discard top 2 bits (2 void), shift in 2 */
1862     hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003);
1863     /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */
1864     hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F);
1865     hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F);
1866     hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF);
1867     hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF);
1868     hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF);
1869     hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF);
1870     g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001);
1871     *depth = 14;
1872   } else {
1873     *depth = 16;
1874   }
1875
1876   GST_LOG ("frame header: %04x%04x%04x%04x", hdr[2], hdr[3], hdr[4], hdr[5]);
1877
1878   num_blocks = (hdr[2] >> 2) & 0x7F;
1879   *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1;
1880   chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14);
1881   *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F];
1882   lfe = (hdr[5] >> 9) & 0x03;
1883
1884   if (num_blocks < 5 || *frame_size < 96 || *sample_rate == 0)
1885     return FALSE;
1886
1887   if (marker == 0x1FFFE800 || marker == 0xFF1F00E8)
1888     *frame_size = (*frame_size * 16) / 14;      /* FIXME: round up? */
1889
1890   if (chans < G_N_ELEMENTS (channels_table))
1891     *channels = channels_table[chans] + ((lfe) ? 1 : 0);
1892   else
1893     *channels = 0;
1894
1895   return TRUE;
1896 }
1897
1898 static void
1899 dts_type_find (GstTypeFind * tf, gpointer unused)
1900 {
1901   DataScanCtx c = { 0, NULL, 0 };
1902
1903   /* Search for an dts frame; not necessarily right at the start, but give it
1904    * a lower probability if not found right at the start. Check that the
1905    * frame is followed by a second frame at the expected offset. */
1906   while (c.offset <= DTS_MAX_FRAMESIZE) {
1907     guint frame_size = 0, rate = 0, chans = 0, depth = 0, endianness = 0;
1908
1909     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, DTS_MIN_FRAMESIZE)))
1910       return;
1911
1912     if (G_UNLIKELY (dts_parse_frame_header (&c, &frame_size, &rate, &chans,
1913                 &depth, &endianness))) {
1914       GstTypeFindProbability prob;
1915       DataScanCtx next_c;
1916
1917       prob = (c.offset == 0) ? GST_TYPE_FIND_LIKELY : GST_TYPE_FIND_POSSIBLE;
1918
1919       /* check for second frame sync */
1920       next_c = c;
1921       data_scan_ctx_advance (tf, &next_c, frame_size);
1922       if (data_scan_ctx_ensure_data (tf, &next_c, 4)) {
1923         GST_LOG ("frame size: %u 0x%04x", frame_size, frame_size);
1924         GST_MEMDUMP ("second frame sync", next_c.data, 4);
1925         if (GST_READ_UINT32_BE (c.data) == GST_READ_UINT32_BE (next_c.data))
1926           prob = GST_TYPE_FIND_MAXIMUM;
1927       }
1928
1929       if (chans > 0) {
1930         gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1931             "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans,
1932             "depth", G_TYPE_INT, depth, "endianness", G_TYPE_INT, endianness,
1933             "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1934       } else {
1935         gst_type_find_suggest_simple (tf, prob, "audio/x-dts",
1936             "rate", G_TYPE_INT, rate, "depth", G_TYPE_INT, depth,
1937             "endianness", G_TYPE_INT, endianness,
1938             "framed", G_TYPE_BOOLEAN, FALSE, NULL);
1939       }
1940
1941       return;
1942     }
1943
1944     data_scan_ctx_advance (tf, &c, 1);
1945   }
1946 }
1947
1948 /*** gsm ***/
1949 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
1950 /* can only be detected by using the extension, in which case we use the default
1951  * GSM properties */
1952 static GstStaticCaps gsm_caps =
1953 GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1");
1954
1955 #define GSM_CAPS (gst_static_caps_get(&gsm_caps))
1956
1957 /*** wavpack ***/
1958
1959 static GstStaticCaps wavpack_caps =
1960 GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false");
1961
1962 #define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps))
1963
1964 static GstStaticCaps wavpack_correction_caps =
1965 GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");
1966
1967 #define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps))
1968
1969 static void
1970 wavpack_type_find (GstTypeFind * tf, gpointer unused)
1971 {
1972   GstTypeFindProbability base_prob = GST_TYPE_FIND_POSSIBLE;
1973   guint64 offset;
1974   guint32 blocksize;
1975   const guint8 *data;
1976   guint count_wv, count_wvc;
1977
1978   data = gst_type_find_peek (tf, 0, 32);
1979   if (!data)
1980     return;
1981
1982   if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k')
1983     return;
1984
1985   /* Note: wavpack blocks can be fairly large (easily 60-110k), possibly
1986    * larger than the max. limits imposed by certain typefinding elements
1987    * like id3demux or apedemux, so typefinding is most likely only going to
1988    * work in pull-mode */
1989   blocksize = GST_READ_UINT32_LE (data + 4);
1990   GST_LOG ("wavpack header, blocksize=0x%04x", blocksize);
1991   /* If bigger than maximum allowed blocksize, refuse */
1992   if (blocksize > 131072)
1993     return;
1994   count_wv = 0;
1995   count_wvc = 0;
1996   offset = 32;
1997   while (offset < 8 + blocksize) {
1998     guint32 sublen;
1999
2000     /* get chunk header */
2001     GST_LOG ("peeking at chunk at offset 0x%04x", (guint) offset);
2002     data = gst_type_find_peek (tf, offset, 4);
2003     if (data == NULL)
2004       break;
2005     sublen = ((guint32) data[1]) << 1;
2006     if (data[0] & 0x80) {
2007       sublen |= (((guint32) data[2]) << 9) | (((guint32) data[3]) << 17);
2008       sublen += 1 + 3;          /* id + length */
2009     } else {
2010       sublen += 1 + 1;          /* id + length */
2011     }
2012     if (offset + sublen > 8 + blocksize) {
2013       GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
2014           blocksize - offset);
2015       break;
2016     }
2017     if ((data[0] & 0x20) == 0) {
2018       switch (data[0] & 0x0f) {
2019         case 0xa:              /* ID_WV_BITSTREAM  */
2020         case 0xc:              /* ID_WVX_BITSTREAM */
2021           ++count_wv;
2022           break;
2023         case 0xb:              /* ID_WVC_BITSTREAM */
2024           ++count_wvc;
2025           break;
2026         default:
2027           break;
2028       }
2029       if (count_wv >= 5 || count_wvc >= 5)
2030         break;
2031     }
2032     offset += sublen;
2033   }
2034
2035   /* check for second block header */
2036   data = gst_type_find_peek (tf, 8 + blocksize, 4);
2037   if (data != NULL && memcmp (data, "wvpk", 4) == 0) {
2038     GST_DEBUG ("found second block sync");
2039     base_prob = GST_TYPE_FIND_LIKELY;
2040   }
2041
2042   GST_DEBUG ("wvc=%d, wv=%d", count_wvc, count_wv);
2043
2044   if (count_wvc > 0 && count_wvc > count_wv) {
2045     gst_type_find_suggest (tf,
2046         MIN (base_prob + 5 * count_wvc, GST_TYPE_FIND_NEARLY_CERTAIN),
2047         WAVPACK_CORRECTION_CAPS);
2048   } else if (count_wv > 0) {
2049     gst_type_find_suggest (tf,
2050         MIN (base_prob + 5 * count_wv, GST_TYPE_FIND_NEARLY_CERTAIN),
2051         WAVPACK_CAPS);
2052   }
2053 }
2054
2055 /*** application/postscrip ***/
2056 static GstStaticCaps postscript_caps =
2057 GST_STATIC_CAPS ("application/postscript");
2058
2059 #define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps))
2060
2061 static void
2062 postscript_type_find (GstTypeFind * tf, gpointer unused)
2063 {
2064   const guint8 *data = gst_type_find_peek (tf, 0, 3);
2065   if (!data)
2066     return;
2067
2068   if (data[0] == 0x04)
2069     data++;
2070   if (data[0] == '%' && data[1] == '!')
2071     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS);
2072
2073 }
2074
2075 /*** image/svg+xml ***/
2076 static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml");
2077
2078 #define SVG_CAPS (gst_static_caps_get(&svg_caps))
2079
2080 static void
2081 svg_type_find (GstTypeFind * tf, gpointer unused)
2082 {
2083   static const gchar svg_doctype[] = "!DOCTYPE svg";
2084   static const gchar svg_tag[] = "<svg";
2085   DataScanCtx c = { 0, NULL, 0 };
2086
2087   while (c.offset <= 1024) {
2088     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
2089       break;
2090
2091     if (memcmp (svg_doctype, c.data, 12) == 0) {
2092       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS);
2093       return;
2094     } else if (memcmp (svg_tag, c.data, 4) == 0) {
2095       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS);
2096       return;
2097     }
2098     data_scan_ctx_advance (tf, &c, 1);
2099   }
2100 }
2101
2102 /*** multipart/x-mixed-replace mimestream ***/
2103
2104 static GstStaticCaps multipart_caps =
2105 GST_STATIC_CAPS ("multipart/x-mixed-replace");
2106 #define MULTIPART_CAPS gst_static_caps_get(&multipart_caps)
2107
2108 /* multipart/x-mixed replace is:
2109  *   <maybe some whitespace>--<some ascii chars>[\r]\n
2110  *   <more ascii chars>[\r]\nContent-type:<more ascii>[\r]\n */
2111 static void
2112 multipart_type_find (GstTypeFind * tf, gpointer unused)
2113 {
2114   const guint8 *data;
2115   const guint8 *x;
2116
2117 #define MULTIPART_MAX_BOUNDARY_OFFSET 16
2118   data = gst_type_find_peek (tf, 0, MULTIPART_MAX_BOUNDARY_OFFSET);
2119   if (!data)
2120     return;
2121
2122   for (x = data;
2123       x - data < MULTIPART_MAX_BOUNDARY_OFFSET - 2 && g_ascii_isspace (*x);
2124       x++);
2125   if (x[0] != '-' || x[1] != '-')
2126     return;
2127
2128   /* Could be okay, peek what should be enough for a complete header */
2129 #define MULTIPART_MAX_HEADER_SIZE 256
2130   data = gst_type_find_peek (tf, 0, MULTIPART_MAX_HEADER_SIZE);
2131   if (!data)
2132     return;
2133
2134   for (x = data; x - data < MULTIPART_MAX_HEADER_SIZE - 14; x++) {
2135     if (!isascii (*x)) {
2136       return;
2137     }
2138     if (*x == '\n' &&
2139         !g_ascii_strncasecmp ("content-type:", (gchar *) x + 1, 13)) {
2140       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MULTIPART_CAPS);
2141       return;
2142     }
2143   }
2144 }
2145 #endif
2146
2147 /*** video/mpeg systemstream ***/
2148 #if defined(TIZEN_PROFILE_TV) || !defined(TIZEN_FEATURE_DISABLE_MIME_TYPES)
2149 static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, "
2150     "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]");
2151
2152 #define MPEG_SYS_CAPS gst_static_caps_get(&mpeg_sys_caps)
2153 #endif
2154 #define IS_MPEG_HEADER(data) (G_UNLIKELY((((guint8 *)(data))[0] == 0x00) &&  \
2155                                          (((guint8 *)(data))[1] == 0x00) &&  \
2156                                          (((guint8 *)(data))[2] == 0x01)))
2157 #if defined(TIZEN_PROFILE_TV) || !defined(TIZEN_FEATURE_DISABLE_MIME_TYPES)
2158 #define IS_MPEG_PACK_CODE(b) ((b) == 0xBA)
2159 #define IS_MPEG_SYS_CODE(b) ((b) == 0xBB)
2160 #define IS_MPEG_PACK_HEADER(data)       (IS_MPEG_HEADER (data) &&            \
2161                                          IS_MPEG_PACK_CODE (((guint8 *)(data))[3]))
2162
2163 #define IS_MPEG_PES_CODE(b) (((b) & 0xF0) == 0xE0 || ((b) & 0xF0) == 0xC0 || \
2164                              (b) >= 0xBD)
2165 #define IS_MPEG_PES_HEADER(data)        (IS_MPEG_HEADER (data) &&            \
2166                                          IS_MPEG_PES_CODE (((guint8 *)(data))[3]))
2167
2168 #define MPEG2_MAX_PROBE_LENGTH (128 * 1024)     /* 128kB should be 64 packs of the
2169                                                  * most common 2kB pack size. */
2170
2171 #define MPEG2_MIN_SYS_HEADERS 2
2172 #define MPEG2_MAX_SYS_HEADERS 5
2173 #endif
2174
2175 static gboolean
2176 mpeg_sys_is_valid_pack (GstTypeFind * tf, const guint8 * data, guint len,
2177     guint * pack_size)
2178 {
2179   /* Check the pack header @ offset for validity, assuming that the 4 byte header
2180    * itself has already been checked. */
2181   guint8 stuff_len;
2182
2183   if (len < 12)
2184     return FALSE;
2185
2186   /* Check marker bits */
2187   if ((data[4] & 0xC4) == 0x44) {
2188     /* MPEG-2 PACK */
2189     if (len < 14)
2190       return FALSE;
2191
2192     if ((data[6] & 0x04) != 0x04 ||
2193         (data[8] & 0x04) != 0x04 ||
2194         (data[9] & 0x01) != 0x01 || (data[12] & 0x03) != 0x03)
2195       return FALSE;
2196
2197     stuff_len = data[13] & 0x07;
2198
2199     /* Check the following header bytes, if we can */
2200     if ((14 + stuff_len + 4) <= len) {
2201       if (!IS_MPEG_HEADER (data + 14 + stuff_len))
2202         return FALSE;
2203     }
2204     if (pack_size)
2205       *pack_size = 14 + stuff_len;
2206     return TRUE;
2207   } else if ((data[4] & 0xF1) == 0x21) {
2208     /* MPEG-1 PACK */
2209     if ((data[6] & 0x01) != 0x01 ||
2210         (data[8] & 0x01) != 0x01 ||
2211         (data[9] & 0x80) != 0x80 || (data[11] & 0x01) != 0x01)
2212       return FALSE;
2213
2214     /* Check the following header bytes, if we can */
2215     if ((12 + 4) <= len) {
2216       if (!IS_MPEG_HEADER (data + 12))
2217         return FALSE;
2218     }
2219     if (pack_size)
2220       *pack_size = 12;
2221     return TRUE;
2222   }
2223
2224   return FALSE;
2225 }
2226
2227 #if defined(TIZEN_PROFILE_TV) || !defined(TIZEN_FEATURE_DISABLE_MIME_TYPES)
2228 static gboolean
2229 mpeg_sys_is_valid_pes (GstTypeFind * tf, const guint8 * data, guint len,
2230     guint * pack_size)
2231 {
2232   guint pes_packet_len;
2233
2234   /* Check the PES header at the given position, assuming the header code itself
2235    * was already checked */
2236   if (len < 6)
2237     return FALSE;
2238
2239   /* For MPEG Program streams, unbounded PES is not allowed, so we must have a
2240    * valid length present */
2241   pes_packet_len = GST_READ_UINT16_BE (data + 4);
2242   if (pes_packet_len == 0)
2243     return FALSE;
2244
2245   /* Check the following header, if we can */
2246   if (6 + pes_packet_len + 4 <= len) {
2247     if (!IS_MPEG_HEADER (data + 6 + pes_packet_len))
2248       return FALSE;
2249   }
2250
2251   if (pack_size)
2252     *pack_size = 6 + pes_packet_len;
2253   return TRUE;
2254 }
2255
2256 static gboolean
2257 mpeg_sys_is_valid_sys (GstTypeFind * tf, const guint8 * data, guint len,
2258     guint * pack_size)
2259 {
2260   guint sys_hdr_len;
2261
2262   /* Check the System header at the given position, assuming the header code itself
2263    * was already checked */
2264   if (len < 6)
2265     return FALSE;
2266   sys_hdr_len = GST_READ_UINT16_BE (data + 4);
2267   if (sys_hdr_len < 6)
2268     return FALSE;
2269
2270   /* Check the following header, if we can */
2271   if (6 + sys_hdr_len + 4 <= len) {
2272     if (!IS_MPEG_HEADER (data + 6 + sys_hdr_len))
2273       return FALSE;
2274   }
2275
2276   if (pack_size)
2277     *pack_size = 6 + sys_hdr_len;
2278
2279   return TRUE;
2280 }
2281
2282 /* calculation of possibility to identify random data as mpeg systemstream:
2283  * bits that must match in header detection:            32 (or more)
2284  * chance that random data is identifed:                1/2^32
2285  * chance that MPEG2_MIN_PACK_HEADERS headers are identified:
2286  *       1/2^(32*MPEG2_MIN_PACK_HEADERS)
2287  * chance that this happens in MPEG2_MAX_PROBE_LENGTH bytes:
2288  *       1-(1+1/2^(32*MPEG2_MIN_PACK_HEADERS)^MPEG2_MAX_PROBE_LENGTH)
2289  * for current values:
2290  *       1-(1+1/2^(32*4)^101024)
2291  *       = <some_number>
2292  * Since we also check marker bits and pes packet lengths, this probability is a
2293  * very coarse upper bound.
2294  */
2295 static void
2296 mpeg_sys_type_find (GstTypeFind * tf, gpointer unused)
2297 {
2298   const guint8 *data, *data0, *first_sync, *end;
2299   gint mpegversion = 0;
2300   guint pack_headers = 0;
2301   guint pes_headers = 0;
2302   guint pack_size;
2303   guint since_last_sync = 0;
2304   guint32 sync_word = 0xffffffff;
2305   guint potential_headers = 0;
2306
2307   G_STMT_START {
2308     gint len;
2309
2310     len = MPEG2_MAX_PROBE_LENGTH;
2311
2312     while (len >= 16) {
2313       data = gst_type_find_peek (tf, 0, 5 + len);
2314       if (data != NULL)
2315         break;
2316       len = len / 2;
2317     }
2318
2319     if (!data)
2320       return;
2321
2322     end = data + len;
2323   }
2324   G_STMT_END;
2325
2326   data0 = data;
2327   first_sync = NULL;
2328
2329   while (data < end) {
2330     sync_word <<= 8;
2331     if (sync_word == 0x00000100) {
2332       /* Found potential sync word */
2333       if (first_sync == NULL)
2334         first_sync = data - 3;
2335
2336       if (since_last_sync > 4) {
2337         /* If more than 4 bytes since the last sync word, reset our counters,
2338          * as we're only interested in counting contiguous packets */
2339         pes_headers = pack_headers = 0;
2340       }
2341       pack_size = 0;
2342
2343       potential_headers++;
2344       if (IS_MPEG_PACK_CODE (data[0])) {
2345         if ((data[1] & 0xC0) == 0x40) {
2346           /* MPEG-2 */
2347           mpegversion = 2;
2348         } else if ((data[1] & 0xF0) == 0x20) {
2349           mpegversion = 1;
2350         }
2351         if (mpegversion != 0 &&
2352             mpeg_sys_is_valid_pack (tf, data - 3, end - data + 3, &pack_size)) {
2353           pack_headers++;
2354         }
2355       } else if (IS_MPEG_PES_CODE (data[0])) {
2356         /* PES stream */
2357         if (mpeg_sys_is_valid_pes (tf, data - 3, end - data + 3, &pack_size)) {
2358           pes_headers++;
2359           if (mpegversion == 0)
2360             mpegversion = 2;
2361         }
2362       } else if (IS_MPEG_SYS_CODE (data[0])) {
2363         if (mpeg_sys_is_valid_sys (tf, data - 3, end - data + 3, &pack_size)) {
2364           pack_headers++;
2365         }
2366       }
2367
2368       /* If we found a packet with a known size, skip the bytes in it and loop
2369        * around to check the next packet. */
2370       if (pack_size != 0) {
2371         data += pack_size - 3;
2372         sync_word = 0xffffffff;
2373         since_last_sync = 0;
2374         continue;
2375       }
2376     }
2377
2378     sync_word |= data[0];
2379     since_last_sync++;
2380     data++;
2381
2382     /* If we have found MAX headers, and *some* were pes headers (pack headers
2383      * are optional in an mpeg system stream) then return our high-probability
2384      * result */
2385     if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MAX_SYS_HEADERS)
2386       goto suggest;
2387   }
2388
2389   /* If we at least saw MIN headers, and *some* were pes headers (pack headers
2390    * are optional in an mpeg system stream) then return a lower-probability
2391    * result */
2392   if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MIN_SYS_HEADERS)
2393     goto suggest;
2394
2395   return;
2396 suggest:
2397   {
2398     guint prob;
2399
2400     prob = GST_TYPE_FIND_POSSIBLE + (10 * (pack_headers + pes_headers));
2401     prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2402
2403     /* With the above test, we get into problems when we try to typefind
2404        a MPEG stream from a small amount of data, which can happen when
2405        we get data pushed from a HTTP source. We thus make a second test
2406        to give higher probability if all the potential headers were either
2407        pack or pes headers (ie, no potential header was unrecognized). */
2408     if (potential_headers == pack_headers + pes_headers) {
2409       GST_LOG ("Only %u headers, but all were recognized", potential_headers);
2410       prob += 10;
2411       prob = MIN (prob, GST_TYPE_FIND_MAXIMUM);
2412     }
2413
2414     /* lower probability if the first packet wasn't right at the start */
2415     if (data0 != first_sync && prob >= 10)
2416       prob -= 10;
2417
2418     GST_LOG ("Suggesting MPEG %d system stream, %d packs, %d pes, prob %u%%",
2419         mpegversion, pack_headers, pes_headers, prob);
2420
2421     gst_type_find_suggest_simple (tf, prob, "video/mpeg",
2422         "systemstream", G_TYPE_BOOLEAN, TRUE,
2423         "mpegversion", G_TYPE_INT, mpegversion, NULL);
2424   }
2425 };
2426 #endif
2427
2428 /*** video/mpegts Transport Stream ***/
2429 static GstStaticCaps mpegts_caps = GST_STATIC_CAPS ("video/mpegts, "
2430     "systemstream = (boolean) true, packetsize = (int) [ 188, 208 ]");
2431 #define MPEGTS_CAPS gst_static_caps_get(&mpegts_caps)
2432
2433 #define GST_MPEGTS_TYPEFIND_MIN_HEADERS 4
2434 #define GST_MPEGTS_TYPEFIND_MAX_HEADERS 10
2435 #define GST_MPEGTS_MAX_PACKET_SIZE 208
2436 #define GST_MPEGTS_TYPEFIND_SYNC_SIZE \
2437             (GST_MPEGTS_TYPEFIND_MIN_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2438 #define GST_MPEGTS_TYPEFIND_MAX_SYNC \
2439             (GST_MPEGTS_TYPEFIND_MAX_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE)
2440 #define GST_MPEGTS_TYPEFIND_SCAN_LENGTH \
2441             (GST_MPEGTS_TYPEFIND_MAX_SYNC * 4)
2442
2443 #define MPEGTS_HDR_SIZE 4
2444 /* Check for sync byte, error_indicator == 0 and packet has payload.
2445  * Adaptation control field (data[3] & 0x30) may be zero for TS packets with
2446  * null PIDs. Still, these streams are valid TS streams (for null packets,
2447  * AFC is supposed to be 0x1, but the spec also says decoders should just
2448  * discard any packets with AFC = 0x00) */
2449 #define IS_MPEGTS_HEADER(data) (data[0] == 0x47 && \
2450                                 (data[1] & 0x80) == 0x00 && \
2451                                 ((data[3] & 0x30) != 0x00 || \
2452                                 ((data[3] & 0x30) == 0x00 && (data[1] & 0x1f) == 0x1f && (data[2] & 0xff) == 0xff)))
2453
2454 /* Helper function to search ahead at intervals of packet_size for mpegts
2455  * headers */
2456 static gint
2457 mpeg_ts_probe_headers (GstTypeFind * tf, guint64 offset, gint packet_size)
2458 {
2459   /* We always enter this function having found at least one header already */
2460   gint found = 1;
2461   const guint8 *data = NULL;
2462
2463   GST_LOG ("looking for mpeg-ts packets of size %u", packet_size);
2464   while (found < GST_MPEGTS_TYPEFIND_MAX_HEADERS) {
2465     offset += packet_size;
2466
2467     data = gst_type_find_peek (tf, offset, MPEGTS_HDR_SIZE);
2468     if (data == NULL || !IS_MPEGTS_HEADER (data))
2469       return found;
2470
2471     found++;
2472     GST_LOG ("mpeg-ts sync #%2d at offset %" G_GUINT64_FORMAT, found, offset);
2473   }
2474
2475   return found;
2476 }
2477
2478 /* Try and detect at least 4 packets in at most 10 packets worth of
2479  * data. Need to try several possible packet sizes */
2480 static void
2481 mpeg_ts_type_find (GstTypeFind * tf, gpointer unused)
2482 {
2483   /* TS packet sizes to test: normal, DVHS packet size and
2484    * FEC with 16 or 20 byte codes packet size. */
2485   const gint pack_sizes[] = { 188, 192, 204, 208 };
2486   const guint8 *data = NULL;
2487   guint size = 0;
2488   guint64 skipped = 0;
2489
2490   while (skipped < GST_MPEGTS_TYPEFIND_SCAN_LENGTH) {
2491     if (size < MPEGTS_HDR_SIZE) {
2492       data = gst_type_find_peek (tf, skipped, GST_MPEGTS_TYPEFIND_SYNC_SIZE);
2493       if (!data)
2494         break;
2495       size = GST_MPEGTS_TYPEFIND_SYNC_SIZE;
2496     }
2497
2498     /* Have at least MPEGTS_HDR_SIZE bytes at this point */
2499     if (IS_MPEGTS_HEADER (data)) {
2500       gsize p;
2501
2502       GST_LOG ("possible mpeg-ts sync at offset %" G_GUINT64_FORMAT, skipped);
2503
2504       for (p = 0; p < G_N_ELEMENTS (pack_sizes); p++) {
2505         gint found;
2506
2507         /* Probe ahead at size pack_sizes[p] */
2508         found = mpeg_ts_probe_headers (tf, skipped, pack_sizes[p]);
2509         if (found >= GST_MPEGTS_TYPEFIND_MIN_HEADERS) {
2510           gint probability;
2511
2512           /* found at least 4 headers. 10 headers = MAXIMUM probability.
2513            * Arbitrarily, I assigned 10% probability for each header we
2514            * found, 40% -> 100% */
2515           probability = MIN (10 * found, GST_TYPE_FIND_MAXIMUM);
2516
2517           gst_type_find_suggest_simple (tf, probability, "video/mpegts",
2518               "systemstream", G_TYPE_BOOLEAN, TRUE,
2519               "packetsize", G_TYPE_INT, pack_sizes[p], NULL);
2520           return;
2521         }
2522       }
2523     }
2524     data++;
2525     skipped++;
2526     size--;
2527   }
2528 }
2529
2530 #define GST_MPEGVID_TYPEFIND_TRY_PICTURES 6
2531 #define GST_MPEGVID_TYPEFIND_TRY_SYNC (100 * 1024)      /* 100 kB */
2532
2533 /* Scan ahead a maximum of max_extra_offset bytes until the next IS_MPEG_HEADER
2534  * offset.  After the call, offset will be after the 0x000001, i.e. at the 4th
2535  * byte of the MPEG header.  Returns TRUE if a header was found, FALSE if not.
2536  */
2537 static gboolean
2538 mpeg_find_next_header (GstTypeFind * tf, DataScanCtx * c,
2539     guint64 max_extra_offset)
2540 {
2541   guint64 extra_offset;
2542
2543   for (extra_offset = 0; extra_offset <= max_extra_offset; ++extra_offset) {
2544     if (!data_scan_ctx_ensure_data (tf, c, 4))
2545       return FALSE;
2546     if (IS_MPEG_HEADER (c->data)) {
2547       data_scan_ctx_advance (tf, c, 3);
2548       return TRUE;
2549     }
2550     data_scan_ctx_advance (tf, c, 1);
2551   }
2552   return FALSE;
2553 }
2554
2555 /*** video/mpeg MPEG-4 elementary video stream ***/
2556
2557 static GstStaticCaps mpeg4_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2558     "systemstream=(boolean)false, mpegversion=4, parsed=(boolean)false");
2559 #define MPEG4_VIDEO_CAPS gst_static_caps_get(&mpeg4_video_caps)
2560
2561 /*
2562  * This typefind is based on the elementary video header defined in
2563  * http://xhelmboyx.tripod.com/formats/mpeg-layout.txt
2564  * In addition, it allows the visual object sequence header to be
2565  * absent, and even the VOS header to be absent.  In the latter case,
2566  * a number of VOPs have to be present.
2567  */
2568 static void
2569 mpeg4_video_type_find (GstTypeFind * tf, gpointer unused)
2570 {
2571   DataScanCtx c = { 0, NULL, 0 };
2572   gboolean seen_vios_at_0 = FALSE;
2573   gboolean seen_vios = FALSE;
2574   gboolean seen_vos = FALSE;
2575   gboolean seen_vol = FALSE;
2576   guint num_vop_headers = 0;
2577   guint8 sc;
2578
2579   while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2580     if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2581       break;
2582
2583     if (!mpeg_find_next_header (tf, &c,
2584             GST_MPEGVID_TYPEFIND_TRY_SYNC - c.offset))
2585       break;
2586
2587     sc = c.data[0];
2588
2589     /* visual_object_sequence_start_code */
2590     if (sc == 0xB0) {
2591       if (seen_vios)
2592         break;                  /* Terminate at second vios */
2593       if (c.offset == 0)
2594         seen_vios_at_0 = TRUE;
2595       seen_vios = TRUE;
2596       data_scan_ctx_advance (tf, &c, 2);
2597       if (!mpeg_find_next_header (tf, &c, 0))
2598         break;
2599
2600       sc = c.data[0];
2601
2602       /* Optional metadata */
2603       if (sc == 0xB2)
2604         if (!mpeg_find_next_header (tf, &c, 24))
2605           break;
2606     }
2607
2608     /* visual_object_start_code (consider it optional) */
2609     if (sc == 0xB5) {
2610       data_scan_ctx_advance (tf, &c, 2);
2611       /* may contain ID marker and YUV clamping */
2612       if (!mpeg_find_next_header (tf, &c, 7))
2613         break;
2614
2615       sc = c.data[0];
2616     }
2617
2618     /* video_object_start_code */
2619     if (sc <= 0x1F) {
2620       if (seen_vos)
2621         break;                  /* Terminate at second vos */
2622       seen_vos = TRUE;
2623       data_scan_ctx_advance (tf, &c, 2);
2624       continue;
2625     }
2626
2627     /* video_object_layer_start_code */
2628     if (sc >= 0x20 && sc <= 0x2F) {
2629       seen_vol = TRUE;
2630       data_scan_ctx_advance (tf, &c, 5);
2631       continue;
2632     }
2633
2634     /* video_object_plane_start_code */
2635     if (sc == 0xB6) {
2636       num_vop_headers++;
2637       data_scan_ctx_advance (tf, &c, 2);
2638       continue;
2639     }
2640
2641     /* Unknown start code. */
2642   }
2643
2644   if (num_vop_headers > 0 || seen_vol) {
2645     GstTypeFindProbability probability = 0;
2646
2647     GST_LOG ("Found %d pictures, vios: %d, vos:%d, vol:%d", num_vop_headers,
2648         seen_vios, seen_vos, seen_vol);
2649
2650     if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios_at_0
2651         && seen_vos && seen_vol)
2652       probability = GST_TYPE_FIND_MAXIMUM - 1;
2653     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios
2654         && seen_vos && seen_vol)
2655       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
2656     else if (seen_vios_at_0 && seen_vos && seen_vol)
2657       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2658     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vos
2659         && seen_vol)
2660       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6;
2661     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vol)
2662       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
2663     else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2664       probability = GST_TYPE_FIND_LIKELY - 1;
2665     else if (num_vop_headers > 2 && seen_vios && seen_vos && seen_vol)
2666       probability = GST_TYPE_FIND_LIKELY - 9;
2667     else if (seen_vios && seen_vos && seen_vol)
2668       probability = GST_TYPE_FIND_LIKELY - 20;
2669     else if (num_vop_headers > 0 && seen_vos && seen_vol)
2670       probability = GST_TYPE_FIND_POSSIBLE;
2671     else if (num_vop_headers > 0)
2672       probability = GST_TYPE_FIND_POSSIBLE - 10;
2673     else if (seen_vos && seen_vol)
2674       probability = GST_TYPE_FIND_POSSIBLE - 20;
2675
2676     gst_type_find_suggest (tf, probability, MPEG4_VIDEO_CAPS);
2677   }
2678 }
2679
2680 /*** video/x-h263 H263 video stream ***/
2681 static GstStaticCaps h263_video_caps =
2682 GST_STATIC_CAPS ("video/x-h263, variant=(string)itu");
2683
2684 #define H263_VIDEO_CAPS gst_static_caps_get(&h263_video_caps)
2685
2686 #define H263_MAX_PROBE_LENGTH (128 * 1024)
2687
2688 static void
2689 h263_video_type_find (GstTypeFind * tf, gpointer unused)
2690 {
2691   DataScanCtx c = { 0, NULL, 0 };
2692   guint64 data = 0xffff;        /* prevents false positive for first 2 bytes */
2693   guint64 psc = 0;
2694   guint8 ptype = 0;
2695   guint format;
2696   guint good = 0;
2697   guint bad = 0;
2698   guint pc_type, pb_mode;
2699
2700   while (c.offset < H263_MAX_PROBE_LENGTH) {
2701     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2702       break;
2703
2704     /* Find the picture start code */
2705     data = (data << 8) + c.data[0];
2706     psc = data & G_GUINT64_CONSTANT (0xfffffc0000);
2707     if (psc == 0x800000) {
2708       /* Found PSC */
2709       /* PTYPE */
2710       ptype = (data & 0x3fc) >> 2;
2711       /* Source Format */
2712       format = ptype & 0x07;
2713
2714       /* Now that we have a Valid PSC, check if we also have a valid PTYPE and
2715          the Source Format, which should range between 1 and 5 */
2716       if (((ptype >> 6) == 0x2) && (format > 0 && format < 6)) {
2717         pc_type = data & 0x02;
2718         pb_mode = c.data[1] & 0x20 >> 4;
2719         if (!pc_type && pb_mode)
2720           bad++;
2721         else
2722           good++;
2723       } else
2724         bad++;
2725
2726       /* FIXME: maybe bail out early if we get mostly bad syncs ? */
2727     }
2728
2729     data_scan_ctx_advance (tf, &c, 1);
2730   }
2731
2732   GST_LOG ("good: %d, bad: %d", good, bad);
2733
2734   if (good > 2 * bad)
2735     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H263_VIDEO_CAPS);
2736
2737   return;
2738 }
2739
2740 /*** video/x-h264 H264 elementary video stream ***/
2741
2742 static GstStaticCaps h264_video_caps =
2743 GST_STATIC_CAPS ("video/x-h264,stream-format=byte-stream");
2744
2745 #define H264_VIDEO_CAPS gst_static_caps_get(&h264_video_caps)
2746
2747 #define H264_MAX_PROBE_LENGTH (128 * 1024)      /* 128kB for HD should be enough. */
2748
2749 static void
2750 h264_video_type_find (GstTypeFind * tf, gpointer unused)
2751 {
2752   DataScanCtx c = { 0, NULL, 0 };
2753
2754   /* Stream consists of: a series of sync codes (00 00 00 01) followed
2755    * by NALs
2756    */
2757   gboolean seen_idr = FALSE;
2758   gboolean seen_sps = FALSE;
2759   gboolean seen_pps = FALSE;
2760   gboolean seen_ssps = FALSE;
2761   int nut, ref;
2762   int good = 0;
2763   int bad = 0;
2764
2765   while (c.offset < H264_MAX_PROBE_LENGTH) {
2766     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
2767       break;
2768
2769     if (IS_MPEG_HEADER (c.data)) {
2770       nut = c.data[3] & 0x9f;   /* forbiden_zero_bit | nal_unit_type */
2771       ref = c.data[3] & 0x60;   /* nal_ref_idc */
2772
2773       /* if forbidden bit is different to 0 won't be h264 */
2774       if (nut > 0x1f) {
2775         bad++;
2776         break;
2777       }
2778
2779       /* collect statistics about the NAL types */
2780       if ((nut >= 1 && nut <= 13) || nut == 19) {
2781         if ((nut == 5 && ref == 0) ||
2782             ((nut == 6 || (nut >= 9 && nut <= 12)) && ref != 0)) {
2783           bad++;
2784         } else {
2785           if (nut == 7)
2786             seen_sps = TRUE;
2787           else if (nut == 8)
2788             seen_pps = TRUE;
2789           else if (nut == 5)
2790             seen_idr = TRUE;
2791
2792           good++;
2793         }
2794       } else if (nut >= 14 && nut <= 33) {
2795         if (nut == 15) {
2796           seen_ssps = TRUE;
2797           good++;
2798         } else if (nut == 14 || nut == 20) {
2799           /* Sometimes we see NAL 14 or 20 without SSPS
2800            * if dropped into the middle of a stream -
2801            * just ignore those (don't add to bad count) */
2802           if (seen_ssps)
2803             good++;
2804         } else {
2805           /* reserved */
2806           /* Theoretically these are good, since if they exist in the
2807              stream it merely means that a newer backwards-compatible
2808              h.264 stream.  But we should be identifying that separately. */
2809           bad++;
2810         }
2811       } else {
2812         /* unspecified, application specific */
2813         /* don't consider these bad */
2814       }
2815
2816       GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, idr:%d ssps:%d", good, bad,
2817           seen_pps, seen_sps, seen_idr, seen_ssps);
2818
2819       if (seen_sps && seen_pps && seen_idr && good >= 10 && bad < 4) {
2820         gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H264_VIDEO_CAPS);
2821         return;
2822       }
2823
2824       data_scan_ctx_advance (tf, &c, 4);
2825     }
2826     data_scan_ctx_advance (tf, &c, 1);
2827   }
2828
2829   GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, idr:%d ssps=%d", good, bad,
2830       seen_pps, seen_sps, seen_idr, seen_ssps);
2831
2832   if (good >= 2 && bad == 0) {
2833     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H264_VIDEO_CAPS);
2834   }
2835 }
2836
2837 /*** video/x-h265 H265 elementary video stream ***/
2838
2839 static GstStaticCaps h265_video_caps =
2840 GST_STATIC_CAPS ("video/x-h265,stream-format=byte-stream");
2841
2842 #define H265_VIDEO_CAPS gst_static_caps_get(&h265_video_caps)
2843
2844 #define H265_MAX_PROBE_LENGTH (128 * 1024)      /* 128kB for HD should be enough. */
2845
2846 static void
2847 h265_video_type_find (GstTypeFind * tf, gpointer unused)
2848 {
2849   DataScanCtx c = { 0, NULL, 0 };
2850
2851   /* Stream consists of: a series of sync codes (00 00 00 01) followed
2852    * by NALs
2853    */
2854   gboolean seen_irap = FALSE;
2855   gboolean seen_vps = FALSE;
2856   gboolean seen_sps = FALSE;
2857   gboolean seen_pps = FALSE;
2858   int nut;
2859   int good = 0;
2860   int bad = 0;
2861
2862   while (c.offset < H265_MAX_PROBE_LENGTH) {
2863     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5)))
2864       break;
2865
2866     if (IS_MPEG_HEADER (c.data)) {
2867       /* forbiden_zero_bit | nal_unit_type */
2868       nut = c.data[3] & 0xfe;
2869
2870       /* if forbidden bit is different to 0 won't be h265 */
2871       if (nut > 0x7e) {
2872         bad++;
2873         break;
2874       }
2875       nut = nut >> 1;
2876
2877       /* if nuh_layer_id is not zero or nuh_temporal_id_plus1 is zero then
2878        * it won't be h265 */
2879       if ((c.data[3] & 0x01) || (c.data[4] & 0xf8) || !(c.data[4] & 0x07)) {
2880         bad++;
2881         break;
2882       }
2883
2884       /* collect statistics about the NAL types */
2885       if ((nut >= 0 && nut <= 9) || (nut >= 16 && nut <= 21) || (nut >= 32
2886               && nut <= 40)) {
2887         if (nut == 32)
2888           seen_vps = TRUE;
2889         else if (nut == 33)
2890           seen_sps = TRUE;
2891         else if (nut == 34)
2892           seen_pps = TRUE;
2893         else if (nut >= 16 && nut <= 21) {
2894           /* BLA, IDR and CRA pictures are belongs to be IRAP picture */
2895           /* we are not counting the reserved IRAP pictures (22 and 23) to good */
2896           seen_irap = TRUE;
2897         }
2898
2899         good++;
2900       } else if ((nut >= 10 && nut <= 15) || (nut >= 22 && nut <= 31)
2901           || (nut >= 41 && nut <= 47)) {
2902         /* reserved values are counting as bad */
2903         bad++;
2904       } else {
2905         /* unspecified (48..63), application specific */
2906         /* don't consider these as bad */
2907       }
2908
2909       GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, vps:%d, irap:%d", good, bad,
2910           seen_pps, seen_sps, seen_vps, seen_irap);
2911
2912       if (seen_sps && seen_pps && seen_irap && good >= 10 && bad < 4) {
2913         gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H265_VIDEO_CAPS);
2914         return;
2915       }
2916
2917       data_scan_ctx_advance (tf, &c, 5);
2918     }
2919     data_scan_ctx_advance (tf, &c, 1);
2920   }
2921
2922   GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, vps:%d, irap:%d", good, bad,
2923       seen_pps, seen_sps, seen_vps, seen_irap);
2924
2925   if (good >= 2 && bad == 0) {
2926     gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H265_VIDEO_CAPS);
2927   }
2928 }
2929
2930 /*** video/mpeg video stream ***/
2931
2932 static GstStaticCaps mpeg_video_caps = GST_STATIC_CAPS ("video/mpeg, "
2933     "systemstream = (boolean) false");
2934 #define MPEG_VIDEO_CAPS gst_static_caps_get(&mpeg_video_caps)
2935
2936 /*
2937  * Idea is the same as MPEG system stream typefinding: We check each
2938  * byte of the stream to see if - from that point on - the stream
2939  * matches a predefined set of marker bits as defined in the MPEG
2940  * video specs.
2941  *
2942  * I'm sure someone will do a chance calculation here too.
2943  */
2944
2945 static void
2946 mpeg_video_stream_type_find (GstTypeFind * tf, gpointer unused)
2947 {
2948   DataScanCtx c = { 0, NULL, 0 };
2949   gboolean seen_seq_at_0 = FALSE;
2950   gboolean seen_seq = FALSE;
2951   gboolean seen_gop = FALSE;
2952   guint64 last_pic_offset = 0;
2953   gint num_pic_headers = 0;
2954   gint found = 0;
2955
2956   while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) {
2957     if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
2958       break;
2959
2960     if (!data_scan_ctx_ensure_data (tf, &c, 5))
2961       break;
2962
2963     if (!IS_MPEG_HEADER (c.data))
2964       goto next;
2965
2966     /* a pack header indicates that this isn't an elementary stream */
2967     if (c.data[3] == 0xBA && mpeg_sys_is_valid_pack (tf, c.data, c.size, NULL))
2968       return;
2969
2970     /* do we have a sequence header? */
2971     if (c.data[3] == 0xB3) {
2972       seen_seq_at_0 = seen_seq_at_0 || (c.offset == 0);
2973       seen_seq = TRUE;
2974       data_scan_ctx_advance (tf, &c, 4 + 8);
2975       continue;
2976     }
2977
2978     /* or a GOP header */
2979     if (c.data[3] == 0xB8) {
2980       seen_gop = TRUE;
2981       data_scan_ctx_advance (tf, &c, 8);
2982       continue;
2983     }
2984
2985     /* but what we'd really like to see is a picture header */
2986     if (c.data[3] == 0x00) {
2987       ++num_pic_headers;
2988       last_pic_offset = c.offset;
2989       data_scan_ctx_advance (tf, &c, 8);
2990       continue;
2991     }
2992
2993     /* ... each followed by a slice header with slice_vertical_pos=1 that's
2994      * not too far away from the previously seen picture header. */
2995     if (c.data[3] == 0x01 && num_pic_headers > found &&
2996         (c.offset - last_pic_offset) >= 4 &&
2997         (c.offset - last_pic_offset) <= 64) {
2998       data_scan_ctx_advance (tf, &c, 4);
2999       found += 1;
3000       continue;
3001     }
3002
3003   next:
3004
3005     data_scan_ctx_advance (tf, &c, 1);
3006   }
3007
3008   if (found > 0 || seen_seq) {
3009     GstTypeFindProbability probability = 0;
3010
3011     GST_LOG ("Found %d pictures, seq:%d, gop:%d", found, seen_seq, seen_gop);
3012
3013     if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq && seen_gop)
3014       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1;
3015     else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_seq)
3016       probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9;
3017     else if (found >= GST_MPEGVID_TYPEFIND_TRY_PICTURES)
3018       probability = GST_TYPE_FIND_LIKELY;
3019     else if (seen_seq_at_0 && seen_gop && found > 2)
3020       probability = GST_TYPE_FIND_LIKELY - 10;
3021     else if (seen_seq && seen_gop && found > 2)
3022       probability = GST_TYPE_FIND_LIKELY - 20;
3023     else if (seen_seq_at_0 && found > 0)
3024       probability = GST_TYPE_FIND_POSSIBLE;
3025     else if (seen_seq && found > 0)
3026       probability = GST_TYPE_FIND_POSSIBLE - 5;
3027     else if (found > 0)
3028       probability = GST_TYPE_FIND_POSSIBLE - 10;
3029     else if (seen_seq)
3030       probability = GST_TYPE_FIND_POSSIBLE - 20;
3031
3032     gst_type_find_suggest_simple (tf, probability, "video/mpeg",
3033         "systemstream", G_TYPE_BOOLEAN, FALSE,
3034         "mpegversion", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
3035   }
3036 }
3037
3038 /*** audio/x-aiff ***/
3039
3040 static GstStaticCaps aiff_caps = GST_STATIC_CAPS ("audio/x-aiff");
3041
3042 #define AIFF_CAPS gst_static_caps_get(&aiff_caps)
3043 static void
3044 aiff_type_find (GstTypeFind * tf, gpointer unused)
3045 {
3046   const guint8 *data = gst_type_find_peek (tf, 0, 16);
3047
3048   if (data && memcmp (data, "FORM", 4) == 0) {
3049     data += 8;
3050     if (memcmp (data, "AIFF", 4) == 0 || memcmp (data, "AIFC", 4) == 0)
3051       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AIFF_CAPS);
3052   }
3053 }
3054
3055 /*** audio/x-svx ***/
3056 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
3057 static GstStaticCaps svx_caps = GST_STATIC_CAPS ("audio/x-svx");
3058
3059 #define SVX_CAPS gst_static_caps_get(&svx_caps)
3060 static void
3061 svx_type_find (GstTypeFind * tf, gpointer unused)
3062 {
3063   const guint8 *data = gst_type_find_peek (tf, 0, 16);
3064
3065   if (data && memcmp (data, "FORM", 4) == 0) {
3066     data += 8;
3067     if (memcmp (data, "8SVX", 4) == 0 || memcmp (data, "16SV", 4) == 0)
3068       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVX_CAPS);
3069   }
3070 }
3071
3072 /*** audio/x-shorten ***/
3073
3074 static GstStaticCaps shn_caps = GST_STATIC_CAPS ("audio/x-shorten");
3075
3076 #define SHN_CAPS gst_static_caps_get(&shn_caps)
3077 static void
3078 shn_type_find (GstTypeFind * tf, gpointer unused)
3079 {
3080   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3081
3082   if (data && memcmp (data, "ajkg", 4) == 0) {
3083     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
3084   }
3085   data = gst_type_find_peek (tf, -8, 8);
3086   if (data && memcmp (data, "SHNAMPSK", 8) == 0) {
3087     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SHN_CAPS);
3088   }
3089 }
3090
3091 /*** application/x-ape ***/
3092
3093 static GstStaticCaps ape_caps = GST_STATIC_CAPS ("application/x-ape");
3094
3095 #define APE_CAPS gst_static_caps_get(&ape_caps)
3096 static void
3097 ape_type_find (GstTypeFind * tf, gpointer unused)
3098 {
3099   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3100
3101   if (data && memcmp (data, "MAC ", 4) == 0) {
3102     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, APE_CAPS);
3103   }
3104 }
3105 #endif
3106 /*** ISO FORMATS ***/
3107
3108 /*** audio/x-m4a ***/
3109
3110 static GstStaticCaps m4a_caps = GST_STATIC_CAPS ("audio/x-m4a");
3111
3112 #define M4A_CAPS (gst_static_caps_get(&m4a_caps))
3113 static void
3114 m4a_type_find (GstTypeFind * tf, gpointer unused)
3115 {
3116   const guint8 *data = gst_type_find_peek (tf, 4, 8);
3117
3118   if (data && (memcmp (data, "ftypM4A ", 8) == 0)) {
3119     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, M4A_CAPS);
3120   }
3121 }
3122
3123 /*** application/x-3gp ***/
3124
3125 /* The Q is there because variables can't start with a number. */
3126 static GstStaticCaps q3gp_caps = GST_STATIC_CAPS ("application/x-3gp");
3127 #define Q3GP_CAPS (gst_static_caps_get(&q3gp_caps))
3128
3129 static const gchar *
3130 q3gp_type_find_get_profile (const guint8 * data)
3131 {
3132   switch (GST_MAKE_FOURCC (data[0], data[1], data[2], 0)) {
3133     case GST_MAKE_FOURCC ('3', 'g', 'g', 0):
3134       return "general";
3135     case GST_MAKE_FOURCC ('3', 'g', 'p', 0):
3136       return "basic";
3137     case GST_MAKE_FOURCC ('3', 'g', 's', 0):
3138       return "streaming-server";
3139     case GST_MAKE_FOURCC ('3', 'g', 'r', 0):
3140       return "progressive-download";
3141     default:
3142       break;
3143   }
3144   return NULL;
3145 }
3146
3147 static void
3148 q3gp_type_find (GstTypeFind * tf, gpointer unused)
3149 {
3150   const gchar *profile;
3151   guint32 ftyp_size = 0;
3152   guint32 offset = 0;
3153   const guint8 *data = NULL;
3154
3155   if ((data = gst_type_find_peek (tf, 0, 12)) == NULL) {
3156     return;
3157   }
3158
3159   data += 4;
3160   if (memcmp (data, "ftyp", 4) != 0) {
3161     return;
3162   }
3163
3164   /* check major brand */
3165   data += 4;
3166   if ((profile = q3gp_type_find_get_profile (data))) {
3167     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3168         "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
3169     return;
3170   }
3171
3172   /* check compatible brands */
3173   if ((data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3174     ftyp_size = GST_READ_UINT32_BE (data);
3175   }
3176   if ((data = gst_type_find_peek (tf, 0, ftyp_size)) != NULL) {
3177     for (offset = 16; offset + 4 < ftyp_size; offset += 4) {
3178       if ((profile = q3gp_type_find_get_profile (data + offset))) {
3179         gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
3180             "application/x-3gp", "profile", G_TYPE_STRING, profile, NULL);
3181         return;
3182       }
3183     }
3184   }
3185
3186   return;
3187
3188 }
3189
3190 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
3191 /*** video/mj2 and image/jp2 ***/
3192 static GstStaticCaps mj2_caps = GST_STATIC_CAPS ("video/mj2");
3193
3194 #define MJ2_CAPS gst_static_caps_get(&mj2_caps)
3195
3196 static GstStaticCaps jp2_caps = GST_STATIC_CAPS ("image/jp2");
3197
3198 #define JP2_CAPS gst_static_caps_get(&jp2_caps)
3199
3200 static void
3201 jp2_type_find (GstTypeFind * tf, gpointer unused)
3202 {
3203   const guint8 *data;
3204
3205   data = gst_type_find_peek (tf, 0, 24);
3206   if (!data)
3207     return;
3208
3209   /* jp2 signature */
3210   if (memcmp (data, "\000\000\000\014jP  \015\012\207\012", 12) != 0)
3211     return;
3212
3213   /* check ftyp box */
3214   data += 12;
3215   if (memcmp (data + 4, "ftyp", 4) == 0) {
3216     if (memcmp (data + 8, "jp2 ", 4) == 0)
3217       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, JP2_CAPS);
3218     else if (memcmp (data + 8, "mjp2", 4) == 0)
3219       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MJ2_CAPS);
3220   }
3221 }
3222
3223
3224 static GstStaticCaps jpc_caps = GST_STATIC_CAPS ("image/x-jpc");
3225
3226 #define JPC_CAPS gst_static_caps_get(&jpc_caps)
3227
3228 static void
3229 jpc_type_find (GstTypeFind * tf, gpointer unused)
3230 {
3231   gboolean found_cod = FALSE;
3232   gboolean found_qcd = FALSE;
3233   gboolean found_sot = FALSE;
3234   const guint8 *data;
3235   gint offset = 0;
3236   const guint8 soc_siz[] = { 0xff, 0x4f, 0xff, 0x51 };
3237
3238 #define GST_TYPE_FIND_JPC_MARKER_SOT  0xFF90
3239 #define GST_TYPE_FIND_JPC_MARKER_COD  0xFF52
3240 #define GST_TYPE_FIND_JPC_MARKER_QCD  0xFF5C
3241 #define GST_TYPE_FIND_JPC_MARKER_COC  0xFF53
3242 #define GST_TYPE_FIND_JPC_MARKER_RGN  0xFF5E
3243 #define GST_TYPE_FIND_JPC_MARKER_QCC  0xFF5D
3244 #define GST_TYPE_FIND_JPC_MARKER_POC  0xFF5F
3245 #define GST_TYPE_FIND_JPC_MARKER_PLM  0xFF57
3246 #define GST_TYPE_FIND_JPC_MARKER_PPM  0xFF60
3247 #define GST_TYPE_FIND_JPC_MARKER_TLM  0xFF55
3248 #define GST_TYPE_FIND_JPC_MARKER_CRG  0xFF63
3249 #define GST_TYPE_FIND_JPC_MARKER_COM  0xFF64
3250 #define GST_TYPE_FIND_JPC_MARKER_CBD  0xFF78
3251 #define GST_TYPE_FIND_JPC_MARKER_MCC  0xFF75
3252 #define GST_TYPE_FIND_JPC_MARKER_MCT  0xFF74
3253 #define GST_TYPE_FIND_JPC_MARKER_MCO  0xFF77
3254
3255
3256   /* SOC marker + SIZ marker */
3257   if ((data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3258     if (memcmp (data, soc_siz, 4) != 0)
3259       return;
3260     offset += 4;
3261   } else {
3262     return;
3263   }
3264
3265   while (!found_sot) {
3266
3267     /* skip actual marker data */
3268     if ((data = gst_type_find_peek (tf, offset, 2)) != NULL) {
3269       offset += GST_READ_UINT16_BE (data);
3270     } else {
3271       return;
3272     }
3273
3274     /* read marker */
3275     if ((data = gst_type_find_peek (tf, offset, 2)) != NULL) {
3276       guint16 marker = GST_READ_UINT16_BE (data);
3277       switch (marker) {
3278         case GST_TYPE_FIND_JPC_MARKER_SOT:
3279           found_sot = TRUE;
3280           break;
3281         case GST_TYPE_FIND_JPC_MARKER_COD:
3282           found_cod = TRUE;
3283           break;
3284         case GST_TYPE_FIND_JPC_MARKER_QCD:
3285           found_qcd = TRUE;
3286           break;
3287           /* optional header markers */
3288         case GST_TYPE_FIND_JPC_MARKER_COC:
3289         case GST_TYPE_FIND_JPC_MARKER_RGN:
3290         case GST_TYPE_FIND_JPC_MARKER_QCC:
3291         case GST_TYPE_FIND_JPC_MARKER_POC:
3292         case GST_TYPE_FIND_JPC_MARKER_PLM:
3293         case GST_TYPE_FIND_JPC_MARKER_PPM:
3294         case GST_TYPE_FIND_JPC_MARKER_TLM:
3295         case GST_TYPE_FIND_JPC_MARKER_CRG:
3296         case GST_TYPE_FIND_JPC_MARKER_COM:
3297         case GST_TYPE_FIND_JPC_MARKER_CBD:
3298         case GST_TYPE_FIND_JPC_MARKER_MCC:
3299         case GST_TYPE_FIND_JPC_MARKER_MCT:
3300         case GST_TYPE_FIND_JPC_MARKER_MCO:
3301           break;
3302           /* unrecognized marker */
3303         default:
3304           return;
3305       }
3306       offset += 2;
3307     } else {
3308       return;
3309     }
3310   }
3311
3312   if (found_cod && found_qcd && found_sot)
3313     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, JPC_CAPS);
3314 }
3315 #endif
3316
3317 /*** video/quicktime ***/
3318
3319 static GstStaticCaps qt_caps = GST_STATIC_CAPS ("video/quicktime");
3320
3321 #define QT_CAPS gst_static_caps_get(&qt_caps)
3322 #define STRNCMP(x,y,z) (strncmp ((char*)(x), (char*)(y), z))
3323
3324 /* FIXME 0.11: go through http://www.ftyps.com/ */
3325 static void
3326 qt_type_find (GstTypeFind * tf, gpointer unused)
3327 {
3328   const guint8 *data;
3329   guint tip = 0;
3330   guint64 offset = 0;
3331   guint64 size;
3332   const gchar *variant = NULL;
3333
3334   while ((data = gst_type_find_peek (tf, offset, 12)) != NULL) {
3335     guint64 new_offset;
3336
3337     if (STRNCMP (&data[4], "ftypqt  ", 8) == 0) {
3338       tip = GST_TYPE_FIND_MAXIMUM;
3339       break;
3340     }
3341
3342     if (STRNCMP (&data[4], "ftypisom", 8) == 0 ||
3343         STRNCMP (&data[4], "ftypavc1", 8) == 0 ||
3344 #ifdef TIZEN_FEATURE_TYPEFIND_ENHANCEMENT
3345         STRNCMP (&data[4], "ftypwmf ", 8) == 0 ||
3346 #endif
3347         STRNCMP (&data[4], "ftypmp42", 8) == 0) {
3348       tip = GST_TYPE_FIND_MAXIMUM;
3349       variant = "iso";
3350       break;
3351     }
3352
3353     if (STRNCMP (&data[4], "ftypisml", 8) == 0 ||
3354         STRNCMP (&data[4], "ftypavc3", 8) == 0) {
3355       tip = GST_TYPE_FIND_MAXIMUM;
3356       variant = "iso-fragmented";
3357       break;
3358     }
3359
3360     if (STRNCMP (&data[4], "ftypccff", 8) == 0) {
3361       tip = GST_TYPE_FIND_MAXIMUM;
3362       variant = "ccff";
3363       break;
3364     }
3365
3366     /* box/atom types that are in common with ISO base media file format */
3367     if (STRNCMP (&data[4], "moov", 4) == 0 ||
3368         STRNCMP (&data[4], "mdat", 4) == 0 ||
3369         STRNCMP (&data[4], "ftyp", 4) == 0 ||
3370         STRNCMP (&data[4], "free", 4) == 0 ||
3371         STRNCMP (&data[4], "uuid", 4) == 0 ||
3372         STRNCMP (&data[4], "moof", 4) == 0 ||
3373         STRNCMP (&data[4], "skip", 4) == 0) {
3374       if (tip == 0) {
3375         tip = GST_TYPE_FIND_LIKELY;
3376       } else {
3377         tip = GST_TYPE_FIND_NEARLY_CERTAIN;
3378       }
3379     }
3380     /* other box/atom types, apparently quicktime specific */
3381     else if (STRNCMP (&data[4], "pnot", 4) == 0 ||
3382         STRNCMP (&data[4], "PICT", 4) == 0 ||
3383         STRNCMP (&data[4], "wide", 4) == 0 ||
3384         STRNCMP (&data[4], "prfl", 4) == 0) {
3385       tip = GST_TYPE_FIND_MAXIMUM;
3386       break;
3387     } else {
3388       tip = 0;
3389       break;
3390     }
3391
3392     size = GST_READ_UINT32_BE (data);
3393     if (size + offset >= G_MAXINT64)
3394       break;
3395     /* check compatible brands rather than ever expaning major brands above */
3396     if ((STRNCMP (&data[4], "ftyp", 4) == 0) && (size >= 16)) {
3397       data = gst_type_find_peek (tf, offset, size);
3398       if (data == NULL)
3399         goto done;
3400       new_offset = 12;
3401       while (new_offset + 4 <= size) {
3402         if (STRNCMP (&data[new_offset], "isom", 4) == 0 ||
3403             STRNCMP (&data[new_offset], "dash", 4) == 0 ||
3404             STRNCMP (&data[new_offset], "avc1", 4) == 0 ||
3405             STRNCMP (&data[new_offset], "avc3", 4) == 0 ||
3406             STRNCMP (&data[new_offset], "mp41", 4) == 0 ||
3407             STRNCMP (&data[new_offset], "mp42", 4) == 0) {
3408           tip = GST_TYPE_FIND_MAXIMUM;
3409           variant = "iso";
3410           goto done;
3411         }
3412         new_offset += 4;
3413       }
3414     }
3415     if (size == 1) {
3416       const guint8 *sizedata;
3417
3418       sizedata = gst_type_find_peek (tf, offset + 8, 8);
3419       if (sizedata == NULL)
3420         break;
3421
3422       size = GST_READ_UINT64_BE (sizedata);
3423     } else {
3424       if (size < 8)
3425         break;
3426     }
3427     new_offset = offset + size;
3428     if (new_offset <= offset)
3429       break;
3430     if (new_offset + 16 >= G_MAXINT64)
3431       break;
3432     offset = new_offset;
3433   }
3434
3435 done:
3436   if (tip > 0) {
3437     if (variant) {
3438       GstCaps *caps = gst_caps_copy (QT_CAPS);
3439
3440       gst_caps_set_simple (caps, "variant", G_TYPE_STRING, variant, NULL);
3441       gst_type_find_suggest (tf, tip, caps);
3442       gst_caps_unref (caps);
3443     } else {
3444       gst_type_find_suggest (tf, tip, QT_CAPS);
3445     }
3446   }
3447 };
3448
3449
3450 /*** image/x-quicktime ***/
3451 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
3452 static GstStaticCaps qtif_caps = GST_STATIC_CAPS ("image/x-quicktime");
3453
3454 #define QTIF_CAPS gst_static_caps_get(&qtif_caps)
3455
3456 /* how many atoms we check before we give up */
3457 #define QTIF_MAXROUNDS 25
3458
3459 static void
3460 qtif_type_find (GstTypeFind * tf, gpointer unused)
3461 {
3462   const guint8 *data;
3463   gboolean found_idsc = FALSE;
3464   gboolean found_idat = FALSE;
3465   guint64 offset = 0;
3466   guint rounds = 0;
3467
3468   while ((data = gst_type_find_peek (tf, offset, 8)) != NULL) {
3469     guint64 size;
3470
3471     size = GST_READ_UINT32_BE (data);
3472     if (size == 1) {
3473       const guint8 *sizedata;
3474
3475       sizedata = gst_type_find_peek (tf, offset + 8, 8);
3476       if (sizedata == NULL)
3477         break;
3478
3479       size = GST_READ_UINT64_BE (sizedata);
3480     }
3481     if (size < 8)
3482       break;
3483
3484     if (STRNCMP (data + 4, "idsc", 4) == 0)
3485       found_idsc = TRUE;
3486     if (STRNCMP (data + 4, "idat", 4) == 0)
3487       found_idat = TRUE;
3488
3489     if (found_idsc && found_idat) {
3490       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, QTIF_CAPS);
3491       return;
3492     }
3493
3494     offset += size;
3495     if (offset + 8 >= G_MAXINT64)
3496       break;
3497     if (++rounds > QTIF_MAXROUNDS)
3498       break;
3499   }
3500
3501   if (found_idsc || found_idat) {
3502     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, QTIF_CAPS);
3503     return;
3504   }
3505 };
3506
3507 /*** audio/x-mod ***/
3508
3509 static GstStaticCaps mod_caps = GST_STATIC_CAPS ("audio/x-mod");
3510
3511 #define MOD_CAPS gst_static_caps_get(&mod_caps)
3512 /* FIXME: M15 CheckType to do */
3513 static void
3514 mod_type_find (GstTypeFind * tf, gpointer unused)
3515 {
3516   const guint8 *data;
3517   GstTypeFindProbability probability;
3518   const char *mod_type = NULL;
3519
3520   /* MOD */
3521   if ((data = gst_type_find_peek (tf, 1080, 4)) != NULL) {
3522     /* Protracker and variants */
3523     if ((memcmp (data, "M.K.", 4) == 0) ||
3524         (memcmp (data, "M!K!", 4) == 0) ||
3525         (memcmp (data, "M&K!", 4) == 0) || (memcmp (data, "N.T.", 4) == 0) ||
3526         /* Star Tracker */
3527         (memcmp (data, "FLT", 3) == 0 && isdigit (data[3])) ||
3528         (memcmp (data, "EXO", 3) == 0 && isdigit (data[3])) ||
3529         /* Oktalyzer (Amiga) */
3530         (memcmp (data, "OKTA", 4) == 0) || (memcmp (data, "OCTA", 4) == 0) ||
3531         /* Oktalyser (Atari) */
3532         (memcmp (data, "CD81", 4) == 0) ||
3533         /* Taketracker */
3534         (memcmp (data, "TDZ", 3) == 0 && isdigit (data[3])) ||
3535         /* Fasttracker */
3536         (memcmp (data + 1, "CHN", 3) == 0 && isdigit (data[0])) ||
3537         /* Fasttracker or Taketracker */
3538         (memcmp (data + 2, "CH", 2) == 0 && isdigit (data[0])
3539             && isdigit (data[1])) || (memcmp (data + 2, "CN", 2) == 0
3540             && isdigit (data[0]) && isdigit (data[1]))) {
3541       mod_type = "mod";
3542       probability = GST_TYPE_FIND_MAXIMUM;
3543       goto suggest_audio_mod_caps;
3544     }
3545   }
3546   /* J2B (Jazz Jackrabbit 2) */
3547   if ((data = gst_type_find_peek (tf, 0, 8)) != NULL) {
3548     if ((memcmp (data, "MUSE\xDE\xAD", 4) == 0) &&
3549         ((memcmp (data + 6, "\xBE\xEF", 2) == 0) ||
3550             (memcmp (data + 6, "\xBA\xBE", 2) == 0))) {
3551       mod_type = "j2b";
3552       probability = GST_TYPE_FIND_MAXIMUM;
3553       goto suggest_audio_mod_caps;
3554     }
3555   }
3556   /* AMS (Velvet Studio) */
3557   if ((data = gst_type_find_peek (tf, 0, 7)) != NULL) {
3558     if (memcmp (data, "AMShdr\x1A", 7) == 0) {
3559       mod_type = "velvet-ams";
3560       probability = GST_TYPE_FIND_MAXIMUM;
3561       goto suggest_audio_mod_caps;
3562     }
3563   }
3564   /* AMS (Extreme Tracker) */
3565   if ((data = gst_type_find_peek (tf, 0, 9)) != NULL) {
3566     if ((memcmp (data, "Extreme", 7) == 0) && (data[8] == 1)) {
3567       mod_type = "extreme-ams";
3568       probability = GST_TYPE_FIND_LIKELY;
3569       goto suggest_audio_mod_caps;
3570     }
3571   }
3572   /* ULT (Ultratracker) */
3573   if ((data = gst_type_find_peek (tf, 0, 14)) != NULL) {
3574     if (memcmp (data, "MAS_UTrack_V00", 14) == 0) {
3575       mod_type = "ult";
3576       probability = GST_TYPE_FIND_MAXIMUM;
3577       goto suggest_audio_mod_caps;
3578     }
3579   }
3580   /* DIGI (DigiBooster) */
3581   if ((data = gst_type_find_peek (tf, 0, 20)) != NULL) {
3582     if (memcmp (data, "DIGI Booster module\0", 20) == 0) {
3583       mod_type = "digi";
3584       probability = GST_TYPE_FIND_MAXIMUM;
3585       goto suggest_audio_mod_caps;
3586     }
3587   }
3588   /* PTM (PolyTracker) */
3589   if ((data = gst_type_find_peek (tf, 0x2C, 4)) != NULL) {
3590     if (memcmp (data, "PTMF", 4) == 0) {
3591       mod_type = "ptm";
3592       probability = GST_TYPE_FIND_LIKELY;
3593       goto suggest_audio_mod_caps;
3594     }
3595   }
3596   /* XM */
3597   if ((data = gst_type_find_peek (tf, 0, 38)) != NULL) {
3598     if ((memcmp (data, "Extended Module: ", 17) == 0) && (data[37] == 0x1A)) {
3599       mod_type = "xm";
3600       probability = GST_TYPE_FIND_MAXIMUM;
3601       goto suggest_audio_mod_caps;
3602     }
3603   }
3604   /* OKT */
3605   if (data || (data = gst_type_find_peek (tf, 0, 8)) != NULL) {
3606     if (memcmp (data, "OKTASONG", 8) == 0) {
3607       mod_type = "okt";
3608       probability = GST_TYPE_FIND_MAXIMUM;
3609       goto suggest_audio_mod_caps;
3610     }
3611   }
3612   /* Various formats with a 4-byte magic ID at the beginning of the file */
3613   if (data || (data = gst_type_find_peek (tf, 0, 4)) != NULL) {
3614     /* PSM (Protracker Studio PSM) */
3615     if (memcmp (data, "PSM", 3) == 0) {
3616       unsigned char fbyte = data[3];
3617       if ((fbyte == ' ') || (fbyte == 254)) {
3618         mod_type = "psm";
3619         probability = GST_TYPE_FIND_MAXIMUM;
3620         goto suggest_audio_mod_caps;
3621       }
3622     }
3623     /* 669 */
3624     if ((memcmp (data, "if", 2) == 0) || (memcmp (data, "JN", 2) == 0)) {
3625       mod_type = "669";
3626       probability = GST_TYPE_FIND_LIKELY;
3627       goto suggest_audio_mod_caps;
3628     }
3629     /* AMF */
3630     if ((memcmp (data, "AMF", 3) == 0) && (data[3] > 10) && (data[3] < 14)) {
3631       mod_type = "dsmi-amf";
3632       probability = GST_TYPE_FIND_MAXIMUM;
3633       goto suggest_audio_mod_caps;
3634     }
3635     /* IT */
3636     if (memcmp (data, "IMPM", 4) == 0) {
3637       mod_type = "it";
3638       probability = GST_TYPE_FIND_MAXIMUM;
3639       goto suggest_audio_mod_caps;
3640     }
3641     /* DBM (DigiBooster Pro) */
3642     if (memcmp (data, "DBM0", 4) == 0) {
3643       mod_type = "dbm";
3644       probability = GST_TYPE_FIND_MAXIMUM;
3645       goto suggest_audio_mod_caps;
3646     }
3647     /* MDL (DigiTrakker) */
3648     if (memcmp (data, "DMDL", 4) == 0) {
3649       mod_type = "mdl";
3650       probability = GST_TYPE_FIND_MAXIMUM;
3651       goto suggest_audio_mod_caps;
3652     }
3653     /* MT2 (MadTracker 2.0) */
3654     if (memcmp (data, "MT20", 4) == 0) {
3655       mod_type = "mt2";
3656       probability = GST_TYPE_FIND_MAXIMUM;
3657       goto suggest_audio_mod_caps;
3658     }
3659     /* DMF (X-Tracker) */
3660     if (memcmp (data, "DDMF", 4) == 0) {
3661       mod_type = "dmf";
3662       probability = GST_TYPE_FIND_MAXIMUM;
3663       goto suggest_audio_mod_caps;
3664     }
3665     /* MED */
3666     if ((memcmp (data, "MMD0", 4) == 0) || (memcmp (data, "MMD1", 4) == 0)) {
3667       mod_type = "med";
3668       probability = GST_TYPE_FIND_MAXIMUM;
3669       goto suggest_audio_mod_caps;
3670     }
3671     /* MTM */
3672     if (memcmp (data, "MTM", 3) == 0) {
3673       mod_type = "mtm";
3674       probability = GST_TYPE_FIND_MAXIMUM;
3675       goto suggest_audio_mod_caps;
3676     }
3677     /* DSM */
3678     if (memcmp (data, "RIFF", 4) == 0) {
3679       const guint8 *data2 = gst_type_find_peek (tf, 8, 4);
3680
3681       if (data2) {
3682         if (memcmp (data2, "DSMF", 4) == 0) {
3683           mod_type = "dsm";
3684           probability = GST_TYPE_FIND_MAXIMUM;
3685           goto suggest_audio_mod_caps;
3686         }
3687       }
3688     }
3689     /* FAR (Farandole) */
3690     if (memcmp (data, "FAR\xFE", 4) == 0) {
3691       mod_type = "far";
3692       probability = GST_TYPE_FIND_MAXIMUM;
3693       goto suggest_audio_mod_caps;
3694     }
3695     /* FAM */
3696     if (memcmp (data, "FAM\xFE", 4) == 0) {
3697       const guint8 *data2 = gst_type_find_peek (tf, 44, 3);
3698
3699       if (data2) {
3700         if (memcmp (data2, "compare", 3) == 0) {
3701           mod_type = "fam";
3702           probability = GST_TYPE_FIND_MAXIMUM;
3703           goto suggest_audio_mod_caps;
3704         }
3705         /* otherwise do not suggest anything */
3706       } else {
3707         mod_type = "fam";
3708         probability = GST_TYPE_FIND_LIKELY;
3709         goto suggest_audio_mod_caps;
3710       }
3711     }
3712     /* GDM */
3713     if (memcmp (data, "GDM\xFE", 4) == 0) {
3714       const guint8 *data2 = gst_type_find_peek (tf, 71, 4);
3715
3716       if (data2) {
3717         if (memcmp (data2, "GMFS", 4) == 0) {
3718           mod_type = "gdm";
3719           probability = GST_TYPE_FIND_MAXIMUM;
3720           goto suggest_audio_mod_caps;
3721         }
3722         /* otherwise do not suggest anything */
3723       } else {
3724         mod_type = "gdm";
3725         probability = GST_TYPE_FIND_LIKELY;
3726         goto suggest_audio_mod_caps;
3727       }
3728     }
3729     /* UMX */
3730     if (memcmp (data, "\xC1\x83\x2A\x9E", 4) == 0) {
3731       mod_type = "umx";
3732       probability = GST_TYPE_FIND_POSSIBLE;
3733       goto suggest_audio_mod_caps;
3734     }
3735   }
3736   /* FAR (Farandole) (secondary detection) */
3737   if ((data = gst_type_find_peek (tf, 44, 3)) != NULL) {
3738     if (memcmp (data, "\x0D\x0A\x1A", 3) == 0) {
3739       mod_type = "far";
3740       probability = GST_TYPE_FIND_POSSIBLE;
3741       goto suggest_audio_mod_caps;
3742     }
3743   }
3744   /* IMF */
3745   if ((data = gst_type_find_peek (tf, 60, 4)) != NULL) {
3746     if (memcmp (data, "IM10", 4) == 0) {
3747       mod_type = "imf";
3748       probability = GST_TYPE_FIND_MAXIMUM;
3749       goto suggest_audio_mod_caps;
3750     }
3751   }
3752   /* S3M */
3753   if ((data = gst_type_find_peek (tf, 44, 4)) != NULL) {
3754     if (memcmp (data, "SCRM", 4) == 0) {
3755       mod_type = "s3m";
3756       probability = GST_TYPE_FIND_MAXIMUM;
3757       goto suggest_audio_mod_caps;
3758     }
3759   }
3760   /* STM */
3761   if ((data = gst_type_find_peek (tf, 20, 8)) != NULL) {
3762     if (g_ascii_strncasecmp ((gchar *) data, "!Scream!", 8) == 0 ||
3763         g_ascii_strncasecmp ((gchar *) data, "BMOD2STM", 8) == 0) {
3764       const guint8 *id, *stmtype;
3765
3766       if ((id = gst_type_find_peek (tf, 28, 1)) == NULL)
3767         return;
3768       if ((stmtype = gst_type_find_peek (tf, 29, 1)) == NULL)
3769         return;
3770       if (*id == 0x1A && *stmtype == 2) {
3771         mod_type = "stm";
3772         probability = GST_TYPE_FIND_MAXIMUM;
3773         goto suggest_audio_mod_caps;
3774       }
3775     }
3776   }
3777   /* AMF */
3778   if ((data = gst_type_find_peek (tf, 0, 19)) != NULL) {
3779     if (memcmp (data, "ASYLUM Music Format", 19) == 0) {
3780       mod_type = "asylum-amf";
3781       probability = GST_TYPE_FIND_MAXIMUM;
3782       goto suggest_audio_mod_caps;
3783     }
3784   }
3785
3786 suggest_audio_mod_caps:
3787   if (mod_type != NULL) {
3788     GstCaps *caps = gst_caps_new_simple ("audio/x-mod",
3789         "type", G_TYPE_STRING, mod_type, NULL);
3790
3791     gst_type_find_suggest (tf, probability, caps);
3792     gst_caps_unref (caps);
3793   }
3794 }
3795
3796 /*** application/x-shockwave-flash ***/
3797
3798 static GstStaticCaps swf_caps =
3799 GST_STATIC_CAPS ("application/x-shockwave-flash");
3800 #define SWF_CAPS (gst_static_caps_get(&swf_caps))
3801 static void
3802 swf_type_find (GstTypeFind * tf, gpointer unused)
3803 {
3804   const guint8 *data = gst_type_find_peek (tf, 0, 4);
3805
3806   if (data && (data[0] == 'F' || data[0] == 'C') &&
3807       data[1] == 'W' && data[2] == 'S') {
3808     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SWF_CAPS);
3809   }
3810 }
3811 #endif
3812 /*** application/vnd.ms-sstr+xml ***/
3813
3814 static void
3815 mss_manifest_load_utf16 (gunichar2 * utf16_ne, const guint8 * utf16_data,
3816     gsize data_size, guint data_endianness)
3817 {
3818   memcpy (utf16_ne, utf16_data, data_size);
3819   if (data_endianness != G_BYTE_ORDER) {
3820     guint i;
3821
3822     for (i = 0; i < data_size / 2; ++i)
3823       utf16_ne[i] = GUINT16_SWAP_LE_BE (utf16_ne[i]);
3824   }
3825 }
3826
3827 static GstStaticCaps mss_manifest_caps =
3828 GST_STATIC_CAPS ("application/vnd.ms-sstr+xml");
3829 #define MSS_MANIFEST_CAPS (gst_static_caps_get(&mss_manifest_caps))
3830 static void
3831 mss_manifest_type_find (GstTypeFind * tf, gpointer unused)
3832 {
3833   gunichar2 utf16_ne[512];
3834   const guint8 *data;
3835   guint data_endianness = 0;
3836   glong n_read = 0, size = 0;
3837   guint length;
3838   gchar *utf8;
3839   gboolean utf8_bom_detected = FALSE;
3840
3841   if (xml_check_first_element (tf, "SmoothStreamingMedia", 20, TRUE)) {
3842     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3843     return;
3844   }
3845
3846   length = gst_type_find_get_length (tf);
3847
3848   /* try detecting the charset */
3849   data = gst_type_find_peek (tf, 0, 3);
3850
3851   if (data == NULL)
3852     return;
3853
3854   /* look for a possible BOM */
3855   if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
3856     utf8_bom_detected = TRUE;
3857   else if (data[0] == 0xFF && data[1] == 0xFE)
3858     data_endianness = G_LITTLE_ENDIAN;
3859   else if (data[0] == 0xFE && data[1] == 0xFF)
3860     data_endianness = G_BIG_ENDIAN;
3861   else
3862     return;
3863
3864   /* try a default that should be enough */
3865   if (length == 0)
3866     length = 512;
3867   else if (length < 64)
3868     return;
3869   else                          /* the first few bytes should be enough */
3870     length = MIN (1024, length);
3871
3872   data = gst_type_find_peek (tf, 0, length);
3873
3874   if (data == NULL)
3875     return;
3876
3877   /* skip the BOM */
3878   data += 2;
3879   length -= 2;
3880
3881   if (utf8_bom_detected) {
3882     /* skip last byte of the BOM */
3883     data++;
3884     length--;
3885
3886     if (xml_check_first_element_from_data (data, length,
3887             "SmoothStreamingMedia", 20, TRUE))
3888       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3889   } else {
3890     length = GST_ROUND_DOWN_2 (length);
3891
3892     /* convert to native endian UTF-16 */
3893     mss_manifest_load_utf16 (utf16_ne, data, length, data_endianness);
3894
3895     /* and now convert to UTF-8 */
3896     utf8 = g_utf16_to_utf8 (utf16_ne, length / 2, &n_read, &size, NULL);
3897     if (utf8 != NULL && n_read > 0) {
3898       if (xml_check_first_element_from_data ((const guint8 *) utf8, size,
3899               "SmoothStreamingMedia", 20, TRUE))
3900         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MSS_MANIFEST_CAPS);
3901     }
3902     g_free (utf8);
3903   }
3904 }
3905
3906 /*** image/jpeg ***/
3907
3908 #define JPEG_MARKER_IS_START_OF_FRAME(x) \
3909     ((x)>=0xc0 && (x) <= 0xcf && (x)!=0xc4 && (x)!=0xc8 && (x)!=0xcc)
3910
3911 static GstStaticCaps jpeg_caps = GST_STATIC_CAPS ("image/jpeg");
3912
3913 #define JPEG_CAPS (gst_static_caps_get(&jpeg_caps))
3914 static void
3915 jpeg_type_find (GstTypeFind * tf, gpointer unused)
3916 {
3917   GstTypeFindProbability prob = GST_TYPE_FIND_POSSIBLE;
3918   DataScanCtx c = { 0, NULL, 0 };
3919   GstCaps *caps;
3920   guint num_markers;
3921
3922   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 2)))
3923     return;
3924
3925   if (c.data[0] != 0xff || c.data[1] != 0xd8)
3926     return;
3927
3928   num_markers = 1;
3929   data_scan_ctx_advance (tf, &c, 2);
3930
3931   caps = gst_caps_copy (JPEG_CAPS);
3932
3933   while (data_scan_ctx_ensure_data (tf, &c, 4) && c.offset < (200 * 1024)) {
3934     guint16 len;
3935     guint8 marker;
3936
3937     if (c.data[0] != 0xff)
3938       break;
3939
3940     marker = c.data[1];
3941     if (G_UNLIKELY (marker == 0xff)) {
3942       data_scan_ctx_advance (tf, &c, 1);
3943       continue;
3944     }
3945
3946     data_scan_ctx_advance (tf, &c, 2);
3947
3948     /* we assume all markers we'll see before SOF have a payload length; if
3949      * that's not the case we'll just detect a false sync and bail out, but
3950      * still report POSSIBLE probability */
3951     len = GST_READ_UINT16_BE (c.data);
3952
3953     GST_LOG ("possible JPEG marker 0x%02x (@0x%04x), segment length %u",
3954         marker, (guint) c.offset, len);
3955
3956     if (!data_scan_ctx_ensure_data (tf, &c, len))
3957       break;
3958
3959     if (marker == 0xc4 ||       /* DEFINE_HUFFMAN_TABLES          */
3960         marker == 0xcc ||       /* DEFINE_ARITHMETIC_CONDITIONING */
3961         marker == 0xdb ||       /* DEFINE_QUANTIZATION_TABLES     */
3962         marker == 0xdd ||       /* DEFINE_RESTART_INTERVAL        */
3963         marker == 0xfe) {       /* COMMENT                        */
3964       data_scan_ctx_advance (tf, &c, len);
3965       ++num_markers;
3966     } else if (marker == 0xe0 && len >= (2 + 4) &&      /* APP0 */
3967         data_scan_ctx_memcmp (tf, &c, 2, "JFIF", 4)) {
3968       GST_LOG ("found JFIF tag");
3969       prob = GST_TYPE_FIND_MAXIMUM;
3970       data_scan_ctx_advance (tf, &c, len);
3971       ++num_markers;
3972       /* we continue until we find a start of frame marker */
3973     } else if (marker == 0xe1 && len >= (2 + 4) &&      /* APP1 */
3974         data_scan_ctx_memcmp (tf, &c, 2, "Exif", 4)) {
3975       GST_LOG ("found Exif tag");
3976       prob = GST_TYPE_FIND_MAXIMUM;
3977       data_scan_ctx_advance (tf, &c, len);
3978       ++num_markers;
3979       /* we continue until we find a start of frame marker */
3980     } else if (marker >= 0xe0 && marker <= 0xef) {      /* APPn */
3981       data_scan_ctx_advance (tf, &c, len);
3982       ++num_markers;
3983     } else if (JPEG_MARKER_IS_START_OF_FRAME (marker) && len >= (2 + 8)) {
3984       int h, w;
3985
3986       h = GST_READ_UINT16_BE (c.data + 2 + 1);
3987       w = GST_READ_UINT16_BE (c.data + 2 + 1 + 2);
3988       if (h == 0 || w == 0) {
3989         GST_WARNING ("bad width %u and/or height %u in SOF header", w, h);
3990         break;
3991       }
3992
3993       GST_LOG ("SOF at offset %" G_GUINT64_FORMAT ", num_markers=%d, "
3994           "WxH=%dx%d", c.offset - 2, num_markers, w, h);
3995
3996       if (num_markers >= 5 || prob == GST_TYPE_FIND_MAXIMUM)
3997         prob = GST_TYPE_FIND_MAXIMUM;
3998       else
3999         prob = GST_TYPE_FIND_LIKELY;
4000
4001       gst_caps_set_simple (caps, "width", G_TYPE_INT, w,
4002           "height", G_TYPE_INT, h, "sof-marker", G_TYPE_INT, marker & 0xf,
4003           NULL);
4004
4005       break;
4006     } else {
4007       GST_WARNING ("bad length or unexpected JPEG marker 0xff 0x%02x", marker);
4008       break;
4009     }
4010   }
4011
4012   gst_type_find_suggest (tf, prob, caps);
4013   gst_caps_unref (caps);
4014 }
4015
4016 /*** image/bmp ***/
4017
4018 static GstStaticCaps bmp_caps = GST_STATIC_CAPS ("image/bmp");
4019
4020 #define BMP_CAPS (gst_static_caps_get(&bmp_caps))
4021 static void
4022 bmp_type_find (GstTypeFind * tf, gpointer unused)
4023 {
4024   DataScanCtx c = { 0, NULL, 0 };
4025   guint32 struct_size, w, h, planes, bpp;
4026
4027   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 54)))
4028     return;
4029
4030   if (c.data[0] != 'B' || c.data[1] != 'M')
4031     return;
4032
4033   /* skip marker + size */
4034   data_scan_ctx_advance (tf, &c, 2 + 4);
4035
4036   /* reserved, must be 0 */
4037   if (c.data[0] != 0 || c.data[1] != 0 || c.data[2] != 0 || c.data[3] != 0)
4038     return;
4039
4040   data_scan_ctx_advance (tf, &c, 2 + 2);
4041
4042   /* offset to start of image data in bytes (check for sanity) */
4043   GST_LOG ("offset=%u", GST_READ_UINT32_LE (c.data));
4044   if (GST_READ_UINT32_LE (c.data) > (10 * 1024 * 1024))
4045     return;
4046
4047   struct_size = GST_READ_UINT32_LE (c.data + 4);
4048   GST_LOG ("struct_size=%u", struct_size);
4049
4050   data_scan_ctx_advance (tf, &c, 4 + 4);
4051
4052   if (struct_size == 0x0C) {
4053     w = GST_READ_UINT16_LE (c.data);
4054     h = GST_READ_UINT16_LE (c.data + 2);
4055     planes = GST_READ_UINT16_LE (c.data + 2 + 2);
4056     bpp = GST_READ_UINT16_LE (c.data + 2 + 2 + 2);
4057   } else if (struct_size == 40 || struct_size == 64 || struct_size == 108
4058       || struct_size == 124 || struct_size == 0xF0) {
4059     w = GST_READ_UINT32_LE (c.data);
4060     h = GST_READ_UINT32_LE (c.data + 4);
4061     planes = GST_READ_UINT16_LE (c.data + 4 + 4);
4062     bpp = GST_READ_UINT16_LE (c.data + 4 + 4 + 2);
4063   } else {
4064     return;
4065   }
4066
4067   /* image sizes sanity check */
4068   GST_LOG ("w=%u, h=%u, planes=%u, bpp=%u", w, h, planes, bpp);
4069   if (w == 0 || w > 0xfffff || h == 0 || h > 0xfffff || planes != 1 ||
4070       (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32))
4071     return;
4072
4073   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "image/bmp",
4074       "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, "bpp", G_TYPE_INT, bpp,
4075       NULL);
4076 }
4077
4078 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
4079 /*** image/tiff ***/
4080 static GstStaticCaps tiff_caps = GST_STATIC_CAPS ("image/tiff, "
4081     "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }");
4082 #define TIFF_CAPS (gst_static_caps_get(&tiff_caps))
4083 static GstStaticCaps tiff_be_caps = GST_STATIC_CAPS ("image/tiff, "
4084     "endianness = (int) BIG_ENDIAN");
4085 #define TIFF_BE_CAPS (gst_static_caps_get(&tiff_be_caps))
4086 static GstStaticCaps tiff_le_caps = GST_STATIC_CAPS ("image/tiff, "
4087     "endianness = (int) LITTLE_ENDIAN");
4088 #define TIFF_LE_CAPS (gst_static_caps_get(&tiff_le_caps))
4089 static void
4090 tiff_type_find (GstTypeFind * tf, gpointer ununsed)
4091 {
4092   const guint8 *data = gst_type_find_peek (tf, 0, 8);
4093   guint8 le_header[4] = { 0x49, 0x49, 0x2A, 0x00 };
4094   guint8 be_header[4] = { 0x4D, 0x4D, 0x00, 0x2A };
4095
4096   if (data) {
4097     if (memcmp (data, le_header, 4) == 0) {
4098       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_LE_CAPS);
4099     } else if (memcmp (data, be_header, 4) == 0) {
4100       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TIFF_BE_CAPS);
4101     }
4102   }
4103 }
4104 #endif
4105
4106 /*** image/x-exr ***/
4107 static GstStaticCaps exr_caps = GST_STATIC_CAPS ("image/x-exr");
4108 #define EXR_CAPS (gst_static_caps_get(&exr_caps))
4109 static void
4110 exr_type_find (GstTypeFind * tf, gpointer ununsed)
4111 {
4112   const guint8 *data = gst_type_find_peek (tf, 0, 8);
4113
4114   if (data) {
4115     guint32 flags;
4116
4117     if (GST_READ_UINT32_LE (data) != 0x01312f76)
4118       return;
4119
4120     flags = GST_READ_UINT32_LE (data + 4);
4121     if ((flags & 0xff) != 1 && (flags & 0xff) != 2)
4122       return;
4123
4124     /* If bit 9 is set, bit 11 and 12 must be 0 */
4125     if ((flags & 0x200) && (flags & 0x1800))
4126       return;
4127
4128     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, EXR_CAPS);
4129   }
4130 }
4131
4132
4133 /*** PNM ***/
4134
4135 static GstStaticCaps pnm_caps = GST_STATIC_CAPS ("image/x-portable-bitmap; "
4136     "image/x-portable-graymap; image/x-portable-pixmap; "
4137     "image/x-portable-anymap");
4138
4139 #define PNM_CAPS (gst_static_caps_get(&pnm_caps))
4140
4141 #define IS_PNM_WHITESPACE(c) \
4142     ((c) == ' ' || (c) == '\r' || (c) == '\n' || (c) == 't')
4143
4144 static void
4145 pnm_type_find (GstTypeFind * tf, gpointer ununsed)
4146 {
4147   const gchar *media_type = NULL;
4148   DataScanCtx c = { 0, NULL, 0 };
4149   guint h = 0, w = 0;
4150
4151   if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 16)))
4152     return;
4153
4154   /* see http://en.wikipedia.org/wiki/Netpbm_format */
4155   if (c.data[0] != 'P' || c.data[1] < '1' || c.data[1] > '7' ||
4156       !IS_PNM_WHITESPACE (c.data[2]) ||
4157       (c.data[3] != '#' && c.data[3] < '0' && c.data[3] > '9'))
4158     return;
4159
4160   switch (c.data[1]) {
4161     case '1':
4162       media_type = "image/x-portable-bitmap";   /* ASCII */
4163       break;
4164     case '2':
4165       media_type = "image/x-portable-graymap";  /* ASCII */
4166       break;
4167     case '3':
4168       media_type = "image/x-portable-pixmap";   /* ASCII */
4169       break;
4170     case '4':
4171       media_type = "image/x-portable-bitmap";   /* Raw */
4172       break;
4173     case '5':
4174       media_type = "image/x-portable-graymap";  /* Raw */
4175       break;
4176     case '6':
4177       media_type = "image/x-portable-pixmap";   /* Raw */
4178       break;
4179     case '7':
4180       media_type = "image/x-portable-anymap";
4181       break;
4182     default:
4183       g_return_if_reached ();
4184   }
4185
4186   /* try to extract width and height as well */
4187   if (c.data[1] != '7') {
4188     gchar s[64] = { 0, }
4189     , sep1, sep2;
4190
4191     /* need to skip any comment lines first */
4192     data_scan_ctx_advance (tf, &c, 3);
4193
4194     if (!data_scan_ctx_ensure_data (tf, &c, 1))
4195       return;
4196
4197     while (c.data[0] == '#') {  /* we know there's still data left */
4198       data_scan_ctx_advance (tf, &c, 1);
4199       if (!data_scan_ctx_ensure_data (tf, &c, 1))
4200         return;
4201
4202       while (c.data[0] != '\n' && c.data[0] != '\r') {
4203         data_scan_ctx_advance (tf, &c, 1);
4204         if (!data_scan_ctx_ensure_data (tf, &c, 1))
4205           return;
4206       }
4207       data_scan_ctx_advance (tf, &c, 1);
4208       GST_LOG ("skipped comment line in PNM header");
4209       if (!data_scan_ctx_ensure_data (tf, &c, 1))
4210         return;
4211     }
4212
4213     if (!data_scan_ctx_ensure_data (tf, &c, 32) &&
4214         !data_scan_ctx_ensure_data (tf, &c, 4)) {
4215       return;
4216     }
4217
4218     /* need to NUL-terminate data for sscanf */
4219     memcpy (s, c.data, MIN (sizeof (s) - 1, c.size));
4220     if (sscanf (s, "%u%c%u%c", &w, &sep1, &h, &sep2) == 4 &&
4221         IS_PNM_WHITESPACE (sep1) && IS_PNM_WHITESPACE (sep2) &&
4222         w > 0 && w < G_MAXINT && h > 0 && h < G_MAXINT) {
4223       GST_LOG ("extracted PNM width and height: %dx%d", w, h);
4224     } else {
4225       w = 0;
4226       h = 0;
4227     }
4228   } else {
4229     /* FIXME: extract width + height for anymaps too */
4230   }
4231
4232   if (w > 0 && h > 0) {
4233     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type,
4234         "width", G_TYPE_INT, w, "height", G_TYPE_INT, h, NULL);
4235   } else {
4236     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, media_type, NULL);
4237   }
4238 }
4239
4240 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
4241 static GstStaticCaps sds_caps = GST_STATIC_CAPS ("audio/x-sds");
4242
4243 #define SDS_CAPS (gst_static_caps_get(&sds_caps))
4244 static void
4245 sds_type_find (GstTypeFind * tf, gpointer ununsed)
4246 {
4247   const guint8 *data = gst_type_find_peek (tf, 0, 4);
4248   guint8 mask[4] = { 0xFF, 0xFF, 0x80, 0xFF };
4249   guint8 match[4] = { 0xF0, 0x7E, 0, 0x01 };
4250   gint x;
4251
4252   if (data) {
4253     for (x = 0; x < 4; x++) {
4254       if ((data[x] & mask[x]) != match[x]) {
4255         return;
4256       }
4257     }
4258     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDS_CAPS);
4259   }
4260 }
4261
4262 static GstStaticCaps ircam_caps = GST_STATIC_CAPS ("audio/x-ircam");
4263
4264 #define IRCAM_CAPS (gst_static_caps_get(&ircam_caps))
4265 static void
4266 ircam_type_find (GstTypeFind * tf, gpointer ununsed)
4267 {
4268   const guint8 *data = gst_type_find_peek (tf, 0, 4);
4269   guint8 mask[4] = { 0xFF, 0xFF, 0xF8, 0xFF };
4270   guint8 match[4] = { 0x64, 0xA3, 0x00, 0x00 };
4271   gint x;
4272   gboolean matched = TRUE;
4273
4274   if (!data) {
4275     return;
4276   }
4277   for (x = 0; x < 4; x++) {
4278     if ((data[x] & mask[x]) != match[x]) {
4279       matched = FALSE;
4280     }
4281   }
4282   if (matched) {
4283     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, IRCAM_CAPS);
4284     return;
4285   }
4286   /* now try the reverse version */
4287   matched = TRUE;
4288   for (x = 0; x < 4; x++) {
4289     if ((data[x] & mask[3 - x]) != match[3 - x]) {
4290       matched = FALSE;
4291     }
4292   }
4293 }
4294 #endif
4295 /*** Matroska/WebM ***/
4296
4297 #define EBML_HEADER           0x1A45DFA3
4298 #define EBML_VERSION          0x4286
4299 #define EBML_DOCTYPE          0x4282
4300 #define EBML_DOCTYPE_VERSION  0x4287
4301 #define MATROSKA_SEGMENT      0x18538067
4302 #define MATROSKA_CLUSTER      0x1F43B675
4303 #define MATROSKA_TRACKS       0x1654AE6B
4304 #define MATROSKA_TRACK_ENTRY  0xAE
4305 #define MATROSKA_TRACK_TYPE   0x83
4306 #define MATROSKA_STEREO_MODE  0x53B8
4307
4308 #define EBML_MAX_LEN (2 * 1024 * 1024)
4309
4310 typedef enum
4311 {
4312   EBML_DOCTYPE_UNKNOWN = 0,
4313   EBML_DOCTYPE_MATROSKA,
4314   EBML_DOCTYPE_WEBM
4315 } GstEbmlDocType;
4316
4317 typedef struct
4318 {
4319   GstEbmlDocType doctype;
4320   guint audio;
4321   guint video;
4322   guint other;
4323   guint video_stereo;
4324   guint chunks;
4325   guint tracks_ok;              /* if we've seen and fully parsed the TRACKS element */
4326 } GstMatroskaInfo;
4327
4328 static inline guint
4329 ebml_read_chunk_header (GstTypeFind * tf, DataScanCtx * c, guint max_size,
4330     guint32 * id, guint64 * size)
4331 {
4332   guint64 mask;
4333   guint msbit_set, i, len, id_len;
4334
4335   if (c->size < 12 || max_size < 1)
4336     return 0;
4337
4338   /* element ID */
4339   *id = c->data[0];
4340   if ((c->data[0] & 0x80) == 0x80) {
4341     id_len = 1;
4342   } else if ((c->data[0] & 0xC0) == 0x40) {
4343     id_len = 2;
4344   } else if ((c->data[0] & 0xE0) == 0x20) {
4345     id_len = 3;
4346   } else if ((c->data[0] & 0xF0) == 0x10) {
4347     id_len = 4;
4348   } else {
4349     return 0;
4350   }
4351
4352   if (max_size < id_len)
4353     return 0;
4354
4355   for (i = 1; i < id_len; ++i) {
4356     *id = (*id << 8) | c->data[i];
4357   }
4358
4359   data_scan_ctx_advance (tf, c, id_len);
4360   max_size -= id_len;
4361
4362   /* size */
4363   if (max_size < 1 || c->data[0] == 0)
4364     return 0;
4365
4366   msbit_set = g_bit_nth_msf (c->data[0], 8);
4367   mask = ((1 << msbit_set) - 1);
4368   *size = c->data[0] & mask;
4369   len = 7 - msbit_set;
4370
4371   if (max_size < 1 + len)
4372     return 0;
4373   for (i = 0; i < len; ++i) {
4374     mask = (mask << 8) | 0xff;
4375     *size = (*size << 8) | c->data[1 + i];
4376   }
4377
4378   data_scan_ctx_advance (tf, c, 1 + len);
4379
4380   /* undefined/unknown size? (all bits 1) */
4381   if (*size == mask) {
4382     /* allow unknown size for SEGMENT chunk, bail out otherwise */
4383     if (*id == MATROSKA_SEGMENT)
4384       *size = G_MAXUINT64;
4385     else
4386       return 0;
4387   }
4388
4389   return id_len + (1 + len);
4390 }
4391
4392 static gboolean
4393 ebml_parse_chunk (GstTypeFind * tf, DataScanCtx * ctx, guint32 chunk_id,
4394     guint chunk_size, GstMatroskaInfo * info, guint depth)
4395 {                               /* FIXME: make sure input size is clipped to 32 bit */
4396   static const gchar SPACES[] = "                ";
4397   DataScanCtx c = *ctx;
4398   guint64 element_size = 0;
4399   guint32 id, hdr_len;
4400
4401   if (depth >= 8)               /* keep SPACES large enough for depth */
4402     return FALSE;
4403
4404   while (chunk_size > 0) {
4405     if (c.offset > EBML_MAX_LEN || !data_scan_ctx_ensure_data (tf, &c, 64))
4406       return FALSE;
4407
4408     hdr_len = ebml_read_chunk_header (tf, &c, chunk_size, &id, &element_size);
4409     if (hdr_len == 0)
4410       return FALSE;
4411
4412     g_assert (hdr_len <= chunk_size);
4413     chunk_size -= hdr_len;
4414
4415     if (element_size > chunk_size)
4416       return FALSE;
4417
4418     GST_DEBUG ("%s %08x, size %" G_GUINT64_FORMAT " / %" G_GUINT64_FORMAT,
4419         SPACES + sizeof (SPACES) - 1 - (2 * depth), id, element_size,
4420         hdr_len + element_size);
4421
4422     if (element_size >= G_MAXUINT32) {
4423       GST_DEBUG ("Chunk too big for typefinding");
4424       return FALSE;
4425     }
4426
4427     if (!data_scan_ctx_ensure_data (tf, &c, element_size)) {
4428       GST_DEBUG ("not enough data");
4429       return FALSE;
4430     }
4431
4432     switch (id) {
4433       case EBML_DOCTYPE:
4434         if (element_size >= 8 && memcmp (c.data, "matroska", 8) == 0)
4435           info->doctype = EBML_DOCTYPE_MATROSKA;
4436         else if (element_size >= 4 && memcmp (c.data, "webm", 4) == 0)
4437           info->doctype = EBML_DOCTYPE_WEBM;
4438         break;
4439       case MATROSKA_SEGMENT:
4440         GST_LOG ("parsing segment");
4441         ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1);
4442         GST_LOG ("parsed segment, done");
4443         return FALSE;
4444       case MATROSKA_TRACKS:
4445         GST_LOG ("parsing tracks");
4446         info->tracks_ok =
4447             ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1);
4448         GST_LOG ("parsed tracks: %s, done (after %" G_GUINT64_FORMAT " bytes)",
4449             info->tracks_ok ? "ok" : "FAIL", c.offset + element_size);
4450         return FALSE;
4451       case MATROSKA_TRACK_ENTRY:
4452         GST_LOG ("parsing track entry");
4453         if (!ebml_parse_chunk (tf, &c, id, element_size, info, depth + 1))
4454           return FALSE;
4455         break;
4456       case MATROSKA_TRACK_TYPE:{
4457         guint type = 0, i;
4458
4459         /* is supposed to always be 1-byte, but not everyone's following that */
4460         for (i = 0; i < element_size; ++i)
4461           type = (type << 8) | c.data[i];
4462
4463         GST_DEBUG ("%s   track type %u",
4464             SPACES + sizeof (SPACES) - 1 - (2 * depth), type);
4465
4466         if (type == 1)
4467           ++info->video;
4468         else if (c.data[0] == 2)
4469           ++info->audio;
4470         else
4471           ++info->other;
4472         break;
4473       }
4474       case MATROSKA_STEREO_MODE:
4475         ++info->video_stereo;
4476         break;
4477       case MATROSKA_CLUSTER:
4478         GST_WARNING ("cluster, bailing out (should've found tracks by now)");
4479         return FALSE;
4480       default:
4481         break;
4482     }
4483     data_scan_ctx_advance (tf, &c, element_size);
4484     chunk_size -= element_size;
4485     ++info->chunks;
4486   }
4487
4488   return TRUE;
4489 }
4490
4491 static GstStaticCaps matroska_caps = GST_STATIC_CAPS ("video/x-matroska");
4492
4493 #define MATROSKA_CAPS (gst_static_caps_get(&matroska_caps))
4494 static void
4495 matroska_type_find (GstTypeFind * tf, gpointer ununsed)
4496 {
4497   GstTypeFindProbability prob;
4498   GstMatroskaInfo info = { 0, };
4499   const gchar *type_name;
4500   DataScanCtx c = { 0, NULL, 0 };
4501   gboolean is_audio;
4502   guint64 size;
4503   guint32 id, hdr_len;
4504
4505   if (!data_scan_ctx_ensure_data (tf, &c, 64))
4506     return;
4507
4508   if (GST_READ_UINT32_BE (c.data) != EBML_HEADER)
4509     return;
4510
4511   while (c.offset < EBML_MAX_LEN && data_scan_ctx_ensure_data (tf, &c, 64)) {
4512     hdr_len = ebml_read_chunk_header (tf, &c, c.size, &id, &size);
4513     if (hdr_len == 0)
4514       return;
4515
4516     GST_INFO ("=== top-level chunk %08x, size %" G_GUINT64_FORMAT
4517         " / %" G_GUINT64_FORMAT, id, size, size + hdr_len);
4518
4519     if (!ebml_parse_chunk (tf, &c, id, size, &info, 0))
4520       break;
4521     data_scan_ctx_advance (tf, &c, size);
4522     GST_INFO ("=== done with chunk %08x", id);
4523     if (id == MATROSKA_SEGMENT)
4524       break;
4525   }
4526
4527   GST_INFO ("audio=%u video=%u other=%u chunks=%u doctype=%d all_tracks=%d",
4528       info.audio, info.video, info.other, info.chunks, info.doctype,
4529       info.tracks_ok);
4530
4531   /* perhaps we should bail out if tracks_ok is FALSE and wait for more data?
4532    * (we would need new API to signal this properly and prevent other
4533    * typefinders from taking over the decision then) */
4534   is_audio = (info.audio > 0 && info.video == 0 && info.other == 0);
4535
4536   if (info.doctype == EBML_DOCTYPE_WEBM) {
4537     type_name = (is_audio) ? "audio/webm" : "video/webm";
4538   } else if (info.video > 0 && info.video_stereo) {
4539     type_name = "video/x-matroska-3d";
4540   } else {
4541     type_name = (is_audio) ? "audio/x-matroska" : "video/x-matroska";
4542   }
4543
4544   if (info.doctype == EBML_DOCTYPE_UNKNOWN)
4545     prob = GST_TYPE_FIND_LIKELY;
4546   else
4547     prob = GST_TYPE_FIND_MAXIMUM;
4548
4549   gst_type_find_suggest_simple (tf, prob, type_name, NULL);
4550 }
4551
4552 /*** application/mxf ***/
4553 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
4554 static GstStaticCaps mxf_caps = GST_STATIC_CAPS ("application/mxf");
4555
4556 #define MXF_MAX_PROBE_LENGTH (1024 * 64)
4557 #define MXF_CAPS (gst_static_caps_get(&mxf_caps))
4558
4559 /*
4560  * MXF files start with a header partition pack key of 16 bytes which is defined
4561  * at SMPTE-377M 6.1. Before this there can be up to 64K of run-in which _must_
4562  * not contain the partition pack key.
4563  */
4564 static void
4565 mxf_type_find (GstTypeFind * tf, gpointer ununsed)
4566 {
4567   static const guint8 partition_pack_key[] =
4568       { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01,
4569     0x01
4570   };
4571   DataScanCtx c = { 0, NULL, 0 };
4572
4573   while (c.offset <= MXF_MAX_PROBE_LENGTH) {
4574     guint i;
4575     if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 1024)))
4576       break;
4577
4578     /* look over in chunks of 1kbytes to avoid too much overhead */
4579
4580     for (i = 0; i < 1024 - 16; i++) {
4581       /* Check first byte before calling more expensive memcmp function */
4582       if (G_UNLIKELY (c.data[i] == 0x06
4583               && memcmp (c.data + i, partition_pack_key, 13) == 0)) {
4584         /* Header partition pack? */
4585         if (c.data[i + 13] != 0x02)
4586           goto advance;
4587
4588         /* Partition status */
4589         if (c.data[i + 14] >= 0x05)
4590           goto advance;
4591
4592         /* Reserved, must be 0x00 */
4593         if (c.data[i + 15] != 0x00)
4594           goto advance;
4595
4596         gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXF_CAPS);
4597         return;
4598       }
4599     }
4600
4601   advance:
4602     data_scan_ctx_advance (tf, &c, 1024 - 16);
4603   }
4604 }
4605
4606 /*** video/x-dv ***/
4607
4608 static GstStaticCaps dv_caps = GST_STATIC_CAPS ("video/x-dv, "
4609     "systemstream = (boolean) true");
4610 #define DV_CAPS (gst_static_caps_get(&dv_caps))
4611 static void
4612 dv_type_find (GstTypeFind * tf, gpointer private)
4613 {
4614   const guint8 *data;
4615
4616   data = gst_type_find_peek (tf, 0, 5);
4617
4618   /* check for DIF  and DV flag */
4619   if (data && (data[0] == 0x1f) && (data[1] == 0x07) && (data[2] == 0x00)) {
4620     const gchar *format;
4621
4622     if (data[3] & 0x80) {
4623       format = "PAL";
4624     } else {
4625       format = "NTSC";
4626     }
4627
4628     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, "video/x-dv",
4629         "systemstream", G_TYPE_BOOLEAN, TRUE,
4630         "format", G_TYPE_STRING, format, NULL);
4631   }
4632 }
4633 #endif
4634
4635 /*** Ogg variants ***/
4636 static GstStaticCaps ogg_caps =
4637     GST_STATIC_CAPS ("application/ogg;video/ogg;audio/ogg;application/kate");
4638
4639 #define OGG_CAPS (gst_static_caps_get(&ogg_caps))
4640
4641 typedef enum
4642 {
4643   OGG_AUDIO = 0,
4644   OGG_VIDEO,
4645   OGG_KATE,
4646   OGG_OTHER,
4647   OGG_SKELETON,
4648   OGG_ANNODEX,
4649   OGG_NUM
4650 } GstOggStreamType;
4651
4652 static void
4653 ogganx_type_find (GstTypeFind * tf, gpointer private)
4654 {
4655   const gchar *media_type;
4656   DataScanCtx c = { 0, NULL, 0 };
4657   guint ogg_syncs = 0;
4658   guint hdr_count[OGG_NUM] = { 0, };
4659   static const struct
4660   {
4661     const gchar marker[10];
4662     guint8 marker_size;
4663     GstOggStreamType stream_type;
4664   } markers[] = {
4665     {
4666     "\001vorbis", 7, OGG_AUDIO}, {
4667     "\200theora", 7, OGG_VIDEO}, {
4668     "fLaC", 4, OGG_AUDIO}, {
4669     "\177FLAC", 5, OGG_AUDIO}, {
4670     "Speex", 5, OGG_AUDIO}, {
4671     "CMML\0\0\0\0", 8, OGG_OTHER}, {
4672     "PCM     ", 8, OGG_AUDIO}, {
4673     "Annodex", 7, OGG_ANNODEX}, {
4674     "fishead", 7, OGG_SKELETON}, {
4675     "AnxData", 7, OGG_ANNODEX}, {
4676     "CELT    ", 8, OGG_AUDIO}, {
4677     "\200kate\0\0\0", 8, OGG_KATE}, {
4678     "BBCD\0", 5, OGG_VIDEO}, {
4679     "OVP80\1\1", 7, OGG_VIDEO}, {
4680     "OpusHead", 8, OGG_AUDIO}, {
4681     "\001audio\0\0\0", 9, OGG_AUDIO}, {
4682     "\001video\0\0\0", 9, OGG_VIDEO}, {
4683     "\001text\0\0\0", 9, OGG_OTHER}
4684   };
4685
4686   while (c.offset < 4096 && data_scan_ctx_ensure_data (tf, &c, 64)) {
4687     guint size, i;
4688
4689     if (memcmp (c.data, "OggS", 5) != 0)
4690       break;
4691
4692     ++ogg_syncs;
4693
4694     /* check if BOS */
4695     if (c.data[5] != 0x02)
4696       break;
4697
4698     /* headers should only have one segment */
4699     if (c.data[26] != 1)
4700       break;
4701
4702     size = c.data[27];
4703     if (size < 8)
4704       break;
4705
4706     data_scan_ctx_advance (tf, &c, 28);
4707
4708     if (!data_scan_ctx_ensure_data (tf, &c, MAX (size, 8)))
4709       break;
4710
4711     for (i = 0; i < G_N_ELEMENTS (markers); ++i) {
4712       if (memcmp (c.data, markers[i].marker, markers[i].marker_size) == 0) {
4713         ++hdr_count[markers[i].stream_type];
4714         break;
4715       }
4716     }
4717
4718     if (i == G_N_ELEMENTS (markers)) {
4719       GST_MEMDUMP ("unknown Ogg stream marker", c.data, size);
4720       ++hdr_count[OGG_OTHER];
4721     }
4722
4723     data_scan_ctx_advance (tf, &c, size);
4724   }
4725
4726   if (ogg_syncs == 0)
4727     return;
4728
4729   /* We don't bother with annodex types. FIXME: what about XSPF? */
4730   if (hdr_count[OGG_VIDEO] > 0) {
4731     media_type = "video/ogg";
4732   } else if (hdr_count[OGG_AUDIO] > 0) {
4733     media_type = "audio/ogg";
4734   } else if (hdr_count[OGG_KATE] > 0 && hdr_count[OGG_OTHER] == 0) {
4735     media_type = "application/kate";
4736   } else {
4737     media_type = "application/ogg";
4738   }
4739
4740   GST_INFO ("found %s (audio:%u, video:%u, annodex:%u, skeleton:%u, other:%u)",
4741       media_type, hdr_count[OGG_AUDIO], hdr_count[OGG_VIDEO],
4742       hdr_count[OGG_ANNODEX], hdr_count[OGG_SKELETON], hdr_count[OGG_OTHER]);
4743
4744   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM, media_type, NULL);
4745 }
4746
4747 /*** audio/x-vorbis ***/
4748 static GstStaticCaps vorbis_caps = GST_STATIC_CAPS ("audio/x-vorbis");
4749
4750 #define VORBIS_CAPS (gst_static_caps_get(&vorbis_caps))
4751 static void
4752 vorbis_type_find (GstTypeFind * tf, gpointer private)
4753 {
4754   const guint8 *data = gst_type_find_peek (tf, 0, 30);
4755
4756   if (data) {
4757     guint blocksize_0;
4758     guint blocksize_1;
4759
4760     /* 1 byte packet type (identification=0x01)
4761        6 byte string "vorbis"
4762        4 byte vorbis version */
4763     if (memcmp (data, "\001vorbis\000\000\000\000", 11) != 0)
4764       return;
4765     data += 11;
4766     /* 1 byte channels must be != 0 */
4767     if (data[0] == 0)
4768       return;
4769     data++;
4770     /* 4 byte samplerate must be != 0 */
4771     if (GST_READ_UINT32_LE (data) == 0)
4772       return;
4773     data += 16;
4774     /* blocksize checks */
4775     blocksize_0 = data[0] & 0x0F;
4776     blocksize_1 = (data[0] & 0xF0) >> 4;
4777     if (blocksize_0 > blocksize_1)
4778       return;
4779     if (blocksize_0 < 6 || blocksize_0 > 13)
4780       return;
4781     if (blocksize_1 < 6 || blocksize_1 > 13)
4782       return;
4783     data++;
4784     /* framing bit */
4785     if ((data[0] & 0x01) != 1)
4786       return;
4787     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VORBIS_CAPS);
4788   }
4789 }
4790
4791 /*** video/x-theora ***/
4792
4793 static GstStaticCaps theora_caps = GST_STATIC_CAPS ("video/x-theora");
4794
4795 #define THEORA_CAPS (gst_static_caps_get(&theora_caps))
4796 static void
4797 theora_type_find (GstTypeFind * tf, gpointer private)
4798 {
4799   const guint8 *data = gst_type_find_peek (tf, 0, 7);   //42);
4800
4801   if (data) {
4802     if (data[0] != 0x80)
4803       return;
4804     if (memcmp (&data[1], "theora", 6) != 0)
4805       return;
4806     /* FIXME: make this more reliable when specs are out */
4807
4808     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, THEORA_CAPS);
4809   }
4810 }
4811
4812 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
4813 /*** kate ***/
4814 static void
4815 kate_type_find (GstTypeFind * tf, gpointer private)
4816 {
4817   const guint8 *data = gst_type_find_peek (tf, 0, 64);
4818   gchar category[16] = { 0, };
4819
4820   if (G_UNLIKELY (data == NULL))
4821     return;
4822
4823   /* see: http://wiki.xiph.org/index.php/OggKate#Format_specification */
4824   if (G_LIKELY (memcmp (data, "\200kate\0\0\0", 8) != 0))
4825     return;
4826
4827   /* make sure we always have a NUL-terminated string */
4828   memcpy (category, data + 48, 15);
4829   GST_LOG ("kate category: %s", category);
4830   /* canonical categories for subtitles: subtitles, spu-subtitles, SUB, K-SPU */
4831   if (strcmp (category, "subtitles") == 0 || strcmp (category, "SUB") == 0 ||
4832       strcmp (category, "spu-subtitles") == 0 ||
4833       strcmp (category, "K-SPU") == 0) {
4834     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
4835         "subtitle/x-kate", NULL);
4836   } else {
4837     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
4838         "application/x-kate", NULL);
4839   }
4840 }
4841 #endif
4842
4843 /*** WEBVTTT subtitles ***/
4844 static GstStaticCaps webvtt_caps =
4845 GST_STATIC_CAPS ("application/x-subtitle-vtt, parsed=(boolean)false");
4846 #define WEBVTT_CAPS (gst_static_caps_get(&webvtt_caps))
4847
4848 static void
4849 webvtt_type_find (GstTypeFind * tf, gpointer private)
4850 {
4851   const guint8 *data;
4852
4853   data = gst_type_find_peek (tf, 0, 10);
4854
4855   if (data == NULL)
4856     return;
4857
4858   /* there might be a UTF-8 BOM at the beginning */
4859   if (memcmp (data, "WEBVTT", 6) != 0 && memcmp (data + 3, "WEBVTT", 6) != 0) {
4860     return;
4861   }
4862
4863   if (data[0] != 'W') {
4864     if (data[0] != 0xef || data[1] != 0xbb || data[2] != 0xbf)
4865       return;                   /* Not a UTF-8 BOM */
4866     data += 3;
4867   }
4868
4869   /* After the WEBVTT magic must be one of these chars:
4870    *   0x20 (space), 0x9 (tab), 0xa (LF) or 0xd (CR) */
4871   if (data[6] != 0x20 && data[6] != 0x9 && data[6] != 0xa && data[6] != 0xd) {
4872     return;
4873   }
4874
4875   gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, WEBVTT_CAPS);
4876 }
4877
4878 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
4879 /*** application/x-ogm-video or audio***/
4880
4881 static GstStaticCaps ogmvideo_caps =
4882 GST_STATIC_CAPS ("application/x-ogm-video");
4883 #define OGMVIDEO_CAPS (gst_static_caps_get(&ogmvideo_caps))
4884 static void
4885 ogmvideo_type_find (GstTypeFind * tf, gpointer private)
4886 {
4887   const guint8 *data = gst_type_find_peek (tf, 0, 9);
4888
4889   if (data) {
4890     if (memcmp (data, "\001video\000\000\000", 9) != 0)
4891       return;
4892     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMVIDEO_CAPS);
4893   }
4894 }
4895
4896 static GstStaticCaps ogmaudio_caps =
4897 GST_STATIC_CAPS ("application/x-ogm-audio");
4898 #define OGMAUDIO_CAPS (gst_static_caps_get(&ogmaudio_caps))
4899 static void
4900 ogmaudio_type_find (GstTypeFind * tf, gpointer private)
4901 {
4902   const guint8 *data = gst_type_find_peek (tf, 0, 9);
4903
4904   if (data) {
4905     if (memcmp (data, "\001audio\000\000\000", 9) != 0)
4906       return;
4907     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMAUDIO_CAPS);
4908   }
4909 }
4910
4911 static GstStaticCaps ogmtext_caps = GST_STATIC_CAPS ("application/x-ogm-text");
4912
4913 #define OGMTEXT_CAPS (gst_static_caps_get(&ogmtext_caps))
4914 static void
4915 ogmtext_type_find (GstTypeFind * tf, gpointer private)
4916 {
4917   const guint8 *data = gst_type_find_peek (tf, 0, 9);
4918
4919   if (data) {
4920     if (memcmp (data, "\001text\000\000\000\000", 9) != 0)
4921       return;
4922     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGMTEXT_CAPS);
4923   }
4924 }
4925
4926 /*** audio/x-speex ***/
4927
4928 static GstStaticCaps speex_caps = GST_STATIC_CAPS ("audio/x-speex");
4929
4930 #define SPEEX_CAPS (gst_static_caps_get(&speex_caps))
4931 static void
4932 speex_type_find (GstTypeFind * tf, gpointer private)
4933 {
4934   const guint8 *data = gst_type_find_peek (tf, 0, 80);
4935
4936   if (data) {
4937     /* 8 byte string "Speex   "
4938        24 byte speex version string + int */
4939     if (memcmp (data, "Speex   ", 8) != 0)
4940       return;
4941     data += 32;
4942
4943     /* 4 byte header size >= 80 */
4944     if (GST_READ_UINT32_LE (data) < 80)
4945       return;
4946     data += 4;
4947
4948     /* 4 byte sample rate <= 48000 */
4949     if (GST_READ_UINT32_LE (data) > 48000)
4950       return;
4951     data += 4;
4952
4953     /* currently there are only 3 speex modes. */
4954     if (GST_READ_UINT32_LE (data) > 3)
4955       return;
4956     data += 12;
4957
4958     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SPEEX_CAPS);
4959   }
4960 }
4961
4962 /*** audio/x-celt ***/
4963
4964 static GstStaticCaps celt_caps = GST_STATIC_CAPS ("audio/x-celt");
4965
4966 #define CELT_CAPS (gst_static_caps_get(&celt_caps))
4967 static void
4968 celt_type_find (GstTypeFind * tf, gpointer private)
4969 {
4970   const guint8 *data = gst_type_find_peek (tf, 0, 8);
4971
4972   if (data) {
4973     /* 8 byte string "CELT   " */
4974     if (memcmp (data, "CELT    ", 8) != 0)
4975       return;
4976
4977     /* TODO: Check other values of the CELT header */
4978     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CELT_CAPS);
4979   }
4980 }
4981
4982 /*** application/x-ogg-skeleton ***/
4983 static GstStaticCaps ogg_skeleton_caps =
4984 GST_STATIC_CAPS ("application/x-ogg-skeleton, parsed=(boolean)FALSE");
4985 #define OGG_SKELETON_CAPS (gst_static_caps_get(&ogg_skeleton_caps))
4986 static void
4987 oggskel_type_find (GstTypeFind * tf, gpointer private)
4988 {
4989   const guint8 *data = gst_type_find_peek (tf, 0, 12);
4990
4991   if (data) {
4992     /* 8 byte string "fishead\0" for the ogg skeleton stream */
4993     if (memcmp (data, "fishead\0", 8) != 0)
4994       return;
4995     data += 8;
4996
4997     /* Require that the header contains version 3.0 */
4998     if (GST_READ_UINT16_LE (data) != 3)
4999       return;
5000     data += 2;
5001     if (GST_READ_UINT16_LE (data) != 0)
5002       return;
5003
5004     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OGG_SKELETON_CAPS);
5005   }
5006 }
5007
5008 static GstStaticCaps cmml_caps = GST_STATIC_CAPS ("text/x-cmml");
5009
5010 #define CMML_CAPS (gst_static_caps_get(&cmml_caps))
5011 static void
5012 cmml_type_find (GstTypeFind * tf, gpointer private)
5013 {
5014   /* Header is 12 bytes minimum (though we don't check the minor version */
5015   const guint8 *data = gst_type_find_peek (tf, 0, 12);
5016
5017   if (data) {
5018
5019     /* 8 byte string "CMML\0\0\0\0" for the magic number */
5020     if (memcmp (data, "CMML\0\0\0\0", 8) != 0)
5021       return;
5022     data += 8;
5023
5024     /* Require that the header contains at least version 2.0 */
5025     if (GST_READ_UINT16_LE (data) < 2)
5026       return;
5027
5028     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, CMML_CAPS);
5029   }
5030 }
5031
5032 /*** application/x-tar ***/
5033
5034 static GstStaticCaps tar_caps = GST_STATIC_CAPS ("application/x-tar");
5035
5036 #define TAR_CAPS (gst_static_caps_get(&tar_caps))
5037 #define OLDGNU_MAGIC "ustar  "  /* 7 chars and a NUL */
5038 #define NEWGNU_MAGIC "ustar"    /* 5 chars and a NUL */
5039 static void
5040 tar_type_find (GstTypeFind * tf, gpointer unused)
5041 {
5042   const guint8 *data = gst_type_find_peek (tf, 257, 8);
5043
5044   /* of course we are not certain, but we don't want other typefind funcs
5045    * to detect formats of files within the tar archive, e.g. mp3s */
5046   if (data) {
5047     if (memcmp (data, OLDGNU_MAGIC, 8) == 0) {  /* sic */
5048       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
5049     } else if (memcmp (data, NEWGNU_MAGIC, 6) == 0 &&   /* sic */
5050         g_ascii_isdigit (data[6]) && g_ascii_isdigit (data[7])) {
5051       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, TAR_CAPS);
5052     }
5053   }
5054 }
5055
5056 /*** application/x-ar ***/
5057
5058 static GstStaticCaps ar_caps = GST_STATIC_CAPS ("application/x-ar");
5059
5060 #define AR_CAPS (gst_static_caps_get(&ar_caps))
5061 static void
5062 ar_type_find (GstTypeFind * tf, gpointer unused)
5063 {
5064   const guint8 *data = gst_type_find_peek (tf, 0, 24);
5065
5066   if (data && memcmp (data, "!<arch>", 7) == 0) {
5067     gint i;
5068
5069     for (i = 7; i < 24; ++i) {
5070       if (!g_ascii_isprint (data[i]) && data[i] != '\n') {
5071         gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AR_CAPS);
5072       }
5073     }
5074
5075     gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AR_CAPS);
5076   }
5077 }
5078 #endif
5079
5080 /*** audio/x-au ***/
5081
5082 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
5083  * as it is only possible to register one typefind factory per 'name'
5084  * (which is in this case the caps), and the first one would be replaced by
5085  * the second one. */
5086 static GstStaticCaps au_caps = GST_STATIC_CAPS ("audio/x-au");
5087
5088 #define AU_CAPS (gst_static_caps_get(&au_caps))
5089 static void
5090 au_type_find (GstTypeFind * tf, gpointer unused)
5091 {
5092   const guint8 *data = gst_type_find_peek (tf, 0, 4);
5093
5094   if (data) {
5095     if (memcmp (data, ".snd", 4) == 0 || memcmp (data, "dns.", 4) == 0) {
5096       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, AU_CAPS);
5097     }
5098   }
5099 }
5100
5101
5102 /*** video/x-nuv ***/
5103
5104 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
5105  * as it is only possible to register one typefind factory per 'name'
5106  * (which is in this case the caps), and the first one would be replaced by
5107  * the second one. */
5108 static GstStaticCaps nuv_caps = GST_STATIC_CAPS ("video/x-nuv");
5109
5110 #define NUV_CAPS (gst_static_caps_get(&nuv_caps))
5111 static void
5112 nuv_type_find (GstTypeFind * tf, gpointer unused)
5113 {
5114   const guint8 *data = gst_type_find_peek (tf, 0, 11);
5115
5116   if (data) {
5117     if (memcmp (data, "MythTVVideo", 11) == 0
5118         || memcmp (data, "NuppelVideo", 11) == 0) {
5119       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, NUV_CAPS);
5120     }
5121   }
5122 }
5123
5124 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5125 /*** audio/x-paris ***/
5126 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
5127 static GstStaticCaps paris_caps = GST_STATIC_CAPS ("audio/x-paris");
5128
5129 #define PARIS_CAPS (gst_static_caps_get(&paris_caps))
5130 static void
5131 paris_type_find (GstTypeFind * tf, gpointer unused)
5132 {
5133   const guint8 *data = gst_type_find_peek (tf, 0, 4);
5134
5135   if (data) {
5136     if (memcmp (data, " paf", 4) == 0 || memcmp (data, "fap ", 4) == 0) {
5137       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, PARIS_CAPS);
5138     }
5139   }
5140 }
5141
5142 /*** audio/x-sbc ***/
5143 static GstStaticCaps sbc_caps = GST_STATIC_CAPS ("audio/x-sbc");
5144 #define SBC_CAPS (gst_static_caps_get(&sbc_caps))
5145
5146 static gsize
5147 sbc_check_header (const guint8 * data, gsize len, guint * rate,
5148     guint * channels)
5149 {
5150   static const guint16 sbc_rates[4] = { 16000, 32000, 44100, 48000 };
5151   static const guint8 sbc_blocks[4] = { 4, 8, 12, 16 };
5152   guint n_blocks, ch_mode, n_subbands, bitpool;
5153
5154   if (data[0] != 0x9C || len < 4)
5155     return 0;
5156
5157   n_blocks = sbc_blocks[(data[1] >> 4) & 0x03];
5158   ch_mode = (data[1] >> 2) & 0x03;
5159   n_subbands = (data[1] & 0x01) ? 8 : 4;
5160   bitpool = data[2];
5161   if (bitpool < 2)
5162     return 0;
5163
5164   *rate = sbc_rates[(data[1] >> 6) & 0x03];
5165   *channels = (ch_mode == 0) ? 1 : 2;
5166
5167   if (ch_mode == 0)
5168     return 4 + (n_subbands * 1) / 2 + (n_blocks * 1 * bitpool) / 8;
5169   else if (ch_mode == 1)
5170     return 4 + (n_subbands * 2) / 2 + (n_blocks * 2 * bitpool) / 8;
5171   else if (ch_mode == 2)
5172     return 4 + (n_subbands * 2) / 2 + (n_blocks * bitpool) / 8;
5173   else if (ch_mode == 3)
5174     return 4 + (n_subbands * 2) / 2 + (n_subbands + n_blocks * bitpool) / 8;
5175
5176   return 0;
5177 }
5178
5179 static void
5180 sbc_type_find (GstTypeFind * tf, gpointer unused)
5181 {
5182   const guint8 *data;
5183   gsize frame_len;
5184   guint i, rate, channels, offset = 0;
5185
5186   for (i = 0; i < 10; ++i) {
5187     data = gst_type_find_peek (tf, offset, 8);
5188     if (data == NULL)
5189       return;
5190
5191     frame_len = sbc_check_header (data, 8, &rate, &channels);
5192     if (frame_len == 0)
5193       return;
5194
5195     offset += frame_len;
5196   }
5197   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE, "audio/x-sbc",
5198       "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels,
5199       "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
5200 }
5201
5202 /*** audio/iLBC-sh ***/
5203 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
5204 static GstStaticCaps ilbc_caps = GST_STATIC_CAPS ("audio/iLBC-sh");
5205
5206 #define ILBC_CAPS (gst_static_caps_get(&ilbc_caps))
5207 static void
5208 ilbc_type_find (GstTypeFind * tf, gpointer unused)
5209 {
5210   const guint8 *data = gst_type_find_peek (tf, 0, 8);
5211
5212   if (data) {
5213     if (memcmp (data, "#!iLBC30", 8) == 0 || memcmp (data, "#!iLBC20", 8) == 0) {
5214       gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, ILBC_CAPS);
5215     }
5216   }
5217 }
5218
5219 /*** application/x-ms-dos-executable ***/
5220
5221 static GstStaticCaps msdos_caps =
5222 GST_STATIC_CAPS ("application/x-ms-dos-executable");
5223 #define MSDOS_CAPS (gst_static_caps_get(&msdos_caps))
5224 /* see http://www.madchat.org/vxdevl/papers/winsys/pefile/pefile.htm */
5225 static void
5226 msdos_type_find (GstTypeFind * tf, gpointer unused)
5227 {
5228   const guint8 *data = gst_type_find_peek (tf, 0, 64);
5229
5230   if (data && data[0] == 'M' && data[1] == 'Z' &&
5231       GST_READ_UINT16_LE (data + 8) == 4) {
5232     guint32 pe_offset = GST_READ_UINT32_LE (data + 60);
5233
5234     data = gst_type_find_peek (tf, pe_offset, 2);
5235     if (data && data[0] == 'P' && data[1] == 'E') {
5236       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, MSDOS_CAPS);
5237     }
5238   }
5239 }
5240
5241 /*** application/x-mmsh ***/
5242
5243 static GstStaticCaps mmsh_caps = GST_STATIC_CAPS ("application/x-mmsh");
5244
5245 #define MMSH_CAPS gst_static_caps_get(&mmsh_caps)
5246
5247 /* This is to recognise mssh-over-http */
5248 static void
5249 mmsh_type_find (GstTypeFind * tf, gpointer unused)
5250 {
5251   static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
5252     0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
5253   };
5254
5255   const guint8 *data;
5256
5257   data = gst_type_find_peek (tf, 0, 2 + 2 + 4 + 2 + 2 + 16);
5258   if (data && data[0] == 0x24 && data[1] == 0x48 &&
5259       GST_READ_UINT16_LE (data + 2) > 2 + 2 + 4 + 2 + 2 + 16 &&
5260       memcmp (data + 2 + 2 + 4 + 2 + 2, asf_marker, 16) == 0) {
5261     gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, MMSH_CAPS);
5262   }
5263 }
5264
5265 /*** video/x-dirac ***/
5266
5267 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
5268  * as it is only possible to register one typefind factory per 'name'
5269  * (which is in this case the caps), and the first one would be replaced by
5270  * the second one. */
5271 static GstStaticCaps dirac_caps = GST_STATIC_CAPS ("video/x-dirac");
5272
5273 #define DIRAC_CAPS (gst_static_caps_get(&dirac_caps))
5274 static void
5275 dirac_type_find (GstTypeFind * tf, gpointer unused)
5276 {
5277   const guint8 *data = gst_type_find_peek (tf, 0, 8);
5278
5279   if (data) {
5280     if (memcmp (data, "BBCD", 4) == 0 || memcmp (data, "KW-DIRAC", 8) == 0) {
5281       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DIRAC_CAPS);
5282     }
5283   }
5284 }
5285
5286 /*** audio/x-tap-tap ***/
5287
5288 /* NOTE: we cannot replace this function with TYPE_FIND_REGISTER_START_WITH,
5289  * as it is only possible to register one typefind factory per 'name'
5290  * (which is in this case the caps), and the first one would be replaced by
5291  * the second one. */
5292 static GstStaticCaps tap_caps = GST_STATIC_CAPS ("audio/x-tap-tap");
5293
5294 #define TAP_CAPS (gst_static_caps_get(&tap_caps))
5295 static void
5296 tap_type_find (GstTypeFind * tf, gpointer unused)
5297 {
5298   const guint8 *data = gst_type_find_peek (tf, 0, 16);
5299
5300   if (data) {
5301     if (memcmp (data, "C64-TAPE-RAW", 12) == 0
5302         || memcmp (data, "C16-TAPE-RAW", 12) == 0) {
5303       gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TAP_CAPS);
5304     }
5305   }
5306 }
5307
5308 /*** video/vivo ***/
5309
5310 static GstStaticCaps vivo_caps = GST_STATIC_CAPS ("video/vivo");
5311
5312 #define VIVO_CAPS gst_static_caps_get(&vivo_caps)
5313
5314 static void
5315 vivo_type_find (GstTypeFind * tf, gpointer unused)
5316 {
5317   static const guint8 vivo_marker[] = { 'V', 'e', 'r', 's', 'i', 'o', 'n',
5318     ':', 'V', 'i', 'v', 'o', '/'
5319   };
5320   const guint8 *data;
5321   guint hdr_len, pos;
5322
5323   data = gst_type_find_peek (tf, 0, 1024);
5324   if (data == NULL || data[0] != 0x00)
5325     return;
5326
5327   if ((data[1] & 0x80)) {
5328     if ((data[2] & 0x80))
5329       return;
5330     hdr_len = ((guint) (data[1] & 0x7f)) << 7;
5331     hdr_len += data[2];
5332     if (hdr_len > 2048)
5333       return;
5334     pos = 3;
5335   } else {
5336     hdr_len = data[1];
5337     pos = 2;
5338   }
5339
5340   /* 1008 = 1022 - strlen ("Version:Vivo/") - 1 */
5341   while (pos < 1008 && data[pos] == '\r' && data[pos + 1] == '\n')
5342     pos += 2;
5343
5344   if (memcmp (data + pos, vivo_marker, sizeof (vivo_marker)) == 0) {
5345     gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, VIVO_CAPS);
5346   }
5347 }
5348 #endif
5349 /*** XDG MIME typefinder (to avoid false positives mostly) ***/
5350
5351 #ifdef USE_GIO
5352 static void
5353 xdgmime_typefind (GstTypeFind * find, gpointer user_data)
5354 {
5355   gchar *mimetype;
5356   gsize length = 16384;
5357   guint64 tf_length;
5358   const guint8 *data;
5359   gchar *tmp;
5360
5361   if ((tf_length = gst_type_find_get_length (find)) > 0)
5362     length = MIN (length, tf_length);
5363
5364   if ((data = gst_type_find_peek (find, 0, length)) == NULL)
5365     return;
5366
5367   tmp = g_content_type_guess (NULL, data, length, NULL);
5368   if (tmp == NULL || g_content_type_is_unknown (tmp)) {
5369     g_free (tmp);
5370     return;
5371   }
5372
5373   mimetype = g_content_type_get_mime_type (tmp);
5374   g_free (tmp);
5375
5376   if (mimetype == NULL)
5377     return;
5378
5379   GST_DEBUG ("Got mimetype '%s'", mimetype);
5380
5381   /* Ignore audio/video types:
5382    *  - our own typefinders in -base are likely to be better at this
5383    *    (and if they're not, we really want to fix them, that's why we don't
5384    *    report xdg-detected audio/video types at all, not even with a low
5385    *    probability)
5386    *  - we want to detect GStreamer media types and not MIME types
5387    *  - the purpose of this xdg mime finder is mainly to prevent false
5388    *    positives of non-media formats, not to typefind audio/video formats */
5389   if (g_str_has_prefix (mimetype, "audio/") ||
5390       g_str_has_prefix (mimetype, "video/")) {
5391     GST_LOG ("Ignoring audio/video mime type");
5392     g_free (mimetype);
5393     return;
5394   }
5395
5396   /* Again, we mainly want the xdg typefinding to prevent false-positives on
5397    * non-media formats, so suggest the type with a probability that trumps
5398    * uncertain results of our typefinders, but not more than that. */
5399   GST_LOG ("Suggesting '%s' with probability POSSIBLE", mimetype);
5400   gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE, mimetype, NULL);
5401   g_free (mimetype);
5402 }
5403 #endif /* USE_GIO */
5404
5405 /*** Windows icon typefinder (to avoid false positives mostly) ***/
5406 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5407 static void
5408 windows_icon_typefind (GstTypeFind * find, gpointer user_data)
5409 {
5410   const guint8 *data;
5411   gint64 datalen;
5412   guint16 type, nimages;
5413   gint32 size, offset;
5414
5415   datalen = gst_type_find_get_length (find);
5416   if (datalen < 22)
5417     return;
5418   if ((data = gst_type_find_peek (find, 0, 6)) == NULL)
5419     return;
5420
5421   /* header - simple and not enough to rely on it alone */
5422   if (GST_READ_UINT16_LE (data) != 0)
5423     return;
5424   type = GST_READ_UINT16_LE (data + 2);
5425   if (type != 1 && type != 2)
5426     return;
5427   nimages = GST_READ_UINT16_LE (data + 4);
5428   if (nimages == 0)             /* we can assume we can't have an empty image file ? */
5429     return;
5430
5431   /* first image */
5432   if (data[6 + 3] != 0)
5433     return;
5434   if (type == 1) {
5435     guint16 planes = GST_READ_UINT16_LE (data + 6 + 4);
5436     if (planes > 1)
5437       return;
5438   }
5439   size = GST_READ_UINT32_LE (data + 6 + 8);
5440   offset = GST_READ_UINT32_LE (data + 6 + 12);
5441   if (offset < 0 || size <= 0 || size >= datalen || offset >= datalen
5442       || size + offset > datalen)
5443     return;
5444
5445   gst_type_find_suggest_simple (find, GST_TYPE_FIND_NEARLY_CERTAIN,
5446       "image/x-icon", NULL);
5447 }
5448
5449 /*** WAP WBMP typefinder ***/
5450
5451 static void
5452 wbmp_typefind (GstTypeFind * find, gpointer user_data)
5453 {
5454   const guint8 *data;
5455   gint64 datalen;
5456   guint w, h, size;
5457
5458   /* http://en.wikipedia.org/wiki/Wireless_Application_Protocol_Bitmap_Format */
5459   datalen = gst_type_find_get_length (find);
5460   if (datalen == 0)
5461     return;
5462
5463   data = gst_type_find_peek (find, 0, 5);
5464   if (data == NULL)
5465     return;
5466
5467   /* want 0x00 0x00 at start */
5468   if (*data++ != 0 || *data++ != 0)
5469     return;
5470
5471   /* min header size */
5472   size = 4;
5473
5474   /* let's assume max width/height is 65536 */
5475   w = *data++;
5476   if ((w & 0x80)) {
5477     w = (w << 8) | *data++;
5478     if ((w & 0x80))
5479       return;
5480     ++size;
5481     data = gst_type_find_peek (find, 4, 2);
5482     if (data == NULL)
5483       return;
5484   }
5485   h = *data++;
5486   if ((h & 0x80)) {
5487     h = (h << 8) | *data++;
5488     if ((h & 0x80))
5489       return;
5490     ++size;
5491   }
5492
5493   if (w == 0 || h == 0)
5494     return;
5495
5496   /* now add bitmap size */
5497   size += h * (GST_ROUND_UP_8 (w) / 8);
5498
5499   if (datalen == size) {
5500     gst_type_find_suggest_simple (find, GST_TYPE_FIND_POSSIBLE - 10,
5501         "image/vnd.wap.wbmp", NULL);
5502   }
5503 }
5504
5505 /*** DEGAS Atari images (also to avoid false positives, see #625129) ***/
5506 static void
5507 degas_type_find (GstTypeFind * tf, gpointer private)
5508 {
5509   /* No magic, but it should have a fixed size and a few invalid values */
5510   /* http://www.fileformat.info/format/atari/spec/6ecf9f6eb5be494284a47feb8a214687/view.htm */
5511   gint64 len;
5512   const guint8 *data;
5513   guint16 resolution;
5514   int n;
5515
5516   len = gst_type_find_get_length (tf);
5517   if (len < 34)                 /* smallest header of the lot */
5518     return;
5519   data = gst_type_find_peek (tf, 0, 4);
5520   if (G_UNLIKELY (data == NULL))
5521     return;
5522   resolution = GST_READ_UINT16_BE (data);
5523   if (len == 32034) {
5524     /* could be DEGAS */
5525     if (resolution <= 2)
5526       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5527           "image/x-degas", NULL);
5528   } else if (len == 32066) {
5529     /* could be DEGAS Elite */
5530     if (resolution <= 2) {
5531       data = gst_type_find_peek (tf, len - 16, 8);
5532       if (G_UNLIKELY (data == NULL))
5533         return;
5534       for (n = 0; n < 4; n++) {
5535         if (GST_READ_UINT16_BE (data + n * 2) > 2)
5536           return;
5537       }
5538       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5539           "image/x-degas", NULL);
5540     }
5541   } else if (len >= 66 && len < 32066) {
5542     /* could be compressed DEGAS Elite, but it's compressed and so we can't rely on size,
5543        it does have 4 16 bytes values near the end that are 0-2 though. */
5544     if ((resolution & 0x8000) && (resolution & 0x7fff) <= 2) {
5545       data = gst_type_find_peek (tf, len - 16, 8);
5546       if (G_UNLIKELY (data == NULL))
5547         return;
5548       for (n = 0; n < 4; n++) {
5549         if (GST_READ_UINT16_BE (data + n * 2) > 2)
5550           return;
5551       }
5552       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE + 5,
5553           "image/x-degas", NULL);
5554     }
5555   }
5556 }
5557
5558 /*** y4m ***/
5559
5560 static void
5561 y4m_typefind (GstTypeFind * tf, gpointer private)
5562 {
5563   const guint8 *data;
5564
5565   data = gst_type_find_peek (tf, 0, 10);
5566   if (data != NULL && memcmp (data, "YUV4MPEG2 ", 10) == 0) {
5567     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY,
5568         "application/x-yuv4mpeg", "y4mversion", G_TYPE_INT, 2, NULL);
5569   }
5570 }
5571 #endif
5572
5573 /*** DVD ISO images (looks like H.264, see #674069) ***/
5574 static void
5575 dvdiso_type_find (GstTypeFind * tf, gpointer private)
5576 {
5577   /* 0x8000 bytes of zeros, then "\001CD001" */
5578   gint64 len;
5579   const guint8 *data;
5580
5581   len = gst_type_find_get_length (tf);
5582   if (len < 0x8006)
5583     return;
5584   data = gst_type_find_peek (tf, 0, 0x8006);
5585   if (G_UNLIKELY (data == NULL))
5586     return;
5587   for (len = 0; len < 0x8000; len++)
5588     if (data[len])
5589       return;
5590   /* Can the '1' be anything else ? My three samples all have '1'. */
5591   if (memcmp (data + 0x8000, "\001CD001", 6))
5592     return;
5593
5594   /* May need more inspection, we may be able to demux some of them */
5595   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY,
5596       "application/octet-stream", NULL);
5597 }
5598
5599 /* SSA/ASS subtitles
5600  *
5601  * http://en.wikipedia.org/wiki/SubStation_Alpha
5602  * http://matroska.org/technical/specs/subtitles/ssa.html
5603  */
5604 static void
5605 ssa_type_find (GstTypeFind * tf, gpointer private)
5606 {
5607   const gchar *start, *end, *ver_str, *media_type = NULL;
5608   const guint8 *data;
5609   gchar *str, *script_type, *p = NULL;
5610   gint64 len;
5611
5612   data = gst_type_find_peek (tf, 0, 32);
5613
5614   if (data == NULL)
5615     return;
5616
5617   /* FIXME: detect utf-16/32 BOM and convert before typefinding the rest */
5618
5619   /* there might be a UTF-8 BOM at the beginning */
5620   if (memcmp (data, "[Script Info]", 13) != 0 &&
5621       memcmp (data + 3, "[Script Info]", 13) != 0) {
5622     return;
5623   }
5624
5625   /* now check if we have SSA or ASS */
5626   len = gst_type_find_get_length (tf);
5627   if (len > 8192)
5628     len = 8192;
5629
5630   data = gst_type_find_peek (tf, 0, len);
5631   if (data == NULL)
5632     return;
5633
5634   /* skip BOM */
5635   start = (gchar *) memchr (data, '[', 5);
5636   g_assert (start);
5637   len -= (start - (gchar *) data);
5638
5639   /* ignore anything non-UTF8 for now, in future we might at least allow
5640    * other UTF variants that are clearly prefixed with the appropriate BOM */
5641   if (!g_utf8_validate (start, len, &end) && (len - (end - start)) > 6) {
5642     GST_FIXME ("non-UTF8 SSA/ASS file");
5643     return;
5644   }
5645
5646   /* something at start,  but not a UTF-8 BOM? */
5647   if (data[0] != '[' && (data[0] != 0xEF || data[1] != 0xBB || data[2] != 0xBF))
5648     return;
5649
5650   /* ignore any partial UTF-8 characters at the end */
5651   len = end - start;
5652
5653   /* create a NUL-terminated string so it's easier to process it safely */
5654   str = g_strndup (start, len - 1);
5655   script_type = strstr (str, "ScriptType:");
5656   if (script_type != NULL) {
5657     gdouble version;
5658
5659     ver_str = script_type + 11;
5660     while (*ver_str == ' ' || *ver_str == 'v' || *ver_str == 'V')
5661       ++ver_str;
5662     version = g_ascii_strtod (ver_str, &p);
5663     if (version == 4.0 && p != NULL && *p == '+')
5664       media_type = "application/x-ass";
5665     else if (version >= 1.0 && version <= 4.0)
5666       media_type = "application/x-ssa";
5667   }
5668
5669   if (media_type == NULL) {
5670     if (strstr (str, "[v4+ Styles]") || strstr (str, "[V4+ Styles]"))
5671       media_type = "application/x-ass";
5672     else if (strstr (str, "[v4 Styles]") || strstr (str, "[V4 Styles]"))
5673       media_type = "application/x-ssa";
5674   }
5675
5676   if (media_type != NULL) {
5677     gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
5678         media_type, "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
5679   } else {
5680     GST_WARNING ("could not detect SSA/ASS variant");
5681   }
5682
5683   g_free (str);
5684 }
5685
5686 /*** application/x-mcc ***/
5687 static GstStaticCaps mcc_caps = GST_STATIC_CAPS ("application/x-mcc");
5688
5689 #define MCC_CAPS gst_static_caps_get(&mcc_caps)
5690
5691 static void
5692 mcc_type_find (GstTypeFind * tf, gpointer private)
5693 {
5694   const guint8 *data;
5695
5696   data = gst_type_find_peek (tf, 0, 31);
5697
5698   if (data == NULL)
5699     return;
5700
5701   /* MCC files always start with this followed by the version */
5702   if (memcmp (data, "File Format=MacCaption_MCC V", 28) != 0 ||
5703       !g_ascii_isdigit (data[28]) || data[29] != '.' ||
5704       !g_ascii_isdigit (data[30])) {
5705     return;
5706   }
5707
5708   gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
5709       "application/x-mcc", "version", G_TYPE_INT, data[28] - '0', NULL);
5710 }
5711
5712 /*** video/x-pva ***/
5713
5714 static GstStaticCaps pva_caps = GST_STATIC_CAPS ("video/x-pva");
5715
5716 #define PVA_CAPS gst_static_caps_get(&pva_caps)
5717
5718 static void
5719 pva_type_find (GstTypeFind * tf, gpointer private)
5720 {
5721   const guint8 *data;
5722
5723   data = gst_type_find_peek (tf, 0, 5);
5724
5725   if (data == NULL)
5726     return;
5727
5728   if (data[0] == 'A' && data[1] == 'V' && data[2] < 3 && data[4] == 0x55)
5729     gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, PVA_CAPS);
5730 }
5731
5732 /*** audio/audible ***/
5733
5734 /* derived from pyaudibletags
5735  * http://code.google.com/p/pyaudibletags/source/browse/trunk/pyaudibletags.py
5736  */
5737 static GstStaticCaps aa_caps = GST_STATIC_CAPS ("audio/x-audible");
5738
5739 #define AA_CAPS gst_static_caps_get(&aa_caps)
5740
5741 static void
5742 aa_type_find (GstTypeFind * tf, gpointer private)
5743 {
5744   const guint8 *data;
5745
5746   data = gst_type_find_peek (tf, 0, 12);
5747   if (data == NULL)
5748     return;
5749
5750   if (GST_READ_UINT32_BE (data + 4) == 0x57907536) {
5751     guint64 media_len;
5752
5753     media_len = gst_type_find_get_length (tf);
5754     if (media_len > 0 && GST_READ_UINT32_BE (data) == media_len)
5755       gst_type_find_suggest (tf, GST_TYPE_FIND_NEARLY_CERTAIN, AA_CAPS);
5756     else
5757       gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AA_CAPS);
5758   }
5759 }
5760
5761 /*** generic typefind for streams that have some data at a specific position***/
5762 typedef struct
5763 {
5764   const guint8 *data;
5765   guint size;
5766   guint probability;
5767   GstCaps *caps;
5768 }
5769 GstTypeFindData;
5770
5771 static void
5772 start_with_type_find (GstTypeFind * tf, gpointer private)
5773 {
5774   GstTypeFindData *start_with = (GstTypeFindData *) private;
5775   const guint8 *data;
5776
5777   GST_LOG ("trying to find mime type %s with the first %u bytes of data",
5778       gst_structure_get_name (gst_caps_get_structure (start_with->caps, 0)),
5779       start_with->size);
5780   data = gst_type_find_peek (tf, 0, start_with->size);
5781   if (data && memcmp (data, start_with->data, start_with->size) == 0) {
5782     gst_type_find_suggest (tf, start_with->probability, start_with->caps);
5783   }
5784 }
5785
5786 static void
5787 sw_data_destroy (GstTypeFindData * sw_data)
5788 {
5789   if (G_LIKELY (sw_data->caps != NULL))
5790     gst_caps_unref (sw_data->caps);
5791   g_slice_free (GstTypeFindData, sw_data);
5792 }
5793
5794 #define TYPE_FIND_REGISTER_START_WITH(plugin,name,rank,ext,_data,_size,_probability)\
5795 G_BEGIN_DECLS{                                                          \
5796   GstTypeFindData *sw_data = g_slice_new (GstTypeFindData);             \
5797   sw_data->data = (const guint8 *)_data;                                \
5798   sw_data->size = _size;                                                \
5799   sw_data->probability = _probability;                                  \
5800   sw_data->caps = gst_caps_new_empty_simple (name);                     \
5801   if (!gst_type_find_register (plugin, name, rank, start_with_type_find,\
5802                      ext, sw_data->caps, sw_data,                       \
5803                      (GDestroyNotify) (sw_data_destroy))) {             \
5804     sw_data_destroy (sw_data);                                          \
5805   }                                                                     \
5806 }G_END_DECLS
5807
5808 /*** same for riff types ***/
5809
5810 static void
5811 riff_type_find (GstTypeFind * tf, gpointer private)
5812 {
5813   GstTypeFindData *riff_data = (GstTypeFindData *) private;
5814   const guint8 *data = gst_type_find_peek (tf, 0, 12);
5815
5816   if (data && (memcmp (data, "RIFF", 4) == 0 || memcmp (data, "AVF0", 4) == 0)) {
5817     data += 8;
5818     if (memcmp (data, riff_data->data, 4) == 0)
5819       gst_type_find_suggest (tf, riff_data->probability, riff_data->caps);
5820   }
5821 }
5822
5823 #define TYPE_FIND_REGISTER_RIFF(plugin,name,rank,ext,_data)             \
5824 G_BEGIN_DECLS{                                                          \
5825   GstTypeFindData *sw_data = g_slice_new (GstTypeFindData);             \
5826   sw_data->data = (gpointer)_data;                                      \
5827   sw_data->size = 4;                                                    \
5828   sw_data->probability = GST_TYPE_FIND_MAXIMUM;                         \
5829   sw_data->caps = gst_caps_new_empty_simple (name);                     \
5830   if (!gst_type_find_register (plugin, name, rank, riff_type_find,      \
5831                       ext, sw_data->caps, sw_data,                      \
5832                       (GDestroyNotify) (sw_data_destroy))) {            \
5833     sw_data_destroy (sw_data);                                          \
5834   }                                                                     \
5835 }G_END_DECLS
5836
5837
5838 /*** plugin initialization ***/
5839
5840 #define TYPE_FIND_REGISTER(plugin,name,rank,func,ext,caps,priv,notify) \
5841 G_BEGIN_DECLS{\
5842   if (!gst_type_find_register (plugin, name, rank, func, ext, caps, priv, notify))\
5843     return FALSE; \
5844 }G_END_DECLS
5845
5846
5847 static gboolean
5848 plugin_init (GstPlugin * plugin)
5849 {
5850   /* can't initialize this via a struct as caps can't be statically initialized */
5851
5852   GST_DEBUG_CATEGORY_INIT (type_find_debug, "typefindfunctions",
5853       GST_DEBUG_FG_GREEN | GST_DEBUG_BG_RED, "generic type find functions");
5854
5855   /* note: asx/wax/wmx are XML files, asf doesn't handle them */
5856   /* must use strings, macros don't accept initializers */
5857   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ms-asf", GST_RANK_SECONDARY,
5858       "asf,wm,wma,wmv",
5859       "\060\046\262\165\216\146\317\021\246\331\000\252\000\142\316\154", 16,
5860       GST_TYPE_FIND_MAXIMUM);
5861   TYPE_FIND_REGISTER (plugin, "audio/x-musepack", GST_RANK_PRIMARY,
5862       musepack_type_find, "mpc,mpp,mp+", MUSEPACK_CAPS, NULL, NULL);
5863   TYPE_FIND_REGISTER (plugin, "audio/x-au", GST_RANK_MARGINAL,
5864       au_type_find, "au,snd", AU_CAPS, NULL, NULL);
5865   TYPE_FIND_REGISTER_RIFF (plugin, "video/x-msvideo", GST_RANK_PRIMARY,
5866       "avi", "AVI ");
5867 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5868   TYPE_FIND_REGISTER_RIFF (plugin, "audio/qcelp", GST_RANK_PRIMARY,
5869       "qcp", "QLCM");
5870   TYPE_FIND_REGISTER_RIFF (plugin, "video/x-cdxa", GST_RANK_PRIMARY,
5871       "dat", "CDXA");
5872   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-vcd", GST_RANK_PRIMARY,
5873       "dat", "\000\377\377\377\377\377\377\377\377\377\377\000", 12,
5874       GST_TYPE_FIND_MAXIMUM);
5875 #endif
5876   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-imelody", GST_RANK_PRIMARY,
5877       "imy,ime,imelody", "BEGIN:IMELODY", 13, GST_TYPE_FIND_MAXIMUM);
5878   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-scc", GST_RANK_PRIMARY,
5879       "scc", "Scenarist_SCC V1.0", 18, GST_TYPE_FIND_MAXIMUM);
5880   TYPE_FIND_REGISTER (plugin, "application/x-mcc", GST_RANK_PRIMARY,
5881       mcc_type_find, "mcc", MCC_CAPS, NULL, NULL);
5882
5883 #if 0
5884   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-smoke", GST_RANK_PRIMARY,
5885       NULL, "\x80smoke\x00\x01\x00", 6, GST_TYPE_FIND_MAXIMUM);
5886 #endif
5887   TYPE_FIND_REGISTER (plugin, "audio/midi", GST_RANK_PRIMARY, mid_type_find,
5888       "mid,midi", MID_CAPS, NULL, NULL);
5889   TYPE_FIND_REGISTER_RIFF (plugin, "audio/riff-midi", GST_RANK_PRIMARY,
5890       "mid,midi", "RMID");
5891   TYPE_FIND_REGISTER (plugin, "audio/mobile-xmf", GST_RANK_PRIMARY,
5892       mxmf_type_find, "mxmf", MXMF_CAPS, NULL, NULL);
5893 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5894   TYPE_FIND_REGISTER (plugin, "video/x-fli", GST_RANK_MARGINAL, flx_type_find,
5895       "flc,fli", FLX_CAPS, NULL, NULL);
5896 #endif
5897   TYPE_FIND_REGISTER (plugin, "application/x-id3v2", GST_RANK_PRIMARY + 103,
5898       id3v2_type_find, "mp3,mp2,mp1,mpga,ogg,flac,tta", ID3_CAPS, NULL, NULL);
5899   TYPE_FIND_REGISTER (plugin, "application/x-id3v1", GST_RANK_PRIMARY + 101,
5900       id3v1_type_find, "mp3,mp2,mp1,mpga,ogg,flac,tta", ID3_CAPS, NULL, NULL);
5901   TYPE_FIND_REGISTER (plugin, "application/x-apetag", GST_RANK_PRIMARY + 102,
5902       apetag_type_find, "mp3,ape,mpc,wv", APETAG_CAPS, NULL, NULL);
5903 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5904   TYPE_FIND_REGISTER (plugin, "audio/x-ttafile", GST_RANK_PRIMARY,
5905       tta_type_find, "tta", TTA_CAPS, NULL, NULL);
5906   TYPE_FIND_REGISTER (plugin, "audio/x-mod", GST_RANK_SECONDARY, mod_type_find,
5907       "669,amf,ams,dbm,digi,dmf,dsm,gdm,far,imf,it,j2b,mdl,med,mod,mt2,mtm,"
5908       "okt,psm,ptm,sam,s3m,stm,stx,ult,umx,xm", MOD_CAPS, NULL, NULL);
5909 #endif
5910   TYPE_FIND_REGISTER (plugin, "audio/mpeg", GST_RANK_PRIMARY, mp3_type_find,
5911       "mp3,mp2,mp1,mpga", MP3_CAPS, NULL, NULL);
5912   TYPE_FIND_REGISTER (plugin, "audio/x-ac3", GST_RANK_PRIMARY, ac3_type_find,
5913       "ac3,eac3", AC3_CAPS, NULL, NULL);
5914   TYPE_FIND_REGISTER (plugin, "audio/x-dts", GST_RANK_SECONDARY, dts_type_find,
5915       "dts", DTS_CAPS, NULL, NULL);
5916 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5917   TYPE_FIND_REGISTER (plugin, "audio/x-gsm", GST_RANK_PRIMARY, NULL, "gsm",
5918       GSM_CAPS, NULL, NULL);
5919 #endif
5920 #ifdef TIZEN_PROFILE_TV
5921   TYPE_FIND_REGISTER (plugin, "video/mpeg-sys", GST_RANK_PRIMARY,
5922       mpeg_sys_type_find, "mpe,mpeg,mpg", MPEG_SYS_CAPS, NULL, NULL);
5923 #endif
5924   TYPE_FIND_REGISTER (plugin, "video/mpegts", GST_RANK_PRIMARY,
5925       mpeg_ts_type_find, "ts,mts", MPEGTS_CAPS, NULL, NULL);
5926   TYPE_FIND_REGISTER (plugin, "application/ogg", GST_RANK_PRIMARY,
5927       ogganx_type_find, "ogg,oga,ogv,ogm,ogx,spx,anx,axa,axv", OGG_CAPS,
5928       NULL, NULL);
5929   TYPE_FIND_REGISTER (plugin, "video/mpeg-elementary", GST_RANK_MARGINAL,
5930       mpeg_video_stream_type_find, "mpv,mpeg,mpg", MPEG_VIDEO_CAPS, NULL, NULL);
5931   TYPE_FIND_REGISTER (plugin, "video/mpeg4", GST_RANK_PRIMARY,
5932       mpeg4_video_type_find, "m4v", MPEG_VIDEO_CAPS, NULL, NULL);
5933   TYPE_FIND_REGISTER (plugin, "video/x-h263", GST_RANK_SECONDARY,
5934       h263_video_type_find, "h263,263", H263_VIDEO_CAPS, NULL, NULL);
5935   TYPE_FIND_REGISTER (plugin, "video/x-h264", GST_RANK_PRIMARY,
5936       h264_video_type_find, "h264,x264,264", H264_VIDEO_CAPS, NULL, NULL);
5937   TYPE_FIND_REGISTER (plugin, "video/x-h265", GST_RANK_PRIMARY,
5938       h265_video_type_find, "h265,x265,265", H265_VIDEO_CAPS, NULL, NULL);
5939   TYPE_FIND_REGISTER (plugin, "video/x-nuv", GST_RANK_SECONDARY, nuv_type_find,
5940       "nuv", NUV_CAPS, NULL, NULL);
5941
5942   /* ISO formats */
5943   TYPE_FIND_REGISTER (plugin, "audio/x-m4a", GST_RANK_PRIMARY, m4a_type_find,
5944       "m4a", M4A_CAPS, NULL, NULL);
5945   TYPE_FIND_REGISTER (plugin, "application/x-3gp", GST_RANK_PRIMARY,
5946       q3gp_type_find, "3gp", Q3GP_CAPS, NULL, NULL);
5947   TYPE_FIND_REGISTER (plugin, "video/quicktime", GST_RANK_PRIMARY,
5948       qt_type_find, "mov,mp4", QT_CAPS, NULL, NULL);
5949 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5950   TYPE_FIND_REGISTER (plugin, "image/x-quicktime", GST_RANK_SECONDARY,
5951       qtif_type_find, "qif,qtif,qti", QTIF_CAPS, NULL, NULL);
5952   TYPE_FIND_REGISTER (plugin, "image/jp2", GST_RANK_PRIMARY,
5953       jp2_type_find, "jp2", JP2_CAPS, NULL, NULL);
5954   TYPE_FIND_REGISTER (plugin, "image/x-jpc", GST_RANK_PRIMARY,
5955       jpc_type_find, "jpc,j2k", JPC_CAPS, NULL, NULL);
5956   TYPE_FIND_REGISTER (plugin, "video/mj2", GST_RANK_PRIMARY,
5957       jp2_type_find, "mj2", MJ2_CAPS, NULL, NULL);
5958
5959   TYPE_FIND_REGISTER (plugin, "text/html", GST_RANK_SECONDARY, html_type_find,
5960       "htm,html", HTML_CAPS, NULL, NULL);
5961 #endif
5962   TYPE_FIND_REGISTER_START_WITH (plugin, "application/vnd.rn-realmedia",
5963       GST_RANK_SECONDARY, "ra,ram,rm,rmvb", ".RMF", 4, GST_TYPE_FIND_MAXIMUM);
5964   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-pn-realaudio",
5965       GST_RANK_SECONDARY, "ra,ram,rm,rmvb", ".ra\375", 4,
5966       GST_TYPE_FIND_MAXIMUM);
5967 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5968   TYPE_FIND_REGISTER (plugin, "application/x-shockwave-flash",
5969       GST_RANK_SECONDARY, swf_type_find, "swf,swfl", SWF_CAPS, NULL, NULL);
5970 #endif    
5971   TYPE_FIND_REGISTER (plugin, "application/xges",
5972       GST_RANK_PRIMARY, xges_type_find, "xges", XGES_CAPS, NULL, NULL);
5973   TYPE_FIND_REGISTER (plugin, "application/dash+xml",
5974       GST_RANK_PRIMARY, dash_mpd_type_find, "mpd,MPD", DASH_CAPS, NULL, NULL);
5975   TYPE_FIND_REGISTER (plugin, "application/vnd.ms-sstr+xml",
5976       GST_RANK_PRIMARY, mss_manifest_type_find, NULL, MSS_MANIFEST_CAPS, NULL,
5977       NULL);
5978   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-flv", GST_RANK_SECONDARY,
5979       "flv", "FLV", 3, GST_TYPE_FIND_MAXIMUM);
5980   TYPE_FIND_REGISTER (plugin, "text/plain", GST_RANK_MARGINAL, utf8_type_find,
5981       "txt", UTF8_CAPS, NULL, NULL);
5982   TYPE_FIND_REGISTER (plugin, "text/utf-16", GST_RANK_MARGINAL, utf16_type_find,
5983       "txt", UTF16_CAPS, NULL, NULL);
5984   TYPE_FIND_REGISTER (plugin, "text/utf-32", GST_RANK_MARGINAL, utf32_type_find,
5985       "txt", UTF32_CAPS, NULL, NULL);
5986 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5987   TYPE_FIND_REGISTER (plugin, "text/uri-list", GST_RANK_MARGINAL, uri_type_find,
5988       "ram", URI_CAPS, NULL, NULL);
5989 #endif
5990   TYPE_FIND_REGISTER (plugin, "application/itc", GST_RANK_SECONDARY,
5991       itc_type_find, "itc", ITC_CAPS, NULL, NULL);
5992   TYPE_FIND_REGISTER (plugin, "application/x-hls", GST_RANK_MARGINAL,
5993       hls_type_find, "m3u8", HLS_CAPS, NULL, NULL);
5994   TYPE_FIND_REGISTER (plugin, "application/sdp", GST_RANK_SECONDARY,
5995       sdp_type_find, "sdp", SDP_CAPS, NULL, NULL);
5996 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
5997   TYPE_FIND_REGISTER (plugin, "application/smil", GST_RANK_SECONDARY,
5998       smil_type_find, "smil", SMIL_CAPS, NULL, NULL);
5999   TYPE_FIND_REGISTER (plugin, "application/ttml+xml", GST_RANK_SECONDARY,
6000       ttml_xml_type_find, "ttml+xml", TTML_XML_CAPS, NULL, NULL);
6001   TYPE_FIND_REGISTER (plugin, "application/xml", GST_RANK_MARGINAL,
6002       xml_type_find, "xml", GENERIC_XML_CAPS, NULL, NULL);
6003 #endif
6004   TYPE_FIND_REGISTER_RIFF (plugin, "audio/x-wav", GST_RANK_PRIMARY, "wav",
6005       "WAVE");
6006   TYPE_FIND_REGISTER (plugin, "audio/x-aiff", GST_RANK_SECONDARY,
6007       aiff_type_find, "aiff,aif,aifc", AIFF_CAPS, NULL, NULL);
6008 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6009   TYPE_FIND_REGISTER (plugin, "audio/x-svx", GST_RANK_SECONDARY, svx_type_find,
6010       "iff,svx", SVX_CAPS, NULL, NULL);
6011   TYPE_FIND_REGISTER (plugin, "audio/x-paris", GST_RANK_SECONDARY,
6012       paris_type_find, "paf", PARIS_CAPS, NULL, NULL);
6013   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nist", GST_RANK_SECONDARY,
6014       "nist", "NIST", 4, GST_TYPE_FIND_MAXIMUM);
6015   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-voc", GST_RANK_SECONDARY,
6016       "voc", "Creative", 8, GST_TYPE_FIND_MAXIMUM);
6017   TYPE_FIND_REGISTER (plugin, "audio/x-sds", GST_RANK_SECONDARY, sds_type_find,
6018       "sds", SDS_CAPS, NULL, NULL);
6019   TYPE_FIND_REGISTER (plugin, "audio/x-ircam", GST_RANK_SECONDARY,
6020       ircam_type_find, "sf", IRCAM_CAPS, NULL, NULL);
6021   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-w64", GST_RANK_SECONDARY,
6022       "w64", "riff", 4, GST_TYPE_FIND_MAXIMUM);
6023   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-rf64", GST_RANK_PRIMARY,
6024       "rf64", "RF64", 4, GST_TYPE_FIND_MAXIMUM);
6025   TYPE_FIND_REGISTER (plugin, "audio/x-shorten", GST_RANK_SECONDARY,
6026       shn_type_find, "shn", SHN_CAPS, NULL, NULL);
6027   TYPE_FIND_REGISTER (plugin, "application/x-ape", GST_RANK_SECONDARY,
6028       ape_type_find, "ape", APE_CAPS, NULL, NULL);
6029 #endif
6030   TYPE_FIND_REGISTER (plugin, "image/jpeg", GST_RANK_PRIMARY + 15,
6031       jpeg_type_find, "jpg,jpe,jpeg", JPEG_CAPS, NULL, NULL);
6032   TYPE_FIND_REGISTER_START_WITH (plugin, "image/gif", GST_RANK_PRIMARY, "gif",
6033       "GIF8", 4, GST_TYPE_FIND_MAXIMUM);
6034   TYPE_FIND_REGISTER_START_WITH (plugin, "image/png", GST_RANK_PRIMARY + 14,
6035       "png", "\211PNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
6036   TYPE_FIND_REGISTER (plugin, "image/bmp", GST_RANK_PRIMARY, bmp_type_find,
6037       "bmp", BMP_CAPS, NULL, NULL);
6038 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6039   TYPE_FIND_REGISTER (plugin, "image/tiff", GST_RANK_PRIMARY, tiff_type_find,
6040       "tif,tiff", TIFF_CAPS, NULL, NULL);
6041   TYPE_FIND_REGISTER_RIFF (plugin, "image/webp", GST_RANK_PRIMARY,
6042       "webp", "WEBP");
6043 #endif
6044   TYPE_FIND_REGISTER (plugin, "image/x-exr", GST_RANK_PRIMARY, exr_type_find,
6045       "exr", EXR_CAPS, NULL, NULL);
6046   TYPE_FIND_REGISTER (plugin, "image/x-portable-pixmap", GST_RANK_SECONDARY,
6047       pnm_type_find, "pnm,ppm,pgm,pbm", PNM_CAPS, NULL, NULL);
6048   TYPE_FIND_REGISTER (plugin, "video/x-matroska", GST_RANK_PRIMARY,
6049       matroska_type_find, "mkv,mka,mk3d,webm", MATROSKA_CAPS, NULL, NULL);
6050 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6051   TYPE_FIND_REGISTER (plugin, "application/mxf", GST_RANK_PRIMARY,
6052       mxf_type_find, "mxf", MXF_CAPS, NULL, NULL);
6053   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mve", GST_RANK_SECONDARY,
6054       "mve", "Interplay MVE File\032\000\032\000\000\001\063\021", 26,
6055       GST_TYPE_FIND_MAXIMUM);
6056   TYPE_FIND_REGISTER (plugin, "video/x-dv", GST_RANK_SECONDARY, dv_type_find,
6057       "dv,dif", DV_CAPS, NULL, NULL);
6058 #endif
6059   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-nb-sh", GST_RANK_PRIMARY,
6060       "amr", "#!AMR", 5, GST_TYPE_FIND_LIKELY);
6061   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-amr-wb-sh", GST_RANK_PRIMARY,
6062       "amr", "#!AMR-WB", 7, GST_TYPE_FIND_MAXIMUM);
6063 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6064   TYPE_FIND_REGISTER (plugin, "audio/iLBC-sh", GST_RANK_PRIMARY, ilbc_type_find,
6065       "ilbc", ILBC_CAPS, NULL, NULL);
6066   TYPE_FIND_REGISTER (plugin, "audio/x-sbc", GST_RANK_MARGINAL, sbc_type_find,
6067       "sbc", SBC_CAPS, NULL, NULL);
6068   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sid", GST_RANK_MARGINAL,
6069       "sid", "PSID", 4, GST_TYPE_FIND_MAXIMUM);
6070   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xcf", GST_RANK_SECONDARY,
6071       "xcf", "gimp xcf", 8, GST_TYPE_FIND_MAXIMUM);
6072   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-mng", GST_RANK_SECONDARY,
6073       "mng", "\212MNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
6074   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-jng", GST_RANK_SECONDARY,
6075       "jng", "\213JNG\015\012\032\012", 8, GST_TYPE_FIND_MAXIMUM);
6076   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xpixmap", GST_RANK_SECONDARY,
6077       "xpm", "/* XPM */", 9, GST_TYPE_FIND_MAXIMUM);
6078   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-sun-raster",
6079       GST_RANK_SECONDARY, "ras", "\131\246\152\225", 4, GST_TYPE_FIND_MAXIMUM);
6080   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-bzip",
6081       GST_RANK_SECONDARY, "bz2", "BZh", 3, GST_TYPE_FIND_LIKELY);
6082   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-gzip",
6083       GST_RANK_SECONDARY, "gz", "\037\213", 2, GST_TYPE_FIND_LIKELY);
6084   TYPE_FIND_REGISTER_START_WITH (plugin, "application/zip", GST_RANK_SECONDARY,
6085       "zip", "PK\003\004", 4, GST_TYPE_FIND_LIKELY);
6086   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-compress",
6087       GST_RANK_SECONDARY, "Z", "\037\235", 2, GST_TYPE_FIND_LIKELY);
6088   TYPE_FIND_REGISTER (plugin, "subtitle/x-kate", GST_RANK_MARGINAL,
6089       kate_type_find, NULL, NULL, NULL, NULL);
6090 #endif
6091   TYPE_FIND_REGISTER (plugin, "application/x-subtitle-vtt", GST_RANK_MARGINAL,
6092       webvtt_type_find, "vtt", WEBVTT_CAPS, NULL, NULL);
6093   TYPE_FIND_REGISTER (plugin, "audio/x-flac", GST_RANK_PRIMARY, flac_type_find,
6094       "flac", FLAC_CAPS, NULL, NULL);
6095   TYPE_FIND_REGISTER (plugin, "audio/x-vorbis", GST_RANK_PRIMARY,
6096       vorbis_type_find, NULL, VORBIS_CAPS, NULL, NULL);
6097   TYPE_FIND_REGISTER (plugin, "video/x-theora", GST_RANK_PRIMARY,
6098       theora_type_find, NULL, THEORA_CAPS, NULL, NULL);
6099 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6100   TYPE_FIND_REGISTER (plugin, "application/x-ogm-video", GST_RANK_PRIMARY,
6101       ogmvideo_type_find, NULL, OGMVIDEO_CAPS, NULL, NULL);
6102   TYPE_FIND_REGISTER (plugin, "application/x-ogm-audio", GST_RANK_PRIMARY,
6103       ogmaudio_type_find, NULL, OGMAUDIO_CAPS, NULL, NULL);
6104   TYPE_FIND_REGISTER (plugin, "application/x-ogm-text", GST_RANK_PRIMARY,
6105       ogmtext_type_find, NULL, OGMTEXT_CAPS, NULL, NULL);
6106   TYPE_FIND_REGISTER (plugin, "audio/x-speex", GST_RANK_PRIMARY,
6107       speex_type_find, NULL, SPEEX_CAPS, NULL, NULL);
6108   TYPE_FIND_REGISTER (plugin, "audio/x-celt", GST_RANK_PRIMARY, celt_type_find,
6109       NULL, CELT_CAPS, NULL, NULL);
6110   TYPE_FIND_REGISTER (plugin, "application/x-ogg-skeleton", GST_RANK_PRIMARY,
6111       oggskel_type_find, NULL, OGG_SKELETON_CAPS, NULL, NULL);
6112   TYPE_FIND_REGISTER (plugin, "text/x-cmml", GST_RANK_PRIMARY, cmml_type_find,
6113       NULL, CMML_CAPS, NULL, NULL);
6114   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-executable",
6115       GST_RANK_MARGINAL, NULL, "\177ELF", 4, GST_TYPE_FIND_MAXIMUM);
6116 #endif
6117   TYPE_FIND_REGISTER (plugin, "audio/aac", GST_RANK_SECONDARY, aac_type_find,
6118       "aac,adts,adif,loas", AAC_CAPS, NULL, NULL);
6119 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6120   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-spc", GST_RANK_SECONDARY,
6121       "spc", "SNES-SPC700 Sound File Data", 27, GST_TYPE_FIND_MAXIMUM);
6122   TYPE_FIND_REGISTER (plugin, "audio/x-wavpack", GST_RANK_SECONDARY,
6123       wavpack_type_find, "wv,wvp", WAVPACK_CAPS, NULL, NULL);
6124   TYPE_FIND_REGISTER (plugin, "audio/x-wavpack-correction", GST_RANK_SECONDARY,
6125       wavpack_type_find, "wvc", WAVPACK_CORRECTION_CAPS, NULL, NULL);
6126   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-caf", GST_RANK_SECONDARY,
6127       "caf", "caff\000\001", 6, GST_TYPE_FIND_MAXIMUM);
6128   TYPE_FIND_REGISTER (plugin, "application/postscript", GST_RANK_SECONDARY,
6129       postscript_type_find, "ps", POSTSCRIPT_CAPS, NULL, NULL);
6130   TYPE_FIND_REGISTER (plugin, "image/svg+xml", GST_RANK_SECONDARY,
6131       svg_type_find, "svg", SVG_CAPS, NULL, NULL);
6132   TYPE_FIND_REGISTER_START_WITH (plugin, "application/x-rar",
6133       GST_RANK_SECONDARY, "rar", "Rar!", 4, GST_TYPE_FIND_LIKELY);
6134   TYPE_FIND_REGISTER (plugin, "application/x-tar", GST_RANK_SECONDARY,
6135       tar_type_find, "tar", TAR_CAPS, NULL, NULL);
6136   TYPE_FIND_REGISTER (plugin, "application/x-ar", GST_RANK_SECONDARY,
6137       ar_type_find, "a", AR_CAPS, NULL, NULL);
6138   TYPE_FIND_REGISTER (plugin, "application/x-ms-dos-executable",
6139       GST_RANK_SECONDARY, msdos_type_find, "dll,exe,ocx,sys,scr,msstyles,cpl",
6140       MSDOS_CAPS, NULL, NULL);
6141   TYPE_FIND_REGISTER (plugin, "video/x-dirac", GST_RANK_PRIMARY,
6142       dirac_type_find, NULL, DIRAC_CAPS, NULL, NULL);
6143   TYPE_FIND_REGISTER (plugin, "multipart/x-mixed-replace", GST_RANK_SECONDARY,
6144       multipart_type_find, NULL, MULTIPART_CAPS, NULL, NULL);
6145   TYPE_FIND_REGISTER (plugin, "application/x-mmsh", GST_RANK_SECONDARY,
6146       mmsh_type_find, NULL, MMSH_CAPS, NULL, NULL);
6147   TYPE_FIND_REGISTER (plugin, "video/vivo", GST_RANK_SECONDARY, vivo_type_find,
6148       "viv", VIVO_CAPS, NULL, NULL);
6149   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-nsf", GST_RANK_SECONDARY,
6150       "nsf", "NESM\x1a", 5, GST_TYPE_FIND_MAXIMUM);
6151   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gym", GST_RANK_SECONDARY,
6152       "gym", "GYMX", 4, GST_TYPE_FIND_MAXIMUM);
6153   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-ay", GST_RANK_SECONDARY, "ay",
6154       "ZXAYEMUL", 8, GST_TYPE_FIND_MAXIMUM);
6155   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-gbs", GST_RANK_SECONDARY,
6156       "gbs", "GBS\x01", 4, GST_TYPE_FIND_MAXIMUM);
6157   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-vgm", GST_RANK_SECONDARY,
6158       "vgm", "Vgm\x20", 4, GST_TYPE_FIND_MAXIMUM);
6159   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sap", GST_RANK_SECONDARY,
6160       "sap", "SAP\x0d\x0a" "AUTHOR\x20", 12, GST_TYPE_FIND_MAXIMUM);
6161   TYPE_FIND_REGISTER_START_WITH (plugin, "video/x-ivf", GST_RANK_SECONDARY,
6162       "ivf", "DKIF", 4, GST_TYPE_FIND_NEARLY_CERTAIN);
6163   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-kss", GST_RANK_SECONDARY,
6164       "kss", "KSSX\0", 5, GST_TYPE_FIND_MAXIMUM);
6165   TYPE_FIND_REGISTER_START_WITH (plugin, "application/pdf", GST_RANK_SECONDARY,
6166       "pdf", "%PDF-", 5, GST_TYPE_FIND_LIKELY);
6167   TYPE_FIND_REGISTER_START_WITH (plugin, "application/msword",
6168       GST_RANK_SECONDARY, "doc", "\320\317\021\340\241\261\032\341", 8,
6169       GST_TYPE_FIND_LIKELY);
6170 #endif
6171   /* Mac OS X .DS_Store files tend to be taken for video/mpeg */
6172   TYPE_FIND_REGISTER_START_WITH (plugin, "application/octet-stream",
6173       GST_RANK_SECONDARY, "DS_Store", "\000\000\000\001Bud1", 8,
6174       GST_TYPE_FIND_LIKELY);
6175 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6176   TYPE_FIND_REGISTER_START_WITH (plugin, "image/vnd.adobe.photoshop",
6177       GST_RANK_SECONDARY, "psd", "8BPS\000\001\000\000\000\000", 10,
6178       GST_TYPE_FIND_LIKELY);
6179   TYPE_FIND_REGISTER (plugin, "image/vnd.wap.wbmp", GST_RANK_MARGINAL,
6180       wbmp_typefind, NULL, NULL, NULL, NULL);
6181   TYPE_FIND_REGISTER (plugin, "application/x-yuv4mpeg", GST_RANK_SECONDARY,
6182       y4m_typefind, NULL, NULL, NULL, NULL);
6183   TYPE_FIND_REGISTER (plugin, "image/x-icon", GST_RANK_MARGINAL,
6184       windows_icon_typefind, NULL, NULL, NULL, NULL);
6185 #endif
6186
6187 #ifdef USE_GIO
6188   TYPE_FIND_REGISTER (plugin, "xdgmime-base", GST_RANK_MARGINAL,
6189       xdgmime_typefind, NULL, NULL, NULL, NULL);
6190 #endif
6191
6192 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6193   TYPE_FIND_REGISTER (plugin, "image/x-degas", GST_RANK_MARGINAL,
6194       degas_type_find, NULL, NULL, NULL, NULL);
6195 #endif
6196   TYPE_FIND_REGISTER (plugin, "application/octet-stream", GST_RANK_MARGINAL,
6197       dvdiso_type_find, NULL, NULL, NULL, NULL);
6198
6199   TYPE_FIND_REGISTER (plugin, "application/x-ssa", GST_RANK_SECONDARY,
6200       ssa_type_find, "ssa,ass", NULL, NULL, NULL);
6201
6202   TYPE_FIND_REGISTER (plugin, "video/x-pva", GST_RANK_SECONDARY,
6203       pva_type_find, "pva", PVA_CAPS, NULL, NULL);
6204
6205   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-xi", GST_RANK_SECONDARY,
6206       "xi", "Extended Instrument: ", 21, GST_TYPE_FIND_MAXIMUM);
6207
6208   TYPE_FIND_REGISTER (plugin, "audio/audible", GST_RANK_MARGINAL,
6209       aa_type_find, "aa,aax", AA_CAPS, NULL, NULL);
6210 #ifndef TIZEN_FEATURE_DISABLE_MIME_TYPES
6211   TYPE_FIND_REGISTER (plugin, "audio/x-tap-tap", GST_RANK_PRIMARY,
6212       tap_type_find, "tap", TAP_CAPS, NULL, NULL);
6213   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-tap-dmp",
6214       GST_RANK_SECONDARY, "dmp", "DC2N-TAP-RAW", 12, GST_TYPE_FIND_LIKELY);
6215 #endif    
6216
6217   return TRUE;
6218 }
6219
6220 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
6221     GST_VERSION_MINOR,
6222     typefindfunctions,
6223     "default typefind functions",
6224     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)