Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong length for MPEG-2 Layer 3 #15

Open
Woodgnome opened this issue Jun 15, 2023 · 0 comments
Open

Wrong length for MPEG-2 Layer 3 #15

Woodgnome opened this issue Jun 15, 2023 · 0 comments

Comments

@Woodgnome
Copy link

Woodgnome commented Jun 15, 2023

Rename the attached test.txt to test.mp3 (Github doesn't allow MP3 attachment).

The MP3 is detected as MPEG-2 Layer 3:

falahati\PHPMP3\MpegAudioFrameHeader Object
(
    [bitRate:falahati\PHPMP3\MpegAudioFrameHeader:private] => 48000
    [sampleRate:falahati\PHPMP3\MpegAudioFrameHeader:private] => 11025
    [version:falahati\PHPMP3\MpegAudioFrameHeader:private] => 2
    [profile:falahati\PHPMP3\MpegAudioFrameHeader:private] => 3
    [duration:falahati\PHPMP3\MpegAudioFrameHeader:private] => 0.1045
    [offset:falahati\PHPMP3\MpegAudioFrameHeader:private] => 3336
    [length:falahati\PHPMP3\MpegAudioFrameHeader:private] => 627
    [padding:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [errorProtection:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [privateBit:falahati\PHPMP3\MpegAudioFrameHeader:private] =>
    [copyrighted:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [original:falahati\PHPMP3\MpegAudioFrameHeader:private] => 1
    [mode:falahati\PHPMP3\MpegAudioFrameHeader:private] => 3
    [middleSideStereoJoining:falahati\PHPMP3\MpegAudioFrameHeader:private] =>
    [intensityStereoMode:falahati\PHPMP3\MpegAudioFrameHeader:private] => 0
)

The current length calculation in MpegAudioFrameHeader::tryParse() is:

if ($frame->profile == self::Profile_1) {
	$frame->length = (((12 * $frame->bitRate) / $frame->sampleRate) + $frame->padding) * 4;
} else if ($frame->profile == self::Profile_2 || $frame->profile == self::Profile_3) {
	$frame->length = ((144 * $frame->bitRate) / $frame->sampleRate) + $frame->padding;
}

I cannot find a mention of this in the ISO, but I've seen several sources claim that MPEG-2 Layer 3 samples per frame is acutally 576 / 8 = 72 instead of 1152 / 8 = 144, for example, https://stackoverflow.com/questions/62536328/mpeg-2-and-2-5-problems-calculating-frame-sizes-in-bytes.

Also, claims that padding should be part of the denominator (144 * bitRate / (sampleRate + padding) instead of 144 * bitRate / sampleRate + padding), for example, https://stackoverflow.com/questions/57103676/formula-from-mp3-frame-length.

When I try to append the test MP3 to another MP3 it fails because of this check in MpegAudio::read():

if ($this->memoryPointer >= $this->memoryLength) {
	return false;
}

Updating the frame length calculation to $frame->length = 72 * $frame->bitRate / ($frame->sampleRate + $frame->padding); fixes this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant