michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* MPEG format parsing */ michael@0: michael@0: #include "mp3sniff.h" michael@0: michael@0: /* Maximum packet size is 320 kbits/s * 144 / 32 kHz + 1 padding byte */ michael@0: #define MP3_MAX_SIZE 1441 michael@0: michael@0: typedef struct { michael@0: int version; michael@0: int layer; michael@0: int errp; michael@0: int bitrate; michael@0: int freq; michael@0: int pad; michael@0: int priv; michael@0: int mode; michael@0: int modex; michael@0: int copyright; michael@0: int original; michael@0: int emphasis; michael@0: } mp3_header; michael@0: michael@0: /* Parse the 4-byte header in p and fill in the header struct. */ michael@0: static void mp3_parse(const uint8_t *p, mp3_header *header) michael@0: { michael@0: const int bitrates[2][16] = { michael@0: /* MPEG version 1 layer 3 bitrates. */ michael@0: {0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, michael@0: 112000, 128000, 160000, 192000, 224000, 256000, 320000, 0}, michael@0: /* MPEG Version 2 and 2.5 layer 3 bitrates */ michael@0: {0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, michael@0: 80000, 96000, 112000, 128000, 144000, 160000, 0} }; michael@0: const int samplerates[4] = {44100, 48000, 32000, 0}; michael@0: michael@0: header->version = (p[1] & 0x18) >> 3; michael@0: header->layer = 4 - ((p[1] & 0x06) >> 1); michael@0: header->errp = (p[1] & 0x01); michael@0: michael@0: header->bitrate = bitrates[(header->version & 1) ? 0 : 1][(p[2] & 0xf0) >> 4]; michael@0: header->freq = samplerates[(p[2] & 0x0c) >> 2]; michael@0: if (header->version == 2) header->freq >>= 1; michael@0: else if (header->version == 0) header->freq >>= 2; michael@0: header->pad = (p[2] & 0x02) >> 1; michael@0: header->priv = (p[2] & 0x01); michael@0: michael@0: header->mode = (p[3] & 0xc0) >> 6; michael@0: header->modex = (p[3] & 0x30) >> 4; michael@0: header->copyright = (p[3] & 0x08) >> 3; michael@0: header->original = (p[3] & 0x04) >> 2; michael@0: header->emphasis = (p[3] & 0x03); michael@0: } michael@0: michael@0: /* calculate the size of an mp3 frame from its header */ michael@0: static int mp3_framesize(mp3_header *header) michael@0: { michael@0: int size; michael@0: int scale; michael@0: michael@0: if ((header->version & 1) == 0) scale = 72; michael@0: else scale = 144; michael@0: size = header->bitrate * scale / header->freq; michael@0: if (header->pad) size += 1; michael@0: michael@0: return size; michael@0: } michael@0: michael@0: static int is_mp3(const uint8_t *p, long length) { michael@0: /* Do we have enough room to see a 4 byte header? */ michael@0: if (length < 4) return 0; michael@0: /* Do we have a sync pattern? */ michael@0: if (p[0] == 0xff && (p[1] & 0xe0) == 0xe0) { michael@0: /* Do we have any illegal field values? */ michael@0: if (((p[1] & 0x06) >> 1) == 0) return 0; /* No layer 4 */ michael@0: if (((p[2] & 0xf0) >> 4) == 15) return 0; /* Bitrate can't be 1111 */ michael@0: if (((p[2] & 0x0c) >> 2) == 3) return 0; /* Samplerate can't be 11 */ michael@0: /* Looks like a header. */ michael@0: if ((4 - ((p[1] & 0x06) >> 1)) != 3) return 0; /* Only want level 3 */ michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* Identify an ID3 tag based on its header. */ michael@0: /* http://id3.org/id3v2.4.0-structure */ michael@0: static int is_id3(const uint8_t *p, long length) { michael@0: /* Do we have enough room to see the header? */ michael@0: if (length < 10) return 0; michael@0: /* Do we have a sync pattern? */ michael@0: if (p[0] == 'I' && p[1] == 'D' && p[2] == '3') { michael@0: if (p[3] == 0xff || p[4] == 0xff) return 0; /* Illegal version. */ michael@0: if (p[6] & 0x80 || p[7] & 0x80 || michael@0: p[8] & 0x80) return 0; /* Bad length encoding. */ michael@0: /* Looks like an id3 header. */ michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* Calculate the size of an id3 tag structure from its header. */ michael@0: static int id3_framesize(const uint8_t *p, long length) michael@0: { michael@0: int size; michael@0: michael@0: /* Header is 10 bytes. */ michael@0: if (length < 10) { michael@0: return 0; michael@0: } michael@0: /* Frame is header plus declared size. */ michael@0: size = 10 + (p[9] | (p[8] << 7) | (p[7] << 14) | (p[6] << 21)); michael@0: michael@0: return size; michael@0: } michael@0: michael@0: int mp3_sniff(const uint8_t *buf, long length) michael@0: { michael@0: mp3_header header; michael@0: const uint8_t *p, *q; michael@0: long skip; michael@0: long avail; michael@0: michael@0: p = buf; michael@0: q = p; michael@0: avail = length; michael@0: while (avail >= 4) { michael@0: if (is_id3(p, avail)) { michael@0: /* Skip over any id3 tags */ michael@0: skip = id3_framesize(p, avail); michael@0: p += skip; michael@0: avail -= skip; michael@0: } else if (is_mp3(p, avail)) { michael@0: mp3_parse(p, &header); michael@0: skip = mp3_framesize(&header); michael@0: if (skip < 4 || skip + 4 >= avail) { michael@0: return 0; michael@0: } michael@0: p += skip; michael@0: avail -= skip; michael@0: /* Check for a second header at the expected offset. */ michael@0: if (is_mp3(p, avail)) { michael@0: /* Looks like mp3. */ michael@0: return 1; michael@0: } else { michael@0: /* No second header. Not mp3. */ michael@0: return 0; michael@0: } michael@0: } else { michael@0: /* No id3 tag or mp3 header. Not mp3. */ michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: return 0; michael@0: }