OSDN Git Service

Set supported input formats for FHG AAC encoder.
[lamexp/LameXP.git] / src / Encoder_MP3.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Encoder_MP3.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29 #include <limits.h>
30
31 static const int g_lameAgorithmQualityLUT[] = {9, 7, 5, 2, 0, INT_MAX};
32
33 MP3Encoder::MP3Encoder(void)
34 :
35         m_binary(lamexp_lookup_tool("lame.exe"))
36 {
37         if(m_binary.isEmpty())
38         {
39                 throw "Error initializing MP3 encoder. Tool 'lame.exe' is not registred!";
40         }
41         
42         m_algorithmQuality = 3;
43         m_configBitrateMaximum = 0;
44         m_configBitrateMinimum = 0;
45         m_configSamplingRate = 0;
46         m_configChannelMode = 0;
47 }
48
49 MP3Encoder::~MP3Encoder(void)
50 {
51 }
52
53 bool MP3Encoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
54 {
55         QProcess process;
56         QStringList args;
57
58         args << "--nohist";
59         args << "-q" << QString::number(g_lameAgorithmQualityLUT[m_algorithmQuality]);
60                 
61         switch(m_configRCMode)
62         {
63         case SettingsModel::VBRMode:
64                 args << "-V" << QString::number(9 - qMin(9, m_configBitrate));
65                 break;
66         case SettingsModel::ABRMode:
67                 args << "--abr" << QString::number(SettingsModel::mp3Bitrates[qMax(0, qMin(13, m_configBitrate))]);
68                 break;
69         case SettingsModel::CBRMode:
70                 args << "--cbr";
71                 args << "-b" << QString::number(SettingsModel::mp3Bitrates[qMax(0, qMin(13, m_configBitrate))]);
72                 break;
73         default:
74                 throw "Bad rate-control mode!";
75                 break;
76         }
77
78         if((m_configBitrateMaximum > 0) && (m_configBitrateMinimum > 0) && (m_configBitrateMinimum <= m_configBitrateMaximum))
79         {
80                 if(m_configRCMode != SettingsModel::CBRMode)
81                 {
82                         args << "-b" << QString::number(clipBitrate(m_configBitrateMinimum));
83                         args << "-B" << QString::number(clipBitrate(m_configBitrateMaximum));
84                 }
85         }
86
87         if(m_configSamplingRate > 0)
88         {
89                 args << "--resample" << QString::number(m_configSamplingRate);
90         }
91
92         switch(m_configChannelMode)
93         {
94         case 1:
95                 args << "-m" << "j";
96                 break;
97         case 2:
98                 args << "-m" << "f";
99                 break;
100         case 3:
101                 args << "-m" << "s";
102                 break;
103         case 4:
104                 args << "-m" << "d";
105                 break;
106         case 5:
107                 args << "-m" << "m";
108                 break;
109         }
110
111         bool bUseUCS2 = false;
112
113         if(!metaInfo.fileName().isEmpty() && isUnicode(metaInfo.fileName())) bUseUCS2 = true;
114         if(!metaInfo.fileArtist().isEmpty() && isUnicode(metaInfo.fileArtist())) bUseUCS2 = true;
115         if(!metaInfo.fileAlbum().isEmpty() && isUnicode(metaInfo.fileAlbum())) bUseUCS2 = true;
116         if(!metaInfo.fileGenre().isEmpty() && isUnicode(metaInfo.fileGenre())) bUseUCS2 = true;
117         if(!metaInfo.fileComment().isEmpty() && isUnicode(metaInfo.fileComment())) bUseUCS2 = true;
118
119         if(bUseUCS2) args << "--id3v2-ucs2"; //Must specify this BEFORE "--tt" and friends!
120
121         if(!metaInfo.fileName().isEmpty()) args << "--tt" << metaInfo.fileName();
122         if(!metaInfo.fileArtist().isEmpty()) args << "--ta" << metaInfo.fileArtist();
123         if(!metaInfo.fileAlbum().isEmpty()) args << "--tl" << metaInfo.fileAlbum();
124         if(!metaInfo.fileGenre().isEmpty()) args << "--tg" << metaInfo.fileGenre();
125         if(!metaInfo.fileComment().isEmpty()) args << "--tc" << metaInfo.fileComment();
126         if(metaInfo.fileYear()) args << "--ty" << QString::number(metaInfo.fileYear());
127         if(metaInfo.filePosition()) args << "--tn" << QString::number(metaInfo.filePosition());
128         if(!metaInfo.fileCover().isEmpty()) args << "--ti" << QDir::toNativeSeparators(metaInfo.fileCover());
129
130         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
131
132         args << QDir::toNativeSeparators(sourceFile);
133         args << QDir::toNativeSeparators(outputFile);
134
135         if(!startProcess(process, m_binary, args))
136         {
137                 return false;
138         }
139
140         bool bTimeout = false;
141         bool bAborted = false;
142         int prevProgress = -1;
143
144         QRegExp regExp("\\(.*(\\d+)%\\)\\|");
145
146         while(process.state() != QProcess::NotRunning)
147         {
148                 if(*abortFlag)
149                 {
150                         process.kill();
151                         bAborted = true;
152                         emit messageLogged("\nABORTED BY USER !!!");
153                         break;
154                 }
155                 process.waitForReadyRead(m_processTimeoutInterval);
156                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
157                 {
158                         process.kill();
159                         qWarning("LAME process timed out <-- killing!");
160                         emit messageLogged("\nPROCESS TIMEOUT !!!");
161                         bTimeout = true;
162                         break;
163                 }
164                 while(process.bytesAvailable() > 0)
165                 {
166                         QByteArray line = process.readLine();
167                         QString text = QString::fromUtf8(line.constData()).simplified();
168                         if(regExp.lastIndexIn(text) >= 0)
169                         {
170                                 bool ok = false;
171                                 int progress = regExp.cap(1).toInt(&ok);
172                                 if(ok && (progress > prevProgress))
173                                 {
174                                         emit statusUpdated(progress);
175                                         prevProgress = qMin(progress + 2, 99);
176                                 }
177                         }
178                         else if(!text.isEmpty())
179                         {
180                                 emit messageLogged(text);
181                         }
182                 }
183         }
184
185         process.waitForFinished();
186         if(process.state() != QProcess::NotRunning)
187         {
188                 process.kill();
189                 process.waitForFinished(-1);
190         }
191         
192         emit statusUpdated(100);
193         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
194
195         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
196         {
197                 return false;
198         }
199         
200         return true;
201 }
202
203 QString MP3Encoder::extension(void)
204 {
205         return "mp3";
206 }
207
208 bool MP3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
209 {
210         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
211         {
212                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
213                 {
214                         return true;
215                 }
216         }
217         else if(containerType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
218         {
219                 if(formatType.compare("MPEG Audio", Qt::CaseInsensitive) == 0)
220                 {
221                         if(formatProfile.compare("Layer 3", Qt::CaseInsensitive) == 0 || formatProfile.compare("Layer 2", Qt::CaseInsensitive) == 0)
222                         {
223                                 if(formatVersion.compare("Version 1", Qt::CaseInsensitive) == 0 || formatVersion.compare("Version 2", Qt::CaseInsensitive) == 0)
224                                 {
225                                         return true;
226                                 }
227                         }
228                 }
229         }
230
231         return false;
232 }
233
234 const unsigned int *MP3Encoder::supportedChannelCount(void)
235 {
236         static const unsigned int supportedChannels[] = {1, 2, NULL};
237         return supportedChannels;
238 }
239
240 void MP3Encoder::setAlgoQuality(int value)
241 {
242         m_algorithmQuality = value;
243 }
244
245 void MP3Encoder::setBitrateLimits(int minimumBitrate, int maximumBitrate)
246 {
247         m_configBitrateMinimum = minimumBitrate;
248         m_configBitrateMaximum = maximumBitrate;
249 }
250
251 void MP3Encoder::setSamplingRate(int value)
252 {
253         m_configSamplingRate = value;
254 }
255
256 void MP3Encoder::setChannelMode(int value)
257 {
258         m_configChannelMode = value;
259 }
260
261 int MP3Encoder::clipBitrate(int bitrate)
262 {
263         int targetBitrate = qMin(qMax(bitrate, 32), 320);
264         
265         int minDiff = INT_MAX;
266         int minIndx = -1;
267
268         for(int i = 0; SettingsModel::mp3Bitrates[i] > 0; i++)
269         {
270                 int currentDiff = abs(targetBitrate - SettingsModel::mp3Bitrates[i]);
271                 if(currentDiff < minDiff)
272                 {
273                         minDiff = currentDiff;
274                         minIndx = i;
275                 }
276         }
277
278         if(minIndx >= 0)
279         {
280                 return SettingsModel::mp3Bitrates[minIndx];
281         }
282
283         return targetBitrate;
284 }