1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/***************************************************************************
* Copyright (C) 2008-2021 by Ilya Kotov *
* forkotov02@ya.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 <QDialog>
#include <QMessageBox>
#include <QSettings>
#include <QFile>
#include <QTextCodec>
#include <QtDebug>
#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>
#include <taglib/tfilestream.h>
#include <taglib/id3v2tag.h>
#include <taglib/id3v2header.h>
#include <taglib/textidentificationframe.h>
#include <taglib/id3v2framefactory.h>
#include "tagextractor.h"
#include "mpegmetadatamodel.h"
#include "settingsdialog.h"
#ifdef WITH_MAD
#include "decoder_mad.h"
#endif
#ifdef WITH_MPG123
#include "decoder_mpg123.h"
#endif
#include "decodermpegfactory.h"
#define CSTR_TO_QSTR(str,utf) codec->toUnicode(str.toCString(utf)).trimmed()
// DecoderMPEGFactory
DecoderMPEGFactory::DecoderMPEGFactory()
{
//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;
TagExtractor::setForceUtf8(m_using_rusxmms);
}
}
bool DecoderMPEGFactory::canDecode(QIODevice *input) const
{
char buf[8192];
qint64 dataSize = sizeof(buf);
if(input->peek(buf, sizeof(buf)) != sizeof(buf))
return false;
if (!memcmp(buf, "FLV", 3)) //skip Macromedia Flash Video
return false;
if (!memcmp(buf + 8, "WAVE", 4))
return !memcmp(buf + 20, "U" ,1);
if(!memcmp(buf, "ID3", 3))
{
TagLib::ByteVector byteVector(buf, dataSize);
TagLib::ID3v2::Header header(byteVector);
//skip id3v2tag if possible
if(input->isSequential())
{
if(header.tagSize() >= dataSize)
return false;
dataSize -= header.tagSize();
memmove(buf, buf + header.tagSize(), dataSize);
}
else
{
input->seek(header.tagSize());
dataSize = input->read(buf, sizeof(buf));
input->seek(0); //restore initial position
}
}
if(dataSize <= 0)
return false;
QString decoderName;
#if defined(WITH_MAD) && defined(WITH_MPG123)
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
decoderName = settings.value("MPEG/decoder", "mad").toString();
#elif defined(WITH_MAD)
decoderName = "mad";
#elif defined(WITH_MPG123)
decoderName = "mpg123";
#endif
#ifdef WITH_MAD
if(decoderName != "mpg123")
{
struct mad_stream stream;
struct mad_header header;
struct mad_frame frame;
int dec_res;
mad_stream_init(&stream);
mad_header_init(&header);
mad_frame_init(&frame);
mad_stream_buffer (&stream, (unsigned char *) buf, dataSize);
stream.error = MAD_ERROR_NONE;
while ((dec_res = mad_header_decode(&header, &stream)) == -1
&& MAD_RECOVERABLE(stream.error))
;
if(dec_res == 0)
{
while ((dec_res = mad_frame_decode(&frame, &stream)) == -1
&& MAD_RECOVERABLE(stream.error))
;
}
mad_stream_finish(&stream);
mad_frame_finish(&frame);
return dec_res == 0;
}
#endif
#ifdef WITH_MPG123
if (decoderName == "mpg123")
{
mpg123_init();
mpg123_handle *handle = mpg123_new(nullptr, nullptr);
if (!handle)
return false;
if(mpg123_open_feed(handle) != MPG123_OK)
{
mpg123_delete(handle);
return false;
}
if (mpg123_format(handle, 44100, MPG123_STEREO, MPG123_ENC_SIGNED_16) != MPG123_OK)
{
mpg123_close(handle);
mpg123_delete(handle);
return false;
}
size_t out_size = 0;
int ret = mpg123_decode(handle, (unsigned char*) buf, dataSize, nullptr, 0, &out_size);
mpg123_close(handle);
mpg123_delete(handle);
return ret == MPG123_DONE || ret == MPG123_NEW_FORMAT;
}
#endif
return false;
}
DecoderProperties DecoderMPEGFactory::properties() const
{
DecoderProperties properties;
properties.name = tr("MPEG Plugin");
properties.shortName = "mpeg";
properties.filters = QStringList { "*.mp1", "*.mp2", "*.mp3", "*.wav" };
properties.description = tr("MPEG Files");
properties.contentTypes = QStringList { "audio/mp3", "audio/mpeg" };
properties.hasAbout = true;
properties.hasSettings = true;
return properties;
}
Decoder *DecoderMPEGFactory::create(const QString &, QIODevice *input)
{
Decoder *d = nullptr;
#if defined(WITH_MAD) && defined(WITH_MPG123)
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
if(settings.value("MPEG/decoder", "mad").toString() == "mpg123")
{
qDebug("DecoderMPEGFactory: using mpg123 decoder");
d = new DecoderMPG123(input);
}
else
{
qDebug("DecoderMPEGFactory: using MAD decoder");
bool crc = settings.value("MPEG/enable_crc", false).toBool();
d = new DecoderMAD(crc, input);
}
#elif defined(WITH_MAD)
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
bool crc = settings.value("MPEG/enable_crc", false).toBool();
d = new DecoderMAD(crc, input);
#elif defined(WITH_MPG123)
d = new DecoderMPG123(input);
#endif
return d;
}
QList<TrackInfo *> DecoderMPEGFactory::createPlayList(const QString &path, TrackInfo::Parts parts, QStringList *)
{
TrackInfo *info = new TrackInfo(path);
if(parts == TrackInfo::Parts())
return QList<TrackInfo*>() << info;
TagLib::FileStream stream(QStringToFileName(path), true);
TagLib::MPEG::File fileRef(&stream, TagLib::ID3v2::FrameFactory::instance());
if (parts & TrackInfo::MetaData)
{
QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
settings.beginGroup("MPEG");
QList< QMap<Qmmp::MetaData, QString> > metaData;
uint tag_array[3];
tag_array[0] = settings.value("tag_1", SettingsDialog::ID3v2).toInt();
tag_array[1] = settings.value("tag_2", SettingsDialog::APE).toInt();
tag_array[2] = settings.value("tag_3", SettingsDialog::ID3v1).toInt();
bool merge = settings.value("merge_tags", false).toBool();
for (int i = 0; i < 3; ++i)
{
QTextCodec *codec = nullptr;
TagLib::Tag *tag = nullptr;
QByteArray codecName;
switch ((uint) tag_array[i])
{
case SettingsDialog::ID3v1:
codecName = settings.value("ID3v1_encoding","ISO-8859-1").toByteArray();
tag = fileRef.ID3v1Tag();
break;
case SettingsDialog::ID3v2:
codecName = settings.value("ID3v2_encoding","UTF-8").toByteArray();
tag = fileRef.ID3v2Tag();
break;
case SettingsDialog::APE:
codecName = "UTF-8";
tag = fileRef.APETag();
break;
case SettingsDialog::Disabled:
break;
}
if(m_using_rusxmms || codecName.contains("UTF"))
codec = QTextCodec::codecForName("UTF-8");
else if(!codecName.isEmpty())
codec = QTextCodec::codecForName(codecName);
if (!codec)
codec = QTextCodec::codecForName("UTF-8");
if (tag && codec && !tag->isEmpty())
{
if((tag == fileRef.ID3v1Tag() || tag == fileRef.ID3v2Tag()) && !m_using_rusxmms &&
settings.value("detect_encoding", false).toBool())
{
QTextCodec *detectedCodec = TagExtractor::detectCharset(tag);
codec = detectedCodec ? detectedCodec : codec;
}
bool utf = codec->name().contains("UTF");
QMap<Qmmp::MetaData, QString> tags = {
{ Qmmp::ARTIST, CSTR_TO_QSTR(tag->artist(), utf) },
{ Qmmp::ALBUM, CSTR_TO_QSTR(tag->album(), utf) },
{ Qmmp::COMMENT, CSTR_TO_QSTR(tag->comment(), utf) },
{ Qmmp::GENRE, CSTR_TO_QSTR(tag->genre(), utf) },
{ Qmmp::TITLE, CSTR_TO_QSTR(tag->title(), utf) },
{ Qmmp::YEAR, QString::number(tag->year()) },
{ Qmmp::TRACK, QString::number(tag->track()) },
};
if(tag == fileRef.ID3v2Tag())
{
if(!fileRef.ID3v2Tag()->frameListMap()["TPE2"].isEmpty())
{
TagLib::String albumArtist = fileRef.ID3v2Tag()->frameListMap()["TPE2"].front()->toString();
tags.insert(Qmmp::ALBUMARTIST, CSTR_TO_QSTR(albumArtist, utf));
}
if(!fileRef.ID3v2Tag()->frameListMap()["TCOM"].isEmpty())
{
TagLib::String composer = fileRef.ID3v2Tag()->frameListMap()["TCOM"].front()->toString();
tags.insert(Qmmp::COMPOSER, CSTR_TO_QSTR(composer, utf));
}
if(!fileRef.ID3v2Tag()->frameListMap()["TPOS"].isEmpty())
{
TagLib::String disc = fileRef.ID3v2Tag()->frameListMap()["TPOS"].front()->toString();
tags.insert(Qmmp::DISCNUMBER, CSTR_TO_QSTR(disc, utf));
}
}
else if(tag == fileRef.APETag())
{
TagLib::APE::Item fld;
if(!(fld = fileRef.APETag()->itemListMap()["ALBUM ARTIST"]).isEmpty())
tags.insert(Qmmp::ALBUMARTIST, CSTR_TO_QSTR(fld.toString(), true));
if(!(fld = fileRef.APETag()->itemListMap()["COMPOSER"]).isEmpty())
tags.insert(Qmmp::COMPOSER, CSTR_TO_QSTR(fld.toString(), true));
}
metaData << tags;
if(!merge)
break;
}
}
settings.endGroup();
for(const QMap<Qmmp::MetaData, QString> &tags : qAsConst(metaData))
{
for(int i = Qmmp::TITLE; i <= Qmmp::DISCNUMBER; ++i)
{
Qmmp::MetaData key = static_cast<Qmmp::MetaData>(i);
if(info->value(key).length() < tags.value(key).length())
info->setValue(key, tags.value(key));
}
}
}
if((parts & TrackInfo::Properties) && fileRef.audioProperties())
{
info->setValue(Qmmp::BITRATE, fileRef.audioProperties()->bitrate());
info->setValue(Qmmp::SAMPLERATE, fileRef.audioProperties()->sampleRate());
info->setValue(Qmmp::CHANNELS, fileRef.audioProperties()->channels());
info->setValue(Qmmp::BITS_PER_SAMPLE, 32); //float
switch(fileRef.audioProperties()->version())
{
case TagLib::MPEG::Header::Version1:
info->setValue(Qmmp::FORMAT_NAME, QString("MPEG-1 layer %1").arg(fileRef.audioProperties()->layer()));
break;
case TagLib::MPEG::Header::Version2:
info->setValue(Qmmp::FORMAT_NAME, QString("MPEG-2 layer %1").arg(fileRef.audioProperties()->layer()));
break;
case TagLib::MPEG::Header::Version2_5:
info->setValue(Qmmp::FORMAT_NAME, QString("MPEG-2.5 layer %1").arg(fileRef.audioProperties()->layer()));
}
info->setDuration(fileRef.audioProperties()->lengthInMilliseconds());
}
if(parts & TrackInfo::ReplayGainInfo)
{
if(fileRef.ID3v2Tag() && !fileRef.ID3v2Tag()->isEmpty())
{
TagLib::ID3v2::Tag *tag = fileRef.ID3v2Tag();
TagLib::ID3v2::UserTextIdentificationFrame *frame = nullptr;
TagLib::ID3v2::FrameList frames = tag->frameList("TXXX");
for(TagLib::ID3v2::FrameList::Iterator it = frames.begin(); it != frames.end(); ++it)
{
frame = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*it);
if(!frame || frame->fieldList().size() < 2)
continue;
TagLib::String desc = frame->description().upper();
if (desc == "REPLAYGAIN_TRACK_GAIN")
info->setValue(Qmmp::REPLAYGAIN_TRACK_GAIN, TStringToQString(frame->fieldList()[1]));
else if (desc == "REPLAYGAIN_TRACK_PEAK")
info->setValue(Qmmp::REPLAYGAIN_TRACK_PEAK, TStringToQString(frame->fieldList()[1]));
else if (desc == "REPLAYGAIN_ALBUM_GAIN")
info->setValue(Qmmp::REPLAYGAIN_ALBUM_GAIN, TStringToQString(frame->fieldList()[1]));
else if (desc == "REPLAYGAIN_ALBUM_PEAK")
info->setValue(Qmmp::REPLAYGAIN_ALBUM_PEAK, TStringToQString(frame->fieldList()[1]));
}
}
if(info->replayGainInfo().isEmpty() && fileRef.APETag() && !fileRef.APETag()->isEmpty())
{
TagLib::APE::Tag *tag = fileRef.APETag();
TagLib::APE::ItemListMap items = tag->itemListMap();
if (items.contains("REPLAYGAIN_TRACK_GAIN"))
info->setValue(Qmmp::REPLAYGAIN_TRACK_GAIN,TStringToQString(items["REPLAYGAIN_TRACK_GAIN"].values()[0]));
if (items.contains("REPLAYGAIN_TRACK_PEAK"))
info->setValue(Qmmp::REPLAYGAIN_TRACK_PEAK,TStringToQString(items["REPLAYGAIN_TRACK_PEAK"].values()[0]));
if (items.contains("REPLAYGAIN_ALBUM_GAIN"))
info->setValue(Qmmp::REPLAYGAIN_ALBUM_GAIN,TStringToQString(items["REPLAYGAIN_ALBUM_GAIN"].values()[0]));
if (items.contains("REPLAYGAIN_ALBUM_PEAK"))
info->setValue(Qmmp::REPLAYGAIN_ALBUM_PEAK,TStringToQString(items["REPLAYGAIN_ALBUM_PEAK"].values()[0]));
}
}
return QList<TrackInfo*>() << info;
}
MetaDataModel* DecoderMPEGFactory::createMetaDataModel(const QString &path, bool readOnly)
{
return new MPEGMetaDataModel(m_using_rusxmms, path, readOnly);
}
void DecoderMPEGFactory::showSettings(QWidget *parent)
{
SettingsDialog *s = new SettingsDialog(m_using_rusxmms, parent);
s->show();
}
void DecoderMPEGFactory::showAbout(QWidget *parent)
{
QMessageBox::about (parent, tr("About MPEG Audio Plugin"),
tr("MPEG 1.0/2.0/2.5 layer 1/2/3 audio decoder")+"\n"+
tr("Compiled against:") + "\n" +
#ifdef WITH_MAD
QString("libmad-%1.%2.%3%4")
.arg(MAD_VERSION_MAJOR)
.arg(MAD_VERSION_MINOR)
.arg(MAD_VERSION_PATCH)
.arg(MAD_VERSION_EXTRA) + "\n" +
#endif
#ifdef WITH_MPG123
tr("mpg123, API version: %1")
.arg(MPG123_API_VERSION) + "\n" +
#endif
tr("Written by: Ilya Kotov <forkotov02@ya.ru>")+"\n"+
tr("Source code based on mq3 and madplay projects")
);
}
QString DecoderMPEGFactory::translation() const
{
return QLatin1String(":/mpeg_plugin_");
}
|