gsttypefindfunctions.c 123 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3
4
 * Copyright (C) 2005-2009 Tim-Philipp Müller <tim centricular net>
 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

28
#include <glib.h>
29

30
31
32
33
34
35
/* 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

36
37
38
39
#include <gst/gsttypefind.h>
#include <gst/gstelement.h>
#include <gst/gstversion.h>
#include <gst/gstinfo.h>
40
#include <gst/gstutils.h>
41

42
#include <stdio.h>
43
44
#include <string.h>
#include <ctype.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45

46
47
48
GST_DEBUG_CATEGORY_STATIC (type_find_debug);
#define GST_CAT_DEFAULT type_find_debug

49
50
51
52
53
54
55
56
/* 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;
57
  const guint8 *data;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  gint 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, gint min_len)
{
77
  const guint8 *data;
78
  guint64 len;
79
  guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len);
80
81
82
83

  if (G_LIKELY (c->size >= min_len))
    return TRUE;

84
  data = gst_type_find_peek (tf, c->offset, chunk_len);
85
86
  if (G_LIKELY (data != NULL)) {
    c->data = data;
87
    c->size = chunk_len;
88
89
90
91
92
93
94
95
    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) {
96
    len = CLAMP (len - c->offset, min_len, chunk_len);
97
98
99
100
  } else {
    len = min_len;
  }

101
102
103
  data = gst_type_find_peek (tf, c->offset, len);
  if (data != NULL) {
    c->data = data;
104
105
106
107
108
109
110
    c->size = len;
    return TRUE;
  }

  return FALSE;
}

111
/*** text/plain ***/
112
static gboolean xml_check_first_element (GstTypeFind * tf,
113
    const gchar * element, guint elen, gboolean strict);
114
static gboolean sdp_check_header (GstTypeFind * tf);
115

David Schleef's avatar
David Schleef committed
116
static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
117

118
#define UTF8_CAPS gst_static_caps_get(&utf8_caps)
119
120
121
122

static gboolean
utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset,
    GstTypeFindProbability * prob)
123
124
{
  guint8 *data;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
125

126
  /* randomly decided values */
127
128
  guint min_size = 16;          /* minimum size  */
  guint size = 32 * 1024;       /* starting size */
129
  guint probability = 95;       /* starting probability */
130
131
  guint step = 10;              /* how much we reduce probability in each
                                 * iteration */
132

133
134
  while (probability > step && size > min_size) {
    data = gst_type_find_peek (tf, offset, size);
135
136
137
138
    if (data) {
      gchar *end;
      gchar *start = (gchar *) data;

139
      if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) {   /* allow last char to be cut off */
140
141
        *prob = probability;
        return TRUE;
142
      }
143
144
      *prob = 0;
      return FALSE;
145
146
147
148
    }
    size /= 2;
    probability -= step;
  }
149
150
151
152
153
154
155
156
157
158
159
  *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 */
160
  if (xml_check_first_element (tf, "", 0, TRUE))
161
162
    return;

163
164
165
166
  /* leave sdp to the sdp typefinders */
  if (sdp_check_header (tf))
    return;

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
  /* 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);
194
195
}

196
/*** text/uri-list ***/
197

David Schleef's avatar
David Schleef committed
198
static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
199

200
#define URI_CAPS (gst_static_caps_get(&uri_caps))
201
#define BUFFER_SIZE 16          /* If the string is < 16 bytes we're screwed */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
202
203
204
205
206
207
208
209
210
211
#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++;                                                             \
  }                                                                     \
212
213
}
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
214
uri_type_find (GstTypeFind * tf, gpointer unused)
215
216
217
218
{
  guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE);
  guint pos = 0;
  guint offset = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
219

220
221
222
223
224
  if (data) {
    /* Search for # comment lines */
    while (*data == '#') {
      /* Goto end of line */
      while (*data != '\n') {
225
        INC_BUFFER;
226
227
228
229
230
231
232
233
234
235
236
      }

      INC_BUFFER;
    }

    if (!g_ascii_isalpha (*data)) {
      /* Had a non alpha char - can't be uri-list */
      return;
    }

    INC_BUFFER;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
237

238
239
240
241
242
243
244
245
246
247
248
    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);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
249
250
251
    if (data == NULL)
      return;

252
253
254
255
256
257
258
259
    if (data[1] != '/' && data[2] != '/') {
      return;
    }

    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, URI_CAPS);
  }
}

260
261
262

