/*
 * mpegv-podcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2006 Tobias Grimm <vdr@e-tobi.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: mpaframeheader.cc 3211 2006-04-14 21:01:37Z svntobi $
 *
 */

#include "mpaframeheader.h"

MpaFrameHeader::MpaFrameHeader(MpaHeaderData headerData)
{
    mpegVersion = ExtractMpegVersion(headerData);
    layer = ExtractLayer(headerData);
    samplingRate = ExtractSamplingRate(headerData);
    bitRate = ExtractBitRate(headerData);
    channelMode = ExtractChannelMode(headerData);
    padding = ExtractPadding(headerData);
}

unsigned int MpaFrameHeader::GetSamplingRate()
{
    return samplingRate;
}

unsigned int MpaFrameHeader::GetBitRate()
{
    return bitRate;
}

unsigned int MpaFrameHeader::GetSideInformationSize()
{
    if (channelMode == 3)
    {
        if (mpegVersion == 1)
        {
            return 17;
        }
        else
        {
            return 9;
        }
    }
    else
    {
        if (mpegVersion == 1)
        {
            return 32;
        }
        else
        {
            return 17;
        }
    }
}

int MpaFrameHeader::GetFrameSize()
{
    if ((samplingRate > 0) && (bitRate > 0))
    {
        int byteRate = bitRate / 8;
        double bytesPerSample = double(byteRate) / samplingRate;
        double bytesPerFrame = GetSamplesPerFrame() * bytesPerSample;
        double slotsPerFrame = bytesPerFrame / GetSlotSize();

        return int(slotsPerFrame + padding) * GetSlotSize();
    }
    else
    {
        return 4;
    }
}

unsigned int MpaFrameHeader::GetValueOfBitsWithinByteBoundary(
  unsigned char buffer[4], int indexOfFirstBit, int numberOfBits)
{
    unsigned char byte = buffer[indexOfFirstBit / 8];
    unsigned char mask = ~(0xFF << numberOfBits);
    return (byte >> (7 - (indexOfFirstBit + numberOfBits - 1) % 8)) & mask;
}

const int MpaFrameHeader::mpegVersions[4] = {0, 0, 2, 1};

int MpaFrameHeader::ExtractMpegVersion(MpaHeaderData headerData)
{
    int mpegVersionIndex = GetValueOfBitsWithinByteBoundary(headerData, 11, 2);
    return mpegVersions[mpegVersionIndex];
}

const int MpaFrameHeader::layers[4] = {0, 3, 2, 1};

int MpaFrameHeader::ExtractLayer(MpaHeaderData headerData)
{
    int layerIndex = GetValueOfBitsWithinByteBoundary(headerData, 13, 2);
    return layers[layerIndex];
}

const int MpaFrameHeader::samplingRates[2][4] =
  {{44100, 48000, 32000, 0},
   {22050, 24000, 16000, 0}};

int MpaFrameHeader::ExtractSamplingRate(MpaHeaderData headerData)
{
    if ((mpegVersion >= 1) and (mpegVersion <= 2))
    {
        int samplingRateIndex = GetValueOfBitsWithinByteBoundary(
          headerData, 20, 2);
        return samplingRates[mpegVersion - 1][samplingRateIndex];
    }
    else
    {
        return 0;
    }
}

const int MpaFrameHeader::bitRates[2][3][16] =
  {{{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
    {0, 32, 48, 56, 64,  80,  96,  112, 128, 160, 192, 224, 256, 320, 384, 0},
    {0, 32, 40, 48, 56,  64,  80,  96,  112, 128, 160, 192, 224, 256, 320, 0}},
   {{0, 32, 48, 56, 64,  80,  96,  112, 128, 144, 160, 176, 192, 224, 256, 0},
    {0, 8,  16, 24, 32,  40,  48,  56,  64,  80,  96,  112, 128, 144, 160, 0},
    {0, 8,  16, 24, 32,  40,  48,  56,  64,  80,  96,  112, 128, 144, 160, 0}}};

int MpaFrameHeader::ExtractBitRate(MpaHeaderData headerData)
{
    if ((mpegVersion >= 1) and (mpegVersion <= 2) and (layer > 0))
    {
        int bitRateIndex = GetValueOfBitsWithinByteBoundary(headerData, 16, 4);
        return bitRates[mpegVersion - 1][layer - 1][bitRateIndex] * 1000;
    }
    else
    {
        return 0;
    }
}

int MpaFrameHeader::ExtractChannelMode(MpaHeaderData headerData)
{
    return GetValueOfBitsWithinByteBoundary(headerData, 24, 2);
}

int MpaFrameHeader::ExtractPadding(MpaHeaderData headerData)
{
    return GetValueOfBitsWithinByteBoundary(headerData, 22, 1);
}

const int MpaFrameHeader::samplesPerFrame[2][3] =
  {{384, 1152, 1152},
   {384, 1152,  576}};

int MpaFrameHeader::GetSamplesPerFrame()
{
    if (mpegVersion == 1)
    {
        return this->samplesPerFrame[0][layer - 1];
    }
    else
    {
        return this->samplesPerFrame[1][layer - 1];
    }
}

int MpaFrameHeader::GetSlotSize()
{
    if (layer == 1)
    {
        return 4;
    }
    else
    {
        return 1;
    }
}
