gsttypefindfunctions.c 82.3 KB
Newer Older
1
/* GStreamer
2
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
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
 *
 * 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

#include <glib/gstrfuncs.h>

#include <gst/gsttypefind.h>
#include <gst/gstelement.h>
#include <gst/gstversion.h>
#include <gst/gstinfo.h>
32
#include <gst/gstutils.h>
33
34
35

#include <string.h>
#include <ctype.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
36

37
38
39
GST_DEBUG_CATEGORY_STATIC (type_find_debug);
#define GST_CAT_DEFAULT type_find_debug

40
/*** text/plain ***/
41
static gboolean xml_check_first_element (GstTypeFind * tf,
42
    const gchar * element, guint elen, gboolean strict);
43

44

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

47
#define UTF8_CAPS gst_static_caps_get(&utf8_caps)
48
49
50
51

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

55
  /* randomly decided values */
56
57
  guint min_size = 16;          /* minimum size  */
  guint size = 32 * 1024;       /* starting size */
58
  guint probability = 95;       /* starting probability */
59
60
  guint step = 10;              /* how much we reduce probability in each
                                 * iteration */
61

62
63
  while (probability > step && size > min_size) {
    data = gst_type_find_peek (tf, offset, size);
64
65
66
67
    if (data) {
      gchar *end;
      gchar *start = (gchar *) data;

68
      if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) {   /* allow last char to be cut off */
69
70
        *prob = probability;
        return TRUE;
71
      }
72
73
      *prob = 0;
      return FALSE;
74
75
76
77
    }
    size /= 2;
    probability -= step;
  }
78
79
80
81
82
83
84
85
86
87
88
  *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 */
89
  if (xml_check_first_element (tf, "", 0, TRUE))
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
    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);
119
120
}

121
/*** text/uri-list ***/
122

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

125
#define URI_CAPS (gst_static_caps_get(&uri_caps))
126
#define BUFFER_SIZE 16          /* If the string is < 16 bytes we're screwed */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
127
128
129
130
131
132
133
134
135
136
#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++;                                                             \
  }                                                                     \
137
138
}
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
139
uri_type_find (GstTypeFind * tf, gpointer unused)
140
141
142
143
{
  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
144

145
146
147
148
149
  if (data) {
    /* Search for # comment lines */
    while (*data == '#') {
      /* Goto end of line */
      while (*data != '\n') {
150
        INC_BUFFER;
151
152
153
154
155
156
157
158
159
160
161
      }

      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
162

163
164
165
166
167
168
169
170
171
172
173
    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
174
175
176
    if (data == NULL)
      return;

177
178
179
180
181
182
183
184
    if (data[1] != '/' && data[2] != '/') {
      return;
    }

    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, URI_CAPS);
  }
}

185
186
187
188

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

#define XML_BUFFER_SIZE 256
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
189
190
191
192
193
194
195
196
197
198
#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++;                                                             \
  }                                                                     \
199
200
201
}

static gboolean
202
203
xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen,
    gboolean strict)
204
{
205
206
  gboolean got_xmldec;
  guint8 *data;
207
208
209
  guint offset = 0;
  guint pos = 0;

210
211
212
213
214
  data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE);
  if (!data)
    return FALSE;

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

  if (strict && !got_xmldec)
220
221
    return FALSE;

222
223
224
225
226
  /* skip XMLDec in any case if we've got one */
  if (got_xmldec) {
    pos += 5;
    data += 5;
  }
227

228
  /* look for the first element, it has to be the requested element */
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
  while (data) {
    while (*data != '<') {
      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)
{
256
  if (xml_check_first_element (tf, "", 0, TRUE)) {
257
258
259
260
261
262
263
264
265
266
267
268
    gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_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)
{
269
  if (xml_check_first_element (tf, "smil", 4, FALSE)) {
270
271
272
273
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS);
  }
}

274
/*** video/x-fli ***/
275

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

278
#define FLX_CAPS gst_static_caps_get(&flx_caps)
279
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
280
flx_type_find (GstTypeFind * tf, gpointer unused)
281
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
282
  guint8 *data = gst_type_find_peek (tf, 0, 134);
283
284
285
286

  if (data) {
    /* check magic and the frame type of the first frame */
    if ((data[4] == 0x11 || data[4] == 0x12 ||
287
288
289
            data[4] == 0x30 || data[4] == 0x44) &&
        data[5] == 0xaf &&
        ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) {
290
291
292
293
294
295
296
297
      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 ||
298
            data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) {
299
300
301
302
303
304
      gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS);
    }
    return;
  }
}

