/
gsttypefindfunctions.c
6077 lines (5104 loc) · 182 KB
/
gsttypefindfunctions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GStreamer
* Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
* Copyright (C) 2005-2009 Tim-Philipp Müller <tim centricular net>
* Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* gsttypefindfunctions.c: collection of various typefind functions
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include <glib/gprintf.h>
/* don't want to add gio xdgmime typefinder if gio was disabled via configure */
#ifdef HAVE_GIO
#include <gio/gio.h>
#define USE_GIO
#endif
#include <gst/gst.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <gst/pbutils/pbutils.h>
#include <gst/base/gstbytereader.h>
#include "gsttypefindfunctionsplugin.h"
/* DataScanCtx: helper for typefind functions that scan through data
* step-by-step, to avoid doing a peek at each and every offset */
#define DATA_SCAN_CTX_CHUNK_SIZE 4096
typedef struct
{
guint64 offset;
const guint8 *data;
guint size;
} DataScanCtx;
static inline void
data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip)
{
c->offset += bytes_to_skip;
if (G_LIKELY (c->size > bytes_to_skip)) {
c->size -= bytes_to_skip;
c->data += bytes_to_skip;
} else {
c->data += c->size;
c->size = 0;
}
}
static inline gboolean
data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, guint min_len)
{
const guint8 *data;
guint64 len;
guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
if (G_LIKELY (c->size >= min_len))
return TRUE;
data = gst_type_find_peek (tf, c->offset, chunk_len);
if (G_LIKELY (data != NULL)) {
c->data = data;
c->size = chunk_len;
return TRUE;
}
/* if there's less than our chunk size, try to get as much as we can, but
* always at least min_len bytes (we might be typefinding the first buffer
* of the stream and not have as much data available as we'd like) */
len = gst_type_find_get_length (tf);
if (len > 0) {
len = CLAMP (len - c->offset, min_len, chunk_len);
} else {
len = min_len;
}
data = gst_type_find_peek (tf, c->offset, len);
if (data != NULL) {
c->data = data;
c->size = len;
return TRUE;
}
return FALSE;
}
static inline gboolean
data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset,
const gchar * data, guint len)
{
if (G_UNLIKELY (offset + len >= G_MAXUINT32))
return FALSE;
if (!data_scan_ctx_ensure_data (tf, c, offset + len))
return FALSE;
return (memcmp (c->data + offset, data, len) == 0);
}
/*** text/plain ***/
static gboolean xml_check_first_element (GstTypeFind * tf,
const gchar * element, guint elen, gboolean strict);
static gboolean sdp_check_header (GstTypeFind * tf);
static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
#define UTF8_CAPS gst_static_caps_get(&utf8_caps)
static gboolean
utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
GstTypeFindProbability * prob)
{
const guint8 *data;
/* randomly decided values */
guint min_size = 16; /* minimum size */
guint size = 32 * 1024; /* starting size */
guint probability = 95; /* starting probability */
guint step = 10; /* how much we reduce probability in each
* iteration */
while (probability > step && size > min_size) {
data = gst_type_find_peek (tf, offset, size);
if (data) {
gchar *end;
gchar *start = (gchar *) data;
if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) { /* allow last char to be cut off */
*prob = probability;
return TRUE;
}
*prob = 0;
return FALSE;
}
size /= 2;
probability -= step;
}
*prob = 0;
return FALSE;
}
static void
utf8_type_find (GstTypeFind * tf, gpointer unused)
{
GstTypeFindProbability start_prob, mid_prob;
guint64 length;
/* leave xml to the xml typefinders */
if (xml_check_first_element (tf, "", 0, TRUE))
return;
/* leave sdp to the sdp typefinders */
if (sdp_check_header (tf))
return;
/* check beginning of stream */
if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob))
return;
GST_LOG ("start is plain text with probability of %u", start_prob);
/* POSSIBLE is the highest probability we ever return if we can't
* probe into the middle of the file and don't know its length */
length = gst_type_find_get_length (tf);
if (length == 0 || length == (guint64) - 1) {
gst_type_find_suggest (tf, MIN (start_prob, GST_TYPE_FIND_POSSIBLE),
UTF8_CAPS);
return;
}
if (length < 64 * 1024) {
gst_type_find_suggest (tf, start_prob, UTF8_CAPS);
return;
}
/* check middle of stream */
if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob))
return;
GST_LOG ("middle is plain text with probability of %u", mid_prob);
gst_type_find_suggest (tf, (start_prob + mid_prob) / 2, UTF8_CAPS);
}
/*** text/utf-16 and text/utf-32} ***/
/* While UTF-8 is unicode too, using text/plain for UTF-16 and UTF-32
is going to break stuff. */
typedef struct
{
size_t bomlen;
const char *const bom;
gboolean (*checker) (const guint8 *, gint, gint);
int boost;
int endianness;
} GstUnicodeTester;
static gboolean
check_utf16 (const guint8 * data, gint len, gint endianness)
{
GstByteReader br;
guint16 high, low;
low = high = 0;
if (len & 1)
return FALSE;
gst_byte_reader_init (&br, data, len);
while (len >= 2) {
/* test first for a single 16 bit value in the BMP */
if (endianness == G_BIG_ENDIAN)
high = gst_byte_reader_get_uint16_be_unchecked (&br);
else
high = gst_byte_reader_get_uint16_le_unchecked (&br);
if (high >= 0xD800 && high <= 0xDBFF) {
/* start of a surrogate pair */
if (len < 4)
return FALSE;
len -= 2;
if (endianness == G_BIG_ENDIAN)
low = gst_byte_reader_get_uint16_be_unchecked (&br);
else
low = gst_byte_reader_get_uint16_le_unchecked (&br);
if (low >= 0xDC00 && low <= 0xDFFF) {
/* second half of the surrogate pair */
} else
return FALSE;
} else {
if (high >= 0xDC00 && high <= 0xDFFF)
return FALSE;
}
len -= 2;
}
return TRUE;
}
static gboolean
check_utf32 (const guint8 * data, gint len, gint endianness)
{
if (len & 3)
return FALSE;
while (len > 3) {
guint32 v;
if (endianness == G_BIG_ENDIAN)
v = GST_READ_UINT32_BE (data);
else
v = GST_READ_UINT32_LE (data);
if (v >= 0x10FFFF)
return FALSE;
data += 4;
len -= 4;
}
return TRUE;
}
static void
unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester,
guint n_tester, const char *media_type, gboolean require_bom)
{
gsize n;
gsize len = 4;
const guint8 *data = gst_type_find_peek (tf, 0, len);
int prob = -1;
const gint max_scan_size = 256 * 1024;
int endianness = 0;
if (!data) {
len = 2;
data = gst_type_find_peek (tf, 0, len);
if (!data)
return;
}
/* find a large enough size that works */
while (len < max_scan_size) {
size_t newlen = len << 1;
const guint8 *newdata = gst_type_find_peek (tf, 0, newlen);
if (!newdata)
break;
len = newlen;
data = newdata;
}
for (n = 0; n < n_tester; ++n) {
int bom_boost = 0, tmpprob;
if (len >= tester[n].bomlen) {
if (!memcmp (data, tester[n].bom, tester[n].bomlen))
bom_boost = tester[n].boost;
}
if (require_bom && bom_boost == 0)
continue;
if (!(*tester[n].checker) (data, len, tester[n].endianness))
continue;
tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost;
if (tmpprob > prob) {
prob = tmpprob;
endianness = tester[n].endianness;
}
}
if (prob > 0) {
GST_DEBUG ("This is valid %s %s", media_type,
endianness == G_BIG_ENDIAN ? "be" : "le");
gst_type_find_suggest_simple (tf, prob, media_type,
"endianness", G_TYPE_INT, endianness, NULL);
}
}
static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16");
#define UTF16_CAPS gst_static_caps_get(&utf16_caps)
static void
utf16_type_find (GstTypeFind * tf, gpointer unused)
{
static const GstUnicodeTester utf16tester[2] = {
{2, "\xff\xfe", check_utf16, 10, G_LITTLE_ENDIAN},
{2, "\xfe\xff", check_utf16, 20, G_BIG_ENDIAN},
};
unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester),
"text/utf-16", TRUE);
}
static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32");
#define UTF32_CAPS gst_static_caps_get(&utf32_caps)
static void
utf32_type_find (GstTypeFind * tf, gpointer unused)
{
static const GstUnicodeTester utf32tester[2] = {
{4, "\xff\xfe\x00\x00", check_utf32, 10, G_LITTLE_ENDIAN},
{4, "\x00\x00\xfe\xff", check_utf32, 20, G_BIG_ENDIAN}
};
unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester),
"text/utf-32", TRUE);
}
/*** text/uri-list ***/
static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
#define URI_CAPS (gst_static_caps_get(&uri_caps))
#define BUFFER_SIZE 16 /* If the string is < 16 bytes we're screwed */
#define INC_BUFFER { \
pos++; \
if (pos == BUFFER_SIZE) { \
pos = 0; \
offset += BUFFER_SIZE; \
data = gst_type_find_peek (tf, offset, BUFFER_SIZE); \
if (data == NULL) return; \
} else { \
data++; \
} \
}
static void
uri_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
guint pos = 0;
guint offset = 0;
if (data) {
/* Search for # comment lines */
while (*data == '#') {
/* Goto end of line */
while (*data != '\n') {
INC_BUFFER;
}
INC_BUFFER;
}
if (!g_ascii_isalpha (*data)) {
/* Had a non alpha char - can't be uri-list */
return;
}
INC_BUFFER;
while (g_ascii_isalnum (*data)) {
INC_BUFFER;
}
if (*data != ':') {
/* First non alpha char is not a : */
return;
}
/* Get the next 2 bytes as well */
data = gst_type_find_peek (tf, offset + pos, 3);
if (data == NULL)
return;
if (data[1] != '/' && data[2] != '/') {
return;
}
gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, URI_CAPS);
}
}
/*** application/itc ***/
static GstStaticCaps itc_caps = GST_STATIC_CAPS ("application/itc");
#define ITC_CAPS (gst_static_caps_get(&itc_caps))
static void
itc_type_find (GstTypeFind * tf, gpointer unused)
{
DataScanCtx c = { 0, NULL, 0 };
guint8 magic[8] = { 0x00, 0x00, 0x01, 0x1C, 0x69, 0x74, 0x63, 0x68 };
guint8 preamble[4] = { 0x00, 0x00, 0x00, 0x02 };
guint8 artwork_marker[8] = { 0x00, 0x00, 0x00, 0x00, 0x61, 0x72, 0x74, 0x77 };
guint8 item_marker[4] = { 0x69, 0x74, 0x65, 0x6D };
GstTypeFindProbability itc_prob = GST_TYPE_FIND_NONE;
int i;
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8)))
return;
if (memcmp (c.data, magic, 8))
return;
/* At least we found the right magic */
itc_prob = GST_TYPE_FIND_MINIMUM;
data_scan_ctx_advance (tf, &c, 8);
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
goto done;
/* Check preamble 3 consecutive times */
for (i = 0; i < 3; i++) {
if (memcmp (c.data, preamble, 4))
goto done;
data_scan_ctx_advance (tf, &c, 4);
}
itc_prob = GST_TYPE_FIND_POSSIBLE;
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8)))
goto done;
if (memcmp (c.data, artwork_marker, 8))
goto done;
itc_prob = GST_TYPE_FIND_LIKELY;
data_scan_ctx_advance (tf, &c, 8);
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 256)))
goto done;
/* ...and 256 0x00 padding bytes on what looks like the header's end */
for (i = 0; i < 256; i++) {
if (c.data[i])
goto done;
}
itc_prob = GST_TYPE_FIND_NEARLY_CERTAIN;
data_scan_ctx_advance (tf, &c, 256);
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8)))
goto done;
if (memcmp (c.data + 4, item_marker, 4))
goto done;
itc_prob = GST_TYPE_FIND_MAXIMUM;
done:
gst_type_find_suggest (tf, itc_prob, ITC_CAPS);
}
/*** application/x-hls ***/
static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls");
#define HLS_CAPS (gst_static_caps_get(&hls_caps))
/* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */
static void
hls_type_find (GstTypeFind * tf, gpointer unused)
{
DataScanCtx c = { 0, NULL, 0 };
/* Minimum useful size is #EXTM3U\n + 1 tag + ':' = 30 bytes */
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 30)))
return;
if (memcmp (c.data, "#EXTM3U", 7))
return;
data_scan_ctx_advance (tf, &c, 7);
/* Check only the first 4KB */
while (c.offset < 4096) {
if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21)))
return;
/* Search for # comment lines */
if (c.data[0] == '#' && (memcmp (c.data, "#EXT-X-TARGETDURATION", 21) == 0
|| memcmp (c.data, "#EXT-X-STREAM-INF", 17) == 0
|| memcmp (c.data, "#EXT-X-MEDIA", 12) == 0)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HLS_CAPS);
return;
}
data_scan_ctx_advance (tf, &c, 1);
}
}
/*** application/xml **********************************************************/
#define XML_BUFFER_SIZE 16
#define XML_INC_BUFFER { \
pos++; \
if (pos == XML_BUFFER_SIZE) { \
pos = 0; \
offset += XML_BUFFER_SIZE; \
data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE); \
if (data == NULL) return FALSE; \
} else { \
data++; \
} \
}
#define XML_INC_BUFFER_DATA { \
pos++; \
if (pos >= length) { \
return FALSE; \
} else { \
data++; \
} \
}
static gboolean
xml_check_first_element_from_data (const guint8 * data, guint length,
const gchar * element, guint elen, gboolean strict)
{
gboolean got_xmldec;
guint pos = 0;
g_return_val_if_fail (data != NULL, FALSE);
if (length <= 5)
return FALSE;
/* look for the XMLDec
* see XML spec 2.8, Prolog and Document Type Declaration
* http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
got_xmldec = (memcmp (data, "<?xml", 5) == 0);
if (strict && !got_xmldec)
return FALSE;
/* skip XMLDec in any case if we've got one */
if (got_xmldec) {
pos += 5;
data += 5;
}
/* look for the first element, it has to be the requested element. Bail
* out if it is not within the first 4kB. */
while (pos < MIN (4096, length)) {
while (*data != '<' && pos < MIN (4096, length)) {
XML_INC_BUFFER_DATA;
}
XML_INC_BUFFER_DATA;
if (!g_ascii_isalpha (*data)) {
/* if not alphabetic, it's a PI or an element / attribute declaration
* like <?xxx or <!xxx */
XML_INC_BUFFER_DATA;
continue;
}
/* the first normal element, check if it's the one asked for */
if (pos + elen + 1 >= length)
return FALSE;
return (element && strncmp ((const char *) data, element, elen) == 0);
}
return FALSE;
}
static gboolean
xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
gboolean strict)
{
gboolean got_xmldec;
const guint8 *data;
guint offset = 0;
guint pos = 0;
data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
if (!data)
return FALSE;
/* look for the XMLDec
* see XML spec 2.8, Prolog and Document Type Declaration
* http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
got_xmldec = (memcmp (data, "<?xml", 5) == 0);
if (strict && !got_xmldec)
return FALSE;
/* skip XMLDec in any case if we've got one */
if (got_xmldec) {
pos += 5;
data += 5;
}
/* look for the first element, it has to be the requested element. Bail
* out if it is not within the first 4kB. */
while (data && (offset + pos) < 4096) {
while (*data != '<' && (offset + pos) < 4096) {
XML_INC_BUFFER;
}
XML_INC_BUFFER;
if (!g_ascii_isalpha (*data)) {
/* if not alphabetic, it's a PI or an element / attribute declaration
* like <?xxx or <!xxx */
XML_INC_BUFFER;
continue;
}
/* the first normal element, check if it's the one asked for */
data = gst_type_find_peek (tf, offset + pos, elen + 1);
return (data && element && strncmp ((char *) data, element, elen) == 0);
}
return FALSE;
}
static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml");
#define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps))
static void
xml_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "", 0, TRUE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
}
}
/*** application/dash+xml ****************************************************/
static GstStaticCaps dash_caps = GST_STATIC_CAPS ("application/dash+xml");
#define DASH_CAPS gst_static_caps_get (&dash_caps)
static void
dash_mpd_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "MPD", 3, FALSE) ||
xml_check_first_element (tf, "mpd", 3, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DASH_CAPS);
}
}
/*** application/xges ****************************************************/
static GstStaticCaps xges_caps = GST_STATIC_CAPS ("application/xges");
#define XGES_CAPS gst_static_caps_get (&xges_caps)
static void
xges_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "ges", 3, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, XGES_CAPS);
}
}
/***application/vnd.apple-fcp+xml ****************************************************/
static GstStaticCaps fcpxml_caps =
GST_STATIC_CAPS ("application/vnd.apple-fcp+xml");
#define FCPXML_CAPS gst_static_caps_get (&fcpxml_caps)
static void
fcpxml_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "fcpxml", 3, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FCPXML_CAPS);
}
}
/*** application/vnd.apple-xmeml+xml ****************************************************/
static GstStaticCaps xmeml_caps =
GST_STATIC_CAPS ("application/vnd.apple-xmeml+xml");
#define XMEML_CAPS gst_static_caps_get (&xmeml_caps)
static void
xmeml_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "xmeml", 3, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, XMEML_CAPS);
}
}
/*** application/otio ****************************************************/
static GstStaticCaps otio_caps =
GST_STATIC_CAPS ("application/vnd.pixar.opentimelineio+json");
#define OTIO_CAPS gst_static_caps_get (&otio_caps)
static void
otio_type_find (GstTypeFind * tf, gpointer unused)
{
const gchar *data, *tmp;
data = (const gchar *) gst_type_find_peek (tf, 0, 30);
if (!data)
return;
tmp = (const gchar *) memchr (data, '{', 30);
if (!tmp)
return;
data = (const gchar *) gst_type_find_peek (tf, tmp - data, 30);
if (!data)
return;
tmp = (const gchar *) memchr (data, '"', 30);
if (!tmp)
return;
data = (const gchar *) gst_type_find_peek (tf, tmp - data, 14);
if (!data)
return;
if (memcmp (data, "\"OTIO_SCHEMA\":", 14) == 0) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, OTIO_CAPS);
}
}
/*** application/sdp *********************************************************/
static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp");
#define SDP_CAPS (gst_static_caps_get(&sdp_caps))
static gboolean
sdp_check_header (GstTypeFind * tf)
{
const guint8 *data;
data = gst_type_find_peek (tf, 0, 5);
if (!data)
return FALSE;
/* sdp must start with v=0[\r]\n */
if (memcmp (data, "v=0", 3))
return FALSE;
if (data[3] == '\r' && data[4] == '\n')
return TRUE;
if (data[3] == '\n')
return TRUE;
return FALSE;
}
static void
sdp_type_find (GstTypeFind * tf, gpointer unused)
{
if (sdp_check_header (tf))
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS);
}
/*** application/smil *********************************************************/
static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil");
#define SMIL_CAPS (gst_static_caps_get(&smil_caps))
static void
smil_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "smil", 4, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
}
}
/*** application/ttml+xml *****************************************************/
static GstStaticCaps ttml_xml_caps = GST_STATIC_CAPS ("application/ttml+xml");
#define TTML_XML_CAPS (gst_static_caps_get(&ttml_xml_caps))
static void
ttml_xml_type_find (GstTypeFind * tf, gpointer unused)
{
if (xml_check_first_element (tf, "tt", 2, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTML_XML_CAPS);
}
}
/*** text/html ***/
static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html");
#define HTML_CAPS gst_static_caps_get (&html_caps)
static void
html_type_find (GstTypeFind * tf, gpointer unused)
{
const gchar *d, *data;
data = (const gchar *) gst_type_find_peek (tf, 0, 16);
if (!data)
return;
if (!g_ascii_strncasecmp (data, "<!DOCTYPE HTML", 14)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
} else if (xml_check_first_element (tf, "html", 4, FALSE)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
} else if ((d = memchr (data, '<', 16))) {
data = (const gchar *) gst_type_find_peek (tf, d - data, 6);
if (data && g_ascii_strncasecmp (data, "<html>", 6) == 0) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS);
}
}
}
/*** audio/midi ***/
static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi");
#define MID_CAPS gst_static_caps_get(&mid_caps)
static void
mid_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = gst_type_find_peek (tf, 0, 4);
/* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */
if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h'
&& data[3] == 'd')
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS);
}
/*** audio/mobile-xmf ***/
static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf");
#define MXMF_CAPS gst_static_caps_get(&mxmf_caps)
static void
mxmf_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = NULL;
/* Search FileId "XMF_" 4 bytes */
data = gst_type_find_peek (tf, 0, 4);
if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
&& data[3] == '_') {
/* Search Format version "2.00" 4 bytes */
data = gst_type_find_peek (tf, 4, 4);
if (data && data[0] == '2' && data[1] == '.' && data[2] == '0'
&& data[3] == '0') {
/* Search TypeId 2 1 byte */
data = gst_type_find_peek (tf, 11, 1);
if (data && data[0] == 2) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS);
}
}
}
}
/*** video/x-fli ***/
static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
#define FLX_CAPS gst_static_caps_get(&flx_caps)
static void
flx_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = gst_type_find_peek (tf, 0, 134);
if (data) {
/* check magic and the frame type of the first frame */
if ((data[4] == 0x11 || data[4] == 0x12 ||
data[4] == 0x30 || data[4] == 0x44) &&
data[5] == 0xaf &&
((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS);
}
return;
}
data = gst_type_find_peek (tf, 0, 6);
if (data) {
/* check magic only */
if ((data[4] == 0x11 || data[4] == 0x12 ||
data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
}
return;
}
}
/*** application/x-id3 ***/
static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
#define ID3_CAPS gst_static_caps_get(&id3_caps)
static void
id3v2_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = gst_type_find_peek (tf, 0, 10);
if (data && memcmp (data, "ID3", 3) == 0 &&
data[3] != 0xFF && data[4] != 0xFF &&
(data[6] & 0x80) == 0 && (data[7] & 0x80) == 0 &&
(data[8] & 0x80) == 0 && (data[9] & 0x80) == 0) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
}
}
static void
id3v1_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = gst_type_find_peek (tf, -128, 3);
if (data && memcmp (data, "TAG", 3) == 0) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
}
}
/*** application/x-ape ***/
static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag");
#define APETAG_CAPS gst_static_caps_get(&apetag_caps)
static void
apetag_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data;
/* APEv1/2 at start of file */
data = gst_type_find_peek (tf, 0, 8);
if (data && !memcmp (data, "APETAGEX", 8)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
return;
}
/* APEv1/2 at end of file */
data = gst_type_find_peek (tf, -32, 8);
if (data && !memcmp (data, "APETAGEX", 8)) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
return;
}
}
/*** audio/x-ttafile ***/
static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
#define TTA_CAPS gst_static_caps_get(&tta_caps)
static void
tta_type_find (GstTypeFind * tf, gpointer unused)
{
const guint8 *data = gst_type_find_peek (tf, 0, 3);
if (data) {
if (memcmp (data, "TTA", 3) == 0) {
gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS);
return;
}
}
}
/*** audio/x-flac ***/
static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");