/*  This file is part of the KDE project
    Copyright (C) 2007 Matthias Kretz <kretz@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#ifndef PHONON_FORMAT_P_H
#define PHONON_FORMAT_P_H

#include "pcmiodevice.h"

namespace Phonon
{

struct Format
{
    inline Format() : sampleRate(0), bitRate(PcmIODevice::UnknownBitRate), channels(0), byteOrder(QSysInfo::ByteOrder) {}

    inline Format(int s, PcmIODevice::BitRate b, int c, QSysInfo::Endian e)
        : sampleRate(s), bitRate(b), channels(c), byteOrder(e) {}

    inline Format(const Format &f)
        : sampleRate(f.sampleRate), bitRate(f.bitRate), channels(f.channels), byteOrder(f.byteOrder) {}

    inline Format &operator=(const Format &f)
    {
        sampleRate = f.sampleRate;
        bitRate = f.bitRate;
        channels = f.channels;
        byteOrder = f.byteOrder;
        return *this;
    }

    bool operator==(const Format &rhs) const { return sampleRate == rhs.sampleRate && bitRate == rhs.bitRate && channels == rhs.channels && byteOrder == rhs.byteOrder; }
    inline bool operator!=(const Format &rhs) const { return !operator==(rhs); }

    /* Sorting:
     * A Format is considered smaller if
     * - smaller bit rate
     * - smaller sample rate
     * - less channels
     * - non-native byte order
     */
    bool operator<(const Format &rhs) const { return bitRate < rhs.bitRate || (bitRate == rhs.bitRate && (sampleRate < rhs.sampleRate || (sampleRate == rhs.sampleRate && (channels < rhs.channels || (channels == rhs.channels && byteOrder != QSysInfo::ByteOrder && rhs.byteOrder == QSysInfo::ByteOrder))))); }

    quint32 key() const {
        return (byteOrder == QSysInfo::ByteOrder ? 1 : 0) + // 1 bit
            (channels << 1) + // give it 8 bits
            (sampleRate << 9) + // 192kHz ~ 18 bits, give it 20 bits
            (bitRate << 29);
    }

    int sampleRate;
    PcmIODevice::BitRate bitRate;
    int channels;
    QSysInfo::Endian byteOrder;
};

inline uint qHash(const Phonon::Format &f)
{
    return f.key(); // f.sampleRate + (f.bitRate << 18) + (static_cast<uint>(f.byteOrder) << 23) + (f.channels << 24);
}

} // namespace Phonon

inline uint qHash(const Phonon::Format &f)
{
    return Phonon::qHash(f);
}

#endif // PHONON_FORMAT_P_H