/*** application/xml **********************************************************/

263
#define XML_BUFFER_SIZE 16
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
264
265
266
267
268
269
270
271
272
273
#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++;                                                             \
  }                                                                     \
274
275
276
}

static gboolean
277
278
xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
    gboolean strict)
279
{
280
281
  gboolean got_xmldec;
  guint8 *data;
282
283
284
  guint offset = 0;
  guint pos = 0;

285
286
287
288
289
  data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
  if (!data)
    return FALSE;

  /* look for the XMLDec
290
291
   * see XML spec 2.8, Prolog and Document Type Declaration
   * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */
292
293
294
  got_xmldec = (memcmp (data, "<?xml", 5) == 0);

  if (strict && !got_xmldec)
295
296
    return FALSE;

297
298
299
300
301
  /* skip XMLDec in any case if we've got one */
  if (got_xmldec) {
    pos += 5;
    data += 5;
  }
302

303
304
305
306
  /* 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) {
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
      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)
{
332
  if (xml_check_first_element (tf, "", 0, TRUE)) {
333
334
335
336
    gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS);
  }
}

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*** 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)
{
  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;

355
356
357
358
  if (data[3] == '\r' && data[4] == '\n')
    return TRUE;
  if (data[3] == '\n')
    return TRUE;
359

360
  return FALSE;
361
362
363
364
365
366
367
368
369
}

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);
}

370
371
372
373
374
375
376
377
/*** 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)
{
378
  if (xml_check_first_element (tf, "smil", 4, FALSE)) {
379
380
381
382
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
  }
}

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
/*** 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)
{
  gchar *d, *data;

  data = (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 = (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);
    }
  }
}

410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*** 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)
{
  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);
}
425

426
427
428
429
430
431
432
433
434
435
/*** 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)
{
  guint8 *data = NULL;

436
  /* Search FileId "XMF_" 4 bytes */
437
438
  data = gst_type_find_peek (tf, 0, 4);
  if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F'
439
440
441
442
443
444
445
446
447
448
449
450
      && 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);
      }
    }
  }
451
452
}

453

454
/*** video/x-fli ***/
455

David Schleef's avatar
David Schleef committed
456
static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
457

458
#define FLX_CAPS gst_static_caps_get(&flx_caps)
459
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
460
flx_type_find (GstTypeFind * tf, gpointer unused)
461
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
462
  guint8 *data = gst_type_find_peek (tf, 0, 134);
463
464
465
466

  if (data) {
    /* check magic and the frame type of the first frame */
    if ((data[4] == 0x11 || data[4] == 0x12 ||
467
468
469
            data[4] == 0x30 || data[4] == 0x44) &&
        data[5] == 0xaf &&
        ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
470
471
472
473
474
475
476
477
      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 ||
478
            data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
479
480
481
482
483
484
      gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
    }
    return;
  }
}

485
/*** application/x-id3 ***/
486

David Schleef's avatar
David Schleef committed
487
static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
488

489
#define ID3_CAPS gst_static_caps_get(&id3_caps)
490
static void
491
id3v2_type_find (GstTypeFind * tf, gpointer unused)
492
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
493
494
  guint8 *data = gst_type_find_peek (tf, 0, 10);

495
496
497
498
499
  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);
500
  }
501
502
503
504
505
506
507
}

static void
id3v1_type_find (GstTypeFind * tf, gpointer unused)
{
  guint8 *data = gst_type_find_peek (tf, -128, 3);

508
  if (data && memcmp (data, "TAG", 3) == 0) {
509
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
510
511
512
  }
}

513
/*** application/x-ape ***/
514
515
516
517
518
519
520
521
522
523
524
525

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)
{
  guint8 *data;

  /* APEv1/2 at start of file */
  data = gst_type_find_peek (tf, 0, 8);
  if (data && !memcmp (data, "APETAGEX", 8)) {
526
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
527
528
529
530
531
532
    return;
  }

  /* APEv1/2 at end of file */
  data = gst_type_find_peek (tf, -32, 8);
  if (data && !memcmp (data, "APETAGEX", 8)) {
533
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
534
535
536
537
    return;
  }
}

538
/*** audio/x-ttafile ***/
539

540
static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555

#define TTA_CAPS gst_static_caps_get(&tta_caps)
static void
tta_type_find (GstTypeFind * tf, gpointer unused)
{
  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;
    }
  }
}

556
557
558
559
560
561
562
563
/*** audio/x-flac ***/
static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac");

