OSDN Git Service

AAC decoding support added.
[lamexp/LameXP.git] / src / Encoder_Vorbis.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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_Vorbis.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29
30 #define max(a,b) (((a) > (b)) ? (a) : (b))
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
32 #define IS_UNICODE(STR) (qstricmp(STR.toUtf8().constData(), QString::fromLocal8Bit(STR.toLocal8Bit()).toUtf8().constData()))
33
34 VorbisEncoder::VorbisEncoder(void)
35 :
36         m_binary_i386(lamexp_lookup_tool("oggenc2_i386.exe")),
37         m_binary_sse2(lamexp_lookup_tool("oggenc2_sse2.exe")),
38         m_binary_x64(lamexp_lookup_tool("oggenc2_x64.exe"))
39 {
40         if(m_binary_i386.isEmpty() || m_binary_sse2.isEmpty() || m_binary_x64.isEmpty())
41         {
42                 throw "Error initializing Vorbis encoder. Tool 'oggenc2.exe' is not registred!";
43         }
44 }
45
46 VorbisEncoder::~VorbisEncoder(void)
47 {
48 }
49
50 bool VorbisEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
51 {
52         QProcess process;
53         QStringList args;
54         const QString baseName = QFileInfo(outputFile).fileName();
55         lamexp_cpu_t cpuFeatures = lamexp_detect_cpu_features();
56         const QString &binary = (cpuFeatures.x64 ? m_binary_x64 : ((cpuFeatures.intel && cpuFeatures.sse && cpuFeatures.sse2) ? m_binary_sse2 : m_binary_i386));
57
58         switch(m_configRCMode)
59         {
60         case SettingsModel::VBRMode:
61                 args << "-q" << QString::number(max(-2, min(10, m_configBitrate)));
62                 break;
63         case SettingsModel::ABRMode:
64                 args << "-b" << QString::number(max(32, min(500, (m_configBitrate * 8))));
65                 break;
66         default:
67                 throw "Bad rate-control mode!";
68                 break;
69         }
70
71         if(!metaInfo.fileName().isEmpty()) args << "-t" << metaInfo.fileName();
72         if(!metaInfo.fileArtist().isEmpty()) args << "-a" << metaInfo.fileArtist();
73         if(!metaInfo.fileAlbum().isEmpty()) args << "-l" << metaInfo.fileAlbum();
74         if(!metaInfo.fileGenre().isEmpty()) args << "-G" << metaInfo.fileGenre();
75         if(!metaInfo.fileComment().isEmpty()) args << "-c" << QString("comment=%1").arg(metaInfo.fileComment());
76         if(metaInfo.fileYear()) args << "-d" << QString::number(metaInfo.fileYear());
77         if(metaInfo.filePosition()) args << "-N" << QString::number(metaInfo.filePosition());
78         
79         //args << "--tv" << QString().sprintf("Encoder=LameXP v%d.%02d.%04d [%s]", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), lamexp_version_release());
80
81         args << "-o" << QDir::toNativeSeparators(outputFile);
82         args << QDir::toNativeSeparators(sourceFile);
83
84         if(!startProcess(process, binary, args))
85         {
86                 return false;
87         }
88
89         bool bTimeout = false;
90         bool bAborted = false;
91
92         QRegExp regExp("\\[.*(\\d+)[.,](\\d+)%\\]");
93
94         while(process.state() != QProcess::NotRunning)
95         {
96                 if(*abortFlag)
97                 {
98                         process.kill();
99                         bAborted = true;
100                         emit messageLogged("\nABORTED BY USER !!!");
101                         break;
102                 }
103                 process.waitForReadyRead();
104                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
105                 {
106                         process.kill();
107                         qWarning("OggEnc process timed out <-- killing!");
108                         bTimeout = true;
109                         break;
110                 }
111                 while(process.bytesAvailable() > 0)
112                 {
113                         QByteArray line = process.readLine();
114                         QString text = QString::fromUtf8(line.constData()).simplified();
115                         if(regExp.lastIndexIn(text) >= 0)
116                         {
117                                 bool ok = false;
118                                 int progress = regExp.cap(1).toInt(&ok);
119                                 if(ok) emit statusUpdated(progress);
120                         }
121                         else if(!text.isEmpty())
122                         {
123                                 emit messageLogged(text);
124                         }
125                 }
126         }
127
128         process.waitForFinished();
129         if(process.state() != QProcess::NotRunning)
130         {
131                 process.kill();
132                 process.waitForFinished(-1);
133         }
134         
135         emit statusUpdated(100);
136         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
137
138         if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
139         {
140                 return false;
141         }
142         
143         return true;
144 }
145
146 QString VorbisEncoder::extension(void)
147 {
148         return "ogg";
149 }
150
151 bool VorbisEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
152 {
153         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
154         {
155                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
156                 {
157                         return true;
158                 }
159         }
160         else if(containerType.compare("FLAC", Qt::CaseInsensitive) == 0)
161         {
162                 if(formatType.compare("FLAC", Qt::CaseInsensitive) == 0)
163                 {
164                         return true;
165                 }
166         }
167
168         return false;
169 }