Bug 862088 - Add raw mp3 sniffing.

Attempt to sniff raw mp3 with no id3 tag by looking for a header
followed by a second header at the expected offset from the packet
length.

Adds mp3 sniffing with and without id3 tags to the mediasniffer
xpcshell tests. These files are copies of files in content/media/test
truncated to 512 bytes. Our method can require up to 1.5k of data,
but these have 208/209 byte packets so sniffing still works.

notags-bad.mp3 has a corrupt header, and verifies sniffing fails.

Attempt to sniff raw mp3 with no id3 tag by looking for a header
followed by a second header at the expected offset from the packet
length.

Adds mp3 sniffing with and without id3 tags to the mediasniffer
xpcshell tests. These files are copies of files in content/media/test
truncated to 512 bytes. Our method can require up to 1.5k of data,
but these have 208/209 byte packets so sniffing still works.

notags-bad.mp3 has a corrupt header, and verifies sniffing fails.
This commit is contained in:
Ralph Giles 2013-05-23 01:07:00 +08:00
parent f47be48b4f
commit e77742990d
9 changed files with 209 additions and 2 deletions

View File

@ -21,5 +21,9 @@ CPPSRCS = \
nsMediaSnifferModule.cpp \
$(NULL)
CSRCS = \
mp3sniff.c
$(NULL)
include $(topsrcdir)/config/config.mk
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,167 @@
/* 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "mp3sniff.h"
/* Maximum packet size is 320 kbits/s * 144 / 32 kHz + 1 padding byte */
#define MP3_MAX_SIZE 1441
typedef struct {
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. */
static void mp3_parse(const uint8_t *p, mp3_header *header)
{
const int bitrates[16] =
{0, 32000, 40000, 48000, 56000, 64000, 80000, 96000,
112000, 128000, 160000, 192000, 224000, 256000, 320000, 0};
const int samplerates[4] = {44100, 48000, 32000, 0};
header->version = (p[1] & 0x08) >> 3;
header->layer = 4 - ((p[1] & 0x06) >> 1);
header->errp = (p[1] & 0x01);
header->bitrate = bitrates[(p[2] & 0xf0) >> 4];
header->freq = samplerates[(p[2] & 0x0c) >> 2];
header->pad = (p[2] & 0x02) >> 1;
header->priv = (p[2] & 0x01);
header->mode = (p[3] & 0xc0) >> 6;
header->modex = (p[3] & 0x30) >> 4;
header->copyright = (p[3] & 0x08) >> 3;
header->original = (p[3] & 0x04) >> 2;
header->emphasis = (p[3] & 0x03);
}
/* calculate the size of an mp3 frame from its header */
static int mp3_framesize(mp3_header *header)
{
int size;
int scale;
if (header->layer == 1) scale = 48;
else scale = 144;
size = header->bitrate * scale / header->freq;
/* divide by an extra factor of 2 for MPEG-2? */
if (header->pad) size += 1;
return size;
}
static int is_mp3(const uint8_t *p, long length) {
/* Do we have enough room to see a 4 byte header? */
if (length < 4) return 0;
/* 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) return 0; /* No layer 4 */
if (((p[2] & 0xf0) >> 4) == 15) return 0; /* Bitrate can't be 1111 */
if (((p[2] & 0x0c) >> 2) == 3) return 0; /* Samplerate can't be 11 */
/* Looks like a header. */
return 1;
}
return 0;
}
/* Identify an ID3 tag based on its header. */
/* http://id3.org/id3v2.4.0-structure */
static int is_id3(const uint8_t *p, long length) {
/* Do we have enough room to see the header? */
if (length < 10) return 0;
/* Do we have a sync pattern? */
if (p[0] == 'I' && p[1] == 'D' && p[2] == '3') {
if (p[3] == 0xff || p[4] == 0xff) return 0; /* Illegal version. */
if (p[6] & 0x80 || p[7] & 0x80 ||
p[8] & 0x80) return 0; /* Bad length encoding. */
/* Looks like an id3 header. */
return 1;
}
return 0;
}
/* Calculate the size of an id3 tag structure from its header. */
static int id3_framesize(const uint8_t *p, long length)
{
int size;
/* Header is 10 bytes. */
if (length < 10) {
return 0;
}
/* 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, *q;
long skip;
long avail;
p = buf;
q = p;
avail = length;
while (avail > 0) {
if (is_id3(p, avail)) {
/* Skip over any id3 tags */
skip = id3_framesize(p, avail);
p += skip;
avail -= skip;
/* Is there enough data to continue? */
if (skip + 4 > avail) {
return 0;
}
} else if (is_mp3(p, avail)) {
mp3_parse(p, &header);
skip = mp3_framesize(&header);
if (skip + 4 > avail) {
return 0;
}
dump_header(&header, out);
p += skip;
avail -= skip;
/* Check for a second header at the expected offset. */
if (is_mp3(p, avail)) {
/* Looks like mp3. */
return 1;
} else {
/* Give up. */
return 0;
}
} else {
/* Advance one byte and look for sync again. */
p++;
avail--;
/* Don't scan for more than the maximum frame size. */
if (length - avail > MP3_MAX_SIZE) {
return 0;
}
}
}
return 0;
}

