/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* MPEG format parsing */
#include"mp3sniff.h"
/* Maximum packet size is 320 kbits/s * 144 / 32 kHz + 1 padding byte */ #define MP3_MAX_SIZE 1441
typedefstruct { int version; int layer; int errp; int bitrate; int freq; int pad; int priv; int mode; int modex; int copyright; int original; int emphasis;
} mp3_header;
/* Parse the 4-byte header in p and fill in the header struct. */ staticvoid mp3_parse(const uint8_t* p, mp3_header* header) { constint bitrates[2][16] = { /* MPEG version 1 layer 3 bitrates. */
{0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 0}, /* MPEG Version 2 and 2.5 layer 3 bitrates */
{0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 0}}; constint samplerates[4] = {44100, 48000, 32000, 0};
staticint is_mp3(const uint8_t* p, long length) { /* Do we have enough room to see a 4 byte header? */ if (length < 4) return0; /* Do we have a sync pattern? */ if (p[0] == 0xff && (p[1] & 0xe0) == 0xe0) { /* Do we have any illegal field values? */ if (((p[1] & 0x06) >> 1) == 0) return0; /* No layer 4 */ if (((p[2] & 0xf0) >> 4) == 15) return0; /* Bitrate can't be 1111 */ if (((p[2] & 0x0c) >> 2) == 3) return0; /* Samplerate can't be 11 */ /* Looks like a header. */ if ((4 - ((p[1] & 0x06) >> 1)) != 3) return0; /* Only want level 3 */ return1;
} return0;
}
/* Identify an ID3 tag based on its header. */ /* http://id3.org/id3v2.4.0-structure */ staticint is_id3(const uint8_t* p, long length) { /* Do we have enough room to see the header? */ if (length < 10) return0; /* Do we have a sync pattern? */ if (p[0] == 'I' && p[1] == 'D' && p[2] == '3') { if (p[3] == 0xff || p[4] == 0xff) return0; /* Illegal version. */ if (p[6] & 0x80 || p[7] & 0x80 || p[8] & 0x80) return0; /* Bad length encoding. */ /* Looks like an id3 header. */ return1;
} return0;
}
/* Calculate the size of an id3 tag structure from its header. */ staticint id3_framesize(const uint8_t* p, long length) { int size;
/* Header is 10 bytes. */ if (length < 10) { return0;
} /* Frame is header plus declared size. */
size = 10 + (p[9] | (p[8] << 7) | (p[7] << 14) | (p[6] << 21));
return size;
}
int mp3_sniff(const uint8_t* buf, long length) {
mp3_header header; const uint8_t* p; long skip; long avail;
p = buf;
avail = length; while (avail >= 4) { if (is_id3(p, avail)) { /* Skip over any id3 tags */
skip = id3_framesize(p, avail);
p += skip;
avail -= skip;
} elseif (is_mp3(p, avail)) {
mp3_parse(p, &header);
skip = mp3_framesize(&header); if (skip < 4 || skip + 4 >= avail) { return0;
}
p += skip;
avail -= skip; /* Check for a second header at the expected offset. */ if (is_mp3(p, avail)) { /* Looks like mp3. */ return1;
} else { /* No second header. Not mp3. */ return0;
}
} else { /* No id3 tag or mp3 header. Not mp3. */ return0;
}
}
return0;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-06)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.