#define FLAC_CAPS (gst_static_caps_get(&flac_caps))

static void
flac_type_find (GstTypeFind * tf, gpointer unused)
{
564
  DataScanCtx c = { 0, NULL, 0 };
565

566
  if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
567
568
    return;

569
570
  /* standard flac (also old/broken flac-in-ogg with an initial 4-byte marker
   * packet and without the usual packet framing) */
571
572
573
574
575
  if (memcmp (c.data, "fLaC", 4) == 0) {
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
    return;
  }

576
577
578
  if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
    return;

579
580
581
582
  /* flac-in-ogg, see http://flac.sourceforge.net/ogg_mapping.html */
  if (memcmp (c.data, "\177FLAC\001", 6) == 0) {
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS);
    return;
583
  }
584

585
586
587
/* disabled because it happily typefinds /dev/urandom as audio/x-flac, and
 * because I yet have to see header-less flac in the wild */
#if 0
588
  /* flac without headers (subset format) */
589
590
  /* 64K should be enough */
  while (c.offset < (64 * 1024)) {
591
    if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4)))
592
593
      break;

594
595
596
    /* look for frame header,
     * http://flac.sourceforge.net/format.html#frame_header
     */
597
    if (c.data[0] == 0xff && (c.data[1] >> 2) == 0x3e) {
598
      /* bit 15 in the header must be 0 */
599
      if (((c.data[1] >> 1) & 0x01) == 0x01)
600
        goto advance;
601
602

      /* blocksize must be != 0x00 */
603
      if ((c.data[2] >> 4) == 0x00)
604
        goto advance;
605
606

      /* samplerate must be != 0x0f */
607
      if ((c.data[2] & 0x0f) == 0x0f)
608
        goto advance;
609
610
      /* also 0 is invalid, as it means get the info from the header and we
       * don't have headers if we are here */
611
      if ((c.data[2] & 0x0f) == 0x00)
612
        goto advance;
613
614

      /* channel assignment must be < 11 */
615
      if ((c.data[3] >> 4) >= 11)
616
        goto advance;
617

618
      /* sample size must be != 0x07 and != 0x05 */
619
      if (((c.data[3] >> 1) & 0x07) == 0x07)
620
        goto advance;
621
      if (((c.data[3] >> 1) & 0x07) == 0x05)
622
623
624
        goto advance;
      /* also 0 is invalid, as it means get the info from the header and we
       * don't have headers if we are here */
625
      if (((c.data[3] >> 1) & 0x07) == 0x00)
626
        goto advance;
627
628

      /* next bit must be 0 */
629
      if ((c.data[3] & 0x01) == 0x01)
630
        goto advance;
631

632
633
634
      /* FIXME: shouldn't we include the crc check ? */

      GST_DEBUG ("Found flac without headers at %d", (gint) c.offset);
635
      gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS);
636
637
      return;
    }
638
  advance:
639
640
    data_scan_ctx_advance (tf, &c, 1);
  }
641
#endif
642
643
}

644
/*** audio/mpeg version 2, 4 ***/
645
646
647
648

static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
    "mpegversion = (int) { 2, 4 }, framed = (bool) false");
#define AAC_CAPS (gst_static_caps_get(&aac_caps))
649
#define AAC_AMOUNT (4096)
650
651
652
static void
aac_type_find (GstTypeFind * tf, gpointer unused)
{
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
  DataScanCtx c = { 0, NULL, 0 };

  while (c.offset < AAC_AMOUNT) {
    guint snc, len;

    /* detect adts header or adif header.
     * The ADIF header is 4 bytes, that should be OK. The ADTS header, on
     * the other hand, is 14 bits only, so we require one valid frame with
     * again a valid syncpoint on the next one (28 bits) for certainty. We
     * require 4 kB, which is quite a lot, since frames are generally 200-400
     * bytes.
     */
    if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6)))
      break;

    snc = GST_READ_UINT16_BE (c.data);
    if (G_UNLIKELY ((snc & 0xfff6) == 0xfff0)) {
      /* ADTS header - find frame length */
671
672
      GST_DEBUG ("Found one ADTS syncpoint at offset 0x%" G_GINT64_MODIFIER
          "x, tracing next...", c.offset);
673
674
675
676
677
678
679
      len = ((c.data[3] & 0x03) << 11) |
          (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5);

      if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
        GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
        goto next;
      }
680

