Skip to content

Commit

Permalink
Remove moot check for sync word at odd offsets for MPEG detection (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit authored Apr 13, 2021
1 parent fd1e72c commit 7021d9a
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1341,50 +1341,47 @@ async function _fromTokenizer(tokenizer) {
};
}

// Check for MPEG header at different starting offsets
for (let start = 0; start < 2 && start < (buffer.length - 16); start++) {
// Check MPEG 1 or 2 Layer 3 header, or 'layer 0' for ADTS (MPEG sync-word 0xFFE)
if (buffer.length >= start + 2 && check([0xFF, 0xE0], {offset: start, mask: [0xFF, 0xE0]})) {
if (check([0x10], {offset: start + 1, mask: [0x16]})) {
// Check for (ADTS) MPEG-2
if (check([0x08], {offset: start + 1, mask: [0x08]})) {
return {
ext: 'aac',
mime: 'audio/aac'
};
}

// Must be (ADTS) MPEG-4
// Check MPEG 1 or 2 Layer 3 header, or 'layer 0' for ADTS (MPEG sync-word 0xFFE)
if (buffer.length >= 2 && check([0xFF, 0xE0], {offset: 0, mask: [0xFF, 0xE0]})) {
if (check([0x10], {offset: 1, mask: [0x16]})) {
// Check for (ADTS) MPEG-2
if (check([0x08], {offset: 1, mask: [0x08]})) {
return {
ext: 'aac',
mime: 'audio/aac'
};
}

// MPEG 1 or 2 Layer 3 header
// Check for MPEG layer 3
if (check([0x02], {offset: start + 1, mask: [0x06]})) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}
// Must be (ADTS) MPEG-4
return {
ext: 'aac',
mime: 'audio/aac'
};
}

// Check for MPEG layer 2
if (check([0x04], {offset: start + 1, mask: [0x06]})) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}
// MPEG 1 or 2 Layer 3 header
// Check for MPEG layer 3
if (check([0x02], {offset: 1, mask: [0x06]})) {
return {
ext: 'mp3',
mime: 'audio/mpeg'
};
}

// Check for MPEG layer 1
if (check([0x06], {offset: start + 1, mask: [0x06]})) {
return {
ext: 'mp1',
mime: 'audio/mpeg'
};
}
// Check for MPEG layer 2
if (check([0x04], {offset: 1, mask: [0x06]})) {
return {
ext: 'mp2',
mime: 'audio/mpeg'
};
}

// Check for MPEG layer 1
if (check([0x06], {offset: 1, mask: [0x06]})) {
return {
ext: 'mp1',
mime: 'audio/mpeg'
};
}
}
}
Expand Down

0 comments on commit 7021d9a

Please sign in to comment.