#include <StreamInfo.h>

using namespace std;

StreamInfo::StreamInfo()
{
    m_dLengthTime = 0.0;
    m_uiMaxKfFrameSize = 0;
    m_uiMinKfFrameSize = 0;
    m_uiKfFrames = 0;
    m_uiKfFramesSize = 0;

    m_uiMaxFrameSize = 0;
    m_uiMinFrameSize = 0;
    m_uiFrames = 0;
    m_uiFramesSize = 0;

    m_Type = 0;
}

StreamInfo::~StreamInfo()
{
}

double StreamInfo::GetBps() const
{
    return (m_dLengthTime) ? GetStreamSize() / m_dLengthTime : 0.0;
}

double StreamInfo::GetFps() const
{
    return (m_dLengthTime) ? GetStreamFrames() /  m_dLengthTime : 0.0;
}

double StreamInfo::GetLengthTime() const
{
    return m_dLengthTime;
}

int64_t StreamInfo::GetStreamSize() const
{
    return m_uiKfFramesSize + m_uiFramesSize;
}

unsigned int StreamInfo::GetStreamFrames() const
{
    return m_uiKfFrames + m_uiFrames;
}

unsigned int  StreamInfo::GetMaxKfFrameSize() const
{
    return m_uiMaxKfFrameSize;
}

unsigned int  StreamInfo::GetMinKfFrameSize() const
{
    return m_uiMinKfFrameSize;
}

unsigned int  StreamInfo::GetAvgKfFrameSize() const
{
    return (m_uiKfFrames > 0) ? m_uiKfFramesSize / m_uiKfFrames : 0;
}

int64_t StreamInfo::GetKfFramesSize() const
{
    return m_uiKfFramesSize;
}

unsigned int StreamInfo::GetKfFrames() const
{
    return m_uiKfFrames;
}

unsigned int  StreamInfo::GetMaxFrameSize() const
{
    return m_uiMaxFrameSize;
}

unsigned int  StreamInfo::GetMinFrameSize() const
{
    return m_uiMinFrameSize;
}

unsigned int  StreamInfo::GetAvgFrameSize() const
{
    return (m_uiFrames > 0) ? m_uiFramesSize / m_uiFrames : 0;
}

int64_t StreamInfo::GetFramesSize() const
{
    return m_uiFramesSize;
}

unsigned int StreamInfo::GetFrames() const
{
    return m_uiFrames;
}

ostream & operator << (ostream& os, StreamInfo& si)
{
    double keyproc = 0, delproc = 0;
    double skeyproc = 0, sdelproc = 0;
    if (si.GetStreamFrames() > 0)
    {
	keyproc = si.GetKfFrames()/(double)si.GetStreamFrames() * 100.0;
	delproc = 100.0 - keyproc;
	skeyproc = (si.GetStreamSize() > 0) ? si.GetKfFramesSize()/(double)si.GetStreamSize() : 0.0;
	skeyproc *= 100.0;
	sdelproc = 100.0 - skeyproc;
    }
    return os << "  Time length: " << si.GetLengthTime()
	<< "  Fps: " << si.GetFps()
	<< "  Size: " << si.GetStreamSize() << " bytes ("
	<< si.GetStreamSize() / 1024.0 << "kB)  Frames: "
	<< si.GetStreamFrames()	<< endl
	<< "  Bps: " << si.GetBps() / 128
	<< "kbps (" << si.GetBps() / 1024.0 << "kB/s)" << endl
	<< "  Key frames/size " << keyproc << "% (" << skeyproc
	<< "%)  delta frames/size " << delproc
	<< "% (" << sdelproc << "%)" << endl
	<< "  Min/avg/max/total key frame size: "
	<< si.GetMinKfFrameSize() << "/"
	<< si.GetAvgKfFrameSize() << "/"
        << si.GetMaxKfFrameSize() << " ("
	<< si.GetKfFramesSize() / 1024.0 << "kB) in "
	<< si.GetKfFrames() << " frames " << endl
	<< "  Min/avg/max/total delta frame size: "
        << si.GetMinFrameSize() << "/"
	<< si.GetAvgFrameSize() << "/"
        << si.GetMaxFrameSize() << " ("
	<< si.GetFramesSize() / 1024.0 << "kB) in "
	<< si.GetFrames() << " frames " << endl;
}

ostream & operator << (ostream& os, StreamInfo* si)
{
    return os << *si;
}
