toolkit/components/mediasniffer/mp3sniff.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/mediasniffer/mp3sniff.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,157 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/* MPEG format parsing */
     1.9 +
    1.10 +#include "mp3sniff.h"
    1.11 +
    1.12 +/* Maximum packet size is 320 kbits/s * 144 / 32 kHz + 1 padding byte */
    1.13 +#define MP3_MAX_SIZE 1441
    1.14 +
    1.15 +typedef struct {
    1.16 +  int version;
    1.17 +  int layer;
    1.18 +  int errp;
    1.19 +  int bitrate;
    1.20 +  int freq;
    1.21 +  int pad;
    1.22 +  int priv;
    1.23 +  int mode;
    1.24 +  int modex;
    1.25 +  int copyright;
    1.26 +  int original;
    1.27 +  int emphasis;
    1.28 +} mp3_header;
    1.29 +
    1.30 +/* Parse the 4-byte header in p and fill in the header struct. */
    1.31 +static void mp3_parse(const uint8_t *p, mp3_header *header)
    1.32 +{
    1.33 +  const int bitrates[2][16] = {
    1.34 +        /* MPEG version 1 layer 3 bitrates. */
    1.35 +	{0,  32000,  40000,  48000,  56000,  64000,  80000,  96000,
    1.36 +         112000, 128000, 160000, 192000, 224000, 256000, 320000, 0},
    1.37 +        /* MPEG Version 2 and 2.5 layer 3 bitrates */
    1.38 +        {0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000,
    1.39 +         80000, 96000, 112000, 128000, 144000, 160000, 0} };
    1.40 +  const int samplerates[4] = {44100, 48000, 32000, 0};
    1.41 +
    1.42 +  header->version = (p[1] & 0x18) >> 3;
    1.43 +  header->layer = 4 - ((p[1] & 0x06) >> 1);
    1.44 +  header->errp = (p[1] & 0x01);
    1.45 +
    1.46 +  header->bitrate = bitrates[(header->version & 1) ? 0 : 1][(p[2] & 0xf0) >> 4];
    1.47 +  header->freq = samplerates[(p[2] & 0x0c) >> 2];
    1.48 +  if (header->version == 2) header->freq >>= 1;
    1.49 +  else if (header->version == 0) header->freq >>= 2;
    1.50 +  header->pad = (p[2] & 0x02) >> 1;
    1.51 +  header->priv = (p[2] & 0x01);
    1.52 +
    1.53 +  header->mode = (p[3] & 0xc0) >> 6;
    1.54 +  header->modex = (p[3] & 0x30) >> 4;
    1.55 +  header->copyright = (p[3] & 0x08) >> 3;
    1.56 +  header->original = (p[3] & 0x04) >> 2;
    1.57 +  header->emphasis = (p[3] & 0x03);
    1.58 +}
    1.59 +
    1.60 +/* calculate the size of an mp3 frame from its header */
    1.61 +static int mp3_framesize(mp3_header *header)
    1.62 +{
    1.63 +  int size;
    1.64 +  int scale;
    1.65 +
    1.66 +  if ((header->version & 1) == 0) scale = 72;
    1.67 +  else scale = 144;
    1.68 +  size = header->bitrate * scale / header->freq;
    1.69 +  if (header->pad) size += 1;
    1.70 +
    1.71 +  return size;
    1.72 +}
    1.73 +
    1.74 +static int is_mp3(const uint8_t *p, long length) {
    1.75 +  /* Do we have enough room to see a 4 byte header? */
    1.76 +  if (length < 4) return 0;
    1.77 +  /* Do we have a sync pattern? */
    1.78 +  if (p[0] == 0xff && (p[1] & 0xe0) == 0xe0) {
    1.79 +    /* Do we have any illegal field values? */
    1.80 +    if (((p[1] & 0x06) >> 1) == 0) return 0;  /* No layer 4 */
    1.81 +    if (((p[2] & 0xf0) >> 4) == 15) return 0; /* Bitrate can't be 1111 */
    1.82 +    if (((p[2] & 0x0c) >> 2) == 3) return 0;  /* Samplerate can't be 11 */
    1.83 +    /* Looks like a header. */
    1.84 +    if ((4 - ((p[1] & 0x06) >> 1)) != 3) return 0; /* Only want level 3 */
    1.85 +    return 1;
    1.86 +  }
    1.87 +  return 0;
    1.88 +}
    1.89 +
    1.90 +/* Identify an ID3 tag based on its header. */
    1.91 +/* http://id3.org/id3v2.4.0-structure */
    1.92 +static int is_id3(const uint8_t *p, long length) {
    1.93 +  /* Do we have enough room to see the header? */
    1.94 +  if (length < 10) return 0;
    1.95 +  /* Do we have a sync pattern? */
    1.96 +  if (p[0] == 'I' && p[1] == 'D' && p[2] == '3') {
    1.97 +    if (p[3] == 0xff || p[4] == 0xff) return 0; /* Illegal version. */
    1.98 +    if (p[6] & 0x80 || p[7] & 0x80 ||
    1.99 +        p[8] & 0x80) return 0; /* Bad length encoding. */
   1.100 +    /* Looks like an id3 header. */
   1.101 +    return 1;
   1.102 +  }
   1.103 +  return 0;
   1.104 +}
   1.105 +
   1.106 +/* Calculate the size of an id3 tag structure from its header. */
   1.107 +static int id3_framesize(const uint8_t *p, long length)
   1.108 +{
   1.109 +  int size;
   1.110 +
   1.111 +  /* Header is 10 bytes. */
   1.112 +  if (length < 10) {
   1.113 +    return 0;
   1.114 +  }
   1.115 +  /* Frame is header plus declared size. */
   1.116 +  size = 10 + (p[9] | (p[8] << 7) | (p[7] << 14) | (p[6] << 21));
   1.117 +
   1.118 +  return size;
   1.119 +}
   1.120 +
   1.121 +int mp3_sniff(const uint8_t *buf, long length)
   1.122 +{
   1.123 +  mp3_header header;
   1.124 +  const uint8_t *p, *q;
   1.125 +  long skip;
   1.126 +  long avail;
   1.127 +
   1.128 +  p = buf;
   1.129 +  q = p;
   1.130 +  avail = length;
   1.131 +  while (avail >= 4) {
   1.132 +    if (is_id3(p, avail)) {
   1.133 +      /* Skip over any id3 tags */
   1.134 +      skip = id3_framesize(p, avail);
   1.135 +      p += skip;
   1.136 +      avail -= skip;
   1.137 +    } else if (is_mp3(p, avail)) {
   1.138 +      mp3_parse(p, &header);
   1.139 +      skip = mp3_framesize(&header);
   1.140 +      if (skip < 4 || skip + 4 >= avail) {
   1.141 +        return 0;
   1.142 +      }
   1.143 +      p += skip;
   1.144 +      avail -= skip;
   1.145 +      /* Check for a second header at the expected offset. */
   1.146 +      if (is_mp3(p, avail)) {
   1.147 +        /* Looks like mp3. */
   1.148 +        return 1;
   1.149 +      } else {
   1.150 +        /* No second header. Not mp3. */
   1.151 +        return 0;
   1.152 +      }
   1.153 +    } else {
   1.154 +      /* No id3 tag or mp3 header. Not mp3. */
   1.155 +      return 0;
   1.156 +    }
   1.157 +  }
   1.158 +
   1.159 +  return 0;
   1.160 +}

mercurial