681
      snc = GST_READ_UINT16_BE (c.data + len);
682
      if ((snc & 0xfff6) == 0xfff0) {
683
        gint mpegversion;
684

685
        mpegversion = (c.data[1] & 0x08) ? 2 : 4;
686
687
        GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%"
            G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len);
688
689
        gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
            "framed", G_TYPE_BOOLEAN, FALSE,
690
691
            "mpegversion", G_TYPE_INT, mpegversion,
            "stream-type", G_TYPE_STRING, "adts", NULL);
692
        break;
693
      }
694
695
696
697

      GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
    } else if (!memcmp (c.data, "ADIF", 4)) {
      /* ADIF header */
698
      gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
699
700
          "framed", G_TYPE_BOOLEAN, FALSE, "mpegversion", G_TYPE_INT, 4,
          "stream-format", G_TYPE_STRING, "adif", NULL);
701
      break;
702
    }
703
704
705
706

  next:

    data_scan_ctx_advance (tf, &c, 1);
707
708
709
  }
}

710
/*** audio/mpeg version 1 ***/
711

712
/*
713
714
715
716
 * The chance that random data is identified as a valid mp3 header is 63 / 2^18
 * (0.024%) per try. This makes the function for calculating false positives
 *   1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize)
 * This has the following probabilities of false positives:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
717
718
719
720
721
722
 * datasize               MIN_HEADERS
 * (bytes)      1       2       3       4
 * 4096         62.6%    0.02%   0%      0%
 * 16384        98%      0.09%   0%      0%
 * 1 MiB       100%      5.88%   0%      0%
 * 1 GiB       100%    100%      1.44%   0%
723
724
725
726
727
728
729
730
731
732
733
 * 1 TiB       100%    100%    100%      0.35%
 * This means that the current choice (3 headers by most of the time 4096 byte
 * buffers is pretty safe for now.
 *
 * The max. size of each frame is 1440 bytes, which means that for N frames to
 * be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 bytes of data.
 * Assuming we step into the stream right after the frame header, this
 * means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes
 * of data (5762) to always detect any mp3.
 */

Stefan Kost's avatar
Stefan Kost committed
734
static const guint mp3types_bitrates[2][3][16] =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
735
736
737
738
739
740
    { {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
    {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
{{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}},
741
742
};

Stefan Kost's avatar
Stefan Kost committed
743
static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
744
745
746
{22050, 24000, 16000},
{44100, 48000, 32000}
};
747
748

static inline guint
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
749
mp3_type_frame_length_from_header (guint32 header, guint * put_layer,
750
751
    guint * put_channels, guint * put_bitrate, guint * put_samplerate,
    gboolean * may_be_free_format, gint possible_free_framelen)
752
{
753
  guint bitrate, layer, length, mode, samplerate, version, channels;
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777

  if ((header & 0xffe00000) != 0xffe00000)
    return 0;

  /* we don't need extension, copyright, original or
   * emphasis for the frame length */
  header >>= 6;

  /* mode */
  mode = header & 0x3;
  header >>= 3;

  /* padding */
  length = header & 0x1;
  header >>= 1;

  /* sampling frequency */
  samplerate = header & 0x3;
  if (samplerate == 3)
    return 0;
  header >>= 2;

  /* bitrate index */
  bitrate = header & 0xF;
778
779
780
781
782
  if (bitrate == 0 && possible_free_framelen == -1) {
    GST_LOG ("Possibly a free format mp3 - signalling");
    *may_be_free_format = TRUE;
  }
  if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1))
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
    return 0;

  /* ignore error correction, too */
  header >>= 5;

  /* layer */
  layer = 4 - (header & 0x3);
  if (layer == 4)
    return 0;
  header >>= 2;

  /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */
  version = header & 0x3;
  if (version == 1)
    return 0;

  /* lookup */
  channels = (mode == 3) ? 1 : 2;
  samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate];
802
803
804
805
806
807
808
809
810
811
  if (bitrate == 0) {
    if (layer == 1) {
      length *= 4;
      length += possible_free_framelen;
      bitrate = length * samplerate / 48000;
    } else {
      length += possible_free_framelen;
      bitrate = length * samplerate /
          ((layer == 3 && version != 3) ? 72000 : 144000);
    }
812
  } else {
813
814
815
816
817
818
819
820
    /* calculating */
    bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate];
    if (layer == 1) {
      length = ((12000 * bitrate / samplerate) + length) * 4;
    } else {
      length += ((layer == 3
              && version != 3) ? 72000 : 144000) * bitrate / samplerate;
    }
