OSDN Git Service

Set supported input formats for FHG AAC encoder.
[lamexp/LameXP.git] / src / Encoder_AAC_FHG.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_AAC_FHG.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
30
31 FHGAACEncoder::FHGAACEncoder(void)
32 :
33         m_binary_enc(lamexp_lookup_tool("fhgaacenc.exe")),
34         m_binary_dll(lamexp_lookup_tool("enc_fhgaac.dll"))
35 {
36         if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
37         {
38                 throw "Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!";
39         }
40
41         m_configProfile = 0;
42 }
43
44 FHGAACEncoder::~FHGAACEncoder(void)
45 {
46 }
47
48 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
49 {
50         QProcess process;
51         QStringList args;
52         
53         int maxBitrate = 500;
54
55         if(m_configRCMode == SettingsModel::CBRMode)
56         {
57                 switch(m_configProfile)
58                 {
59                 case 1:
60                         args << "--profile" << "lc"; //Forces use of LC AAC profile
61                         break;
62                 case 2:
63                         maxBitrate = 128;
64                         args << "--profile" << "he"; //Forces use of HE AAC profile
65                         break;
66                 case 3:
67                         maxBitrate = 56;
68                         args << "--profile" << "hev2"; //Forces use of HEv2 AAC profile
69                         break;
70                 }
71         }
72
73         switch(m_configRCMode)
74         {
75         case SettingsModel::CBRMode:
76                 args << "--cbr" << QString::number(qMax(32, qMin(maxBitrate, (m_configBitrate * 8))));
77                 break;
78         case SettingsModel::VBRMode:
79                 args << "--vbr" << QString::number(qRound(static_cast<double>(m_configBitrate) / 5.0) + 1);
80                 break;
81         default:
82                 throw "Bad rate-control mode!";
83                 break;
84         }
85
86         args << "--dll" << m_binary_dll;
87
88         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
89
90         args << QDir::toNativeSeparators(sourceFile);
91         args << QDir::toNativeSeparators(outputFile);
92
93         if(!startProcess(process, m_binary_enc, args))
94         {
95                 return false;
96         }
97
98         bool bTimeout = false;
99         bool bAborted = false;
100         int prevProgress = -1;
101
102         QRegExp regExp("Progress:\\s*(\\d+)%");
103
104         while(process.state() != QProcess::NotRunning)
105         {
106                 if(*abortFlag)
107                 {
108                         process.kill();
109                         bAborted = true;
110                         emit messageLogged("\nABORTED BY USER !!!");
111                         break;
112                 }
113                 process.waitForReadyRead(m_processTimeoutInterval);
114                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
115                 {
116                         process.kill();
117                         qWarning("FhgAacEnc process timed out <-- killing!");
118                         emit messageLogged("\nPROCESS TIMEOUT !!!");
119                         bTimeout = true;
120                         break;
121                 }
122                 while(process.bytesAvailable() > 0)
123                 {
124                         QByteArray line = process.readLine();
125                         QString text = QString::fromUtf8(line.constData()).simplified();
126                         if(regExp.lastIndexIn(text) >= 0)
127                         {
128                                 bool ok = false;
129                                 int progress = regExp.cap(1).toInt(&ok);
130                                 if(ok && (progress > prevProgress))
131                                 {
132                                         emit statusUpdated(progress);
133                                         prevProgress = qMin(progress + 2, 99);
134                                 }
135                         }
136                         else if(!text.isEmpty())
137                         {
138                                 emit messageLogged(text);
139                         }
140                 }
141         }
142
143         process.waitForFinished();
144         if(process.state() != QProcess::NotRunning)
145         {
146                 process.kill();
147                 process.waitForFinished(-1);
148         }
149         
150         emit statusUpdated(100);
151         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
152
153         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
154         {
155                 return false;
156         }
157
158         return true;
159 }
160
161 QString FHGAACEncoder::extension(void)
162 {
163         return "mp4";
164 }
165
166 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
167 {
168         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
169         {
170                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
171                 {
172                         return true;
173                 }
174         }
175
176         return false;
177 }
178
179 const unsigned int *FHGAACEncoder::supportedChannelCount(void)
180 {
181         static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
182         return supportedChannels;
183 }
184
185 const unsigned int *FHGAACEncoder::supportedSamplerates(void)
186 {
187         static const unsigned int supportedRates[] = {192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 6000, NULL};
188         return supportedRates;
189 }
190
191 const unsigned int *FHGAACEncoder::supportedBitdepths(void)
192 {
193         static const unsigned int supportedBPS[] = {16, 24, NULL};
194         return supportedBPS;
195 }
196
197 void FHGAACEncoder::setProfile(int profile)
198 {
199         m_configProfile = profile;
200 }