OSDN Git Service

Updated Simplified Chinese translation, thanks to Hongchuan Zhuang <kidneybean@sohu...
[lamexp/LameXP.git] / src / Decoder_AAC.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2023 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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_AAC.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/Exception.h>
30
31 //Qt
32 #include <QDir>
33 #include <QProcess>
34 #include <QRegExp>
35 #include <QMutexLocker>
36
37 //Static
38 QMutex AACDecoder::m_regexMutex;
39 MUtils::Lazy<QRegExp> AACDecoder::m_regxFeatures([]
40 {
41         return new QRegExp(L1S("\\bLC\\b"), Qt::CaseInsensitive);
42 });
43
44 AACDecoder::AACDecoder(void)
45 :
46         m_binary(lamexp_tools_lookup("faad.exe"))
47 {
48         if(m_binary.isEmpty())
49         {
50                 MUTILS_THROW("Error initializing AAC decoder. Tool 'faad.exe' is not registred!");
51         }
52 }
53
54 AACDecoder::~AACDecoder(void)
55 {
56 }
57
58 bool AACDecoder::decode(const QString &sourceFile, const QString &outputFile, QAtomicInt &abortFlag)
59 {
60         QProcess process;
61         QStringList args;
62
63         args << "-o" << QDir::toNativeSeparators(outputFile);
64         args << QDir::toNativeSeparators(sourceFile);
65
66         if(!startProcess(process, m_binary, args))
67         {
68                 return false;
69         }
70
71         int prevProgress = -1;
72         QRegExp regExp("\\[(\\d+)%\\]\\s*decoding", Qt::CaseInsensitive); //regExp("\\b(\\d+)%\\s+decoding", Qt::CaseInsensitive);
73
74         const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
75         {
76                 if (regExp.lastIndexIn(text) >= 0)
77                 {
78                         qint32 newProgress;
79                         if (MUtils::regexp_parse_int32(regExp, newProgress))
80                         {
81                                 if (newProgress > prevProgress)
82                                 {
83                                         emit statusUpdated(newProgress);
84                                         prevProgress = NEXT_PROGRESS(newProgress);
85                                 }
86                         }
87                         return true;
88                 }
89                 return false;
90         });
91         
92         return (result == RESULT_SUCCESS);
93 }
94
95 bool AACDecoder::isFormatSupported(const QString &containerType, const QString& /*containerProfile*/, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
96 {
97         if((containerType.compare(QLatin1String("ADTS"), Qt::CaseInsensitive) == 0) || (containerType.compare(QLatin1String("MPEG-4"), Qt::CaseInsensitive) == 0))
98         {
99                 if(formatType.compare(QLatin1String("AAC"), Qt::CaseInsensitive) == 0)
100                 {
101                         QMutexLocker lock(&m_regexMutex);
102                         if (m_regxFeatures->indexIn(formatProfile) >= 0)
103                         {
104                                 if((formatVersion.compare(QLatin1String("2"), Qt::CaseInsensitive) == 0) || (formatVersion.compare(QLatin1String("4"), Qt::CaseInsensitive) == 0) || formatVersion.isEmpty())
105                                 {
106                                         return true;
107                                 }
108                         }
109                 }
110         }
111
112         return false;
113 }
114
115 const AbstractDecoder::supportedType_t *AACDecoder::supportedTypes(void)
116 {
117         static const char *exts[] =
118         {
119                 "mp4", "m4a", "aac", NULL
120         };
121
122         static const supportedType_t s_supportedTypes[] =
123         {
124                 { "Advanced Audio Coding", exts },
125                 { NULL, NULL }
126         };
127
128         return s_supportedTypes;
129 }