821
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
822

823
  GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
824
825
826
827
  GST_LOG
      ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
      " - channels = %u", samplerate, bitrate, layer, version, channels);

828
829
830
831
832
833
834
835
836
837
838
839
840
  if (put_layer)
    *put_layer = layer;
  if (put_channels)
    *put_channels = channels;
  if (put_bitrate)
    *put_bitrate = bitrate;
  if (put_samplerate)
    *put_samplerate = samplerate;

  return length;
}


David Schleef's avatar
David Schleef committed
841
842
static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, "
    "mpegversion = (int) 1, layer = (int) [ 1, 3 ]");
843
#define MP3_CAPS (gst_static_caps_get(&mp3_caps))
844
845
846
847
/*
 * random values for typefinding
 * if no more data is available, we will return a probability of
 * (found_headers/TRY_HEADERS) * (MAXIMUM * (TRY_SYNC - bytes_skipped)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
848
 *        / TRY_SYNC)
849
 * if found_headers >= MIN_HEADERS
850
 */
851
852
#define GST_MP3_TYPEFIND_MIN_HEADERS (2)
#define GST_MP3_TYPEFIND_TRY_HEADERS (5)
853
#define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */
854
#define GST_MP3_TYPEFIND_SYNC_SIZE (2048)
855
#define GST_MP3_WRONG_HEADER (10)
856
857

static void
858
859
mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
    guint * found_layer, GstTypeFindProbability * found_prob)
860
861
{
  guint8 *data = NULL;
862
  guint8 *data_end = NULL;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
863
864
  guint size;
  guint64 skipped;
865
866
  gint last_free_offset = -1;
  gint last_free_framelen = -1;
867
  gboolean headerstart = TRUE;
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882

  *found_layer = 0;
  *found_prob = 0;

  size = 0;
  skipped = 0;
  while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) {
    if (size <= 0) {
      size = GST_MP3_TYPEFIND_SYNC_SIZE * 2;
      do {
        size /= 2;
        data = gst_type_find_peek (tf, skipped + start_off, size);
      } while (size > 10 && !data);
      if (!data)
        break;
883
      data_end = data + size;
884
885
886
887
888
889
890
891
892
893
894
895
896
897
    }
    if (*data == 0xFF) {
      guint8 *head_data = NULL;
      guint layer = 0, bitrate, samplerate, channels;
      guint found = 0;          /* number of valid headers found */
      guint64 offset = skipped;

      while (found < GST_MP3_TYPEFIND_TRY_HEADERS) {
        guint32 head;
        guint length;
        guint prev_layer = 0, prev_bitrate = 0;
        guint prev_channels = 0, prev_samplerate = 0;
        gboolean free = FALSE;

898
899
        if ((gint64) (offset - skipped + 4) >= 0 &&
            data + offset - skipped + 4 < data_end) {
900
901
902
903
904
          head_data = data + offset - skipped;
        } else {
          head_data = gst_type_find_peek (tf, offset + start_off, 4);
        }
        if (!head_data)
905
          break;
906
907
908
909
910
911
912
913
914
915
916
        head = GST_READ_UINT32_BE (head_data);
        if (!(length = mp3_type_frame_length_from_header (head, &layer,
                    &channels, &bitrate, &samplerate, &free,
                    last_free_framelen))) {
          if (free) {
            if (last_free_offset == -1)
              last_free_offset = offset;
            else {
              last_free_framelen = offset - last_free_offset;
              offset = last_free_offset;
              continue;
917
            }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
918
          } else {
919
            last_free_framelen = -1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
920
          }
921

922
923
924
925
          /* Mark the fact that we didn't find a valid header at the beginning */
          if (found == 0)
            headerstart = FALSE;

926
927
928
929
930
          GST_LOG ("%d. header at offset %" G_GUINT64_FORMAT
              " (0x%" G_GINT64_MODIFIER "x) was not an mp3 header "
              "(possibly-free: %s)", found + 1, start_off + offset,
              start_off + offset, free ? "yes" : "no");
          break;
931
        }
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
        if ((prev_layer && prev_layer != layer) ||
            /* (prev_bitrate && prev_bitrate != bitrate) || <-- VBR */
            (prev_samplerate && prev_samplerate != samplerate) ||
            (prev_channels && prev_channels != channels)) {
          /* this means an invalid property, or a change, which might mean
           * that this is not a mp3 but just a random bytestream. It could
           * be a freaking funky encoded mp3 though. We'll just not count
           * this header*/
          prev_layer = layer;
          prev_bitrate = bitrate;
          prev_channels = channels;
          prev_samplerate = samplerate;
        } else {
          found++;
          GST_LOG ("found %d. header at offset %" G_GUINT64_FORMAT " (0x%"
              G_GINT64_MODIFIER "X)", found, start_off + offset,
              start_off + offset);
        }
        offset += length;
      }
      g_assert (found <= GST_MP3_TYPEFIND_TRY_HEADERS);
953
954
955
956
      if (head_data == NULL &&
          gst_type_find_peek (tf, offset + start_off - 1, 1) == NULL)
        /* Incomplete last frame - don't count it. */
        found--;
957
958
959
960
961
962
963
      if (found == GST_MP3_TYPEFIND_TRY_HEADERS ||
          (found >= GST_MP3_TYPEFIND_MIN_HEADERS && head_data == NULL)) {
        /* we can make a valid guess */
        guint probability = found * GST_TYPE_FIND_MAXIMUM *
            (GST_MP3_TYPEFIND_TRY_SYNC - skipped) /
            GST_MP3_TYPEFIND_TRY_HEADERS / GST_MP3_TYPEFIND_TRY_SYNC;

964
        if (!headerstart
965
            && probability > (GST_TYPE_FIND_MINIMUM + GST_MP3_WRONG_HEADER))
966
          probability -= GST_MP3_WRONG_HEADER;
967
968
969
970
971
972
        if (probability < GST_TYPE_FIND_MINIMUM)
          probability = GST_TYPE_FIND_MINIMUM;
        if (start_off > 0)
          probability /= 2;

        GST_INFO
973
974
975
            ("audio/mpeg calculated %u  =  %u  *  %u / %u  *  (%u - %"
            G_GUINT64_FORMAT ") / %u", probability, GST_TYPE_FIND_MAXIMUM,
            found, GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC,
976
            (guint64) skipped, GST_MP3_TYPEFIND_TRY_SYNC);
977
978
        /* make sure we're not id3 tagged */
        head_data = gst_type_find_peek (tf, -128, 3);
979
        if (head_data && (memcmp (head_data, "TAG", 3) == 0)) {
980
          probability = 0;
981
        }
982
983
984
985
986
987
        g_assert (probability <= GST_TYPE_FIND_MAXIMUM);

        *found_prob = probability;
        if (probability > 0)
          *found_layer = layer;
        return;
988
989
      }
    }
990
991
992
    data++;
    skipped++;
    size--;
993
  }