305
/*** application/x-id3 ***/
306

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

309
#define ID3_CAPS gst_static_caps_get(&id3_caps)
310
static void
311
id3v2_type_find (GstTypeFind * tf, gpointer unused)
312
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
313
314
  guint8 *data = gst_type_find_peek (tf, 0, 10);

315
316
317
318
319
  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);
320
  }
321
322
323
324
325
326
327
}

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

328
  if (data && memcmp (data, "TAG", 3) == 0) {
329
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS);
330
331
332
  }
}

333
/*** application/x-ape ***/
334
335
336
337
338
339
340
341
342
343
344
345

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)) {
346
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
347
348
349
350
351
352
    return;
  }

  /* APEv1/2 at end of file */
  data = gst_type_find_peek (tf, -32, 8);
  if (data && !memcmp (data, "APETAGEX", 8)) {
353
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS);
354
355
356
357
    return;
  }
}

358
/*** audio/x-ttafile ***/
359

360
static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile");
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

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

376
/*** audio/mpeg version 2, 4 ***/
377
378
379
380

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))
381
#define AAC_AMOUNT (4096)
382
383
384
static void
aac_type_find (GstTypeFind * tf, gpointer unused)
{
385
386
387
388
389
390
391
392
393
  guint8 *data = gst_type_find_peek (tf, 0, AAC_AMOUNT);
  gint snc;

  /* 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.
394
   */
395
  if (data) {
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    gint n;

    for (n = 0; n < AAC_AMOUNT - 3; n++) {
      snc = GST_READ_UINT16_BE (&data[n]);
      if ((snc & 0xfff6) == 0xfff0) {
        /* ADTS header - find frame length */
        gint len;

        GST_DEBUG ("Found one ADTS syncpoint at offset 0x%x, tracing next...",
            n);
        if (AAC_AMOUNT - n < 5) {
          GST_DEBUG ("Not enough data to parse ADTS header");
          break;
        }
        len = ((data[n + 3] & 0x03) << 11) |
            (data[n + 4] << 3) | ((data[n + 5] & 0xe0) >> 5);
        if (n + len + 2 >= AAC_AMOUNT) {
          GST_DEBUG ("Next frame is not within reach");
          break;
        } else if (len == 0) {
          continue;
        }
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
        snc = GST_READ_UINT16_BE (&data[n + len]);
        if ((snc & 0xfff6) == 0xfff0) {
          gint mpegversion = (data[n + 1] & 0x08) ? 2 : 4;
          GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
              "framed", G_TYPE_BOOLEAN, FALSE,
              "mpegversion", G_TYPE_INT, mpegversion,
              NULL);

          gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps);
          gst_caps_unref (caps);

          GST_DEBUG ("Found ADTS-%d syncpoint at offset 0x%x (framelen %u)",
              mpegversion, n, len);
          break;
        }

        GST_DEBUG ("No next frame found... (should be at 0x%x)", n + len);
      } else if (!memcmp (&data[n], "ADIF", 4)) {
        /* ADIF header */
        GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
            "framed", G_TYPE_BOOLEAN, FALSE,
            "mpegversion", G_TYPE_INT, 4,
            NULL);

        gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps);
        gst_caps_unref (caps);
      }
446
    }
447
448
449
  }
}

450
/*** audio/mpeg version 1 ***/
451

452
/*
453
454
455
456
 * 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
457
458
459
460
461
462
 * 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%
463
464
465
466
467
468
469
470
471
472
473
 * 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
474
static const guint mp3types_bitrates[2][3][16] =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
475
476
477
478
479
480
    { {{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,}},
481
482
};

Stefan Kost's avatar
Stefan Kost committed
483
static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000},
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
484
485
486
{22050, 24000, 16000},
{44100, 48000, 32000}
};
487
488

static inline guint
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
489
mp3_type_frame_length_from_header (guint32 header, guint * put_layer,
490
491
    guint * put_channels, guint * put_bitrate, guint * put_samplerate,
    gboolean * may_be_free_format, gint possible_free_framelen)
492
{
493
  guint bitrate, layer, length, mode, samplerate, version, channels;
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

  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;
518
519
520
521
522
  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))
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
    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];
542
543
544
545
546
547
548
549
550
551
  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);
    }
552
  } else {
553
554
555
556
557
558
559
560
    /* 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;
    }
561
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
562

563
  GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
564
565
566
567
  GST_LOG
      ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u"
      " - channels = %u", samplerate, bitrate, layer, version, channels);

568
569
570
571
572
573
574
575
576
577
578
579
580
  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
581
582
static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, "
    "mpegversion = (int) 1, layer = (int) [ 1, 3 ]");
583
#define MP3_CAPS (gst_static_caps_get(&mp3_caps))
584
585
586
587
/*
 * 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
588
 *        / TRY_SYNC)
589
 * if found_headers >= MIN_HEADERS
590
 */
591
592
#define GST_MP3_TYPEFIND_MIN_HEADERS (2)
#define GST_MP3_TYPEFIND_TRY_HEADERS (5)
593
#define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */
594
#define GST_MP3_TYPEFIND_SYNC_SIZE (2048)
595
596

static void
597
598
mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off,
    guint * found_layer, GstTypeFindProbability * found_prob)
599
600
{
  guint8 *data = NULL;
601
  guint8 *data_end = NULL;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
602
603
  guint size;
  guint64 skipped;
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  gint last_free_offset = -1;
  gint last_free_framelen = -1;

  *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;
621
      data_end = data + size;
622
623
624
625
626
627
628
629
630
631
632
633
634
635
    }
    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;

636
637
        if ((gint64) (offset - skipped + 4) >= 0 &&
            data + offset - skipped + 4 < data_end) {
638
639
640
641
642
          head_data = data + offset - skipped;
        } else {
          head_data = gst_type_find_peek (tf, offset + start_off, 4);
        }
        if (!head_data)
643
          break;
644
645
646
647
648
649
650
651
652
653
654
        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;
655
            }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
656
          } else {
657
            last_free_framelen = -1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
658
          }
659
660
661
662
663
664

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

        if (probability < GST_TYPE_FIND_MINIMUM)
          probability = GST_TYPE_FIND_MINIMUM;
        if (start_off > 0)
          probability /= 2;

        GST_INFO
            ("audio/mpeg calculated %u  =  %u  *  %u / %u  *  (%u - %u) / %u",
            probability, GST_TYPE_FIND_MAXIMUM, found,
            GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC, skipped,
            GST_MP3_TYPEFIND_TRY_SYNC);
        /* make sure we're not id3 tagged */
        head_data = gst_type_find_peek (tf, -128, 3);
706
        if (head_data && (memcmp (head_data, "TAG", 3) == 0)) {
707
          probability = 0;
708
        }
709
710
711
712
713
714
        g_assert (probability <= GST_TYPE_FIND_MAXIMUM);

        *found_prob = probability;
        if (probability > 0)
          *found_layer = layer;
        return;
715
716
      }
    }
717
718
719
    data++;
    skipped++;
    size--;
720
  }
721
}
722

723
724
725
726
727
728
729
static void
mp3_type_find (GstTypeFind * tf, gpointer unused)
{
  GstTypeFindProbability prob, mid_prob;
  guint8 *data;
  guint layer, mid_layer;
  guint64 length;
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
  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 */
765
  data = gst_type_find_peek (tf, 0, 4); /* use min. frame size? */
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
  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:
  {
781
    GstCaps *caps;
782
783
784
785
786
787
788
789
790

    g_assert (layer > 0);

    caps = gst_caps_make_writable (MP3_CAPS);
    gst_structure_set (gst_caps_get_structure (caps, 0), "layer",
        G_TYPE_INT, layer, NULL);
    gst_type_find_suggest (tf, prob, caps);
    gst_caps_unref (caps);
    return;
791
  }
792
793
}

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*** audio/x-musepack ***/

static GstStaticCaps musepack_caps = GST_STATIC_CAPS ("audio/x-musepack");

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

  if (data && memcmp (data, "MP+", 3) == 0) {
    if ((data[3] & 0x7f) == 7) {
      gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MUSEPACK_CAPS);
    } else {
      gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 10, MUSEPACK_CAPS);
    }
  }
}

813
/*** audio/x-ac3 ***/
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3");

#define AC3_CAPS (gst_static_caps_get(&ac3_caps))

static void
ac3_type_find (GstTypeFind * tf, gpointer unused)
{
  guint8 *data = gst_type_find_peek (tf, 0, 2);

  if (data) {
    /* pretty lame method... */
    if (data[0] == 0x0b && data[1] == 0x77) {
      gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, AC3_CAPS);
      return;
    }
  }
}