View File

@ -0,0 +1,15 @@
/* 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/. */
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
int mp3_sniff(const uint8_t *buf, long length);
#ifdef __cplusplus
}
#endif

View File

@ -10,6 +10,7 @@
#include "nsString.h"
#include "nsMimeTypes.h"
#include "mozilla/ModuleUtils.h"
#include "mp3sniff.h"
#ifdef MOZ_WEBM
#include "nestegg/nestegg.h"
#endif
@ -84,6 +85,13 @@ static bool MatchesWebM(const uint8_t* aData, const uint32_t aLength)
#endif
}
// This function implements mp3 sniffing based on parsing
// packet headers and looking for expected boundaries.
static bool MatchesMP3(const uint8_t* aData, const uint32_t aLength)
{
return mp3_sniff(aData, (long)aLength);
}
NS_IMETHODIMP
nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest,
const uint8_t* aData,
@ -134,6 +142,11 @@ nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest,
return NS_OK;
}
if (MatchesMP3(aData, clampedLength)) {
aSniffedType.AssignLiteral(AUDIO_MP3);
return NS_OK;
}
// Could not sniff the media type, we are required to set it to
// application/octet-stream.
aSniffedType.AssignLiteral(APPLICATION_OCTET_STREAM);

View File

@ -18,15 +18,23 @@ var httpserver = new HttpServer();
var testRan = 0;
// The tests files we want to test, and the type we should have after sniffing.
// Those file are real webm and mkv files truncated to 512 bytes.
const tests = [
// Real webm and mkv files truncated to 512 bytes.
{ path: "data/file.webm", expected: "video/webm" },
{ path: "data/file.mkv", expected: "application/octet-stream" },
// MP3 files with and without id3 headers truncated to 512 bytes.
// NB these have 208/209 byte frames, but mp3 can require up to
// 1445 bytes to detect with our method.
{ path: "data/id3tags.mp3", expected: "audio/mpeg" },
{ path: "data/notags.mp3", expected: "audio/mpeg" },
// Padding bit flipped in the first header: sniffing should fail.
{ path: "data/notags-bad.mp3", expected: "application/octet-stream" },
];
// A basic listener that reads checks the if we sniffed properly.
var listener = {
onStartRequest: function(request, context) {
do_print("Sniffing " + tests[testRan].path);
do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, tests[testRan].expected);
},

View File

@ -3,4 +3,4 @@ head =
tail =
[test_mediasniffer.js]
[test_mediasniffer_webm.js]
[test_mediasniffer_ext.js]