994
}
995

996
997
998
999
1000
1001
1002
static void
mp3_type_find (GstTypeFind * tf, gpointer unused)
{
  GstTypeFindProbability prob, mid_prob;
  guint8 *data;
  guint layer, mid_layer;
  guint64 length;
1003

1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
  mp3_type_find_at_offset (tf, 0, &layer, &prob);
  length = gst_type_find_get_length (tf);

  if (length == 0 || length == (guint64) - 1) {
    if (prob != 0)
      goto suggest;
    return;
  }

  /* if we're pretty certain already, skip the additional check */
  if (prob >= GST_TYPE_FIND_LIKELY)
    goto suggest;

  mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob);

  if (mid_prob > 0) {
    if (prob == 0) {
      GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob);
      layer = mid_layer;
      prob = mid_prob;
      goto suggest;
    }

    if (layer != mid_layer) {
      GST_WARNING ("audio/mpeg layer discrepancy: %u vs. %u", layer, mid_layer);
      return;                   /* FIXME: or should we just go with the one in the middle? */
    }

    /* detected mpeg audio both in middle of the file and at the start */
    prob = (prob + mid_prob) / 2;
    goto suggest;
  }

  /* let's see if there's a valid header right at the start */
1038
  data = gst_type_find_peek (tf, 0, 4); /* use min. frame size? */
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  if (data && mp3_type_frame_length_from_header (GST_READ_UINT32_BE (data),
          &layer, NULL, NULL, NULL, NULL, 0) != 0) {
    if (prob == 0)
      prob = GST_TYPE_FIND_POSSIBLE - 10;
    else
      prob = MAX (GST_TYPE_FIND_POSSIBLE - 10, prob + 10);
  }

  if (prob > 0)
    goto suggest;

  return;

