OSDN Git Service

Added project/solution files for VS2019.
[lamexp/LameXP.git] / src / Decoder_MP3.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Decoder_MP3.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/Global.h>
30 #include <MUtils/Exception.h>
31
32 //Qt
33 #include <QDir>
34 #include <QProcess>
35 #include <QRegExp>
36 #include <QMutexLocker>
37
38 //Static
39 QMutex MP3Decoder::m_regexMutex;
40 MUtils::Lazy<QRegExp> MP3Decoder::m_regxLayer([]
41 {
42         return new QRegExp(L1S("^Layer\\s+(1|2|3)\\b"), Qt::CaseInsensitive);
43 });
44 MUtils::Lazy<QRegExp> MP3Decoder::m_regxVersion([]
45 {
46         return new QRegExp(L1S("^(Version\\s+)?(1|2|2\\.5)\\b"), Qt::CaseInsensitive);
47 });
48
49 MP3Decoder::MP3Decoder(void)
50 :
51         m_binary(lamexp_tools_lookup("mpg123.exe"))
52 {
53         if (m_binary.isEmpty())
54         {
55                 MUTILS_THROW("Error initializing MPG123 decoder. Tool 'mpg123.exe' is not registred!");
56         }
57 }
58
59 MP3Decoder::~MP3Decoder(void)
60 {
61 }
62
63 bool MP3Decoder::decode(const QString &sourceFile, const QString &outputFile, QAtomicInt &abortFlag)
64 {
65         QProcess process;
66         QStringList args;
67
68         args << "-v" << "--utf8" << "-w" << QDir::toNativeSeparators(outputFile);
69         args << QDir::toNativeSeparators(sourceFile);
70
71         if (!startProcess(process, m_binary, args))
72         {
73                 return false;
74         }
75
76         int prevProgress = -1;
77         QRegExp regExp("[_=>]\\s+(\\d+)\\+(\\d+)\\s+");
78
79         const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
80         {
81                 if (regExp.lastIndexIn(text) >= 0)
82                 {
83                         quint32 values[2];
84                         if (MUtils::regexp_parse_uint32(regExp, values, 2))
85                         {
86                                 const quint32 total = values[0] + values[1];
87                                 if ((total >= 512U) && (values[0] >= 256U))
88                                 {
89                                         const int newProgress = qRound((static_cast<double>(values[0]) / static_cast<double>(total)) * 100.0);
90                                         if (newProgress > prevProgress)
91                                         {
92                                                 emit statusUpdated(newProgress);
93                                                 prevProgress = NEXT_PROGRESS(newProgress);
94                                         }
95                                 }
96                         }
97                         return true;
98                 }
99                 return false;
100         });
101
102         return (result == RESULT_SUCCESS);
103 }
104
105 bool MP3Decoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
106 {
107         static const QLatin1String mpegAudio("MPEG Audio"), waveAudio("Wave");
108         if((containerType.compare(mpegAudio, Qt::CaseInsensitive) == 0) || (containerType.compare(waveAudio, Qt::CaseInsensitive) == 0))
109         {
110                 if(formatType.compare(mpegAudio, Qt::CaseInsensitive) == 0)
111                 {
112                         QMutexLocker lock(&m_regexMutex);
113                         if (m_regxLayer->indexIn(formatProfile) >= 0)
114                         {
115                                 return (m_regxVersion->indexIn(formatVersion) >= 0);
116                         }
117                 }
118         }
119
120         return false;
121 }
122
123 const AbstractDecoder::supportedType_t *MP3Decoder::supportedTypes(void)
124 {
125         static const char *exts[][3] =
126         {
127                 { "mp3", "mpa", NULL },
128                 { "mp2", "mpa", NULL },
129                 { "mp1", "mpa", NULL }
130         };
131
132         static const supportedType_t s_supportedTypes[] =
133         {
134                 { "MPEG Audio Layer III", exts[0] },
135                 { "MPEG Audio Layer II",  exts[1] },
136                 { "MPEG Audio Layer I",   exts[2] },
137                 { NULL, NULL }
138         };
139
140         return s_supportedTypes;
141 }