/***************************************************************************
* Copyright (C) 2008-2015 by Ilya Kotov *
* forkotov02@hotmail.ru *
* *
* 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., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QtGui>
#include <QDialog>
#include <QMessageBox>
#include <QFile>
#include <mad.h>
#include <taglib/tag.h>
#include <taglib/fileref.h>
#include <taglib/id3v1tag.h>
#include <taglib/id3v2tag.h>
#include <taglib/apetag.h>
#include <taglib/tfile.h>
#include <taglib/mpegfile.h>
#if (TAGLIB_MAJOR_VERSION > 1) || ((TAGLIB_MAJOR_VERSION == 1) && (TAGLIB_MINOR_VERSION >= 8))
#include <taglib/tfilestream.h>
#endif
#include "mpegmetadatamodel.h"
#include "replaygainreader.h"
#include "settingsdialog.h"
#include "decoder_mad.h"
#include "decodermadfactory.h"
// DecoderMADFactory
DecoderMADFactory::DecoderMADFactory()
{
//detecting rusxmms patch
m_using_rusxmms = false;
char str[] = { char(0xF2), char(0xE5), char(0xF1), char(0xF2), '\0'};
QTextCodec *codec = QTextCodec::codecForName ("windows-1251");
TagLib::String tstr(str);
if(codec->toUnicode(str) == QString::fromUtf8(tstr.toCString(true)))
{
qDebug("DecoderMADFactory: found taglib with rusxmms patch");
m_using_rusxmms = true;
}
}
bool DecoderMADFactory::supports(const QString &source) const
{
QString ext = source.right(4).toLower();
if (ext == ".mp1" || ext == ".mp2" || ext == ".mp3")
return true;
else if (ext == ".wav") //check for mp3 wav files
{
QFile file(source);
file.open(QIODevice::ReadOnly);
char buf[22];
file.peek(buf,sizeof(buf));
file.close();
if (!memcmp(buf + 8, "WAVE", 4) && !memcmp(buf + 20, "U" ,1))
{
return true;
}
}
return false;
}
bool DecoderMADFactory::canDecode(QIODevice *input) const
{
char buf[16 * 512];
if (input->peek(buf,sizeof(buf)) == sizeof(buf))
{
struct mad_stream stream;
struct mad_header header;
int dec_res;
mad_stream_init (&stream);
mad_header_init (&header);
mad_stream_buffer (&stream, (unsigned char *) buf, sizeof(buf));
stream.error = MAD_ERROR_NONE;
while ((dec_res = mad_header_decode(&header, &stream)) == -1
&& MAD_RECOVERABLE(stream.error))
;
return dec_res != -1 ? true: false;
}
return false;
}
const DecoderProperties DecoderMADFactory::properties() const
{
DecoderProperties properties;
properties.name = tr("MPEG Plugin");
properties.shortName = "mad";
properties.filters << "*.mp1" << "*.mp2" << "*.mp3" << "*.wav";
properties.description = tr("MPEG Files");
properties.contentTypes << "audio/mp3" << "audio/mpeg";
properties.hasAbout = true;
properties.hasSettings = true;
return properties;
}
Decoder *DecoderMADFactory::create(const QString &url, QIODevice *input)
{
Decoder *d = new DecoderMAD(input);
if(!url.contains("://")) //local file
{
ReplayGainReader rg(url);
d->setReplayGainInfo(rg.replayGainInfo(), true);
}
return d;
}
QList<FileInfo *> DecoderMADFactory::createPlayList(const QString &fileName, bool useMetaData, QStringList *)
{