suggest:
  {
1054
    g_return_if_fail (layer >= 1 && layer <= 3);
1055

1056
1057
    gst_type_find_suggest_simple (tf, prob, "audio/mpeg",
        "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
1058
  }
1059
1060
}

1061
1062
/*** audio/x-musepack ***/

1063
1064
static GstStaticCaps musepack_caps =
GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }");
1065
1066
1067
1068
1069
1070

#define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps))
static void
musepack_type_find (GstTypeFind * tf, gpointer unused)
{
  guint8 *data = gst_type_find_peek (tf, 0, 4);
1071
1072
  GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM;
  gint streamversion = -1;
1073
1074

  if (data && memcmp (data, "MP+", 3) == 0) {
1075
    streamversion = 7;
1076
    if ((data[3] & 0x7f) == 7) {
1077
      prop = GST_TYPE_FIND_MAXIMUM;
1078
    } else {
1079
      prop = GST_TYPE_FIND_LIKELY + 10;
1080
    }
1081
1082
1083
1084
1085
1086
  } else if (data && memcmp (data, "MPCK", 4) == 0) {
    streamversion = 8;
    prop = GST_TYPE_FIND_MAXIMUM;
  }

  if (streamversion != -1) {
1087
1088
    gst_type_find_suggest_simple (tf, prop, "audio/x-musepack",
        "streamversion", G_TYPE_INT, streamversion, NULL);
1089
1090
1091
  }
}

1092
/*** audio/x-ac3 ***/
1093
/* This should be audio/ac3, but isn't for backwards compatibility */
1094
1095
1096
1097
static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3");

#define AC3_CAPS (gst_static_caps_get(&ac3_caps))

1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
struct ac3_frmsize
{
  unsigned short bit_rate;
  unsigned short frm_size[3];
};

static const struct ac3_frmsize ac3_frmsizecod_tbl[] = {
  {32, {64, 69, 96}},
  {32, {64, 70, 96}},
  {40, {80, 87, 120}},
  {40, {80, 88, 120}},
  {48, {96, 104, 144}},
  {48, {96, 105, 144}},
  {56, {112, 121, 168}},
  {56, {112, 122, 168}},
  {64, {128, 139, 192}},
  {64, {128, 140, 192}},
  {80, {160, 174, 240}},
  {80, {160, 175, 240}},
  {96, {192, 208, 288}},
  {96, {192, 209, 288}},
  {112, {224, 243, 336}},
  {112, {224, 244, 336}},
  {128, {256, 278, 384}},
  {128, {256, 279, 384}},
  {160, {320, 348, 480}},
  {160, {320, 349, 480}},
  {192, {384, 417, 576}},
  {192, {384, 418, 576}},
  {224, {448, 487, 672}},
  {224, {448, 488, 672}},
  {256, {512, 557, 768}},
  {256, {512, 558, 768}},
  {320, {640, 696, 960}},
  {320, {640, 697, 960}},
  {384, {768, 835, 1152}},
  {384, {768, 836, 1152}},
  {448, {896, 975, 1344}},
  {448, {896, 976, 1344}},
  {512, {1024, 1114, 1536}},
  {512, {1024, 1115, 1536}},
  {576, {1152, 1253, 1728}},
  {576, {1152, 1254, 1728}},
  {640, {1280, 1393, 1920}},
  {640, {1280, 1394, 1920}}
};


1146
1147
1148
static void
ac3_type_find (GstTypeFind * tf, gpointer unused)
{
1149
  guint8 *data = gst_type_find_peek (tf, 0, 1024);
1150
1151
1152
1153
1154
1155
1156
  gint offset = 0;

  /* Search for an ac3 frame; not neccesarily right at the start, but give it
   * a lower probability if not found right at the start. Check that the
   * frame is followed by a second frame at the expected offset.
   * We could also check the two ac3 CRCs, but we don't do that right now */
  while (data && offset < 1024) {
1157
    if (data[0] == 0x0b && data[1] == 0x77) {
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
      guint fscod = (data[4] >> 6) & 0x03;
      guint frmsizecod = data[4] & 0x3f;

      if (fscod < 3 && frmsizecod < 38) {
        guint frame_size = ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod];

        data = gst_type_find_peek (tf, offset + frame_size * 2, 5);

        if (data) {
          if (data[0] == 0x0b && data[1] == 0x77) {
            guint fscod2 = (data[4] >> 6) & 0x03;
            guint frmsizecod2 = data[4] & 0x3f;

            if (fscod == fscod2 && frmsizecod == frmsizecod2) {
              int prob;

              if (offset == 0)
                prob = GST_TYPE_FIND_MAXIMUM;
              else
                prob = GST_TYPE_FIND_NEARLY_CERTAIN;
              gst_type_find_suggest (tf, prob, AC3_CAPS);
            }
          }
        }
      }
1183
    }
1184
    offset++;
1185
1186
1187
  }
}