832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*** 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)
{
847
848
849
  guint64 offset;
  guint32 blocksize;
  guint8 *data;
850

851
  data = gst_type_find_peek (tf, 0, 32);
852
853
854
  if (!data)
    return;

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
  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) {
      GST_LOG ("chunk length too big (%u > %u)", sublen, blocksize - offset);
      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;
896
897
      }
    }
898
    offset += sublen;
899
900
901
902
  }
}


903
/*** video/mpeg systemstream ***/
904

David Schleef's avatar
David Schleef committed
905
906
static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, "
    "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]");
907

908
#define MPEG_SYS_CAPS gst_static_caps_get(&mpeg_sys_caps)
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
#define IS_MPEG_HEADER(data)            ((((guint8 *)(data))[0] == 0x00) &&  \
                                         (((guint8 *)(data))[1] == 0x00) &&  \
                                         (((guint8 *)(data))[2] == 0x01))

#define IS_MPEG_PACK_HEADER(data)       (IS_MPEG_HEADER (data) &&            \
                                         (((guint8 *)(data))[3] == 0xBA))

#define IS_MPEG_SYSTEM_HEADER(data)     (IS_MPEG_HEADER (data) &&            \
                                         (((guint8 *)(data))[3] == 0xBB))
#define IS_MPEG_PACKET_HEADER(data)     (IS_MPEG_HEADER (data) &&            \
                                         ((((guint8 *)(data))[3] & 0x80) == 0x80))

#define IS_MPEG_PES_HEADER(data)        (IS_MPEG_HEADER (data) &&            \
                                         ((((guint8 *)(data))[3] == 0xE0) || \
                                          (((guint8 *)(data))[3] == 0xC0) || \
                                          (((guint8 *)(data))[3] == 0xBD)))
925

926
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
927
mpeg2_sys_type_find (GstTypeFind * tf, gpointer unused)
928
929
{
  guint8 *data = gst_type_find_peek (tf, 0, 5);
930
  gint mpegversion;
931

932
  if (data && IS_MPEG_PACK_HEADER (data)) {
933
934
    if ((data[4] & 0xC0) == 0x40) {
      /* type 2 */
935
936
      mpegversion = 2;
      goto suggest;
937
    } else if ((data[4] & 0xF0) == 0x20) {
938
939
      mpegversion = 1;
      goto suggest;
940
    }
941
942
  } else if (data && IS_MPEG_PES_HEADER (data)) {
    /* PES stream */
943
944
945
946
947
948
949
    mpegversion = 2;
    goto suggest;
  }

  return;
suggest:
  {
950
951
952
    GstCaps *caps = gst_caps_copy (MPEG_SYS_CAPS);

    gst_structure_set (gst_caps_get_structure (caps, 0), "mpegversion",
953
        G_TYPE_INT, mpegversion, NULL);
954
    gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, caps);
955
    gst_caps_unref (caps);
956
957
  }
};
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
958

959
/* ATTENTION: ugly return value:
960
961
962
963
964
 * 0 -  invalid data
 * 1 - not enough data
 * anything else - size until next package
 */
static guint
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
965
mpeg1_parse_header (GstTypeFind * tf, guint64 offset)
966
{
967
  guint8 *data = gst_type_find_peek (tf, offset, 4);
968
  guint size;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
969

970
  if (!data) {
971
    GST_LOG ("couldn't get MPEG header bytes");
972
973
974
    return 1;
  }

975
  if (data[0] != 0 || data[1] != 0 || data[2] != 1) {
976
977
    return 0;
  }
978
979
980
  offset += 4;

  switch (data[3]) {
981
    case 0xBA:                 /* pack header */
982
983
      data = gst_type_find_peek (tf, offset, 8);
      if (!data) {
984
985
        GST_LOG ("couldn't get MPEG pack header bytes");
        return 1;
986
987
988
989
      }
      size = 12;
      /* check marker bits */
      if ((data[0] & 0xF1) != 0x21 ||
990
991
992
993
          (data[2] & 0x01) != 0x01 ||
          (data[4] & 0x01) != 0x01 ||
          (data[5] & 0x80) != 0x80 || (data[7] & 0x01) != 0x01)
        return 0;
994
995
      break;

996
    case 0xB9:                 /* ISO end code */
997
998
999
      size = 4;
      break;

1000
    case 0xBB:                 /* system header */
For faster browsing, not all history is shown. View entire blame