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