Wim Taymans's avatar
Wim Taymans committed
1188
1189
1190
1191
/*** gsm ***/

/* can only be detected by using the extension, in which case we use the default
 * GSM properties */
1192
1193
static GstStaticCaps gsm_caps =
GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1");
Wim Taymans's avatar
Wim Taymans committed
1194
1195
1196

#define GSM_CAPS (gst_static_caps_get(&gsm_caps))

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
/*** wavpack ***/

static GstStaticCaps wavpack_caps =
GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false");

#define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps))

static GstStaticCaps wavpack_correction_caps =
GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");

#define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps))

static void
wavpack_type_find (GstTypeFind * tf, gpointer unused)
{
1212
1213
1214
  guint64 offset;
  guint32 blocksize;
  guint8 *data;
1215

1216
  data = gst_type_find_peek (tf, 0, 32);
1217
1218
1219
  if (!data)
    return;

1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
  if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k')
    return;

  /* Note: wavpack blocks can be fairly large (easily 60-110k), possibly
   * larger than the max. limits imposed by certain typefinding elements
   * like id3demux or apedemux, so typefinding is most likely only going to
   * work in pull-mode */
  blocksize = GST_READ_UINT32_LE (data + 4);
  GST_LOG ("wavpack header, blocksize=0x%04x", blocksize);
  offset = 32;
  while (offset < 32 + blocksize) {
    guint32 sublen;

    /* get chunk header */
    GST_LOG ("peeking at chunk at offset 0x%04x", (guint) offset);
    data = gst_type_find_peek (tf, offset, 4);
    if (data == NULL)
      break;
    sublen = ((guint32) data[1]) << 1;
    if (data[0] & 0x80) {
      sublen |= (((guint32) data[2]) << 9) | (((guint32) data[3]) << 17);
      sublen += 1 + 3;          /* id + length */
    } else {
      sublen += 1 + 1;          /* id + length */
    }
    if (sublen > blocksize - offset + 32) {
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
1246
1247
      GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
          blocksize - offset);
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
      break;
    }
    if ((data[0] & 0x20) == 0) {
      switch (data[0] & 0x0f) {
        case 0xa:              /* ID_WV_BITSTREAM  */
        case 0xc:              /* ID_WVX_BITSTREAM */
          gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, WAVPACK_CAPS);
          return;
        case 0xb:              /* ID_WVC_BITSTREAM */
          gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY,
              WAVPACK_CORRECTION_CAPS);
          return;
        default:
          break;
1262
1263
      }
    }
1264
    offset += sublen;
1265
1266
1267
  }
}

1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
/*** application/postscrip ***/
static GstStaticCaps postscript_caps =
GST_STATIC_CAPS ("application/postscript");

#define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps))

static void
postscript_type_find (GstTypeFind * tf, gpointer unused)
{
  guint8 *data = gst_type_find_peek (tf, 0, 3);
  if (!data)
    return;

  if (data[0] == 0x04)
    data++;
  if (data[0] == '%' && data[1] == '!')
1284
    gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS);
1285
1286
1287

}

1288
1289
1290
1291
1292
1293
1294
1295
1296
/*** image/svg+xml ***/
static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml");

#define SVG_CAPS (gst_static_caps_get(&svg_caps))

static void
svg_type_find (GstTypeFind * tf, gpointer unused)
{
  static const gchar svg_doctype[] = "!DOCTYPE svg";
1297
  static const gchar svg_tag[] = "<svg";
1298
1299
1300
1301
1302
1303
1304
1305
1306
  DataScanCtx c = { 0, NULL, 0 };

  while (c.offset <= 1024) {
    if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12)))
      break;

    if (memcmp (svg_doctype, c.data, 12) == 0) {
      gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS);
      return;
1307
1308
1309
    } else if (memcmp (svg_tag, c.data, 4) == 0) {
      gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS);
      return;
1310
1311
1312
1313
1314
    }
    data_scan_ctx_advance (tf, &c, 1);
  }
}