Skip to content

Wrong Frame Size for MPEG-2 #10

Open
@kmcclellan

Description

I was getting an exception when decoding MPEG Version 2. Figured out it was because the frame lengths were being calculated incorrectly. I believe the issue is here (in MpegFrame.cs):

// calculate the frame's length
int frameSize;
if (BitRateIndex > 0)
{
    if (Layer == MpegLayer.LayerI)
    {
        frameSize = (12 * BitRate / SampleRate + Padding) * 4;
    }
    else
    {
        frameSize = 144 * BitRate / SampleRate + Padding;
    }
}

Compare to the length calculation used by NAudio:

int coefficient = frame.SampleCount/8;
if (frame.MpegLayer == MpegLayer.Layer1)
{
    frame.FrameLength = (coefficient*frame.BitRate/frame.SampleRate + nPadding)*4;
}
else
{
    frame.FrameLength = (coefficient*frame.BitRate)/frame.SampleRate + nPadding;
}

I don't have access to the specs, but it looks like the hard-coded 144 is only going to be correct for MPEG-1 (when sample count is 1152). Appears to have solved my error switching to use SampleCount / 8.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions