1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "model_options.h"
26 #include <QDesktopServices>
28 #include <QStringList>
29 #include <QApplication>
31 #define COMPARE_VAL(OTHER, NAME) ((this->NAME) == (OTHER->NAME))
32 #define COMPARE_STR(OTHER, NAME) ((this->NAME).compare((model->NAME), Qt::CaseInsensitive) == 0)
33 #define ASSIGN_FROM(OTHER, NAME) ((this->NAME) = (OTHER.NAME))
35 OptionsModel::OptionsModel(void)
37 m_encoderType = EncType_X264;
38 m_encoderArch = EncArch_x32;
39 m_encoderVariant = EncVariant_LoBit;
40 m_rcMode = RCMode_CRF;
46 m_custom_encoder = "";
47 m_custom_avs2yuv = "";
50 OptionsModel::OptionsModel(const OptionsModel &rhs)
52 ASSIGN_FROM(rhs, m_encoderType);
53 ASSIGN_FROM(rhs, m_encoderArch);
54 ASSIGN_FROM(rhs, m_encoderVariant);
55 ASSIGN_FROM(rhs, m_rcMode);
56 ASSIGN_FROM(rhs, m_bitrate);
57 ASSIGN_FROM(rhs, m_quantizer);
58 ASSIGN_FROM(rhs, m_preset);
59 ASSIGN_FROM(rhs, m_tune);
60 ASSIGN_FROM(rhs, m_profile);
61 ASSIGN_FROM(rhs, m_custom_encoder);
62 ASSIGN_FROM(rhs, m_custom_avs2yuv);
65 OptionsModel::~OptionsModel(void)
69 QString OptionsModel::rcMode2String(RCMode mode)
74 return QObject::tr("CRF");
77 return QObject::tr("CQ");
80 return QObject::tr("2-Pass");
83 return QObject::tr("ABR");
91 bool OptionsModel::equals(OptionsModel *model)
95 equal = equal && COMPARE_VAL(model, m_encoderType);
96 equal = equal && COMPARE_VAL(model, m_encoderArch);
97 equal = equal && COMPARE_VAL(model, m_encoderVariant);
98 equal = equal && COMPARE_VAL(model, m_rcMode);
99 equal = equal && COMPARE_VAL(model, m_bitrate);
100 equal = equal && COMPARE_VAL(model, m_quantizer);
101 equal = equal && COMPARE_STR(model, m_preset);
102 equal = equal && COMPARE_STR(model, m_tune);
103 equal = equal && COMPARE_STR(model, m_profile);
104 equal = equal && COMPARE_STR(model, m_custom_encoder);
105 equal = equal && COMPARE_STR(model, m_custom_avs2yuv);
110 bool OptionsModel::saveTemplate(OptionsModel *model, const QString &name)
112 const QString templateName = name.simplified();
113 const QString appDir = x264_data_path();
115 if(templateName.contains('\\') || templateName.contains('/'))
120 QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
121 settings.beginGroup(templateName);
123 settings.setValue("encoder_type", model->m_encoderType);
124 settings.setValue("encoder_arch", model->m_encoderArch);
125 settings.setValue("encoder_variant", model->m_encoderVariant);
126 settings.setValue("rate_control_mode", model->m_rcMode);
127 settings.setValue("target_bitrate", model->m_bitrate);
128 settings.setValue("target_quantizer", model->m_quantizer);
129 settings.setValue("preset_name", model->m_preset);
130 settings.setValue("tuning_name", model->m_tune);
131 settings.setValue("profile_name", model->m_profile);
132 settings.setValue("custom_params_encoder", model->m_custom_encoder);
133 settings.setValue("custom_params_avs2yuv", model->m_custom_avs2yuv);
141 bool OptionsModel::loadTemplate(OptionsModel *model, const QString &name)
143 const QString appDir = x264_data_path();
145 if(name.contains('\\') || name.contains('/'))
150 QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
151 settings.beginGroup(name);
153 //For backward-compatibility
154 if(settings.contains("custom_params"))
156 settings.setValue("custom_params_x264", settings.value("custom_params"));
157 settings.remove("custom_params"); settings.sync();
160 bool complete = true;
161 if(!settings.contains("encoder_type")) complete = false;
162 if(!settings.contains("encoder_arch")) complete = false;
163 if(!settings.contains("encoder_variant")) complete = false;
164 if(!settings.contains("rate_control_mode")) complete = false;
165 if(!settings.contains("target_bitrate")) complete = false;
166 if(!settings.contains("target_quantizer")) complete = false;
167 if(!settings.contains("preset_name")) complete = false;
168 if(!settings.contains("tuning_name")) complete = false;
169 if(!settings.contains("profile_name")) complete = false;
170 if(!settings.contains("custom_params_encoder")) complete = false;
171 if(!settings.contains("custom_params_avs2yuv")) complete = false;
175 model->setEncType(static_cast<OptionsModel::EncType>(settings.value("encoder_type", model->m_encoderType).toInt()));
176 model->setEncArch(static_cast<OptionsModel::EncArch>(settings.value("encoder_arch", model->m_encoderArch).toInt()));
177 model->setEncVariant(static_cast<OptionsModel::EncVariant>(settings.value("encoder_variant", model->m_encoderVariant).toInt()));
178 model->setRCMode(static_cast<OptionsModel::RCMode>(settings.value("rate_control_mode", model->m_rcMode).toInt()));
179 model->setBitrate(settings.value("target_bitrate", model->m_bitrate).toUInt());
180 model->setQuantizer(settings.value("target_quantizer", model->m_quantizer).toDouble());
181 model->setPreset(settings.value("preset_name", model->m_preset).toString());
182 model->setTune(settings.value("tuning_name", model->m_tune).toString());
183 model->setProfile(settings.value("profile_name", model->m_profile).toString());
184 model->setCustomEncParams(settings.value("custom_params_x264", model->m_custom_encoder).toString());
185 model->setCustomAvs2YUV(settings.value("custom_params_avs2yuv", model->m_custom_avs2yuv).toString());
192 QMap<QString, OptionsModel*> OptionsModel::loadAllTemplates(void)
194 QMap<QString, OptionsModel*> list;
195 const QString appDir = x264_data_path();
196 QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
197 QStringList allTemplates = settings.childGroups();
199 while(!allTemplates.isEmpty())
201 QString name = allTemplates.takeFirst();
202 if(!(name.contains('<') || name.contains('>') || name.contains('\\') || name.contains('/')))
204 OptionsModel *options = new OptionsModel();
205 if(loadTemplate(options, name))
207 list.insert(name, options);
210 X264_DELETE(options);
217 bool OptionsModel::templateExists(const QString &name)
219 const QString appDir = x264_data_path();
220 QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
221 QStringList allGroups = settings.childGroups();
222 return allGroups.contains(name, Qt::CaseInsensitive);
225 bool OptionsModel::deleteTemplate(const QString &name)
227 const QString appDir = x264_data_path();
228 QSettings settings(QString("%1/templates.ini").arg(appDir), QSettings::IniFormat);
230 if(settings.childGroups().contains(name, Qt::CaseInsensitive))
232 settings.remove(name);